blob: 83973f6b3c0a4953ad9bd39edb90924631191177 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070076#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070077#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030078#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070079#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060080#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060081#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070082#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060083#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060095
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000106#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
107 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
108 IOSQE_BUFFER_SELECT)
109
Jens Axboe2b188cc2019-01-07 10:46:33 -0700110struct io_uring {
111 u32 head ____cacheline_aligned_in_smp;
112 u32 tail ____cacheline_aligned_in_smp;
113};
114
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * This data is shared with the application through the mmap at offsets
117 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 *
119 * The offsets to the member fields are published through struct
120 * io_sqring_offsets when calling io_uring_setup.
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
124 * Head and tail offsets into the ring; the offsets need to be
125 * masked to get valid indices.
126 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 * The kernel controls head of the sq ring and the tail of the cq ring,
128 * and the application controls tail of the sq ring and the head of the
129 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200132 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 * ring_entries - 1)
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_ring_mask, cq_ring_mask;
137 /* Ring sizes (constant, power of 2) */
138 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 /*
140 * Number of invalid entries dropped by the kernel due to
141 * invalid index stored in array
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application (i.e. get number of "new events" by comparing to
145 * cached value).
146 *
147 * After a new SQ head value was read by the application this
148 * counter includes all submissions that were dropped reaching
149 * the new SQ head (and possibly more).
150 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000151 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200153 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 *
155 * Written by the kernel, shouldn't be modified by the
156 * application.
157 *
158 * The application needs a full memory barrier before checking
159 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200163 * Runtime CQ flags
164 *
165 * Written by the application, shouldn't be modified by the
166 * kernel.
167 */
168 u32 cq_flags;
169 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * Number of completion events lost because the queue was full;
171 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800172 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200173 * the completion queue.
174 *
175 * Written by the kernel, shouldn't be modified by the
176 * application (i.e. get number of "new events" by comparing to
177 * cached value).
178 *
179 * As completion events come in out of order this counter is not
180 * ordered with any other data.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200183 /*
184 * Ring buffer of completion events.
185 *
186 * The kernel writes completion events fresh every time they are
187 * produced, so the application is allowed to modify pending
188 * entries.
189 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000190 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191};
192
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000193enum io_uring_cmd_flags {
194 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000195 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000196};
197
Jens Axboeedafcce2019-01-09 09:16:05 -0700198struct io_mapped_ubuf {
199 u64 ubuf;
200 size_t len;
201 struct bio_vec *bvec;
202 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600203 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700204};
205
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000206struct io_ring_ctx;
207
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000208struct io_rsrc_put {
209 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000210 union {
211 void *rsrc;
212 struct file *file;
213 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000214};
215
216struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600217 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700218};
219
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000220struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800221 struct percpu_ref refs;
222 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000223 struct list_head rsrc_list;
224 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000225 void (*rsrc_put)(struct io_ring_ctx *ctx,
226 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600227 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000228 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800229};
230
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000231struct fixed_rsrc_data {
232 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700233 struct io_ring_ctx *ctx;
234
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000235 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700236 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700237 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800238 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700239};
240
Jens Axboe5a2e7452020-02-23 16:23:11 -0700241struct io_buffer {
242 struct list_head list;
243 __u64 addr;
244 __s32 len;
245 __u16 bid;
246};
247
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200248struct io_restriction {
249 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
250 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
251 u8 sqe_flags_allowed;
252 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200253 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200254};
255
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700256enum {
257 IO_SQ_THREAD_SHOULD_STOP = 0,
258 IO_SQ_THREAD_SHOULD_PARK,
259};
260
Jens Axboe534ca6d2020-09-02 13:52:19 -0600261struct io_sq_data {
262 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600263 struct mutex lock;
264
265 /* ctx's that are using this sqd */
266 struct list_head ctx_list;
267 struct list_head ctx_new_list;
268 struct mutex ctx_lock;
269
Jens Axboe534ca6d2020-09-02 13:52:19 -0600270 struct task_struct *thread;
271 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800272
273 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700274 int sq_cpu;
275 pid_t task_pid;
276
277 unsigned long state;
278 struct completion startup;
279 struct completion completion;
280 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600281};
282
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000283#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000284#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000285#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000286#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000287
288struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000289 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000290 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700291 unsigned int locked_free_nr;
292 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000293 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700294 /* IRQ completion list, under ->completion_lock */
295 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000296};
297
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000298struct io_submit_link {
299 struct io_kiocb *head;
300 struct io_kiocb *last;
301};
302
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000303struct io_submit_state {
304 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000305 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000306
307 /*
308 * io_kiocb alloc cache
309 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000310 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000311 unsigned int free_reqs;
312
313 bool plug_started;
314
315 /*
316 * Batch completion logic
317 */
318 struct io_comp_state comp;
319
320 /*
321 * File reference cache
322 */
323 struct file *file;
324 unsigned int fd;
325 unsigned int file_refs;
326 unsigned int ios_left;
327};
328
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329struct io_ring_ctx {
330 struct {
331 struct percpu_ref refs;
332 } ____cacheline_aligned_in_smp;
333
334 struct {
335 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800336 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800337 unsigned int cq_overflow_flushed: 1;
338 unsigned int drain_next: 1;
339 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200340 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000341 unsigned int sqo_dead: 1;
Jens Axboe5f3f26f2021-02-25 10:17:46 -0700342 unsigned int sqo_exec: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700343
Hristo Venev75b28af2019-08-26 17:23:46 +0000344 /*
345 * Ring buffer of indices into array of io_uring_sqe, which is
346 * mmapped by the application using the IORING_OFF_SQES offset.
347 *
348 * This indirection could e.g. be used to assign fixed
349 * io_uring_sqe entries to operations and only submit them to
350 * the queue when needed.
351 *
352 * The kernel modifies neither the indices array nor the entries
353 * array.
354 */
355 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356 unsigned cached_sq_head;
357 unsigned sq_entries;
358 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700359 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600360 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100361 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700362 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600363
Jens Axboee9418942021-02-19 12:33:30 -0700364 /* hashed buffered write serialization */
365 struct io_wq_hash *hash_map;
366
Jens Axboede0617e2019-04-06 21:51:27 -0600367 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600368 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700369 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700370
Jens Axboead3eb2c2019-12-18 17:12:20 -0700371 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700372 } ____cacheline_aligned_in_smp;
373
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700374 struct {
375 struct mutex uring_lock;
376 wait_queue_head_t wait;
377 } ____cacheline_aligned_in_smp;
378
379 struct io_submit_state submit_state;
380
Hristo Venev75b28af2019-08-26 17:23:46 +0000381 struct io_rings *rings;
382
Jens Axboe2aede0e2020-09-14 10:45:53 -0600383 /*
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700384 * For SQPOLL usage
Jens Axboe2aede0e2020-09-14 10:45:53 -0600385 */
386 struct task_struct *sqo_task;
387
388 /* Only used for accounting purposes */
389 struct mm_struct *mm_account;
390
Jens Axboe534ca6d2020-09-02 13:52:19 -0600391 struct io_sq_data *sq_data; /* if using sq thread polling */
392
Jens Axboe90554202020-09-03 12:12:41 -0600393 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600394 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700395
Jens Axboe6b063142019-01-10 22:13:58 -0700396 /*
397 * If used, fixed file set. Writers must ensure that ->refs is dead,
398 * readers must ensure that ->refs is alive as long as the file* is
399 * used. Only updated through io_uring_register(2).
400 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000401 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700402 unsigned nr_user_files;
403
Jens Axboeedafcce2019-01-09 09:16:05 -0700404 /* if used, fixed mapped user buffers */
405 unsigned nr_user_bufs;
406 struct io_mapped_ubuf *user_bufs;
407
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408 struct user_struct *user;
409
Jens Axboe0f158b42020-05-14 17:18:39 -0600410 struct completion ref_comp;
411 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700412
413#if defined(CONFIG_UNIX)
414 struct socket *ring_sock;
415#endif
416
Jens Axboe5a2e7452020-02-23 16:23:11 -0700417 struct idr io_buffer_idr;
418
Jens Axboe071698e2020-01-28 10:04:42 -0700419 struct idr personality_idr;
420
Jens Axboe206aefd2019-11-07 18:27:42 -0700421 struct {
422 unsigned cached_cq_tail;
423 unsigned cq_entries;
424 unsigned cq_mask;
425 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500426 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700427 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700428 struct wait_queue_head cq_wait;
429 struct fasync_struct *cq_fasync;
430 struct eventfd_ctx *cq_ev_fd;
431 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432
433 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700435
Jens Axboedef596e2019-01-09 08:59:42 -0700436 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300437 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700438 * io_uring instances that don't use IORING_SETUP_SQPOLL.
439 * For SQPOLL, only the single threaded io_sq_thread() will
440 * manipulate the list, hence no extra locking is needed there.
441 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300442 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700443 struct hlist_head *cancel_hash;
444 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700445 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600446
447 spinlock_t inflight_lock;
448 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700449 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600450
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000451 struct delayed_work rsrc_put_work;
452 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000453 struct list_head rsrc_ref_list;
454 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600455
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200456 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700457
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700458 /* exit task_work */
459 struct callback_head *exit_task_work;
460
Jens Axboee9418942021-02-19 12:33:30 -0700461 struct wait_queue_head hash_wait;
462
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700463 /* Keep this last, we don't need it for the fast path */
464 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700465};
466
Jens Axboe09bb8392019-03-13 12:39:28 -0600467/*
468 * First field must be the file pointer in all the
469 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
470 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700471struct io_poll_iocb {
472 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000473 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700474 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600475 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700476 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700477 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700478};
479
Pavel Begunkov018043b2020-10-27 23:17:18 +0000480struct io_poll_remove {
481 struct file *file;
482 u64 addr;
483};
484
Jens Axboeb5dba592019-12-11 14:02:38 -0700485struct io_close {
486 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700487 int fd;
488};
489
Jens Axboead8a48a2019-11-15 08:49:11 -0700490struct io_timeout_data {
491 struct io_kiocb *req;
492 struct hrtimer timer;
493 struct timespec64 ts;
494 enum hrtimer_mode mode;
495};
496
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700497struct io_accept {
498 struct file *file;
499 struct sockaddr __user *addr;
500 int __user *addr_len;
501 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600502 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700503};
504
505struct io_sync {
506 struct file *file;
507 loff_t len;
508 loff_t off;
509 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700510 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700511};
512
Jens Axboefbf23842019-12-17 18:45:56 -0700513struct io_cancel {
514 struct file *file;
515 u64 addr;
516};
517
Jens Axboeb29472e2019-12-17 18:50:29 -0700518struct io_timeout {
519 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300520 u32 off;
521 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300522 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000523 /* head of the link, used by linked timeouts only */
524 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700525};
526
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100527struct io_timeout_rem {
528 struct file *file;
529 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000530
531 /* timeout update */
532 struct timespec64 ts;
533 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100534};
535
Jens Axboe9adbd452019-12-20 08:45:55 -0700536struct io_rw {
537 /* NOTE: kiocb has the file as the first member, so don't do it here */
538 struct kiocb kiocb;
539 u64 addr;
540 u64 len;
541};
542
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700543struct io_connect {
544 struct file *file;
545 struct sockaddr __user *addr;
546 int addr_len;
547};
548
Jens Axboee47293f2019-12-20 08:58:21 -0700549struct io_sr_msg {
550 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700551 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300552 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700553 void __user *buf;
554 };
Jens Axboee47293f2019-12-20 08:58:21 -0700555 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700556 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700557 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700558 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700559};
560
Jens Axboe15b71ab2019-12-11 11:20:36 -0700561struct io_open {
562 struct file *file;
563 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700564 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700565 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600566 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700567};
568
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000569struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700570 struct file *file;
571 u64 arg;
572 u32 nr_args;
573 u32 offset;
574};
575
Jens Axboe4840e412019-12-25 22:03:45 -0700576struct io_fadvise {
577 struct file *file;
578 u64 offset;
579 u32 len;
580 u32 advice;
581};
582
Jens Axboec1ca7572019-12-25 22:18:28 -0700583struct io_madvise {
584 struct file *file;
585 u64 addr;
586 u32 len;
587 u32 advice;
588};
589
Jens Axboe3e4827b2020-01-08 15:18:09 -0700590struct io_epoll {
591 struct file *file;
592 int epfd;
593 int op;
594 int fd;
595 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596};
597
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300598struct io_splice {
599 struct file *file_out;
600 struct file *file_in;
601 loff_t off_out;
602 loff_t off_in;
603 u64 len;
604 unsigned int flags;
605};
606
Jens Axboeddf0322d2020-02-23 16:41:33 -0700607struct io_provide_buf {
608 struct file *file;
609 __u64 addr;
610 __s32 len;
611 __u32 bgid;
612 __u16 nbufs;
613 __u16 bid;
614};
615
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700616struct io_statx {
617 struct file *file;
618 int dfd;
619 unsigned int mask;
620 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700621 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700622 struct statx __user *buffer;
623};
624
Jens Axboe36f4fa62020-09-05 11:14:22 -0600625struct io_shutdown {
626 struct file *file;
627 int how;
628};
629
Jens Axboe80a261f2020-09-28 14:23:58 -0600630struct io_rename {
631 struct file *file;
632 int old_dfd;
633 int new_dfd;
634 struct filename *oldpath;
635 struct filename *newpath;
636 int flags;
637};
638
Jens Axboe14a11432020-09-28 14:27:37 -0600639struct io_unlink {
640 struct file *file;
641 int dfd;
642 int flags;
643 struct filename *filename;
644};
645
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300646struct io_completion {
647 struct file *file;
648 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300649 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300650};
651
Jens Axboef499a022019-12-02 16:28:46 -0700652struct io_async_connect {
653 struct sockaddr_storage address;
654};
655
Jens Axboe03b12302019-12-02 18:50:25 -0700656struct io_async_msghdr {
657 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000658 /* points to an allocated iov, if NULL we use fast_iov instead */
659 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700660 struct sockaddr __user *uaddr;
661 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700662 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700663};
664
Jens Axboef67676d2019-12-02 11:03:47 -0700665struct io_async_rw {
666 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600667 const struct iovec *free_iovec;
668 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600669 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600670 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700671};
672
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300673enum {
674 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
675 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
676 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
677 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
678 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700679 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300681 REQ_F_FAIL_LINK_BIT,
682 REQ_F_INFLIGHT_BIT,
683 REQ_F_CUR_POS_BIT,
684 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300685 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300686 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300687 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700688 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700689 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600690 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800691 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100692 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000693 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700694
695 /* not a real bit, just to check we're not overflowing the space */
696 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300697};
698
699enum {
700 /* ctx owns file */
701 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
702 /* drain existing IO first */
703 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
704 /* linked sqes */
705 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
706 /* doesn't sever on completion < 0 */
707 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
708 /* IOSQE_ASYNC */
709 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700710 /* IOSQE_BUFFER_SELECT */
711 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300712
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300713 /* fail rest of links */
714 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
715 /* on inflight list */
716 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
717 /* read/write uses file position */
718 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
719 /* must not punt to workers */
720 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100721 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300722 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300723 /* regular file */
724 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300725 /* needs cleanup */
726 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700727 /* already went through poll handler */
728 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700729 /* buffer already selected */
730 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600731 /* doesn't need file table for this request */
732 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800733 /* io_wq_work is initialized */
734 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100735 /* linked timeout is active, i.e. prepared by link's head */
736 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000737 /* completion is deferred through io_comp_state */
738 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700739};
740
741struct async_poll {
742 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600743 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300744};
745
Jens Axboe7cbf1722021-02-10 00:03:20 +0000746struct io_task_work {
747 struct io_wq_work_node node;
748 task_work_func_t func;
749};
750
Jens Axboe09bb8392019-03-13 12:39:28 -0600751/*
752 * NOTE! Each of the iocb union members has the file pointer
753 * as the first entry in their struct definition. So you can
754 * access the file pointer through any of the sub-structs,
755 * or directly as just 'ki_filp' in this struct.
756 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700758 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600759 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700760 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700761 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000762 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700763 struct io_accept accept;
764 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700765 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700766 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100767 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700768 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700769 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700770 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700771 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000772 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700773 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700774 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700775 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300776 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700777 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700778 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600779 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600780 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600781 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300782 /* use only after cleaning per-op data, see io_clean_op() */
783 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700784 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700785
Jens Axboee8c2bc12020-08-15 18:44:09 -0700786 /* opcode allocated if it needs to store data for async defer */
787 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700788 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800789 /* polled IO has completed */
790 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700792 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300793 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700794
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300795 struct io_ring_ctx *ctx;
796 unsigned int flags;
797 refcount_t refs;
798 struct task_struct *task;
799 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700800
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000801 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000802 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700803
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300804 /*
805 * 1. used with ctx->iopoll_list with reads/writes
806 * 2. to track reqs with ->files (see io_op_def::file_table)
807 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300808 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000809 union {
810 struct io_task_work io_task_work;
811 struct callback_head task_work;
812 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300813 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
814 struct hlist_node hash_node;
815 struct async_poll *apoll;
816 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700817};
818
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300819struct io_defer_entry {
820 struct list_head list;
821 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300822 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300823};
824
Jens Axboed3656342019-12-18 09:50:26 -0700825struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700826 /* needs req->file assigned */
827 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700828 /* hash wq insertion if file is a regular file */
829 unsigned hash_reg_file : 1;
830 /* unbound wq insertion if file is a non-regular file */
831 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700832 /* opcode is not supported by this kernel */
833 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700834 /* set if opcode supports polled "wait" */
835 unsigned pollin : 1;
836 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700837 /* op supports buffer selection */
838 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 /* must always have async data allocated */
840 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600841 /* should block plug */
842 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700843 /* size of async data needed, if any */
844 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700845};
846
Jens Axboe09186822020-10-13 15:01:40 -0600847static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_NOP] = {},
849 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_file = 1,
851 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700852 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700853 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700854 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600855 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700856 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700859 .needs_file = 1,
860 .hash_reg_file = 1,
861 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700862 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700863 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600864 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700865 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700871 .needs_file = 1,
872 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700873 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600874 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700875 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700876 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300877 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700878 .needs_file = 1,
879 .hash_reg_file = 1,
880 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700881 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600882 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700883 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700884 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300885 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700886 .needs_file = 1,
887 .unbound_nonreg_file = 1,
888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_POLL_REMOVE] = {},
890 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700891 .needs_file = 1,
892 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300893 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700894 .needs_file = 1,
895 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700896 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700897 .needs_async_data = 1,
898 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700899 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300900 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700901 .needs_file = 1,
902 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700903 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700904 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700907 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300908 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700909 .needs_async_data = 1,
910 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700911 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000912 [IORING_OP_TIMEOUT_REMOVE] = {
913 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700918 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700919 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300920 [IORING_OP_ASYNC_CANCEL] = {},
921 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700922 .needs_async_data = 1,
923 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700924 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700926 .needs_file = 1,
927 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700928 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700929 .needs_async_data = 1,
930 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700931 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300932 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700933 .needs_file = 1,
934 },
Jens Axboe44526be2021-02-15 13:32:18 -0700935 [IORING_OP_OPENAT] = {},
936 [IORING_OP_CLOSE] = {},
937 [IORING_OP_FILES_UPDATE] = {},
938 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300939 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700940 .needs_file = 1,
941 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700942 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700943 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600944 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700945 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700946 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300947 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700948 .needs_file = 1,
949 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700950 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600951 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700952 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700955 .needs_file = 1,
956 },
Jens Axboe44526be2021-02-15 13:32:18 -0700957 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300958 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700959 .needs_file = 1,
960 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700961 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700962 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300963 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700964 .needs_file = 1,
965 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700966 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700967 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700968 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300969 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700970 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700971 [IORING_OP_EPOLL_CTL] = {
972 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700973 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300974 [IORING_OP_SPLICE] = {
975 .needs_file = 1,
976 .hash_reg_file = 1,
977 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700978 },
979 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700980 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300981 [IORING_OP_TEE] = {
982 .needs_file = 1,
983 .hash_reg_file = 1,
984 .unbound_nonreg_file = 1,
985 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600986 [IORING_OP_SHUTDOWN] = {
987 .needs_file = 1,
988 },
Jens Axboe44526be2021-02-15 13:32:18 -0700989 [IORING_OP_RENAMEAT] = {},
990 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700991};
992
Pavel Begunkov9936c7c2021-02-04 13:51:56 +0000993static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
994 struct task_struct *task,
995 struct files_struct *files);
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700996static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000997static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +0000998static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000999 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00001000static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001001
Pavel Begunkov23faba32021-02-11 18:28:22 +00001002static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001003static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001004static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001005static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001006static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001007static void io_dismantle_req(struct io_kiocb *req);
1008static void io_put_task(struct task_struct *task, int nr);
1009static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001010static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001011static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001012static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001013static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001014 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001015 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001016static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001017static struct file *io_file_get(struct io_submit_state *state,
1018 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001019static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001020static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001021
Pavel Begunkov847595d2021-02-04 13:52:06 +00001022static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1023 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001024static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1025 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001026 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001027static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001028static void io_submit_flush_completions(struct io_comp_state *cs,
1029 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001030
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031static struct kmem_cache *req_cachep;
1032
Jens Axboe09186822020-10-13 15:01:40 -06001033static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001034
1035struct sock *io_uring_get_socket(struct file *file)
1036{
1037#if defined(CONFIG_UNIX)
1038 if (file->f_op == &io_uring_fops) {
1039 struct io_ring_ctx *ctx = file->private_data;
1040
1041 return ctx->ring_sock->sk;
1042 }
1043#endif
1044 return NULL;
1045}
1046EXPORT_SYMBOL(io_uring_get_socket);
1047
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001048#define io_for_each_link(pos, head) \
1049 for (pos = (head); pos; pos = pos->link)
1050
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001051static inline void io_clean_op(struct io_kiocb *req)
1052{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001053 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001054 __io_clean_op(req);
1055}
1056
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001057static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001058{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001059 struct io_ring_ctx *ctx = req->ctx;
1060
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001061 if (!req->fixed_rsrc_refs) {
1062 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1063 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001064 }
1065}
1066
Pavel Begunkov08d23632020-11-06 13:00:22 +00001067static bool io_match_task(struct io_kiocb *head,
1068 struct task_struct *task,
1069 struct files_struct *files)
1070{
1071 struct io_kiocb *req;
1072
Jens Axboe84965ff2021-01-23 15:51:11 -07001073 if (task && head->task != task) {
1074 /* in terms of cancelation, always match if req task is dead */
1075 if (head->task->flags & PF_EXITING)
1076 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001077 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001078 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001079 if (!files)
1080 return true;
1081
1082 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001083 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1084 continue;
1085 if (req->file && req->file->f_op == &io_uring_fops)
1086 return true;
Jens Axboe4379bf82021-02-15 13:40:22 -07001087 if (req->task->files == files)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001088 return true;
1089 }
1090 return false;
1091}
1092
Jens Axboec40f6372020-06-25 15:39:59 -06001093static inline void req_set_fail_links(struct io_kiocb *req)
1094{
1095 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1096 req->flags |= REQ_F_FAIL_LINK;
1097}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001098
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001099static inline void __io_req_init_async(struct io_kiocb *req)
1100{
1101 memset(&req->work, 0, sizeof(req->work));
1102 req->flags |= REQ_F_WORK_INITIALIZED;
1103}
1104
Jens Axboe1e6fa522020-10-15 08:46:24 -06001105/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001106 * Note: must call io_req_init_async() for the first time you
1107 * touch any members of io_wq_work.
1108 */
1109static inline void io_req_init_async(struct io_kiocb *req)
1110{
1111 if (req->flags & REQ_F_WORK_INITIALIZED)
1112 return;
1113
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001114 __io_req_init_async(req);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001115}
1116
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1118{
1119 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1120
Jens Axboe0f158b42020-05-14 17:18:39 -06001121 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122}
1123
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001124static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1125{
1126 return !req->timeout.off;
1127}
1128
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1130{
1131 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001132 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133
1134 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1135 if (!ctx)
1136 return NULL;
1137
Jens Axboe78076bb2019-12-04 19:56:40 -07001138 /*
1139 * Use 5 bits less than the max cq entries, that should give us around
1140 * 32 entries per hash list if totally full and uniformly spread.
1141 */
1142 hash_bits = ilog2(p->cq_entries);
1143 hash_bits -= 5;
1144 if (hash_bits <= 0)
1145 hash_bits = 1;
1146 ctx->cancel_hash_bits = hash_bits;
1147 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1148 GFP_KERNEL);
1149 if (!ctx->cancel_hash)
1150 goto err;
1151 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1152
Roman Gushchin21482892019-05-07 10:01:48 -07001153 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001154 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1155 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156
1157 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001158 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001159 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001161 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001162 init_completion(&ctx->ref_comp);
1163 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001164 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001165 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001166 mutex_init(&ctx->uring_lock);
1167 init_waitqueue_head(&ctx->wait);
1168 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001169 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001170 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001171 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001172 spin_lock_init(&ctx->inflight_lock);
1173 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001174 spin_lock_init(&ctx->rsrc_ref_lock);
1175 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001176 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1177 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001178 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001179 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001181err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001182 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001183 kfree(ctx);
1184 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001185}
1186
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001187static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001188{
Jens Axboe2bc99302020-07-09 09:43:27 -06001189 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1190 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001191
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001192 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001193 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001194 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001195
Bob Liu9d858b22019-11-13 18:06:25 +08001196 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001197}
1198
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001199static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001200{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001201 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001202 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001203
Jens Axboe4379bf82021-02-15 13:40:22 -07001204 if (req->work.creds) {
1205 put_cred(req->work.creds);
1206 req->work.creds = NULL;
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001207 }
1208 if (req->flags & REQ_F_INFLIGHT) {
1209 struct io_ring_ctx *ctx = req->ctx;
1210 struct io_uring_task *tctx = req->task->io_uring;
1211 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001212
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001213 spin_lock_irqsave(&ctx->inflight_lock, flags);
1214 list_del(&req->inflight_entry);
1215 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1216 req->flags &= ~REQ_F_INFLIGHT;
1217 if (atomic_read(&tctx->in_idle))
1218 wake_up(&tctx->wait);
1219 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001220
Pavel Begunkove86d0042021-02-01 18:59:54 +00001221 req->flags &= ~REQ_F_WORK_INITIALIZED;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001222}
1223
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001224static void io_req_track_inflight(struct io_kiocb *req)
1225{
1226 struct io_ring_ctx *ctx = req->ctx;
1227
1228 if (!(req->flags & REQ_F_INFLIGHT)) {
1229 io_req_init_async(req);
1230 req->flags |= REQ_F_INFLIGHT;
1231
1232 spin_lock_irq(&ctx->inflight_lock);
1233 list_add(&req->inflight_entry, &ctx->inflight_list);
1234 spin_unlock_irq(&ctx->inflight_lock);
1235 }
1236}
1237
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001238static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001239{
Jens Axboed3656342019-12-18 09:50:26 -07001240 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001241 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001242
Pavel Begunkov16d59802020-07-12 16:16:47 +03001243 io_req_init_async(req);
1244
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001245 if (req->flags & REQ_F_FORCE_ASYNC)
1246 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1247
Jens Axboed3656342019-12-18 09:50:26 -07001248 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001249 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001250 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001251 } else {
1252 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001253 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001254 }
Jens Axboe4379bf82021-02-15 13:40:22 -07001255 if (!req->work.creds)
1256 req->work.creds = get_current_cred();
Jens Axboe561fb042019-10-24 07:25:42 -06001257}
1258
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001259static void io_prep_async_link(struct io_kiocb *req)
1260{
1261 struct io_kiocb *cur;
1262
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001263 io_for_each_link(cur, req)
1264 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001265}
1266
Jens Axboe7271ef32020-08-10 09:55:22 -06001267static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001268{
Jackie Liua197f662019-11-08 08:09:12 -07001269 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001270 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001271 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001272
Jens Axboe3bfe6102021-02-16 14:15:30 -07001273 BUG_ON(!tctx);
1274 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001275
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001276 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1277 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001278 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001279 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001280}
1281
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001282static void io_queue_async_work(struct io_kiocb *req)
1283{
Jens Axboe7271ef32020-08-10 09:55:22 -06001284 struct io_kiocb *link;
1285
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001286 /* init ->work of the whole link before punting */
1287 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001288 link = __io_queue_async_work(req);
1289
1290 if (link)
1291 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001292}
1293
Jens Axboe5262f562019-09-17 12:26:57 -06001294static void io_kill_timeout(struct io_kiocb *req)
1295{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001296 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001297 int ret;
1298
Jens Axboee8c2bc12020-08-15 18:44:09 -07001299 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001300 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001301 atomic_set(&req->ctx->cq_timeouts,
1302 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001303 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001304 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001305 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001306 }
1307}
1308
Jens Axboe76e1b642020-09-26 15:05:03 -06001309/*
1310 * Returns true if we found and killed one or more timeouts
1311 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001312static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1313 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001314{
1315 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001316 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001317
1318 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001319 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001320 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001321 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001322 canceled++;
1323 }
Jens Axboef3606e32020-09-22 08:18:24 -06001324 }
Jens Axboe5262f562019-09-17 12:26:57 -06001325 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001326 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001327}
1328
Pavel Begunkov04518942020-05-26 20:34:05 +03001329static void __io_queue_deferred(struct io_ring_ctx *ctx)
1330{
1331 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001332 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1333 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001334
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001335 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001336 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001337 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001338 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001339 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001340 } while (!list_empty(&ctx->defer_list));
1341}
1342
Pavel Begunkov360428f2020-05-30 14:54:17 +03001343static void io_flush_timeouts(struct io_ring_ctx *ctx)
1344{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001345 u32 seq;
1346
1347 if (list_empty(&ctx->timeout_list))
1348 return;
1349
1350 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1351
1352 do {
1353 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001354 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001355 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001356
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001357 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001358 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001359
1360 /*
1361 * Since seq can easily wrap around over time, subtract
1362 * the last seq at which timeouts were flushed before comparing.
1363 * Assuming not more than 2^31-1 events have happened since,
1364 * these subtractions won't have wrapped, so we can check if
1365 * target is in [last_seq, current_seq] by comparing the two.
1366 */
1367 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1368 events_got = seq - ctx->cq_last_tm_flush;
1369 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001370 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001371
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001372 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001373 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001374 } while (!list_empty(&ctx->timeout_list));
1375
1376 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001377}
1378
Jens Axboede0617e2019-04-06 21:51:27 -06001379static void io_commit_cqring(struct io_ring_ctx *ctx)
1380{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001381 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001382
1383 /* order cqe stores with ring update */
1384 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001385
Pavel Begunkov04518942020-05-26 20:34:05 +03001386 if (unlikely(!list_empty(&ctx->defer_list)))
1387 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001388}
1389
Jens Axboe90554202020-09-03 12:12:41 -06001390static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1391{
1392 struct io_rings *r = ctx->rings;
1393
1394 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1395}
1396
Pavel Begunkov888aae22021-01-19 13:32:39 +00001397static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1398{
1399 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1400}
1401
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1403{
Hristo Venev75b28af2019-08-26 17:23:46 +00001404 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405 unsigned tail;
1406
Stefan Bühler115e12e2019-04-24 23:54:18 +02001407 /*
1408 * writes to the cq entry need to come after reading head; the
1409 * control dependency is enough as we're using WRITE_ONCE to
1410 * fill the cq entry
1411 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001412 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413 return NULL;
1414
Pavel Begunkov888aae22021-01-19 13:32:39 +00001415 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001416 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417}
1418
Jens Axboef2842ab2020-01-08 11:04:00 -07001419static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1420{
Jens Axboef0b493e2020-02-01 21:30:11 -07001421 if (!ctx->cq_ev_fd)
1422 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001423 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1424 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001425 if (!ctx->eventfd_async)
1426 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001427 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001428}
1429
Jens Axboeb41e9852020-02-17 09:52:41 -07001430static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001431{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001432 /* see waitqueue_active() comment */
1433 smp_mb();
1434
Jens Axboe8c838782019-03-12 15:48:16 -06001435 if (waitqueue_active(&ctx->wait))
1436 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001437 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1438 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001439 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001440 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001441 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001442 wake_up_interruptible(&ctx->cq_wait);
1443 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1444 }
Jens Axboe8c838782019-03-12 15:48:16 -06001445}
1446
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001447static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1448{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001449 /* see waitqueue_active() comment */
1450 smp_mb();
1451
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001452 if (ctx->flags & IORING_SETUP_SQPOLL) {
1453 if (waitqueue_active(&ctx->wait))
1454 wake_up(&ctx->wait);
1455 }
1456 if (io_should_trigger_evfd(ctx))
1457 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001458 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001459 wake_up_interruptible(&ctx->cq_wait);
1460 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1461 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001462}
1463
Jens Axboec4a2ed72019-11-21 21:01:26 -07001464/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001465static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1466 struct task_struct *tsk,
1467 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001469 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001470 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001471 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001472 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001473 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001474 LIST_HEAD(list);
1475
Pavel Begunkove23de152020-12-17 00:24:37 +00001476 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1477 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001478
Jens Axboeb18032b2021-01-24 16:58:56 -07001479 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001480 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001481 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001482 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001483 continue;
1484
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001485 cqe = io_get_cqring(ctx);
1486 if (!cqe && !force)
1487 break;
1488
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001489 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001490 if (cqe) {
1491 WRITE_ONCE(cqe->user_data, req->user_data);
1492 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001493 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001494 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001495 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001496 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001497 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001498 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001499 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001500 }
1501
Pavel Begunkov09e88402020-12-17 00:24:38 +00001502 all_flushed = list_empty(&ctx->cq_overflow_list);
1503 if (all_flushed) {
1504 clear_bit(0, &ctx->sq_check_overflow);
1505 clear_bit(0, &ctx->cq_check_overflow);
1506 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1507 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001508
Jens Axboeb18032b2021-01-24 16:58:56 -07001509 if (posted)
1510 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001511 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001512 if (posted)
1513 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001514
1515 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001516 req = list_first_entry(&list, struct io_kiocb, compl.list);
1517 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001518 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001519 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001520
Pavel Begunkov09e88402020-12-17 00:24:38 +00001521 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001522}
1523
Pavel Begunkov6c503152021-01-04 20:36:36 +00001524static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1525 struct task_struct *tsk,
1526 struct files_struct *files)
1527{
1528 if (test_bit(0, &ctx->cq_check_overflow)) {
1529 /* iopoll syncs against uring_lock, not completion_lock */
1530 if (ctx->flags & IORING_SETUP_IOPOLL)
1531 mutex_lock(&ctx->uring_lock);
1532 __io_cqring_overflow_flush(ctx, force, tsk, files);
1533 if (ctx->flags & IORING_SETUP_IOPOLL)
1534 mutex_unlock(&ctx->uring_lock);
1535 }
1536}
1537
Jens Axboebcda7ba2020-02-23 16:42:51 -07001538static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001539{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001540 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001541 struct io_uring_cqe *cqe;
1542
Jens Axboe78e19bb2019-11-06 15:21:34 -07001543 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001544
Jens Axboe2b188cc2019-01-07 10:46:33 -07001545 /*
1546 * If we can't get a cq entry, userspace overflowed the
1547 * submission (by quite a lot). Increment the overflow count in
1548 * the ring.
1549 */
1550 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001551 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001552 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001554 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001555 } else if (ctx->cq_overflow_flushed ||
1556 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001557 /*
1558 * If we're in ring overflow flush mode, or in task cancel mode,
1559 * then we cannot store the request for later flushing, we need
1560 * to drop it on the floor.
1561 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001562 ctx->cached_cq_overflow++;
1563 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001564 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001565 if (list_empty(&ctx->cq_overflow_list)) {
1566 set_bit(0, &ctx->sq_check_overflow);
1567 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001568 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001569 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001570 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001571 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001572 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001573 refcount_inc(&req->refs);
1574 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575 }
1576}
1577
Jens Axboebcda7ba2020-02-23 16:42:51 -07001578static void io_cqring_fill_event(struct io_kiocb *req, long res)
1579{
1580 __io_cqring_fill_event(req, res, 0);
1581}
1582
Jens Axboec7dae4b2021-02-09 19:53:37 -07001583static inline void io_req_complete_post(struct io_kiocb *req, long res,
1584 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001585{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001586 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587 unsigned long flags;
1588
1589 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001590 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001592 /*
1593 * If we're the last reference to this request, add to our locked
1594 * free_list cache.
1595 */
1596 if (refcount_dec_and_test(&req->refs)) {
1597 struct io_comp_state *cs = &ctx->submit_state.comp;
1598
1599 io_dismantle_req(req);
1600 io_put_task(req->task, 1);
1601 list_add(&req->compl.list, &cs->locked_free_list);
1602 cs->locked_free_nr++;
1603 } else
1604 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1606
Jens Axboe8c838782019-03-12 15:48:16 -06001607 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001608 if (req) {
1609 io_queue_next(req);
1610 percpu_ref_put(&ctx->refs);
1611 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001612}
1613
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001614static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001615 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001616{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001617 io_clean_op(req);
1618 req->result = res;
1619 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001620 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001621}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001622
Pavel Begunkov889fca72021-02-10 00:03:09 +00001623static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1624 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001625{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001626 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1627 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001628 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001629 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001630}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001631
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001632static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001633{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001634 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001635}
1636
Jens Axboec7dae4b2021-02-09 19:53:37 -07001637static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001638{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001639 struct io_submit_state *state = &ctx->submit_state;
1640 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001641 struct io_kiocb *req = NULL;
1642
Jens Axboec7dae4b2021-02-09 19:53:37 -07001643 /*
1644 * If we have more than a batch's worth of requests in our IRQ side
1645 * locked cache, grab the lock and move them over to our submission
1646 * side cache.
1647 */
1648 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1649 spin_lock_irq(&ctx->completion_lock);
1650 list_splice_init(&cs->locked_free_list, &cs->free_list);
1651 cs->locked_free_nr = 0;
1652 spin_unlock_irq(&ctx->completion_lock);
1653 }
1654
1655 while (!list_empty(&cs->free_list)) {
1656 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001657 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001658 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001659 state->reqs[state->free_reqs++] = req;
1660 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1661 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001664 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665}
1666
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001667static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001669 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001671 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001673 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001674 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001675 int ret;
1676
Jens Axboec7dae4b2021-02-09 19:53:37 -07001677 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001678 goto got_req;
1679
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001680 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1681 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001682
1683 /*
1684 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1685 * retry single alloc to be on the safe side.
1686 */
1687 if (unlikely(ret <= 0)) {
1688 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1689 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001690 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001691 ret = 1;
1692 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001693 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001694 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001695got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001696 state->free_reqs--;
1697 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001698}
1699
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001700static inline void io_put_file(struct io_kiocb *req, struct file *file,
1701 bool fixed)
1702{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001703 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001704 fput(file);
1705}
1706
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001707static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001709 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001710
Jens Axboee8c2bc12020-08-15 18:44:09 -07001711 if (req->async_data)
1712 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001713 if (req->file)
1714 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001715 if (req->fixed_rsrc_refs)
1716 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001717 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001718}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001719
Pavel Begunkov7c660732021-01-25 11:42:21 +00001720static inline void io_put_task(struct task_struct *task, int nr)
1721{
1722 struct io_uring_task *tctx = task->io_uring;
1723
1724 percpu_counter_sub(&tctx->inflight, nr);
1725 if (unlikely(atomic_read(&tctx->in_idle)))
1726 wake_up(&tctx->wait);
1727 put_task_struct_many(task, nr);
1728}
1729
Pavel Begunkov216578e2020-10-13 09:44:00 +01001730static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001731{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001732 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001733
Pavel Begunkov216578e2020-10-13 09:44:00 +01001734 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001735 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001736
Pavel Begunkov3893f392021-02-10 00:03:15 +00001737 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001738 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001739}
1740
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001741static inline void io_remove_next_linked(struct io_kiocb *req)
1742{
1743 struct io_kiocb *nxt = req->link;
1744
1745 req->link = nxt->link;
1746 nxt->link = NULL;
1747}
1748
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001749static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001750{
Jackie Liua197f662019-11-08 08:09:12 -07001751 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001752 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001753 bool cancelled = false;
1754 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001755
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001756 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001757 link = req->link;
1758
Pavel Begunkov900fad42020-10-19 16:39:16 +01001759 /*
1760 * Can happen if a linked timeout fired and link had been like
1761 * req -> link t-out -> link t-out [-> ...]
1762 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001763 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1764 struct io_timeout_data *io = link->async_data;
1765 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001766
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001767 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001768 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001769 ret = hrtimer_try_to_cancel(&io->timer);
1770 if (ret != -1) {
1771 io_cqring_fill_event(link, -ECANCELED);
1772 io_commit_cqring(ctx);
1773 cancelled = true;
1774 }
1775 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001776 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001777 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001778
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001779 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001780 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001781 io_put_req(link);
1782 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001783}
1784
Jens Axboe4d7dd462019-11-20 13:03:52 -07001785
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001786static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001787{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001788 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001789 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001790 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001791
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001792 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001793 link = req->link;
1794 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06001795
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001796 while (link) {
1797 nxt = link->link;
1798 link->link = NULL;
1799
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001800 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001801 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001802
1803 /*
1804 * It's ok to free under spinlock as they're not linked anymore,
1805 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1806 * work.fs->lock.
1807 */
1808 if (link->flags & REQ_F_WORK_INITIALIZED)
1809 io_put_req_deferred(link, 2);
1810 else
1811 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001812 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001813 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001814 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001815 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001816
Jens Axboe2665abf2019-11-05 12:40:47 -07001817 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001818}
1819
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001820static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001821{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001822 if (req->flags & REQ_F_LINK_TIMEOUT)
1823 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001824
Jens Axboe9e645e112019-05-10 16:07:28 -06001825 /*
1826 * If LINK is set, we have dependent requests in this chain. If we
1827 * didn't fail this request, queue the first one up, moving any other
1828 * dependencies to the next request. In case of failure, fail the rest
1829 * of the chain.
1830 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001831 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
1832 struct io_kiocb *nxt = req->link;
1833
1834 req->link = NULL;
1835 return nxt;
1836 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001837 io_fail_links(req);
1838 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001839}
Jens Axboe2665abf2019-11-05 12:40:47 -07001840
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001841static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001842{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001843 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001844 return NULL;
1845 return __io_req_find_next(req);
1846}
1847
Jens Axboe7cbf1722021-02-10 00:03:20 +00001848static bool __tctx_task_work(struct io_uring_task *tctx)
1849{
Jens Axboe65453d12021-02-10 00:03:21 +00001850 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001851 struct io_wq_work_list list;
1852 struct io_wq_work_node *node;
1853
1854 if (wq_list_empty(&tctx->task_list))
1855 return false;
1856
Jens Axboe0b81e802021-02-16 10:33:53 -07001857 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001858 list = tctx->task_list;
1859 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001860 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001861
1862 node = list.first;
1863 while (node) {
1864 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00001865 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001866 struct io_kiocb *req;
1867
1868 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00001869 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001870 req->task_work.func(&req->task_work);
1871 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001872
1873 if (!ctx) {
1874 ctx = this_ctx;
1875 } else if (ctx != this_ctx) {
1876 mutex_lock(&ctx->uring_lock);
1877 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1878 mutex_unlock(&ctx->uring_lock);
1879 ctx = this_ctx;
1880 }
1881 }
1882
1883 if (ctx && ctx->submit_state.comp.nr) {
1884 mutex_lock(&ctx->uring_lock);
1885 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1886 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001887 }
1888
1889 return list.first != NULL;
1890}
1891
1892static void tctx_task_work(struct callback_head *cb)
1893{
1894 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
1895
Jens Axboe1d5f3602021-02-26 14:54:16 -07001896 clear_bit(0, &tctx->task_state);
1897
Jens Axboe7cbf1722021-02-10 00:03:20 +00001898 while (__tctx_task_work(tctx))
1899 cond_resched();
Jens Axboe7cbf1722021-02-10 00:03:20 +00001900}
1901
1902static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
1903 enum task_work_notify_mode notify)
1904{
1905 struct io_uring_task *tctx = tsk->io_uring;
1906 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07001907 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001908 int ret;
1909
1910 WARN_ON_ONCE(!tctx);
1911
Jens Axboe0b81e802021-02-16 10:33:53 -07001912 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001913 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001914 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001915
1916 /* task_work already pending, we're done */
1917 if (test_bit(0, &tctx->task_state) ||
1918 test_and_set_bit(0, &tctx->task_state))
1919 return 0;
1920
1921 if (!task_work_add(tsk, &tctx->task_work, notify))
1922 return 0;
1923
1924 /*
1925 * Slow path - we failed, find and delete work. if the work is not
1926 * in the list, it got run and we're fine.
1927 */
1928 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07001929 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001930 wq_list_for_each(node, prev, &tctx->task_list) {
1931 if (&req->io_task_work.node == node) {
1932 wq_list_del(&tctx->task_list, node, prev);
1933 ret = 1;
1934 break;
1935 }
1936 }
Jens Axboe0b81e802021-02-16 10:33:53 -07001937 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001938 clear_bit(0, &tctx->task_state);
1939 return ret;
1940}
1941
Jens Axboe355fb9e2020-10-22 20:19:35 -06001942static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06001943{
1944 struct task_struct *tsk = req->task;
1945 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06001946 enum task_work_notify_mode notify;
1947 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06001948
Jens Axboe6200b0a2020-09-13 14:38:30 -06001949 if (tsk->flags & PF_EXITING)
1950 return -ESRCH;
1951
Jens Axboec2c4c832020-07-01 15:37:11 -06001952 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001953 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1954 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1955 * processing task_work. There's no reliable way to tell if TWA_RESUME
1956 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001957 */
Jens Axboe91989c72020-10-16 09:02:26 -06001958 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001959 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06001960 notify = TWA_SIGNAL;
1961
Jens Axboe7cbf1722021-02-10 00:03:20 +00001962 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001963 if (!ret)
1964 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001965
Jens Axboec2c4c832020-07-01 15:37:11 -06001966 return ret;
1967}
1968
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001969static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00001970 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001971{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001972 struct io_ring_ctx *ctx = req->ctx;
1973 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001974
1975 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001976 do {
1977 head = READ_ONCE(ctx->exit_task_work);
1978 req->task_work.next = head;
1979 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001980}
1981
Jens Axboec40f6372020-06-25 15:39:59 -06001982static void __io_req_task_cancel(struct io_kiocb *req, int error)
1983{
1984 struct io_ring_ctx *ctx = req->ctx;
1985
1986 spin_lock_irq(&ctx->completion_lock);
1987 io_cqring_fill_event(req, error);
1988 io_commit_cqring(ctx);
1989 spin_unlock_irq(&ctx->completion_lock);
1990
1991 io_cqring_ev_posted(ctx);
1992 req_set_fail_links(req);
1993 io_double_put_req(req);
1994}
1995
1996static void io_req_task_cancel(struct callback_head *cb)
1997{
1998 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001999 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002000
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002001 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002002 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002003 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002004 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002005}
2006
2007static void __io_req_task_submit(struct io_kiocb *req)
2008{
2009 struct io_ring_ctx *ctx = req->ctx;
2010
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002011 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002012 mutex_lock(&ctx->uring_lock);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07002013 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && !current->in_execve)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002014 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002015 else
Jens Axboec40f6372020-06-25 15:39:59 -06002016 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002017 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002018}
2019
Jens Axboec40f6372020-06-25 15:39:59 -06002020static void io_req_task_submit(struct callback_head *cb)
2021{
2022 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2023
2024 __io_req_task_submit(req);
2025}
2026
2027static void io_req_task_queue(struct io_kiocb *req)
2028{
Jens Axboec40f6372020-06-25 15:39:59 -06002029 int ret;
2030
Jens Axboe7cbf1722021-02-10 00:03:20 +00002031 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002032 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002033 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00002034 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002035 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002036 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002037 }
Jens Axboec40f6372020-06-25 15:39:59 -06002038}
2039
Pavel Begunkova3df76982021-02-18 22:32:52 +00002040static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2041{
2042 percpu_ref_get(&req->ctx->refs);
2043 req->result = ret;
2044 req->task_work.func = io_req_task_cancel;
2045
2046 if (unlikely(io_req_task_work_add(req)))
2047 io_req_task_work_add_fallback(req, io_req_task_cancel);
2048}
2049
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002050static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002051{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002052 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002053
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002054 if (nxt)
2055 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002056}
2057
Jens Axboe9e645e112019-05-10 16:07:28 -06002058static void io_free_req(struct io_kiocb *req)
2059{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002060 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002061 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002062}
2063
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002064struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002065 struct task_struct *task;
2066 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002067 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002068};
2069
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002070static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002071{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002072 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002073 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002074 rb->task = NULL;
2075}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002076
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002077static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2078 struct req_batch *rb)
2079{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002080 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002081 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002082 if (rb->ctx_refs)
2083 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002084}
2085
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002086static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2087 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002088{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002089 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002090
Jens Axboee3bc8e92020-09-24 08:45:57 -06002091 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002092 if (rb->task)
2093 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002094 rb->task = req->task;
2095 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002096 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002097 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002098 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002099
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002100 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002101 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002102 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002103 else
2104 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002105}
2106
Pavel Begunkov905c1722021-02-10 00:03:14 +00002107static void io_submit_flush_completions(struct io_comp_state *cs,
2108 struct io_ring_ctx *ctx)
2109{
2110 int i, nr = cs->nr;
2111 struct io_kiocb *req;
2112 struct req_batch rb;
2113
2114 io_init_req_batch(&rb);
2115 spin_lock_irq(&ctx->completion_lock);
2116 for (i = 0; i < nr; i++) {
2117 req = cs->reqs[i];
2118 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2119 }
2120 io_commit_cqring(ctx);
2121 spin_unlock_irq(&ctx->completion_lock);
2122
2123 io_cqring_ev_posted(ctx);
2124 for (i = 0; i < nr; i++) {
2125 req = cs->reqs[i];
2126
2127 /* submission and completion refs */
2128 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002129 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002130 }
2131
2132 io_req_free_batch_finish(ctx, &rb);
2133 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002134}
2135
Jens Axboeba816ad2019-09-28 11:36:45 -06002136/*
2137 * Drop reference to request, return next in chain (if there is one) if this
2138 * was the last reference to this request.
2139 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002140static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002141{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002142 struct io_kiocb *nxt = NULL;
2143
Jens Axboe2a44f462020-02-25 13:25:41 -07002144 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002145 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002146 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002147 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002148 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002149}
2150
Jens Axboe2b188cc2019-01-07 10:46:33 -07002151static void io_put_req(struct io_kiocb *req)
2152{
Jens Axboedef596e2019-01-09 08:59:42 -07002153 if (refcount_dec_and_test(&req->refs))
2154 io_free_req(req);
2155}
2156
Pavel Begunkov216578e2020-10-13 09:44:00 +01002157static void io_put_req_deferred_cb(struct callback_head *cb)
2158{
2159 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2160
2161 io_free_req(req);
2162}
2163
2164static void io_free_req_deferred(struct io_kiocb *req)
2165{
2166 int ret;
2167
Jens Axboe7cbf1722021-02-10 00:03:20 +00002168 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002169 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002170 if (unlikely(ret))
2171 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002172}
2173
2174static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2175{
2176 if (refcount_sub_and_test(refs, &req->refs))
2177 io_free_req_deferred(req);
2178}
2179
Jens Axboe978db572019-11-14 22:39:04 -07002180static void io_double_put_req(struct io_kiocb *req)
2181{
2182 /* drop both submit and complete references */
2183 if (refcount_sub_and_test(2, &req->refs))
2184 io_free_req(req);
2185}
2186
Pavel Begunkov6c503152021-01-04 20:36:36 +00002187static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002188{
2189 /* See comment at the top of this file */
2190 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002191 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002192}
2193
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002194static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2195{
2196 struct io_rings *rings = ctx->rings;
2197
2198 /* make sure SQ entry isn't read before tail */
2199 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2200}
2201
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002202static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002203{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002204 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002205
Jens Axboebcda7ba2020-02-23 16:42:51 -07002206 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2207 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002208 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002209 kfree(kbuf);
2210 return cflags;
2211}
2212
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002213static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2214{
2215 struct io_buffer *kbuf;
2216
2217 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2218 return io_put_kbuf(req, kbuf);
2219}
2220
Jens Axboe4c6e2772020-07-01 11:29:10 -06002221static inline bool io_run_task_work(void)
2222{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002223 /*
2224 * Not safe to run on exiting task, and the task_work handling will
2225 * not add work to such a task.
2226 */
2227 if (unlikely(current->flags & PF_EXITING))
2228 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002229 if (current->task_works) {
2230 __set_current_state(TASK_RUNNING);
2231 task_work_run();
2232 return true;
2233 }
2234
2235 return false;
2236}
2237
Jens Axboedef596e2019-01-09 08:59:42 -07002238/*
2239 * Find and free completed poll iocbs
2240 */
2241static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2242 struct list_head *done)
2243{
Jens Axboe8237e042019-12-28 10:48:22 -07002244 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002245 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002246
2247 /* order with ->result store in io_complete_rw_iopoll() */
2248 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002249
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002250 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002251 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002252 int cflags = 0;
2253
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002254 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002255 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002256
Pavel Begunkovf1613402021-02-11 18:28:21 +00002257 if (READ_ONCE(req->result) == -EAGAIN) {
2258 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002259 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002260 continue;
2261 }
2262
Jens Axboebcda7ba2020-02-23 16:42:51 -07002263 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002264 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002265
2266 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002267 (*nr_events)++;
2268
Pavel Begunkovc3524382020-06-28 12:52:32 +03002269 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002270 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002271 }
Jens Axboedef596e2019-01-09 08:59:42 -07002272
Jens Axboe09bb8392019-03-13 12:39:28 -06002273 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002274 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002275 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002276}
2277
Jens Axboedef596e2019-01-09 08:59:42 -07002278static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2279 long min)
2280{
2281 struct io_kiocb *req, *tmp;
2282 LIST_HEAD(done);
2283 bool spin;
2284 int ret;
2285
2286 /*
2287 * Only spin for completions if we don't have multiple devices hanging
2288 * off our complete list, and we're under the requested amount.
2289 */
2290 spin = !ctx->poll_multi_file && *nr_events < min;
2291
2292 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002293 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002294 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002295
2296 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002297 * Move completed and retryable entries to our local lists.
2298 * If we find a request that requires polling, break out
2299 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002300 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002301 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002302 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002303 continue;
2304 }
2305 if (!list_empty(&done))
2306 break;
2307
2308 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2309 if (ret < 0)
2310 break;
2311
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002312 /* iopoll may have completed current req */
2313 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002314 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002315
Jens Axboedef596e2019-01-09 08:59:42 -07002316 if (ret && spin)
2317 spin = false;
2318 ret = 0;
2319 }
2320
2321 if (!list_empty(&done))
2322 io_iopoll_complete(ctx, nr_events, &done);
2323
2324 return ret;
2325}
2326
2327/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002328 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002329 * non-spinning poll check - we'll still enter the driver poll loop, but only
2330 * as a non-spinning completion check.
2331 */
2332static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2333 long min)
2334{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002335 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002336 int ret;
2337
2338 ret = io_do_iopoll(ctx, nr_events, min);
2339 if (ret < 0)
2340 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002341 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002342 return 0;
2343 }
2344
2345 return 1;
2346}
2347
2348/*
2349 * We can't just wait for polled events to come to us, we have to actively
2350 * find and complete them.
2351 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002352static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002353{
2354 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2355 return;
2356
2357 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002358 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002359 unsigned int nr_events = 0;
2360
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002361 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002362
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002363 /* let it sleep and repeat later if can't complete a request */
2364 if (nr_events == 0)
2365 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002366 /*
2367 * Ensure we allow local-to-the-cpu processing to take place,
2368 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002369 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002370 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002371 if (need_resched()) {
2372 mutex_unlock(&ctx->uring_lock);
2373 cond_resched();
2374 mutex_lock(&ctx->uring_lock);
2375 }
Jens Axboedef596e2019-01-09 08:59:42 -07002376 }
2377 mutex_unlock(&ctx->uring_lock);
2378}
2379
Pavel Begunkov7668b922020-07-07 16:36:21 +03002380static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002381{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002382 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002383 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002384
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002385 /*
2386 * We disallow the app entering submit/complete with polling, but we
2387 * still need to lock the ring to prevent racing with polled issue
2388 * that got punted to a workqueue.
2389 */
2390 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002391 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002392 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002393 * Don't enter poll loop if we already have events pending.
2394 * If we do, we can potentially be spinning for commands that
2395 * already triggered a CQE (eg in error).
2396 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002397 if (test_bit(0, &ctx->cq_check_overflow))
2398 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2399 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002400 break;
2401
2402 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002403 * If a submit got punted to a workqueue, we can have the
2404 * application entering polling for a command before it gets
2405 * issued. That app will hold the uring_lock for the duration
2406 * of the poll right here, so we need to take a breather every
2407 * now and then to ensure that the issue has a chance to add
2408 * the poll to the issued list. Otherwise we can spin here
2409 * forever, while the workqueue is stuck trying to acquire the
2410 * very same mutex.
2411 */
2412 if (!(++iters & 7)) {
2413 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002414 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002415 mutex_lock(&ctx->uring_lock);
2416 }
2417
Pavel Begunkov7668b922020-07-07 16:36:21 +03002418 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002419 if (ret <= 0)
2420 break;
2421 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002422 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002423
Jens Axboe500f9fb2019-08-19 12:15:59 -06002424 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002425 return ret;
2426}
2427
Jens Axboe491381ce2019-10-17 09:20:46 -06002428static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002429{
Jens Axboe491381ce2019-10-17 09:20:46 -06002430 /*
2431 * Tell lockdep we inherited freeze protection from submission
2432 * thread.
2433 */
2434 if (req->flags & REQ_F_ISREG) {
2435 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002436
Jens Axboe491381ce2019-10-17 09:20:46 -06002437 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002438 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002439 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002440}
2441
Jens Axboeb63534c2020-06-04 11:28:00 -06002442#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002443static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002444{
2445 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002446 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002447 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002448
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002449 /* already prepared */
2450 if (req->async_data)
2451 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002452
2453 switch (req->opcode) {
2454 case IORING_OP_READV:
2455 case IORING_OP_READ_FIXED:
2456 case IORING_OP_READ:
2457 rw = READ;
2458 break;
2459 case IORING_OP_WRITEV:
2460 case IORING_OP_WRITE_FIXED:
2461 case IORING_OP_WRITE:
2462 rw = WRITE;
2463 break;
2464 default:
2465 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2466 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002467 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002468 }
2469
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002470 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2471 if (ret < 0)
2472 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002473 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002474}
Jens Axboeb63534c2020-06-04 11:28:00 -06002475#endif
2476
Pavel Begunkov23faba32021-02-11 18:28:22 +00002477static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002478{
2479#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002480 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002481
Jens Axboe355afae2020-09-02 09:30:31 -06002482 if (!S_ISBLK(mode) && !S_ISREG(mode))
2483 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002484 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002485 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002486 /*
2487 * If ref is dying, we might be running poll reap from the exit work.
2488 * Don't attempt to reissue from that path, just let it fail with
2489 * -EAGAIN.
2490 */
2491 if (percpu_ref_is_dying(&req->ctx->refs))
2492 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002493
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002494 lockdep_assert_held(&req->ctx->uring_lock);
2495
Jens Axboe37d1e2e2021-02-17 21:03:43 -07002496 if (io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002497 refcount_inc(&req->refs);
2498 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002499 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002500 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002501 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002502#endif
2503 return false;
2504}
2505
Jens Axboea1d7c392020-06-22 11:09:46 -06002506static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002507 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002508{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002509 int cflags = 0;
2510
Pavel Begunkov23faba32021-02-11 18:28:22 +00002511 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2512 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002513 if (res != req->result)
2514 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002515
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002516 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2517 kiocb_end_write(req);
2518 if (req->flags & REQ_F_BUFFER_SELECTED)
2519 cflags = io_put_rw_kbuf(req);
2520 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002521}
2522
2523static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2524{
Jens Axboe9adbd452019-12-20 08:45:55 -07002525 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002526
Pavel Begunkov889fca72021-02-10 00:03:09 +00002527 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528}
2529
Jens Axboedef596e2019-01-09 08:59:42 -07002530static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2531{
Jens Axboe9adbd452019-12-20 08:45:55 -07002532 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002533
Jens Axboe491381ce2019-10-17 09:20:46 -06002534 if (kiocb->ki_flags & IOCB_WRITE)
2535 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002536
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002537 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002538 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002539
2540 WRITE_ONCE(req->result, res);
2541 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002542 smp_wmb();
2543 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002544}
2545
2546/*
2547 * After the iocb has been issued, it's safe to be found on the poll list.
2548 * Adding the kiocb to the list AFTER submission ensures that we don't
2549 * find it from a io_iopoll_getevents() thread before the issuer is done
2550 * accessing the kiocb cookie.
2551 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002552static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002553{
2554 struct io_ring_ctx *ctx = req->ctx;
2555
2556 /*
2557 * Track whether we have multiple files in our lists. This will impact
2558 * how we do polling eventually, not spinning if we're on potentially
2559 * different devices.
2560 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002561 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002562 ctx->poll_multi_file = false;
2563 } else if (!ctx->poll_multi_file) {
2564 struct io_kiocb *list_req;
2565
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002566 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002567 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002568 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002569 ctx->poll_multi_file = true;
2570 }
2571
2572 /*
2573 * For fast devices, IO may have already completed. If it has, add
2574 * it to the front so we find it first.
2575 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002576 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002577 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002578 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002579 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002580
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002581 /*
2582 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2583 * task context or in io worker task context. If current task context is
2584 * sq thread, we don't need to check whether should wake up sq thread.
2585 */
2586 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002587 wq_has_sleeper(&ctx->sq_data->wait))
2588 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002589}
2590
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002591static inline void io_state_file_put(struct io_submit_state *state)
2592{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002593 if (state->file_refs) {
2594 fput_many(state->file, state->file_refs);
2595 state->file_refs = 0;
2596 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002597}
2598
2599/*
2600 * Get as many references to a file as we have IOs left in this submission,
2601 * assuming most submissions are for one file, or at least that each file
2602 * has more than one submission.
2603 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002604static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002605{
2606 if (!state)
2607 return fget(fd);
2608
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002609 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002610 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002611 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002612 return state->file;
2613 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002614 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002615 }
2616 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002617 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002618 return NULL;
2619
2620 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002621 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002622 return state->file;
2623}
2624
Jens Axboe4503b762020-06-01 10:00:27 -06002625static bool io_bdev_nowait(struct block_device *bdev)
2626{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002627 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002628}
2629
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630/*
2631 * If we tracked the file through the SCM inflight mechanism, we could support
2632 * any file. For now, just ensure that anything potentially problematic is done
2633 * inline.
2634 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002635static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636{
2637 umode_t mode = file_inode(file)->i_mode;
2638
Jens Axboe4503b762020-06-01 10:00:27 -06002639 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002640 if (IS_ENABLED(CONFIG_BLOCK) &&
2641 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002642 return true;
2643 return false;
2644 }
2645 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002647 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002648 if (IS_ENABLED(CONFIG_BLOCK) &&
2649 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002650 file->f_op != &io_uring_fops)
2651 return true;
2652 return false;
2653 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654
Jens Axboec5b85622020-06-09 19:23:05 -06002655 /* any ->read/write should understand O_NONBLOCK */
2656 if (file->f_flags & O_NONBLOCK)
2657 return true;
2658
Jens Axboeaf197f52020-04-28 13:15:06 -06002659 if (!(file->f_mode & FMODE_NOWAIT))
2660 return false;
2661
2662 if (rw == READ)
2663 return file->f_op->read_iter != NULL;
2664
2665 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002666}
2667
Pavel Begunkova88fc402020-09-30 22:57:53 +03002668static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669{
Jens Axboedef596e2019-01-09 08:59:42 -07002670 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002671 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002672 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002673 unsigned ioprio;
2674 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002676 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002677 req->flags |= REQ_F_ISREG;
2678
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002680 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002681 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002682 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002683 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002685 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2686 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2687 if (unlikely(ret))
2688 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002689
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002690 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2691 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2692 req->flags |= REQ_F_NOWAIT;
2693
Jens Axboe2b188cc2019-01-07 10:46:33 -07002694 ioprio = READ_ONCE(sqe->ioprio);
2695 if (ioprio) {
2696 ret = ioprio_check_cap(ioprio);
2697 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002698 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002699
2700 kiocb->ki_ioprio = ioprio;
2701 } else
2702 kiocb->ki_ioprio = get_current_ioprio();
2703
Jens Axboedef596e2019-01-09 08:59:42 -07002704 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002705 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2706 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002707 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002708
Jens Axboedef596e2019-01-09 08:59:42 -07002709 kiocb->ki_flags |= IOCB_HIPRI;
2710 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002711 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002712 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002713 if (kiocb->ki_flags & IOCB_HIPRI)
2714 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002715 kiocb->ki_complete = io_complete_rw;
2716 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002717
Jens Axboe3529d8c2019-12-19 18:24:38 -07002718 req->rw.addr = READ_ONCE(sqe->addr);
2719 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002720 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002721 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002722}
2723
2724static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2725{
2726 switch (ret) {
2727 case -EIOCBQUEUED:
2728 break;
2729 case -ERESTARTSYS:
2730 case -ERESTARTNOINTR:
2731 case -ERESTARTNOHAND:
2732 case -ERESTART_RESTARTBLOCK:
2733 /*
2734 * We can't just restart the syscall, since previously
2735 * submitted sqes may already be in progress. Just fail this
2736 * IO with EINTR.
2737 */
2738 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002739 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002740 default:
2741 kiocb->ki_complete(kiocb, ret, 0);
2742 }
2743}
2744
Jens Axboea1d7c392020-06-22 11:09:46 -06002745static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002746 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002747{
Jens Axboeba042912019-12-25 16:33:42 -07002748 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002749 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002750
Jens Axboe227c0c92020-08-13 11:51:40 -06002751 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002752 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002753 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002754 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002755 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002756 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002757 }
2758
Jens Axboeba042912019-12-25 16:33:42 -07002759 if (req->flags & REQ_F_CUR_POS)
2760 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002761 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002762 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002763 else
2764 io_rw_done(kiocb, ret);
2765}
2766
Pavel Begunkov847595d2021-02-04 13:52:06 +00002767static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002768{
Jens Axboe9adbd452019-12-20 08:45:55 -07002769 struct io_ring_ctx *ctx = req->ctx;
2770 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002771 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002772 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002773 size_t offset;
2774 u64 buf_addr;
2775
Jens Axboeedafcce2019-01-09 09:16:05 -07002776 if (unlikely(buf_index >= ctx->nr_user_bufs))
2777 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002778 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2779 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002780 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002781
2782 /* overflow */
2783 if (buf_addr + len < buf_addr)
2784 return -EFAULT;
2785 /* not inside the mapped region */
2786 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2787 return -EFAULT;
2788
2789 /*
2790 * May not be a start of buffer, set size appropriately
2791 * and advance us to the beginning.
2792 */
2793 offset = buf_addr - imu->ubuf;
2794 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002795
2796 if (offset) {
2797 /*
2798 * Don't use iov_iter_advance() here, as it's really slow for
2799 * using the latter parts of a big fixed buffer - it iterates
2800 * over each segment manually. We can cheat a bit here, because
2801 * we know that:
2802 *
2803 * 1) it's a BVEC iter, we set it up
2804 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2805 * first and last bvec
2806 *
2807 * So just find our index, and adjust the iterator afterwards.
2808 * If the offset is within the first bvec (or the whole first
2809 * bvec, just use iov_iter_advance(). This makes it easier
2810 * since we can just skip the first segment, which may not
2811 * be PAGE_SIZE aligned.
2812 */
2813 const struct bio_vec *bvec = imu->bvec;
2814
2815 if (offset <= bvec->bv_len) {
2816 iov_iter_advance(iter, offset);
2817 } else {
2818 unsigned long seg_skip;
2819
2820 /* skip first vec */
2821 offset -= bvec->bv_len;
2822 seg_skip = 1 + (offset >> PAGE_SHIFT);
2823
2824 iter->bvec = bvec + seg_skip;
2825 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002826 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002827 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002828 }
2829 }
2830
Pavel Begunkov847595d2021-02-04 13:52:06 +00002831 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002832}
2833
Jens Axboebcda7ba2020-02-23 16:42:51 -07002834static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2835{
2836 if (needs_lock)
2837 mutex_unlock(&ctx->uring_lock);
2838}
2839
2840static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2841{
2842 /*
2843 * "Normal" inline submissions always hold the uring_lock, since we
2844 * grab it from the system call. Same is true for the SQPOLL offload.
2845 * The only exception is when we've detached the request and issue it
2846 * from an async worker thread, grab the lock for that case.
2847 */
2848 if (needs_lock)
2849 mutex_lock(&ctx->uring_lock);
2850}
2851
2852static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2853 int bgid, struct io_buffer *kbuf,
2854 bool needs_lock)
2855{
2856 struct io_buffer *head;
2857
2858 if (req->flags & REQ_F_BUFFER_SELECTED)
2859 return kbuf;
2860
2861 io_ring_submit_lock(req->ctx, needs_lock);
2862
2863 lockdep_assert_held(&req->ctx->uring_lock);
2864
2865 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2866 if (head) {
2867 if (!list_empty(&head->list)) {
2868 kbuf = list_last_entry(&head->list, struct io_buffer,
2869 list);
2870 list_del(&kbuf->list);
2871 } else {
2872 kbuf = head;
2873 idr_remove(&req->ctx->io_buffer_idr, bgid);
2874 }
2875 if (*len > kbuf->len)
2876 *len = kbuf->len;
2877 } else {
2878 kbuf = ERR_PTR(-ENOBUFS);
2879 }
2880
2881 io_ring_submit_unlock(req->ctx, needs_lock);
2882
2883 return kbuf;
2884}
2885
Jens Axboe4d954c22020-02-27 07:31:19 -07002886static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2887 bool needs_lock)
2888{
2889 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002890 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002891
2892 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002893 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002894 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2895 if (IS_ERR(kbuf))
2896 return kbuf;
2897 req->rw.addr = (u64) (unsigned long) kbuf;
2898 req->flags |= REQ_F_BUFFER_SELECTED;
2899 return u64_to_user_ptr(kbuf->addr);
2900}
2901
2902#ifdef CONFIG_COMPAT
2903static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2904 bool needs_lock)
2905{
2906 struct compat_iovec __user *uiov;
2907 compat_ssize_t clen;
2908 void __user *buf;
2909 ssize_t len;
2910
2911 uiov = u64_to_user_ptr(req->rw.addr);
2912 if (!access_ok(uiov, sizeof(*uiov)))
2913 return -EFAULT;
2914 if (__get_user(clen, &uiov->iov_len))
2915 return -EFAULT;
2916 if (clen < 0)
2917 return -EINVAL;
2918
2919 len = clen;
2920 buf = io_rw_buffer_select(req, &len, needs_lock);
2921 if (IS_ERR(buf))
2922 return PTR_ERR(buf);
2923 iov[0].iov_base = buf;
2924 iov[0].iov_len = (compat_size_t) len;
2925 return 0;
2926}
2927#endif
2928
2929static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2930 bool needs_lock)
2931{
2932 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2933 void __user *buf;
2934 ssize_t len;
2935
2936 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2937 return -EFAULT;
2938
2939 len = iov[0].iov_len;
2940 if (len < 0)
2941 return -EINVAL;
2942 buf = io_rw_buffer_select(req, &len, needs_lock);
2943 if (IS_ERR(buf))
2944 return PTR_ERR(buf);
2945 iov[0].iov_base = buf;
2946 iov[0].iov_len = len;
2947 return 0;
2948}
2949
2950static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2951 bool needs_lock)
2952{
Jens Axboedddb3e22020-06-04 11:27:01 -06002953 if (req->flags & REQ_F_BUFFER_SELECTED) {
2954 struct io_buffer *kbuf;
2955
2956 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2957 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2958 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002959 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002960 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00002961 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07002962 return -EINVAL;
2963
2964#ifdef CONFIG_COMPAT
2965 if (req->ctx->compat)
2966 return io_compat_import(req, iov, needs_lock);
2967#endif
2968
2969 return __io_iov_buffer_select(req, iov, needs_lock);
2970}
2971
Pavel Begunkov847595d2021-02-04 13:52:06 +00002972static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
2973 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002974{
Jens Axboe9adbd452019-12-20 08:45:55 -07002975 void __user *buf = u64_to_user_ptr(req->rw.addr);
2976 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002977 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07002978 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002979
Pavel Begunkov7d009162019-11-25 23:14:40 +03002980 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002981 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002982 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002983 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984
Jens Axboebcda7ba2020-02-23 16:42:51 -07002985 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002986 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002987 return -EINVAL;
2988
Jens Axboe3a6820f2019-12-22 15:19:35 -07002989 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002990 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002991 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002992 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002993 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002994 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002995 }
2996
Jens Axboe3a6820f2019-12-22 15:19:35 -07002997 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2998 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00002999 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003000 }
3001
Jens Axboe4d954c22020-02-27 07:31:19 -07003002 if (req->flags & REQ_F_BUFFER_SELECT) {
3003 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003004 if (!ret)
3005 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003006 *iovec = NULL;
3007 return ret;
3008 }
3009
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003010 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3011 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003012}
3013
Jens Axboe0fef9482020-08-26 10:36:20 -06003014static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3015{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003016 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003017}
3018
Jens Axboe32960612019-09-23 11:05:34 -06003019/*
3020 * For files that don't have ->read_iter() and ->write_iter(), handle them
3021 * by looping over ->read() or ->write() manually.
3022 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003023static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003024{
Jens Axboe4017eb92020-10-22 14:14:12 -06003025 struct kiocb *kiocb = &req->rw.kiocb;
3026 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003027 ssize_t ret = 0;
3028
3029 /*
3030 * Don't support polled IO through this interface, and we can't
3031 * support non-blocking either. For the latter, this just causes
3032 * the kiocb to be handled from an async context.
3033 */
3034 if (kiocb->ki_flags & IOCB_HIPRI)
3035 return -EOPNOTSUPP;
3036 if (kiocb->ki_flags & IOCB_NOWAIT)
3037 return -EAGAIN;
3038
3039 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003040 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003041 ssize_t nr;
3042
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003043 if (!iov_iter_is_bvec(iter)) {
3044 iovec = iov_iter_iovec(iter);
3045 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003046 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3047 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003048 }
3049
Jens Axboe32960612019-09-23 11:05:34 -06003050 if (rw == READ) {
3051 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003052 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003053 } else {
3054 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003055 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003056 }
3057
3058 if (nr < 0) {
3059 if (!ret)
3060 ret = nr;
3061 break;
3062 }
3063 ret += nr;
3064 if (nr != iovec.iov_len)
3065 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003066 req->rw.len -= nr;
3067 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003068 iov_iter_advance(iter, nr);
3069 }
3070
3071 return ret;
3072}
3073
Jens Axboeff6165b2020-08-13 09:47:43 -06003074static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3075 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003076{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003077 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003078
Jens Axboeff6165b2020-08-13 09:47:43 -06003079 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003080 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003081 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003082 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003083 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003084 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003085 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003086 unsigned iov_off = 0;
3087
3088 rw->iter.iov = rw->fast_iov;
3089 if (iter->iov != fast_iov) {
3090 iov_off = iter->iov - fast_iov;
3091 rw->iter.iov += iov_off;
3092 }
3093 if (rw->fast_iov != fast_iov)
3094 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003095 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003096 } else {
3097 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003098 }
3099}
3100
Jens Axboee8c2bc12020-08-15 18:44:09 -07003101static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003102{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003103 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3104 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3105 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003106}
3107
Jens Axboee8c2bc12020-08-15 18:44:09 -07003108static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003109{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003110 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003111 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003112
Jens Axboee8c2bc12020-08-15 18:44:09 -07003113 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003114}
3115
Jens Axboeff6165b2020-08-13 09:47:43 -06003116static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3117 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003118 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003119{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003120 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003121 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003122 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003123 if (__io_alloc_async_data(req)) {
3124 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003125 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003126 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003127
Jens Axboeff6165b2020-08-13 09:47:43 -06003128 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003129 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003130 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003131}
3132
Pavel Begunkov73debe62020-09-30 22:57:54 +03003133static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003134{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003135 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003136 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003137 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003138
Pavel Begunkov2846c482020-11-07 13:16:27 +00003139 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003140 if (unlikely(ret < 0))
3141 return ret;
3142
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003143 iorw->bytes_done = 0;
3144 iorw->free_iovec = iov;
3145 if (iov)
3146 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003147 return 0;
3148}
3149
Pavel Begunkov73debe62020-09-30 22:57:54 +03003150static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003151{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003152 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3153 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003154 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003155}
3156
Jens Axboec1dd91d2020-08-03 16:43:59 -06003157/*
3158 * This is our waitqueue callback handler, registered through lock_page_async()
3159 * when we initially tried to do the IO with the iocb armed our waitqueue.
3160 * This gets called when the page is unlocked, and we generally expect that to
3161 * happen when the page IO is completed and the page is now uptodate. This will
3162 * queue a task_work based retry of the operation, attempting to copy the data
3163 * again. If the latter fails because the page was NOT uptodate, then we will
3164 * do a thread based blocking retry of the operation. That's the unexpected
3165 * slow path.
3166 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003167static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3168 int sync, void *arg)
3169{
3170 struct wait_page_queue *wpq;
3171 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003172 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003173
3174 wpq = container_of(wait, struct wait_page_queue, wait);
3175
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003176 if (!wake_page_match(wpq, key))
3177 return 0;
3178
Hao Xuc8d317a2020-09-29 20:00:45 +08003179 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003180 list_del_init(&wait->entry);
3181
Jens Axboebcf5a062020-05-22 09:24:42 -06003182 /* submit ref gets dropped, acquire a new one */
3183 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003184 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003185 return 1;
3186}
3187
Jens Axboec1dd91d2020-08-03 16:43:59 -06003188/*
3189 * This controls whether a given IO request should be armed for async page
3190 * based retry. If we return false here, the request is handed to the async
3191 * worker threads for retry. If we're doing buffered reads on a regular file,
3192 * we prepare a private wait_page_queue entry and retry the operation. This
3193 * will either succeed because the page is now uptodate and unlocked, or it
3194 * will register a callback when the page is unlocked at IO completion. Through
3195 * that callback, io_uring uses task_work to setup a retry of the operation.
3196 * That retry will attempt the buffered read again. The retry will generally
3197 * succeed, or in rare cases where it fails, we then fall back to using the
3198 * async worker threads for a blocking retry.
3199 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003200static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003201{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003202 struct io_async_rw *rw = req->async_data;
3203 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003204 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003205
3206 /* never retry for NOWAIT, we just complete with -EAGAIN */
3207 if (req->flags & REQ_F_NOWAIT)
3208 return false;
3209
Jens Axboe227c0c92020-08-13 11:51:40 -06003210 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003211 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003212 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003213
Jens Axboebcf5a062020-05-22 09:24:42 -06003214 /*
3215 * just use poll if we can, and don't attempt if the fs doesn't
3216 * support callback based unlocks
3217 */
3218 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3219 return false;
3220
Jens Axboe3b2a4432020-08-16 10:58:43 -07003221 wait->wait.func = io_async_buf_func;
3222 wait->wait.private = req;
3223 wait->wait.flags = 0;
3224 INIT_LIST_HEAD(&wait->wait.entry);
3225 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003226 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003227 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003228 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003229}
3230
3231static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3232{
3233 if (req->file->f_op->read_iter)
3234 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003235 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003236 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003237 else
3238 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003239}
3240
Pavel Begunkov889fca72021-02-10 00:03:09 +00003241static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003242{
3243 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003244 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003245 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003246 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003247 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003248 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003249
Pavel Begunkov2846c482020-11-07 13:16:27 +00003250 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003251 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003252 iovec = NULL;
3253 } else {
3254 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3255 if (ret < 0)
3256 return ret;
3257 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003258 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003259 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003260
Jens Axboefd6c2e42019-12-18 12:19:41 -07003261 /* Ensure we clear previously set non-block flag */
3262 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003263 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003264 else
3265 kiocb->ki_flags |= IOCB_NOWAIT;
3266
Pavel Begunkov24c74672020-06-21 13:09:51 +03003267 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003268 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3269 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003270 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003271 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003272
Pavel Begunkov632546c2020-11-07 13:16:26 +00003273 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003274 if (unlikely(ret)) {
3275 kfree(iovec);
3276 return ret;
3277 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003278
Jens Axboe227c0c92020-08-13 11:51:40 -06003279 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003280
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003281 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003282 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003283 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003284 /* IOPOLL retry should happen for io-wq threads */
3285 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003286 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003287 /* no retry on NONBLOCK nor RWF_NOWAIT */
3288 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003289 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003290 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003291 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003292 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003293 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003294 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003295 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003296 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003297 }
3298
Jens Axboe227c0c92020-08-13 11:51:40 -06003299 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003300 if (ret2)
3301 return ret2;
3302
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003303 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003304 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003305 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003306 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003307
Pavel Begunkovb23df912021-02-04 13:52:04 +00003308 do {
3309 io_size -= ret;
3310 rw->bytes_done += ret;
3311 /* if we can retry, do so with the callbacks armed */
3312 if (!io_rw_should_retry(req)) {
3313 kiocb->ki_flags &= ~IOCB_WAITQ;
3314 return -EAGAIN;
3315 }
3316
3317 /*
3318 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3319 * we get -EIOCBQUEUED, then we'll get a notification when the
3320 * desired page gets unlocked. We can also get a partial read
3321 * here, and if we do, then just retry at the new offset.
3322 */
3323 ret = io_iter_do_read(req, iter);
3324 if (ret == -EIOCBQUEUED)
3325 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003326 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003327 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003328done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003329 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003330out_free:
3331 /* it's faster to check here then delegate to kfree */
3332 if (iovec)
3333 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003334 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003335}
3336
Pavel Begunkov73debe62020-09-30 22:57:54 +03003337static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003338{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003339 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3340 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003341 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003342}
3343
Pavel Begunkov889fca72021-02-10 00:03:09 +00003344static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003345{
3346 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003347 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003348 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003349 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003350 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003351 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003352
Pavel Begunkov2846c482020-11-07 13:16:27 +00003353 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003354 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003355 iovec = NULL;
3356 } else {
3357 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3358 if (ret < 0)
3359 return ret;
3360 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003361 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003362 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003363
Jens Axboefd6c2e42019-12-18 12:19:41 -07003364 /* Ensure we clear previously set non-block flag */
3365 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003366 kiocb->ki_flags &= ~IOCB_NOWAIT;
3367 else
3368 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003369
Pavel Begunkov24c74672020-06-21 13:09:51 +03003370 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003371 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003372 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003373
Jens Axboe10d59342019-12-09 20:16:22 -07003374 /* file path doesn't support NOWAIT for non-direct_IO */
3375 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3376 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003377 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003378
Pavel Begunkov632546c2020-11-07 13:16:26 +00003379 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003380 if (unlikely(ret))
3381 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003382
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003383 /*
3384 * Open-code file_start_write here to grab freeze protection,
3385 * which will be released by another thread in
3386 * io_complete_rw(). Fool lockdep by telling it the lock got
3387 * released so that it doesn't complain about the held lock when
3388 * we return to userspace.
3389 */
3390 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003391 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003392 __sb_writers_release(file_inode(req->file)->i_sb,
3393 SB_FREEZE_WRITE);
3394 }
3395 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003396
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003397 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003398 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003399 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003400 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003401 else
3402 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003403
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003404 /*
3405 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3406 * retry them without IOCB_NOWAIT.
3407 */
3408 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3409 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003410 /* no retry on NONBLOCK nor RWF_NOWAIT */
3411 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003412 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003413 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003414 /* IOPOLL retry should happen for io-wq threads */
3415 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3416 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003417done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003418 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003419 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003420copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003421 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003422 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003423 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003424 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003425 }
Jens Axboe31b51512019-01-18 22:56:34 -07003426out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003427 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003428 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003429 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003430 return ret;
3431}
3432
Jens Axboe80a261f2020-09-28 14:23:58 -06003433static int io_renameat_prep(struct io_kiocb *req,
3434 const struct io_uring_sqe *sqe)
3435{
3436 struct io_rename *ren = &req->rename;
3437 const char __user *oldf, *newf;
3438
3439 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3440 return -EBADF;
3441
3442 ren->old_dfd = READ_ONCE(sqe->fd);
3443 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3444 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3445 ren->new_dfd = READ_ONCE(sqe->len);
3446 ren->flags = READ_ONCE(sqe->rename_flags);
3447
3448 ren->oldpath = getname(oldf);
3449 if (IS_ERR(ren->oldpath))
3450 return PTR_ERR(ren->oldpath);
3451
3452 ren->newpath = getname(newf);
3453 if (IS_ERR(ren->newpath)) {
3454 putname(ren->oldpath);
3455 return PTR_ERR(ren->newpath);
3456 }
3457
3458 req->flags |= REQ_F_NEED_CLEANUP;
3459 return 0;
3460}
3461
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003462static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003463{
3464 struct io_rename *ren = &req->rename;
3465 int ret;
3466
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003467 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003468 return -EAGAIN;
3469
3470 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3471 ren->newpath, ren->flags);
3472
3473 req->flags &= ~REQ_F_NEED_CLEANUP;
3474 if (ret < 0)
3475 req_set_fail_links(req);
3476 io_req_complete(req, ret);
3477 return 0;
3478}
3479
Jens Axboe14a11432020-09-28 14:27:37 -06003480static int io_unlinkat_prep(struct io_kiocb *req,
3481 const struct io_uring_sqe *sqe)
3482{
3483 struct io_unlink *un = &req->unlink;
3484 const char __user *fname;
3485
3486 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3487 return -EBADF;
3488
3489 un->dfd = READ_ONCE(sqe->fd);
3490
3491 un->flags = READ_ONCE(sqe->unlink_flags);
3492 if (un->flags & ~AT_REMOVEDIR)
3493 return -EINVAL;
3494
3495 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3496 un->filename = getname(fname);
3497 if (IS_ERR(un->filename))
3498 return PTR_ERR(un->filename);
3499
3500 req->flags |= REQ_F_NEED_CLEANUP;
3501 return 0;
3502}
3503
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003504static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003505{
3506 struct io_unlink *un = &req->unlink;
3507 int ret;
3508
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003509 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003510 return -EAGAIN;
3511
3512 if (un->flags & AT_REMOVEDIR)
3513 ret = do_rmdir(un->dfd, un->filename);
3514 else
3515 ret = do_unlinkat(un->dfd, un->filename);
3516
3517 req->flags &= ~REQ_F_NEED_CLEANUP;
3518 if (ret < 0)
3519 req_set_fail_links(req);
3520 io_req_complete(req, ret);
3521 return 0;
3522}
3523
Jens Axboe36f4fa62020-09-05 11:14:22 -06003524static int io_shutdown_prep(struct io_kiocb *req,
3525 const struct io_uring_sqe *sqe)
3526{
3527#if defined(CONFIG_NET)
3528 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3529 return -EINVAL;
3530 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3531 sqe->buf_index)
3532 return -EINVAL;
3533
3534 req->shutdown.how = READ_ONCE(sqe->len);
3535 return 0;
3536#else
3537 return -EOPNOTSUPP;
3538#endif
3539}
3540
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003541static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003542{
3543#if defined(CONFIG_NET)
3544 struct socket *sock;
3545 int ret;
3546
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003547 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003548 return -EAGAIN;
3549
Linus Torvalds48aba792020-12-16 12:44:05 -08003550 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003551 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003552 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003553
3554 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003555 if (ret < 0)
3556 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003557 io_req_complete(req, ret);
3558 return 0;
3559#else
3560 return -EOPNOTSUPP;
3561#endif
3562}
3563
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003564static int __io_splice_prep(struct io_kiocb *req,
3565 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003566{
3567 struct io_splice* sp = &req->splice;
3568 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003569
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003570 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3571 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003572
3573 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003574 sp->len = READ_ONCE(sqe->len);
3575 sp->flags = READ_ONCE(sqe->splice_flags);
3576
3577 if (unlikely(sp->flags & ~valid_flags))
3578 return -EINVAL;
3579
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003580 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3581 (sp->flags & SPLICE_F_FD_IN_FIXED));
3582 if (!sp->file_in)
3583 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003584 req->flags |= REQ_F_NEED_CLEANUP;
3585
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003586 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3587 /*
3588 * Splice operation will be punted aync, and here need to
3589 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3590 */
3591 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003592 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003593 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003594
3595 return 0;
3596}
3597
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003598static int io_tee_prep(struct io_kiocb *req,
3599 const struct io_uring_sqe *sqe)
3600{
3601 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3602 return -EINVAL;
3603 return __io_splice_prep(req, sqe);
3604}
3605
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003606static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003607{
3608 struct io_splice *sp = &req->splice;
3609 struct file *in = sp->file_in;
3610 struct file *out = sp->file_out;
3611 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3612 long ret = 0;
3613
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003614 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003615 return -EAGAIN;
3616 if (sp->len)
3617 ret = do_tee(in, out, sp->len, flags);
3618
3619 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3620 req->flags &= ~REQ_F_NEED_CLEANUP;
3621
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003622 if (ret != sp->len)
3623 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003624 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003625 return 0;
3626}
3627
3628static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3629{
3630 struct io_splice* sp = &req->splice;
3631
3632 sp->off_in = READ_ONCE(sqe->splice_off_in);
3633 sp->off_out = READ_ONCE(sqe->off);
3634 return __io_splice_prep(req, sqe);
3635}
3636
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003637static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003638{
3639 struct io_splice *sp = &req->splice;
3640 struct file *in = sp->file_in;
3641 struct file *out = sp->file_out;
3642 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3643 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003644 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003645
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003646 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003647 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003648
3649 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3650 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003651
Jens Axboe948a7742020-05-17 14:21:38 -06003652 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003653 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003654
3655 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3656 req->flags &= ~REQ_F_NEED_CLEANUP;
3657
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003658 if (ret != sp->len)
3659 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003660 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003661 return 0;
3662}
3663
Jens Axboe2b188cc2019-01-07 10:46:33 -07003664/*
3665 * IORING_OP_NOP just posts a completion event, nothing else.
3666 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003667static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003668{
3669 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670
Jens Axboedef596e2019-01-09 08:59:42 -07003671 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3672 return -EINVAL;
3673
Pavel Begunkov889fca72021-02-10 00:03:09 +00003674 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003675 return 0;
3676}
3677
Pavel Begunkov1155c762021-02-18 18:29:38 +00003678static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003679{
Jens Axboe6b063142019-01-10 22:13:58 -07003680 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003681
Jens Axboe09bb8392019-03-13 12:39:28 -06003682 if (!req->file)
3683 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003684
Jens Axboe6b063142019-01-10 22:13:58 -07003685 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003686 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003687 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003688 return -EINVAL;
3689
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003690 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3691 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3692 return -EINVAL;
3693
3694 req->sync.off = READ_ONCE(sqe->off);
3695 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003696 return 0;
3697}
3698
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003699static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003700{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003701 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003702 int ret;
3703
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003704 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003705 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003706 return -EAGAIN;
3707
Jens Axboe9adbd452019-12-20 08:45:55 -07003708 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003709 end > 0 ? end : LLONG_MAX,
3710 req->sync.flags & IORING_FSYNC_DATASYNC);
3711 if (ret < 0)
3712 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003713 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003714 return 0;
3715}
3716
Jens Axboed63d1b52019-12-10 10:38:56 -07003717static int io_fallocate_prep(struct io_kiocb *req,
3718 const struct io_uring_sqe *sqe)
3719{
3720 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3721 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003722 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3723 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003724
3725 req->sync.off = READ_ONCE(sqe->off);
3726 req->sync.len = READ_ONCE(sqe->addr);
3727 req->sync.mode = READ_ONCE(sqe->len);
3728 return 0;
3729}
3730
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003731static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003732{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003733 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003734
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003735 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003736 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003737 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003738 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3739 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003740 if (ret < 0)
3741 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003742 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003743 return 0;
3744}
3745
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003746static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003747{
Jens Axboef8748882020-01-08 17:47:02 -07003748 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003749 int ret;
3750
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003751 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003752 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003753 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003754 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003755
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003756 /* open.how should be already initialised */
3757 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003758 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003759
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003760 req->open.dfd = READ_ONCE(sqe->fd);
3761 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003762 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003763 if (IS_ERR(req->open.filename)) {
3764 ret = PTR_ERR(req->open.filename);
3765 req->open.filename = NULL;
3766 return ret;
3767 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003768 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003769 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003770 return 0;
3771}
3772
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003773static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3774{
3775 u64 flags, mode;
3776
Jens Axboe14587a462020-09-05 11:36:08 -06003777 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003778 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003779 mode = READ_ONCE(sqe->len);
3780 flags = READ_ONCE(sqe->open_flags);
3781 req->open.how = build_open_how(flags, mode);
3782 return __io_openat_prep(req, sqe);
3783}
3784
Jens Axboecebdb982020-01-08 17:59:24 -07003785static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3786{
3787 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003788 size_t len;
3789 int ret;
3790
Jens Axboe14587a462020-09-05 11:36:08 -06003791 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003792 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003793 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3794 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003795 if (len < OPEN_HOW_SIZE_VER0)
3796 return -EINVAL;
3797
3798 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3799 len);
3800 if (ret)
3801 return ret;
3802
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003803 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003804}
3805
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003806static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003807{
3808 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003809 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003810 bool nonblock_set;
3811 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003812 int ret;
3813
Jens Axboecebdb982020-01-08 17:59:24 -07003814 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003815 if (ret)
3816 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003817 nonblock_set = op.open_flag & O_NONBLOCK;
3818 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003819 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003820 /*
3821 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3822 * it'll always -EAGAIN
3823 */
3824 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3825 return -EAGAIN;
3826 op.lookup_flags |= LOOKUP_CACHED;
3827 op.open_flag |= O_NONBLOCK;
3828 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003829
Jens Axboe4022e7a2020-03-19 19:23:18 -06003830 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003831 if (ret < 0)
3832 goto err;
3833
3834 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003835 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003836 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3837 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003838 /*
3839 * We could hang on to this 'fd', but seems like marginal
3840 * gain for something that is now known to be a slower path.
3841 * So just put it, and we'll get a new one when we retry.
3842 */
3843 put_unused_fd(ret);
3844 return -EAGAIN;
3845 }
3846
Jens Axboe15b71ab2019-12-11 11:20:36 -07003847 if (IS_ERR(file)) {
3848 put_unused_fd(ret);
3849 ret = PTR_ERR(file);
3850 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003851 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003852 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003853 fsnotify_open(file);
3854 fd_install(ret, file);
3855 }
3856err:
3857 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003858 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003859 if (ret < 0)
3860 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003861 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003862 return 0;
3863}
3864
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003865static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003866{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003867 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07003868}
3869
Jens Axboe067524e2020-03-02 16:32:28 -07003870static int io_remove_buffers_prep(struct io_kiocb *req,
3871 const struct io_uring_sqe *sqe)
3872{
3873 struct io_provide_buf *p = &req->pbuf;
3874 u64 tmp;
3875
3876 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3877 return -EINVAL;
3878
3879 tmp = READ_ONCE(sqe->fd);
3880 if (!tmp || tmp > USHRT_MAX)
3881 return -EINVAL;
3882
3883 memset(p, 0, sizeof(*p));
3884 p->nbufs = tmp;
3885 p->bgid = READ_ONCE(sqe->buf_group);
3886 return 0;
3887}
3888
3889static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3890 int bgid, unsigned nbufs)
3891{
3892 unsigned i = 0;
3893
3894 /* shouldn't happen */
3895 if (!nbufs)
3896 return 0;
3897
3898 /* the head kbuf is the list itself */
3899 while (!list_empty(&buf->list)) {
3900 struct io_buffer *nxt;
3901
3902 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3903 list_del(&nxt->list);
3904 kfree(nxt);
3905 if (++i == nbufs)
3906 return i;
3907 }
3908 i++;
3909 kfree(buf);
3910 idr_remove(&ctx->io_buffer_idr, bgid);
3911
3912 return i;
3913}
3914
Pavel Begunkov889fca72021-02-10 00:03:09 +00003915static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07003916{
3917 struct io_provide_buf *p = &req->pbuf;
3918 struct io_ring_ctx *ctx = req->ctx;
3919 struct io_buffer *head;
3920 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003921 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07003922
3923 io_ring_submit_lock(ctx, !force_nonblock);
3924
3925 lockdep_assert_held(&ctx->uring_lock);
3926
3927 ret = -ENOENT;
3928 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3929 if (head)
3930 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003931 if (ret < 0)
3932 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003933
3934 /* need to hold the lock to complete IOPOLL requests */
3935 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003936 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003937 io_ring_submit_unlock(ctx, !force_nonblock);
3938 } else {
3939 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003940 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003941 }
Jens Axboe067524e2020-03-02 16:32:28 -07003942 return 0;
3943}
3944
Jens Axboeddf0322d2020-02-23 16:41:33 -07003945static int io_provide_buffers_prep(struct io_kiocb *req,
3946 const struct io_uring_sqe *sqe)
3947{
3948 struct io_provide_buf *p = &req->pbuf;
3949 u64 tmp;
3950
3951 if (sqe->ioprio || sqe->rw_flags)
3952 return -EINVAL;
3953
3954 tmp = READ_ONCE(sqe->fd);
3955 if (!tmp || tmp > USHRT_MAX)
3956 return -E2BIG;
3957 p->nbufs = tmp;
3958 p->addr = READ_ONCE(sqe->addr);
3959 p->len = READ_ONCE(sqe->len);
3960
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003961 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003962 return -EFAULT;
3963
3964 p->bgid = READ_ONCE(sqe->buf_group);
3965 tmp = READ_ONCE(sqe->off);
3966 if (tmp > USHRT_MAX)
3967 return -E2BIG;
3968 p->bid = tmp;
3969 return 0;
3970}
3971
3972static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3973{
3974 struct io_buffer *buf;
3975 u64 addr = pbuf->addr;
3976 int i, bid = pbuf->bid;
3977
3978 for (i = 0; i < pbuf->nbufs; i++) {
3979 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3980 if (!buf)
3981 break;
3982
3983 buf->addr = addr;
3984 buf->len = pbuf->len;
3985 buf->bid = bid;
3986 addr += pbuf->len;
3987 bid++;
3988 if (!*head) {
3989 INIT_LIST_HEAD(&buf->list);
3990 *head = buf;
3991 } else {
3992 list_add_tail(&buf->list, &(*head)->list);
3993 }
3994 }
3995
3996 return i ? i : -ENOMEM;
3997}
3998
Pavel Begunkov889fca72021-02-10 00:03:09 +00003999static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004000{
4001 struct io_provide_buf *p = &req->pbuf;
4002 struct io_ring_ctx *ctx = req->ctx;
4003 struct io_buffer *head, *list;
4004 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004005 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004006
4007 io_ring_submit_lock(ctx, !force_nonblock);
4008
4009 lockdep_assert_held(&ctx->uring_lock);
4010
4011 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4012
4013 ret = io_add_buffers(p, &head);
4014 if (ret < 0)
4015 goto out;
4016
4017 if (!list) {
4018 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4019 GFP_KERNEL);
4020 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004021 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004022 goto out;
4023 }
4024 }
4025out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004026 if (ret < 0)
4027 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004028
4029 /* need to hold the lock to complete IOPOLL requests */
4030 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004031 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004032 io_ring_submit_unlock(ctx, !force_nonblock);
4033 } else {
4034 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004035 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004036 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004037 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038}
4039
Jens Axboe3e4827b2020-01-08 15:18:09 -07004040static int io_epoll_ctl_prep(struct io_kiocb *req,
4041 const struct io_uring_sqe *sqe)
4042{
4043#if defined(CONFIG_EPOLL)
4044 if (sqe->ioprio || sqe->buf_index)
4045 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004046 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004047 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004048
4049 req->epoll.epfd = READ_ONCE(sqe->fd);
4050 req->epoll.op = READ_ONCE(sqe->len);
4051 req->epoll.fd = READ_ONCE(sqe->off);
4052
4053 if (ep_op_has_event(req->epoll.op)) {
4054 struct epoll_event __user *ev;
4055
4056 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4057 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4058 return -EFAULT;
4059 }
4060
4061 return 0;
4062#else
4063 return -EOPNOTSUPP;
4064#endif
4065}
4066
Pavel Begunkov889fca72021-02-10 00:03:09 +00004067static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004068{
4069#if defined(CONFIG_EPOLL)
4070 struct io_epoll *ie = &req->epoll;
4071 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004072 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004073
4074 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4075 if (force_nonblock && ret == -EAGAIN)
4076 return -EAGAIN;
4077
4078 if (ret < 0)
4079 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004080 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004081 return 0;
4082#else
4083 return -EOPNOTSUPP;
4084#endif
4085}
4086
Jens Axboec1ca7572019-12-25 22:18:28 -07004087static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4088{
4089#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4090 if (sqe->ioprio || sqe->buf_index || sqe->off)
4091 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004092 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4093 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004094
4095 req->madvise.addr = READ_ONCE(sqe->addr);
4096 req->madvise.len = READ_ONCE(sqe->len);
4097 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4098 return 0;
4099#else
4100 return -EOPNOTSUPP;
4101#endif
4102}
4103
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004104static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004105{
4106#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4107 struct io_madvise *ma = &req->madvise;
4108 int ret;
4109
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004110 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004111 return -EAGAIN;
4112
Minchan Kim0726b012020-10-17 16:14:50 -07004113 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004114 if (ret < 0)
4115 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004116 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004117 return 0;
4118#else
4119 return -EOPNOTSUPP;
4120#endif
4121}
4122
Jens Axboe4840e412019-12-25 22:03:45 -07004123static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4124{
4125 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4126 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004127 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4128 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004129
4130 req->fadvise.offset = READ_ONCE(sqe->off);
4131 req->fadvise.len = READ_ONCE(sqe->len);
4132 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4133 return 0;
4134}
4135
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004136static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004137{
4138 struct io_fadvise *fa = &req->fadvise;
4139 int ret;
4140
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004141 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004142 switch (fa->advice) {
4143 case POSIX_FADV_NORMAL:
4144 case POSIX_FADV_RANDOM:
4145 case POSIX_FADV_SEQUENTIAL:
4146 break;
4147 default:
4148 return -EAGAIN;
4149 }
4150 }
Jens Axboe4840e412019-12-25 22:03:45 -07004151
4152 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4153 if (ret < 0)
4154 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004155 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004156 return 0;
4157}
4158
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004159static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4160{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004161 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004162 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004163 if (sqe->ioprio || sqe->buf_index)
4164 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004165 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004166 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004167
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004168 req->statx.dfd = READ_ONCE(sqe->fd);
4169 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004170 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004171 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4172 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004173
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004174 return 0;
4175}
4176
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004177static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004178{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004179 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004180 int ret;
4181
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004182 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004183 /* only need file table for an actual valid fd */
4184 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4185 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004186 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004187 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004188
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004189 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4190 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004191
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004192 if (ret < 0)
4193 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004194 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004195 return 0;
4196}
4197
Jens Axboeb5dba592019-12-11 14:02:38 -07004198static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4199{
Jens Axboe14587a462020-09-05 11:36:08 -06004200 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004201 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004202 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4203 sqe->rw_flags || sqe->buf_index)
4204 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004205 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004206 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004207
4208 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004209 return 0;
4210}
4211
Pavel Begunkov889fca72021-02-10 00:03:09 +00004212static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004213{
Jens Axboe9eac1902021-01-19 15:50:37 -07004214 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004215 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004216 struct fdtable *fdt;
4217 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004218 int ret;
4219
Jens Axboe9eac1902021-01-19 15:50:37 -07004220 file = NULL;
4221 ret = -EBADF;
4222 spin_lock(&files->file_lock);
4223 fdt = files_fdtable(files);
4224 if (close->fd >= fdt->max_fds) {
4225 spin_unlock(&files->file_lock);
4226 goto err;
4227 }
4228 file = fdt->fd[close->fd];
4229 if (!file) {
4230 spin_unlock(&files->file_lock);
4231 goto err;
4232 }
4233
4234 if (file->f_op == &io_uring_fops) {
4235 spin_unlock(&files->file_lock);
4236 file = NULL;
4237 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004238 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004239
4240 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004241 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004242 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004243 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004244 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004245
Jens Axboe9eac1902021-01-19 15:50:37 -07004246 ret = __close_fd_get_file(close->fd, &file);
4247 spin_unlock(&files->file_lock);
4248 if (ret < 0) {
4249 if (ret == -ENOENT)
4250 ret = -EBADF;
4251 goto err;
4252 }
4253
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004254 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004255 ret = filp_close(file, current->files);
4256err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004257 if (ret < 0)
4258 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004259 if (file)
4260 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004261 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004262 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004263}
4264
Pavel Begunkov1155c762021-02-18 18:29:38 +00004265static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004266{
4267 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004268
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004269 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4270 return -EINVAL;
4271 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4272 return -EINVAL;
4273
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004274 req->sync.off = READ_ONCE(sqe->off);
4275 req->sync.len = READ_ONCE(sqe->len);
4276 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004277 return 0;
4278}
4279
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004280static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004281{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004282 int ret;
4283
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004284 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004285 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004286 return -EAGAIN;
4287
Jens Axboe9adbd452019-12-20 08:45:55 -07004288 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004289 req->sync.flags);
4290 if (ret < 0)
4291 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004292 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004293 return 0;
4294}
4295
YueHaibing469956e2020-03-04 15:53:52 +08004296#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004297static int io_setup_async_msg(struct io_kiocb *req,
4298 struct io_async_msghdr *kmsg)
4299{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004300 struct io_async_msghdr *async_msg = req->async_data;
4301
4302 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004303 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004304 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004305 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004306 return -ENOMEM;
4307 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004308 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004309 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004310 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004311 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004312 /* if were using fast_iov, set it to the new one */
4313 if (!async_msg->free_iov)
4314 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4315
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004316 return -EAGAIN;
4317}
4318
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004319static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4320 struct io_async_msghdr *iomsg)
4321{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004322 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004323 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004324 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004325 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004326}
4327
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004328static int io_sendmsg_prep_async(struct io_kiocb *req)
4329{
4330 int ret;
4331
4332 if (!io_op_defs[req->opcode].needs_async_data)
4333 return 0;
4334 ret = io_sendmsg_copy_hdr(req, req->async_data);
4335 if (!ret)
4336 req->flags |= REQ_F_NEED_CLEANUP;
4337 return ret;
4338}
4339
Jens Axboe3529d8c2019-12-19 18:24:38 -07004340static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004341{
Jens Axboee47293f2019-12-20 08:58:21 -07004342 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004343
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004344 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4345 return -EINVAL;
4346
Jens Axboee47293f2019-12-20 08:58:21 -07004347 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004348 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004349 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350
Jens Axboed8768362020-02-27 14:17:49 -07004351#ifdef CONFIG_COMPAT
4352 if (req->ctx->compat)
4353 sr->msg_flags |= MSG_CMSG_COMPAT;
4354#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004355 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004356}
4357
Pavel Begunkov889fca72021-02-10 00:03:09 +00004358static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004359{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004360 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004361 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004362 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004363 int ret;
4364
Florent Revestdba4a922020-12-04 12:36:04 +01004365 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004366 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004367 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004368
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004369 kmsg = req->async_data;
4370 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004371 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004372 if (ret)
4373 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004374 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004375 }
4376
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004377 flags = req->sr_msg.msg_flags;
4378 if (flags & MSG_DONTWAIT)
4379 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004380 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004381 flags |= MSG_DONTWAIT;
4382
4383 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004384 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004385 return io_setup_async_msg(req, kmsg);
4386 if (ret == -ERESTARTSYS)
4387 ret = -EINTR;
4388
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004389 /* fast path, check for non-NULL to avoid function call */
4390 if (kmsg->free_iov)
4391 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004392 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004393 if (ret < 0)
4394 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004395 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004396 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004397}
4398
Pavel Begunkov889fca72021-02-10 00:03:09 +00004399static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004400{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004401 struct io_sr_msg *sr = &req->sr_msg;
4402 struct msghdr msg;
4403 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004404 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004405 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004406 int ret;
4407
Florent Revestdba4a922020-12-04 12:36:04 +01004408 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004409 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004410 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004411
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004412 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4413 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004414 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004415
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004416 msg.msg_name = NULL;
4417 msg.msg_control = NULL;
4418 msg.msg_controllen = 0;
4419 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004420
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004421 flags = req->sr_msg.msg_flags;
4422 if (flags & MSG_DONTWAIT)
4423 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004424 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004425 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004426
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004427 msg.msg_flags = flags;
4428 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004429 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004430 return -EAGAIN;
4431 if (ret == -ERESTARTSYS)
4432 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004433
Jens Axboe03b12302019-12-02 18:50:25 -07004434 if (ret < 0)
4435 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004436 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004437 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004438}
4439
Pavel Begunkov1400e692020-07-12 20:41:05 +03004440static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4441 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004442{
4443 struct io_sr_msg *sr = &req->sr_msg;
4444 struct iovec __user *uiov;
4445 size_t iov_len;
4446 int ret;
4447
Pavel Begunkov1400e692020-07-12 20:41:05 +03004448 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4449 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004450 if (ret)
4451 return ret;
4452
4453 if (req->flags & REQ_F_BUFFER_SELECT) {
4454 if (iov_len > 1)
4455 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004456 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004457 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004458 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004459 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004460 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004461 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004462 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004463 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004464 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004465 if (ret > 0)
4466 ret = 0;
4467 }
4468
4469 return ret;
4470}
4471
4472#ifdef CONFIG_COMPAT
4473static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004474 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004475{
4476 struct compat_msghdr __user *msg_compat;
4477 struct io_sr_msg *sr = &req->sr_msg;
4478 struct compat_iovec __user *uiov;
4479 compat_uptr_t ptr;
4480 compat_size_t len;
4481 int ret;
4482
Pavel Begunkov270a5942020-07-12 20:41:04 +03004483 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004484 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004485 &ptr, &len);
4486 if (ret)
4487 return ret;
4488
4489 uiov = compat_ptr(ptr);
4490 if (req->flags & REQ_F_BUFFER_SELECT) {
4491 compat_ssize_t clen;
4492
4493 if (len > 1)
4494 return -EINVAL;
4495 if (!access_ok(uiov, sizeof(*uiov)))
4496 return -EFAULT;
4497 if (__get_user(clen, &uiov->iov_len))
4498 return -EFAULT;
4499 if (clen < 0)
4500 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004501 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004502 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004503 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004504 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004505 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004506 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004507 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004508 if (ret < 0)
4509 return ret;
4510 }
4511
4512 return 0;
4513}
Jens Axboe03b12302019-12-02 18:50:25 -07004514#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004515
Pavel Begunkov1400e692020-07-12 20:41:05 +03004516static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4517 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004518{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004519 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004520
4521#ifdef CONFIG_COMPAT
4522 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004523 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004524#endif
4525
Pavel Begunkov1400e692020-07-12 20:41:05 +03004526 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004527}
4528
Jens Axboebcda7ba2020-02-23 16:42:51 -07004529static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004530 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004531{
4532 struct io_sr_msg *sr = &req->sr_msg;
4533 struct io_buffer *kbuf;
4534
Jens Axboebcda7ba2020-02-23 16:42:51 -07004535 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4536 if (IS_ERR(kbuf))
4537 return kbuf;
4538
4539 sr->kbuf = kbuf;
4540 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004541 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004542}
4543
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004544static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4545{
4546 return io_put_kbuf(req, req->sr_msg.kbuf);
4547}
4548
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004549static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004550{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004551 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004552
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004553 if (!io_op_defs[req->opcode].needs_async_data)
4554 return 0;
4555 ret = io_recvmsg_copy_hdr(req, req->async_data);
4556 if (!ret)
4557 req->flags |= REQ_F_NEED_CLEANUP;
4558 return ret;
4559}
4560
4561static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4562{
4563 struct io_sr_msg *sr = &req->sr_msg;
4564
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004565 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4566 return -EINVAL;
4567
Jens Axboe3529d8c2019-12-19 18:24:38 -07004568 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004569 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004570 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004571 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004572
Jens Axboed8768362020-02-27 14:17:49 -07004573#ifdef CONFIG_COMPAT
4574 if (req->ctx->compat)
4575 sr->msg_flags |= MSG_CMSG_COMPAT;
4576#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004577 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004578}
4579
Pavel Begunkov889fca72021-02-10 00:03:09 +00004580static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004581{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004582 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004583 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004584 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004585 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004586 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004587 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004588
Florent Revestdba4a922020-12-04 12:36:04 +01004589 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004590 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004591 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004592
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004593 kmsg = req->async_data;
4594 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004595 ret = io_recvmsg_copy_hdr(req, &iomsg);
4596 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004597 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004598 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004599 }
4600
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004601 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004602 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004603 if (IS_ERR(kbuf))
4604 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004605 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004606 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4607 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004608 1, req->sr_msg.len);
4609 }
4610
4611 flags = req->sr_msg.msg_flags;
4612 if (flags & MSG_DONTWAIT)
4613 req->flags |= REQ_F_NOWAIT;
4614 else if (force_nonblock)
4615 flags |= MSG_DONTWAIT;
4616
4617 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4618 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004619 if (force_nonblock && ret == -EAGAIN)
4620 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004621 if (ret == -ERESTARTSYS)
4622 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004623
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004624 if (req->flags & REQ_F_BUFFER_SELECTED)
4625 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004626 /* fast path, check for non-NULL to avoid function call */
4627 if (kmsg->free_iov)
4628 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004629 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004630 if (ret < 0)
4631 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004632 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004633 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004634}
4635
Pavel Begunkov889fca72021-02-10 00:03:09 +00004636static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004637{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004638 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004639 struct io_sr_msg *sr = &req->sr_msg;
4640 struct msghdr msg;
4641 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004642 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004643 struct iovec iov;
4644 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004645 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004646 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004647
Florent Revestdba4a922020-12-04 12:36:04 +01004648 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004649 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004650 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004651
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004652 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004653 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004654 if (IS_ERR(kbuf))
4655 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004656 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004657 }
4658
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004659 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004660 if (unlikely(ret))
4661 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004662
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004663 msg.msg_name = NULL;
4664 msg.msg_control = NULL;
4665 msg.msg_controllen = 0;
4666 msg.msg_namelen = 0;
4667 msg.msg_iocb = NULL;
4668 msg.msg_flags = 0;
4669
4670 flags = req->sr_msg.msg_flags;
4671 if (flags & MSG_DONTWAIT)
4672 req->flags |= REQ_F_NOWAIT;
4673 else if (force_nonblock)
4674 flags |= MSG_DONTWAIT;
4675
4676 ret = sock_recvmsg(sock, &msg, flags);
4677 if (force_nonblock && ret == -EAGAIN)
4678 return -EAGAIN;
4679 if (ret == -ERESTARTSYS)
4680 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004681out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004682 if (req->flags & REQ_F_BUFFER_SELECTED)
4683 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004684 if (ret < 0)
4685 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004686 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004687 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004688}
4689
Jens Axboe3529d8c2019-12-19 18:24:38 -07004690static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004691{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004692 struct io_accept *accept = &req->accept;
4693
Jens Axboe14587a462020-09-05 11:36:08 -06004694 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004695 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004696 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004697 return -EINVAL;
4698
Jens Axboed55e5f52019-12-11 16:12:15 -07004699 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4700 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004701 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004702 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004703 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004704}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004705
Pavel Begunkov889fca72021-02-10 00:03:09 +00004706static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004707{
4708 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004709 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004710 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004711 int ret;
4712
Jiufei Xuee697dee2020-06-10 13:41:59 +08004713 if (req->file->f_flags & O_NONBLOCK)
4714 req->flags |= REQ_F_NOWAIT;
4715
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004716 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004717 accept->addr_len, accept->flags,
4718 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004719 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004720 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004721 if (ret < 0) {
4722 if (ret == -ERESTARTSYS)
4723 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004724 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004725 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004726 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004727 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004728}
4729
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004730static int io_connect_prep_async(struct io_kiocb *req)
4731{
4732 struct io_async_connect *io = req->async_data;
4733 struct io_connect *conn = &req->connect;
4734
4735 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4736}
4737
Jens Axboe3529d8c2019-12-19 18:24:38 -07004738static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004739{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004740 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004741
Jens Axboe14587a462020-09-05 11:36:08 -06004742 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004743 return -EINVAL;
4744 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4745 return -EINVAL;
4746
Jens Axboe3529d8c2019-12-19 18:24:38 -07004747 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4748 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004749 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004750}
4751
Pavel Begunkov889fca72021-02-10 00:03:09 +00004752static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004753{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004754 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004755 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004756 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004757 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004758
Jens Axboee8c2bc12020-08-15 18:44:09 -07004759 if (req->async_data) {
4760 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004761 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004762 ret = move_addr_to_kernel(req->connect.addr,
4763 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004764 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004765 if (ret)
4766 goto out;
4767 io = &__io;
4768 }
4769
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004770 file_flags = force_nonblock ? O_NONBLOCK : 0;
4771
Jens Axboee8c2bc12020-08-15 18:44:09 -07004772 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004773 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004774 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004775 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004776 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004777 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004778 ret = -ENOMEM;
4779 goto out;
4780 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004781 io = req->async_data;
4782 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004783 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004784 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004785 if (ret == -ERESTARTSYS)
4786 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004787out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004788 if (ret < 0)
4789 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004790 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004791 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004792}
YueHaibing469956e2020-03-04 15:53:52 +08004793#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004794#define IO_NETOP_FN(op) \
4795static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4796{ \
4797 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004798}
4799
Jens Axboe99a10082021-02-19 09:35:19 -07004800#define IO_NETOP_PREP(op) \
4801IO_NETOP_FN(op) \
4802static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4803{ \
4804 return -EOPNOTSUPP; \
4805} \
4806
4807#define IO_NETOP_PREP_ASYNC(op) \
4808IO_NETOP_PREP(op) \
4809static int io_##op##_prep_async(struct io_kiocb *req) \
4810{ \
4811 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004812}
4813
Jens Axboe99a10082021-02-19 09:35:19 -07004814IO_NETOP_PREP_ASYNC(sendmsg);
4815IO_NETOP_PREP_ASYNC(recvmsg);
4816IO_NETOP_PREP_ASYNC(connect);
4817IO_NETOP_PREP(accept);
4818IO_NETOP_FN(send);
4819IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004820#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004821
Jens Axboed7718a92020-02-14 22:23:12 -07004822struct io_poll_table {
4823 struct poll_table_struct pt;
4824 struct io_kiocb *req;
4825 int error;
4826};
4827
Jens Axboed7718a92020-02-14 22:23:12 -07004828static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4829 __poll_t mask, task_work_func_t func)
4830{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004831 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004832
4833 /* for instances that support it check for an event match first: */
4834 if (mask && !(mask & poll->events))
4835 return 0;
4836
4837 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4838
4839 list_del_init(&poll->wait.entry);
4840
Jens Axboed7718a92020-02-14 22:23:12 -07004841 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004842 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004843 percpu_ref_get(&req->ctx->refs);
4844
Jens Axboed7718a92020-02-14 22:23:12 -07004845 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004846 * If this fails, then the task is exiting. When a task exits, the
4847 * work gets canceled, so just cancel this request as well instead
4848 * of executing it. We can't safely execute it anyway, as we may not
4849 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004850 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004851 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004852 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004853 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004854 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004855 }
Jens Axboed7718a92020-02-14 22:23:12 -07004856 return 1;
4857}
4858
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004859static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4860 __acquires(&req->ctx->completion_lock)
4861{
4862 struct io_ring_ctx *ctx = req->ctx;
4863
4864 if (!req->result && !READ_ONCE(poll->canceled)) {
4865 struct poll_table_struct pt = { ._key = poll->events };
4866
4867 req->result = vfs_poll(req->file, &pt) & poll->events;
4868 }
4869
4870 spin_lock_irq(&ctx->completion_lock);
4871 if (!req->result && !READ_ONCE(poll->canceled)) {
4872 add_wait_queue(poll->head, &poll->wait);
4873 return true;
4874 }
4875
4876 return false;
4877}
4878
Jens Axboed4e7cd32020-08-15 11:44:50 -07004879static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004880{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004881 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004882 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004883 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004884 return req->apoll->double_poll;
4885}
4886
4887static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4888{
4889 if (req->opcode == IORING_OP_POLL_ADD)
4890 return &req->poll;
4891 return &req->apoll->poll;
4892}
4893
4894static void io_poll_remove_double(struct io_kiocb *req)
4895{
4896 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004897
4898 lockdep_assert_held(&req->ctx->completion_lock);
4899
4900 if (poll && poll->head) {
4901 struct wait_queue_head *head = poll->head;
4902
4903 spin_lock(&head->lock);
4904 list_del_init(&poll->wait.entry);
4905 if (poll->wait.private)
4906 refcount_dec(&req->refs);
4907 poll->head = NULL;
4908 spin_unlock(&head->lock);
4909 }
4910}
4911
4912static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4913{
4914 struct io_ring_ctx *ctx = req->ctx;
4915
Jens Axboed4e7cd32020-08-15 11:44:50 -07004916 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004917 req->poll.done = true;
4918 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4919 io_commit_cqring(ctx);
4920}
4921
Jens Axboe18bceab2020-05-15 11:56:54 -06004922static void io_poll_task_func(struct callback_head *cb)
4923{
4924 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004925 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004926 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004927
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004928 if (io_poll_rewait(req, &req->poll)) {
4929 spin_unlock_irq(&ctx->completion_lock);
4930 } else {
4931 hash_del(&req->hash_node);
4932 io_poll_complete(req, req->result, 0);
4933 spin_unlock_irq(&ctx->completion_lock);
4934
4935 nxt = io_put_req_find_next(req);
4936 io_cqring_ev_posted(ctx);
4937 if (nxt)
4938 __io_req_task_submit(nxt);
4939 }
4940
Jens Axboe6d816e02020-08-11 08:04:14 -06004941 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004942}
4943
4944static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4945 int sync, void *key)
4946{
4947 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004948 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004949 __poll_t mask = key_to_poll(key);
4950
4951 /* for instances that support it check for an event match first: */
4952 if (mask && !(mask & poll->events))
4953 return 0;
4954
Jens Axboe8706e042020-09-28 08:38:54 -06004955 list_del_init(&wait->entry);
4956
Jens Axboe807abcb2020-07-17 17:09:27 -06004957 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004958 bool done;
4959
Jens Axboe807abcb2020-07-17 17:09:27 -06004960 spin_lock(&poll->head->lock);
4961 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004962 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004963 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004964 /* make sure double remove sees this as being gone */
4965 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004966 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06004967 if (!done) {
4968 /* use wait func handler, so it matches the rq type */
4969 poll->wait.func(&poll->wait, mode, sync, key);
4970 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004971 }
4972 refcount_dec(&req->refs);
4973 return 1;
4974}
4975
4976static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4977 wait_queue_func_t wake_func)
4978{
4979 poll->head = NULL;
4980 poll->done = false;
4981 poll->canceled = false;
4982 poll->events = events;
4983 INIT_LIST_HEAD(&poll->wait.entry);
4984 init_waitqueue_func_entry(&poll->wait, wake_func);
4985}
4986
4987static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004988 struct wait_queue_head *head,
4989 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004990{
4991 struct io_kiocb *req = pt->req;
4992
4993 /*
4994 * If poll->head is already set, it's because the file being polled
4995 * uses multiple waitqueues for poll handling (eg one for read, one
4996 * for write). Setup a separate io_poll_iocb if this happens.
4997 */
4998 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01004999 struct io_poll_iocb *poll_one = poll;
5000
Jens Axboe18bceab2020-05-15 11:56:54 -06005001 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005002 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005003 pt->error = -EINVAL;
5004 return;
5005 }
5006 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5007 if (!poll) {
5008 pt->error = -ENOMEM;
5009 return;
5010 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005011 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005012 refcount_inc(&req->refs);
5013 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005014 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005015 }
5016
5017 pt->error = 0;
5018 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005019
5020 if (poll->events & EPOLLEXCLUSIVE)
5021 add_wait_queue_exclusive(head, &poll->wait);
5022 else
5023 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005024}
5025
5026static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5027 struct poll_table_struct *p)
5028{
5029 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005030 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005031
Jens Axboe807abcb2020-07-17 17:09:27 -06005032 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005033}
5034
Jens Axboed7718a92020-02-14 22:23:12 -07005035static void io_async_task_func(struct callback_head *cb)
5036{
5037 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5038 struct async_poll *apoll = req->apoll;
5039 struct io_ring_ctx *ctx = req->ctx;
5040
5041 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5042
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005043 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005044 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005045 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005046 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005047 }
5048
Jens Axboe31067252020-05-17 17:43:31 -06005049 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005050 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005051 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005052
Jens Axboed4e7cd32020-08-15 11:44:50 -07005053 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005054 spin_unlock_irq(&ctx->completion_lock);
5055
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005056 if (!READ_ONCE(apoll->poll.canceled))
5057 __io_req_task_submit(req);
5058 else
5059 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005060
Jens Axboe6d816e02020-08-11 08:04:14 -06005061 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005062 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005063 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005064}
5065
5066static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5067 void *key)
5068{
5069 struct io_kiocb *req = wait->private;
5070 struct io_poll_iocb *poll = &req->apoll->poll;
5071
5072 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5073 key_to_poll(key));
5074
5075 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5076}
5077
5078static void io_poll_req_insert(struct io_kiocb *req)
5079{
5080 struct io_ring_ctx *ctx = req->ctx;
5081 struct hlist_head *list;
5082
5083 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5084 hlist_add_head(&req->hash_node, list);
5085}
5086
5087static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5088 struct io_poll_iocb *poll,
5089 struct io_poll_table *ipt, __poll_t mask,
5090 wait_queue_func_t wake_func)
5091 __acquires(&ctx->completion_lock)
5092{
5093 struct io_ring_ctx *ctx = req->ctx;
5094 bool cancel = false;
5095
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005096 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005097 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005098 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005099 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005100
5101 ipt->pt._key = mask;
5102 ipt->req = req;
5103 ipt->error = -EINVAL;
5104
Jens Axboed7718a92020-02-14 22:23:12 -07005105 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5106
5107 spin_lock_irq(&ctx->completion_lock);
5108 if (likely(poll->head)) {
5109 spin_lock(&poll->head->lock);
5110 if (unlikely(list_empty(&poll->wait.entry))) {
5111 if (ipt->error)
5112 cancel = true;
5113 ipt->error = 0;
5114 mask = 0;
5115 }
5116 if (mask || ipt->error)
5117 list_del_init(&poll->wait.entry);
5118 else if (cancel)
5119 WRITE_ONCE(poll->canceled, true);
5120 else if (!poll->done) /* actually waiting for an event */
5121 io_poll_req_insert(req);
5122 spin_unlock(&poll->head->lock);
5123 }
5124
5125 return mask;
5126}
5127
5128static bool io_arm_poll_handler(struct io_kiocb *req)
5129{
5130 const struct io_op_def *def = &io_op_defs[req->opcode];
5131 struct io_ring_ctx *ctx = req->ctx;
5132 struct async_poll *apoll;
5133 struct io_poll_table ipt;
5134 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005135 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005136
5137 if (!req->file || !file_can_poll(req->file))
5138 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005139 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005140 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005141 if (def->pollin)
5142 rw = READ;
5143 else if (def->pollout)
5144 rw = WRITE;
5145 else
5146 return false;
5147 /* if we can't nonblock try, then no point in arming a poll handler */
5148 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005149 return false;
5150
5151 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5152 if (unlikely(!apoll))
5153 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005154 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005155
5156 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005157 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005158
Nathan Chancellor8755d972020-03-02 16:01:19 -07005159 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005160 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005161 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005162 if (def->pollout)
5163 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005164
5165 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5166 if ((req->opcode == IORING_OP_RECVMSG) &&
5167 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5168 mask &= ~POLLIN;
5169
Jens Axboed7718a92020-02-14 22:23:12 -07005170 mask |= POLLERR | POLLPRI;
5171
5172 ipt.pt._qproc = io_async_queue_proc;
5173
5174 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5175 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005176 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005177 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005178 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005179 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005180 kfree(apoll);
5181 return false;
5182 }
5183 spin_unlock_irq(&ctx->completion_lock);
5184 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5185 apoll->poll.events);
5186 return true;
5187}
5188
5189static bool __io_poll_remove_one(struct io_kiocb *req,
5190 struct io_poll_iocb *poll)
5191{
Jens Axboeb41e9852020-02-17 09:52:41 -07005192 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005193
5194 spin_lock(&poll->head->lock);
5195 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005196 if (!list_empty(&poll->wait.entry)) {
5197 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005198 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005199 }
5200 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005201 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005202 return do_complete;
5203}
5204
5205static bool io_poll_remove_one(struct io_kiocb *req)
5206{
5207 bool do_complete;
5208
Jens Axboed4e7cd32020-08-15 11:44:50 -07005209 io_poll_remove_double(req);
5210
Jens Axboed7718a92020-02-14 22:23:12 -07005211 if (req->opcode == IORING_OP_POLL_ADD) {
5212 do_complete = __io_poll_remove_one(req, &req->poll);
5213 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005214 struct async_poll *apoll = req->apoll;
5215
Jens Axboed7718a92020-02-14 22:23:12 -07005216 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005217 do_complete = __io_poll_remove_one(req, &apoll->poll);
5218 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005219 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005220 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005221 kfree(apoll);
5222 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005223 }
5224
Jens Axboeb41e9852020-02-17 09:52:41 -07005225 if (do_complete) {
5226 io_cqring_fill_event(req, -ECANCELED);
5227 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005228 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005229 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005230 }
5231
5232 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005233}
5234
Jens Axboe76e1b642020-09-26 15:05:03 -06005235/*
5236 * Returns true if we found and killed one or more poll requests
5237 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005238static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5239 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005240{
Jens Axboe78076bb2019-12-04 19:56:40 -07005241 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005242 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005243 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005244
5245 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005246 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5247 struct hlist_head *list;
5248
5249 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005250 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005251 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005252 posted += io_poll_remove_one(req);
5253 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005254 }
5255 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005256
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005257 if (posted)
5258 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005259
5260 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005261}
5262
Jens Axboe47f46762019-11-09 17:43:02 -07005263static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5264{
Jens Axboe78076bb2019-12-04 19:56:40 -07005265 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005266 struct io_kiocb *req;
5267
Jens Axboe78076bb2019-12-04 19:56:40 -07005268 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5269 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005270 if (sqe_addr != req->user_data)
5271 continue;
5272 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005273 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005274 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005275 }
5276
5277 return -ENOENT;
5278}
5279
Jens Axboe3529d8c2019-12-19 18:24:38 -07005280static int io_poll_remove_prep(struct io_kiocb *req,
5281 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005282{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005283 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5284 return -EINVAL;
5285 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5286 sqe->poll_events)
5287 return -EINVAL;
5288
Pavel Begunkov018043b2020-10-27 23:17:18 +00005289 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005290 return 0;
5291}
5292
5293/*
5294 * Find a running poll command that matches one specified in sqe->addr,
5295 * and remove it if found.
5296 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005297static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005298{
5299 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005300 int ret;
5301
Jens Axboe221c5eb2019-01-17 09:41:58 -07005302 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005303 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005304 spin_unlock_irq(&ctx->completion_lock);
5305
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005306 if (ret < 0)
5307 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005308 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005309 return 0;
5310}
5311
Jens Axboe221c5eb2019-01-17 09:41:58 -07005312static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5313 void *key)
5314{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005315 struct io_kiocb *req = wait->private;
5316 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005317
Jens Axboed7718a92020-02-14 22:23:12 -07005318 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005319}
5320
Jens Axboe221c5eb2019-01-17 09:41:58 -07005321static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5322 struct poll_table_struct *p)
5323{
5324 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5325
Jens Axboee8c2bc12020-08-15 18:44:09 -07005326 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005327}
5328
Jens Axboe3529d8c2019-12-19 18:24:38 -07005329static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005330{
5331 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005332 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005333
5334 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5335 return -EINVAL;
5336 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5337 return -EINVAL;
5338
Jiufei Xue5769a352020-06-17 17:53:55 +08005339 events = READ_ONCE(sqe->poll32_events);
5340#ifdef __BIG_ENDIAN
5341 events = swahw32(events);
5342#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005343 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5344 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005345 return 0;
5346}
5347
Pavel Begunkov61e98202021-02-10 00:03:08 +00005348static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005349{
5350 struct io_poll_iocb *poll = &req->poll;
5351 struct io_ring_ctx *ctx = req->ctx;
5352 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005353 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005354
Jens Axboed7718a92020-02-14 22:23:12 -07005355 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005356
Jens Axboed7718a92020-02-14 22:23:12 -07005357 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5358 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005359
Jens Axboe8c838782019-03-12 15:48:16 -06005360 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005361 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005362 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005363 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005364 spin_unlock_irq(&ctx->completion_lock);
5365
Jens Axboe8c838782019-03-12 15:48:16 -06005366 if (mask) {
5367 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005368 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005369 }
Jens Axboe8c838782019-03-12 15:48:16 -06005370 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005371}
5372
Jens Axboe5262f562019-09-17 12:26:57 -06005373static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5374{
Jens Axboead8a48a2019-11-15 08:49:11 -07005375 struct io_timeout_data *data = container_of(timer,
5376 struct io_timeout_data, timer);
5377 struct io_kiocb *req = data->req;
5378 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005379 unsigned long flags;
5380
Jens Axboe5262f562019-09-17 12:26:57 -06005381 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005382 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005383 atomic_set(&req->ctx->cq_timeouts,
5384 atomic_read(&req->ctx->cq_timeouts) + 1);
5385
Jens Axboe78e19bb2019-11-06 15:21:34 -07005386 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005387 io_commit_cqring(ctx);
5388 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5389
5390 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005391 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005392 io_put_req(req);
5393 return HRTIMER_NORESTART;
5394}
5395
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005396static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5397 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005398{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005399 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005400 struct io_kiocb *req;
5401 int ret = -ENOENT;
5402
5403 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5404 if (user_data == req->user_data) {
5405 ret = 0;
5406 break;
5407 }
5408 }
5409
5410 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005411 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005412
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005413 io = req->async_data;
5414 ret = hrtimer_try_to_cancel(&io->timer);
5415 if (ret == -1)
5416 return ERR_PTR(-EALREADY);
5417 list_del_init(&req->timeout.list);
5418 return req;
5419}
5420
5421static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5422{
5423 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5424
5425 if (IS_ERR(req))
5426 return PTR_ERR(req);
5427
5428 req_set_fail_links(req);
5429 io_cqring_fill_event(req, -ECANCELED);
5430 io_put_req_deferred(req, 1);
5431 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005432}
5433
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005434static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5435 struct timespec64 *ts, enum hrtimer_mode mode)
5436{
5437 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5438 struct io_timeout_data *data;
5439
5440 if (IS_ERR(req))
5441 return PTR_ERR(req);
5442
5443 req->timeout.off = 0; /* noseq */
5444 data = req->async_data;
5445 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5446 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5447 data->timer.function = io_timeout_fn;
5448 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5449 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005450}
5451
Jens Axboe3529d8c2019-12-19 18:24:38 -07005452static int io_timeout_remove_prep(struct io_kiocb *req,
5453 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005454{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005455 struct io_timeout_rem *tr = &req->timeout_rem;
5456
Jens Axboeb29472e2019-12-17 18:50:29 -07005457 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5458 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005459 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5460 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005461 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005462 return -EINVAL;
5463
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005464 tr->addr = READ_ONCE(sqe->addr);
5465 tr->flags = READ_ONCE(sqe->timeout_flags);
5466 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5467 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5468 return -EINVAL;
5469 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5470 return -EFAULT;
5471 } else if (tr->flags) {
5472 /* timeout removal doesn't support flags */
5473 return -EINVAL;
5474 }
5475
Jens Axboeb29472e2019-12-17 18:50:29 -07005476 return 0;
5477}
5478
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005479static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5480{
5481 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5482 : HRTIMER_MODE_REL;
5483}
5484
Jens Axboe11365042019-10-16 09:08:32 -06005485/*
5486 * Remove or update an existing timeout command
5487 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005488static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005489{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005490 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005491 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005492 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005493
Jens Axboe11365042019-10-16 09:08:32 -06005494 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005495 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005496 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005497 else
5498 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5499 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005500
Jens Axboe47f46762019-11-09 17:43:02 -07005501 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005502 io_commit_cqring(ctx);
5503 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005504 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005505 if (ret < 0)
5506 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005507 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005508 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005509}
5510
Jens Axboe3529d8c2019-12-19 18:24:38 -07005511static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005512 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005513{
Jens Axboead8a48a2019-11-15 08:49:11 -07005514 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005515 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005516 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005517
Jens Axboead8a48a2019-11-15 08:49:11 -07005518 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005519 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005520 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005521 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005522 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005523 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005524 flags = READ_ONCE(sqe->timeout_flags);
5525 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005526 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005527
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005528 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005529
Jens Axboee8c2bc12020-08-15 18:44:09 -07005530 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005531 return -ENOMEM;
5532
Jens Axboee8c2bc12020-08-15 18:44:09 -07005533 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005534 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005535
5536 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005537 return -EFAULT;
5538
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005539 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005540 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5541 return 0;
5542}
5543
Pavel Begunkov61e98202021-02-10 00:03:08 +00005544static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005545{
Jens Axboead8a48a2019-11-15 08:49:11 -07005546 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005547 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005548 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005549 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005550
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005551 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005552
Jens Axboe5262f562019-09-17 12:26:57 -06005553 /*
5554 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005555 * timeout event to be satisfied. If it isn't set, then this is
5556 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005557 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005558 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005559 entry = ctx->timeout_list.prev;
5560 goto add;
5561 }
Jens Axboe5262f562019-09-17 12:26:57 -06005562
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005563 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5564 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005565
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005566 /* Update the last seq here in case io_flush_timeouts() hasn't.
5567 * This is safe because ->completion_lock is held, and submissions
5568 * and completions are never mixed in the same ->completion_lock section.
5569 */
5570 ctx->cq_last_tm_flush = tail;
5571
Jens Axboe5262f562019-09-17 12:26:57 -06005572 /*
5573 * Insertion sort, ensuring the first entry in the list is always
5574 * the one we need first.
5575 */
Jens Axboe5262f562019-09-17 12:26:57 -06005576 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005577 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5578 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005579
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005580 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005581 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005582 /* nxt.seq is behind @tail, otherwise would've been completed */
5583 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005584 break;
5585 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005586add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005587 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005588 data->timer.function = io_timeout_fn;
5589 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005590 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005591 return 0;
5592}
5593
Jens Axboe62755e32019-10-28 21:49:21 -06005594static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005595{
Jens Axboe62755e32019-10-28 21:49:21 -06005596 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005597
Jens Axboe62755e32019-10-28 21:49:21 -06005598 return req->user_data == (unsigned long) data;
5599}
5600
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005601static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005602{
Jens Axboe62755e32019-10-28 21:49:21 -06005603 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005604 int ret = 0;
5605
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005606 if (!tctx->io_wq)
5607 return -ENOENT;
5608
5609 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005610 switch (cancel_ret) {
5611 case IO_WQ_CANCEL_OK:
5612 ret = 0;
5613 break;
5614 case IO_WQ_CANCEL_RUNNING:
5615 ret = -EALREADY;
5616 break;
5617 case IO_WQ_CANCEL_NOTFOUND:
5618 ret = -ENOENT;
5619 break;
5620 }
5621
Jens Axboee977d6d2019-11-05 12:39:45 -07005622 return ret;
5623}
5624
Jens Axboe47f46762019-11-09 17:43:02 -07005625static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5626 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005627 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005628{
5629 unsigned long flags;
5630 int ret;
5631
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005632 ret = io_async_cancel_one(req->task->io_uring,
5633 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005634 if (ret != -ENOENT) {
5635 spin_lock_irqsave(&ctx->completion_lock, flags);
5636 goto done;
5637 }
5638
5639 spin_lock_irqsave(&ctx->completion_lock, flags);
5640 ret = io_timeout_cancel(ctx, sqe_addr);
5641 if (ret != -ENOENT)
5642 goto done;
5643 ret = io_poll_cancel(ctx, sqe_addr);
5644done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005645 if (!ret)
5646 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005647 io_cqring_fill_event(req, ret);
5648 io_commit_cqring(ctx);
5649 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5650 io_cqring_ev_posted(ctx);
5651
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005652 if (ret < 0)
5653 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005654 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005655}
5656
Jens Axboe3529d8c2019-12-19 18:24:38 -07005657static int io_async_cancel_prep(struct io_kiocb *req,
5658 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005659{
Jens Axboefbf23842019-12-17 18:45:56 -07005660 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005661 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005662 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5663 return -EINVAL;
5664 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005665 return -EINVAL;
5666
Jens Axboefbf23842019-12-17 18:45:56 -07005667 req->cancel.addr = READ_ONCE(sqe->addr);
5668 return 0;
5669}
5670
Pavel Begunkov61e98202021-02-10 00:03:08 +00005671static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005672{
5673 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005674
Pavel Begunkov014db002020-03-03 21:33:12 +03005675 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005676 return 0;
5677}
5678
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005679static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005680 const struct io_uring_sqe *sqe)
5681{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005682 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5683 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005684 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5685 return -EINVAL;
5686 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005687 return -EINVAL;
5688
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005689 req->rsrc_update.offset = READ_ONCE(sqe->off);
5690 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5691 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005692 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005693 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005694 return 0;
5695}
5696
Pavel Begunkov889fca72021-02-10 00:03:09 +00005697static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005698{
5699 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005700 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005701 int ret;
5702
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005703 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005704 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005705
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005706 up.offset = req->rsrc_update.offset;
5707 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005708
5709 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005710 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005711 mutex_unlock(&ctx->uring_lock);
5712
5713 if (ret < 0)
5714 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005715 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005716 return 0;
5717}
5718
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005719static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005720{
Jens Axboed625c6e2019-12-17 19:53:05 -07005721 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005722 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005723 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005724 case IORING_OP_READV:
5725 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005726 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005727 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005728 case IORING_OP_WRITEV:
5729 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005730 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005731 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005732 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005733 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005734 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005735 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005736 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005737 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005738 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005739 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005740 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005741 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005742 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005743 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005744 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005745 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005746 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005747 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005748 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005749 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005750 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005751 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005752 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005753 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005754 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005755 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005756 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005757 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005758 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005759 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005760 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005761 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005762 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005763 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005764 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005765 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005766 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005767 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005768 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005769 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005770 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005771 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005772 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005773 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005774 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005775 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005776 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005777 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005778 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005779 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005780 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005781 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005782 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005783 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005784 case IORING_OP_SHUTDOWN:
5785 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005786 case IORING_OP_RENAMEAT:
5787 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005788 case IORING_OP_UNLINKAT:
5789 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005790 }
5791
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005792 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5793 req->opcode);
5794 return-EINVAL;
5795}
5796
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005797static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005798{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005799 switch (req->opcode) {
5800 case IORING_OP_READV:
5801 case IORING_OP_READ_FIXED:
5802 case IORING_OP_READ:
5803 return io_rw_prep_async(req, READ);
5804 case IORING_OP_WRITEV:
5805 case IORING_OP_WRITE_FIXED:
5806 case IORING_OP_WRITE:
5807 return io_rw_prep_async(req, WRITE);
5808 case IORING_OP_SENDMSG:
5809 case IORING_OP_SEND:
5810 return io_sendmsg_prep_async(req);
5811 case IORING_OP_RECVMSG:
5812 case IORING_OP_RECV:
5813 return io_recvmsg_prep_async(req);
5814 case IORING_OP_CONNECT:
5815 return io_connect_prep_async(req);
5816 }
5817 return 0;
5818}
5819
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005820static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005821{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005822 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005823 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005824 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005825 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005826 return 0;
5827 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005828 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005829 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005830}
5831
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005832static u32 io_get_sequence(struct io_kiocb *req)
5833{
5834 struct io_kiocb *pos;
5835 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005836 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005837
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005838 io_for_each_link(pos, req)
5839 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005840
5841 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5842 return total_submitted - nr_reqs;
5843}
5844
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005845static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005846{
5847 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005848 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005849 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005850 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005851
5852 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005853 if (likely(list_empty_careful(&ctx->defer_list) &&
5854 !(req->flags & REQ_F_IO_DRAIN)))
5855 return 0;
5856
5857 seq = io_get_sequence(req);
5858 /* Still a chance to pass the sequence check */
5859 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005860 return 0;
5861
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005862 ret = io_req_defer_prep(req);
5863 if (ret)
5864 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005865 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005866 de = kmalloc(sizeof(*de), GFP_KERNEL);
5867 if (!de)
5868 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005869
5870 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005871 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005872 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005873 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005874 io_queue_async_work(req);
5875 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005876 }
5877
5878 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005879 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005880 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005881 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005882 spin_unlock_irq(&ctx->completion_lock);
5883 return -EIOCBQUEUED;
5884}
Jens Axboeedafcce2019-01-09 09:16:05 -07005885
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005886static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005887{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005888 if (req->flags & REQ_F_BUFFER_SELECTED) {
5889 switch (req->opcode) {
5890 case IORING_OP_READV:
5891 case IORING_OP_READ_FIXED:
5892 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005893 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005894 break;
5895 case IORING_OP_RECVMSG:
5896 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005897 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005898 break;
5899 }
5900 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005901 }
5902
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005903 if (req->flags & REQ_F_NEED_CLEANUP) {
5904 switch (req->opcode) {
5905 case IORING_OP_READV:
5906 case IORING_OP_READ_FIXED:
5907 case IORING_OP_READ:
5908 case IORING_OP_WRITEV:
5909 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005910 case IORING_OP_WRITE: {
5911 struct io_async_rw *io = req->async_data;
5912 if (io->free_iovec)
5913 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005914 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005915 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005916 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005917 case IORING_OP_SENDMSG: {
5918 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005919
5920 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005921 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005922 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005923 case IORING_OP_SPLICE:
5924 case IORING_OP_TEE:
5925 io_put_file(req, req->splice.file_in,
5926 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5927 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005928 case IORING_OP_OPENAT:
5929 case IORING_OP_OPENAT2:
5930 if (req->open.filename)
5931 putname(req->open.filename);
5932 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06005933 case IORING_OP_RENAMEAT:
5934 putname(req->rename.oldpath);
5935 putname(req->rename.newpath);
5936 break;
Jens Axboe14a11432020-09-28 14:27:37 -06005937 case IORING_OP_UNLINKAT:
5938 putname(req->unlink.filename);
5939 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005940 }
5941 req->flags &= ~REQ_F_NEED_CLEANUP;
5942 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005943}
5944
Pavel Begunkov889fca72021-02-10 00:03:09 +00005945static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07005946{
Jens Axboeedafcce2019-01-09 09:16:05 -07005947 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005948 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005949
Jens Axboed625c6e2019-12-17 19:53:05 -07005950 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005951 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005952 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07005953 break;
5954 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005955 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005956 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005957 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958 break;
5959 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005960 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005961 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005962 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005963 break;
5964 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005965 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005966 break;
5967 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005968 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005969 break;
5970 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005971 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005972 break;
5973 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005974 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005975 break;
5976 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005977 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005978 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005979 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005980 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005981 break;
5982 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005983 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005984 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005985 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005986 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005987 break;
5988 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005989 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005990 break;
5991 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005992 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005993 break;
5994 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005995 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005996 break;
5997 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005998 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005999 break;
6000 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006001 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006002 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006003 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006004 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006005 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006006 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006007 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006008 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006009 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006010 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006011 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006012 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006013 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006014 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006015 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006016 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006017 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006018 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006019 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006020 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006021 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006022 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006023 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006024 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006025 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006026 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006027 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006028 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006029 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006030 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006031 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006032 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006033 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006034 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006035 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006036 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006037 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006038 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006039 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006040 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006041 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006042 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006043 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006044 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006045 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006046 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006047 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006048 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006049 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006050 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006051 default:
6052 ret = -EINVAL;
6053 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006054 }
6055
6056 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006057 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058
Jens Axboeb5325762020-05-19 21:20:27 -06006059 /* If the op doesn't have a file, we're not polling for it */
6060 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006061 const bool in_async = io_wq_current_is_worker();
6062
Jens Axboe11ba8202020-01-15 21:51:17 -07006063 /* workqueue context doesn't hold uring_lock, grab it now */
6064 if (in_async)
6065 mutex_lock(&ctx->uring_lock);
6066
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006067 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006068
6069 if (in_async)
6070 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006071 }
6072
6073 return 0;
6074}
6075
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006076static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006077{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006078 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006079 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006080 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006081
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006082 timeout = io_prep_linked_timeout(req);
6083 if (timeout)
6084 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006085
Jens Axboe4014d942021-01-19 15:53:54 -07006086 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006087 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006088
Jens Axboe561fb042019-10-24 07:25:42 -06006089 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006090 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006091 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006092 /*
6093 * We can get EAGAIN for polled IO even though we're
6094 * forcing a sync submission from here, since we can't
6095 * wait for request slots on the block side.
6096 */
6097 if (ret != -EAGAIN)
6098 break;
6099 cond_resched();
6100 } while (1);
6101 }
Jens Axboe31b51512019-01-18 22:56:34 -07006102
Pavel Begunkova3df76982021-02-18 22:32:52 +00006103 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006104 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006105 /* io-wq is going to take one down */
6106 refcount_inc(&req->refs);
6107 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006108 }
Jens Axboe31b51512019-01-18 22:56:34 -07006109}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110
Jens Axboe65e19f52019-10-26 07:20:21 -06006111static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6112 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006113{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006114 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006115
Jens Axboe05f3fb32019-12-09 11:22:50 -07006116 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006117 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006118}
6119
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006120static struct file *io_file_get(struct io_submit_state *state,
6121 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006122{
6123 struct io_ring_ctx *ctx = req->ctx;
6124 struct file *file;
6125
6126 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006127 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006128 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006129 fd = array_index_nospec(fd, ctx->nr_user_files);
6130 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006131 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006132 } else {
6133 trace_io_uring_file_get(ctx, fd);
6134 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006135 }
6136
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006137 if (file && unlikely(file->f_op == &io_uring_fops))
6138 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006139 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006140}
6141
Jens Axboe2665abf2019-11-05 12:40:47 -07006142static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6143{
Jens Axboead8a48a2019-11-15 08:49:11 -07006144 struct io_timeout_data *data = container_of(timer,
6145 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006146 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006147 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006148 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006149
6150 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006151 prev = req->timeout.head;
6152 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006153
6154 /*
6155 * We don't expect the list to be empty, that will only happen if we
6156 * race with the completion of the linked work.
6157 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006158 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006159 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006160 else
6161 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006162 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6163
6164 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006165 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006166 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006167 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006168 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006169 io_req_complete_post(req, -ETIME, 0);
6170 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006171 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006172 return HRTIMER_NORESTART;
6173}
6174
Jens Axboe7271ef32020-08-10 09:55:22 -06006175static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006176{
Jens Axboe76a46e02019-11-10 23:34:16 -07006177 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006178 * If the back reference is NULL, then our linked request finished
6179 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006180 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006181 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006182 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006183
Jens Axboead8a48a2019-11-15 08:49:11 -07006184 data->timer.function = io_link_timeout_fn;
6185 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6186 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006187 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006188}
6189
6190static void io_queue_linked_timeout(struct io_kiocb *req)
6191{
6192 struct io_ring_ctx *ctx = req->ctx;
6193
6194 spin_lock_irq(&ctx->completion_lock);
6195 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006196 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006197
Jens Axboe2665abf2019-11-05 12:40:47 -07006198 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006199 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006200}
6201
Jens Axboead8a48a2019-11-15 08:49:11 -07006202static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006203{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006204 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006205
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006206 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6207 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006208 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006209
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006210 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006211 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006212 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006213 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006214}
6215
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006216static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006217{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006218 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006219 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220 int ret;
6221
Jens Axboe4379bf82021-02-15 13:40:22 -07006222 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6223 req->work.creds != current_cred())
6224 old_creds = override_creds(req->work.creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006225
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006226 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006227
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006228 if (old_creds)
6229 revert_creds(old_creds);
Jens Axboe491381ce2019-10-17 09:20:46 -06006230
6231 /*
6232 * We async punt it if the file wasn't marked NOWAIT, or if the file
6233 * doesn't support non-blocking read/write attempts
6234 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006235 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006236 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006237 /*
6238 * Queued up for async execution, worker will release
6239 * submit reference when the iocb is actually submitted.
6240 */
6241 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006242 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006243 } else if (likely(!ret)) {
6244 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006245 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006246 struct io_ring_ctx *ctx = req->ctx;
6247 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006248
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006249 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006250 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006251 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006252 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006253 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006254 }
6255 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006256 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006257 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006258 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006259 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006260 if (linked_timeout)
6261 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262}
6263
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006264static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006265{
6266 int ret;
6267
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006268 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006269 if (ret) {
6270 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006271fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006272 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006273 io_put_req(req);
6274 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006275 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006276 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006277 ret = io_req_defer_prep(req);
6278 if (unlikely(ret))
6279 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006280 io_queue_async_work(req);
6281 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006282 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006283 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006284}
6285
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006286/*
6287 * Check SQE restrictions (opcode and flags).
6288 *
6289 * Returns 'true' if SQE is allowed, 'false' otherwise.
6290 */
6291static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6292 struct io_kiocb *req,
6293 unsigned int sqe_flags)
6294{
6295 if (!ctx->restricted)
6296 return true;
6297
6298 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6299 return false;
6300
6301 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6302 ctx->restrictions.sqe_flags_required)
6303 return false;
6304
6305 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6306 ctx->restrictions.sqe_flags_required))
6307 return false;
6308
6309 return true;
6310}
6311
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006312static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006313 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006314{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006315 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006316 unsigned int sqe_flags;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006317 int id, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006318
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006319 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006320 /* same numerical values with corresponding REQ_F_*, safe to copy */
6321 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006322 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006323 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006324 req->file = NULL;
6325 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006326 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006327 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006328 /* one is dropped after submission, the other at completion */
6329 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006330 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006331 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006332
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006333 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006334 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6335 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006336 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006337 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006338
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006339 if (unlikely(req->opcode >= IORING_OP_LAST))
6340 return -EINVAL;
6341
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006342 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6343 return -EACCES;
6344
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006345 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6346 !io_op_defs[req->opcode].buffer_select)
6347 return -EOPNOTSUPP;
6348
6349 id = READ_ONCE(sqe->personality);
6350 if (id) {
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006351 __io_req_init_async(req);
Jens Axboe4379bf82021-02-15 13:40:22 -07006352 req->work.creds = idr_find(&ctx->personality_idr, id);
6353 if (unlikely(!req->work.creds))
6354 return -EINVAL;
6355 get_cred(req->work.creds);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006356 }
6357
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006358 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006359
Jens Axboe27926b62020-10-28 09:33:23 -06006360 /*
6361 * Plug now if we have more than 1 IO left after this, and the target
6362 * is potentially a read/write to block based storage.
6363 */
6364 if (!state->plug_started && state->ios_left > 1 &&
6365 io_op_defs[req->opcode].plug) {
6366 blk_start_plug(&state->plug);
6367 state->plug_started = true;
6368 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006369
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006370 if (io_op_defs[req->opcode].needs_file) {
6371 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006372
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006373 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006374 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006375 ret = -EBADF;
6376 }
6377
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006378 state->ios_left--;
6379 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006380}
6381
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006382static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006383 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006384{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006385 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006386 int ret;
6387
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006388 ret = io_init_req(ctx, req, sqe);
6389 if (unlikely(ret)) {
6390fail_req:
6391 io_put_req(req);
6392 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006393 if (link->head) {
6394 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006395 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006396 io_put_req(link->head);
6397 io_req_complete(link->head, -ECANCELED);
6398 link->head = NULL;
6399 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006400 return ret;
6401 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006402 ret = io_req_prep(req, sqe);
6403 if (unlikely(ret))
6404 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006405
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006406 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006407 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6408 true, ctx->flags & IORING_SETUP_SQPOLL);
6409
Jens Axboe6c271ce2019-01-10 11:22:30 -07006410 /*
6411 * If we already have a head request, queue this one for async
6412 * submittal once the head completes. If we don't have a head but
6413 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6414 * submitted sync once the chain is complete. If none of those
6415 * conditions are true (normal request), then just queue it.
6416 */
6417 if (link->head) {
6418 struct io_kiocb *head = link->head;
6419
6420 /*
6421 * Taking sequential execution of a link, draining both sides
6422 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6423 * requests in the link. So, it drains the head and the
6424 * next after the link request. The last one is done via
6425 * drain_next flag to persist the effect across calls.
6426 */
6427 if (req->flags & REQ_F_IO_DRAIN) {
6428 head->flags |= REQ_F_IO_DRAIN;
6429 ctx->drain_next = 1;
6430 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006431 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006432 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006433 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006434 trace_io_uring_link(ctx, req, head);
6435 link->last->link = req;
6436 link->last = req;
6437
6438 /* last request of a link, enqueue the link */
6439 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006440 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006441 link->head = NULL;
6442 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006443 } else {
6444 if (unlikely(ctx->drain_next)) {
6445 req->flags |= REQ_F_IO_DRAIN;
6446 ctx->drain_next = 0;
6447 }
6448 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006449 link->head = req;
6450 link->last = req;
6451 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006452 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006453 }
6454 }
6455
6456 return 0;
6457}
6458
6459/*
6460 * Batched submission is done, ensure local IO is flushed out.
6461 */
6462static void io_submit_state_end(struct io_submit_state *state,
6463 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006464{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006465 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006466 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006467 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006468 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006469 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006470 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006471 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006472}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006473
Jens Axboe9e645e112019-05-10 16:07:28 -06006474/*
6475 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006476 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006477static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006478 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006479{
6480 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006481 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006482 /* set only head, no need to init link_last in advance */
6483 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006484}
6485
Jens Axboe193155c2020-02-22 23:22:19 -07006486static void io_commit_sqring(struct io_ring_ctx *ctx)
6487{
Jens Axboe75c6a032020-01-28 10:15:23 -07006488 struct io_rings *rings = ctx->rings;
6489
6490 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006491 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006492 * since once we write the new head, the application could
6493 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006494 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006495 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006496}
6497
Jens Axboe9e645e112019-05-10 16:07:28 -06006498/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006499 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006500 * that is mapped by userspace. This means that care needs to be taken to
6501 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006502 * being a good citizen. If members of the sqe are validated and then later
6503 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006504 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006505 */
6506static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006507{
6508 u32 *sq_array = ctx->sq_array;
6509 unsigned head;
6510
6511 /*
6512 * The cached sq head (or cq tail) serves two purposes:
6513 *
6514 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006515 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006516 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006517 * though the application is the one updating it.
6518 */
6519 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6520 if (likely(head < ctx->sq_entries))
6521 return &ctx->sq_sqes[head];
6522
6523 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006524 ctx->cached_sq_dropped++;
6525 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6526 return NULL;
6527}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006528
Jens Axboe0f212202020-09-13 13:09:39 -06006529static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006530{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006531 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006532
Jens Axboec4a2ed72019-11-21 21:01:26 -07006533 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006534 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006535 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006536 return -EBUSY;
6537 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006538
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006539 /* make sure SQ entry isn't read before tail */
6540 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006541
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006542 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6543 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006544
Jens Axboed8a6df12020-10-15 16:24:45 -06006545 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006546 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006547 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006548
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006549 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006550 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006551 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006552
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006553 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006554 if (unlikely(!req)) {
6555 if (!submitted)
6556 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006557 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006558 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006559 sqe = io_get_sqe(ctx);
6560 if (unlikely(!sqe)) {
6561 kmem_cache_free(req_cachep, req);
6562 break;
6563 }
Jens Axboed3656342019-12-18 09:50:26 -07006564 /* will complete beyond this point, count as submitted */
6565 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006566 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006567 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006568 }
6569
Pavel Begunkov9466f432020-01-25 22:34:01 +03006570 if (unlikely(submitted != nr)) {
6571 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006572 struct io_uring_task *tctx = current->io_uring;
6573 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006574
Jens Axboed8a6df12020-10-15 16:24:45 -06006575 percpu_ref_put_many(&ctx->refs, unused);
6576 percpu_counter_sub(&tctx->inflight, unused);
6577 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006578 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006579
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006580 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006581 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6582 io_commit_sqring(ctx);
6583
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584 return submitted;
6585}
6586
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006587static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6588{
6589 /* Tell userspace we may need a wakeup call */
6590 spin_lock_irq(&ctx->completion_lock);
6591 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6592 spin_unlock_irq(&ctx->completion_lock);
6593}
6594
6595static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6596{
6597 spin_lock_irq(&ctx->completion_lock);
6598 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6599 spin_unlock_irq(&ctx->completion_lock);
6600}
6601
Xiaoguang Wang08369242020-11-03 14:15:59 +08006602static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006603{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006604 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006605 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006606
Jens Axboec8d1ba52020-09-14 11:07:26 -06006607 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006608 /* if we're handling multiple rings, cap submit size for fairness */
6609 if (cap_entries && to_submit > 8)
6610 to_submit = 8;
6611
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006612 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6613 unsigned nr_events = 0;
6614
Xiaoguang Wang08369242020-11-03 14:15:59 +08006615 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006616 if (!list_empty(&ctx->iopoll_list))
6617 io_do_iopoll(ctx, &nr_events, 0);
6618
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006619 if (to_submit && !ctx->sqo_dead &&
6620 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006621 ret = io_submit_sqes(ctx, to_submit);
6622 mutex_unlock(&ctx->uring_lock);
6623 }
Jens Axboe90554202020-09-03 12:12:41 -06006624
6625 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6626 wake_up(&ctx->sqo_sq_wait);
6627
Xiaoguang Wang08369242020-11-03 14:15:59 +08006628 return ret;
6629}
6630
6631static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6632{
6633 struct io_ring_ctx *ctx;
6634 unsigned sq_thread_idle = 0;
6635
6636 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6637 if (sq_thread_idle < ctx->sq_thread_idle)
6638 sq_thread_idle = ctx->sq_thread_idle;
6639 }
6640
6641 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006642}
6643
Jens Axboe69fb2132020-09-14 11:16:23 -06006644static void io_sqd_init_new(struct io_sq_data *sqd)
6645{
6646 struct io_ring_ctx *ctx;
6647
6648 while (!list_empty(&sqd->ctx_new_list)) {
6649 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006650 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6651 complete(&ctx->sq_thread_comp);
6652 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006653
6654 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006655}
6656
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006657static bool io_sq_thread_should_stop(struct io_sq_data *sqd)
6658{
6659 return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6660}
6661
6662static bool io_sq_thread_should_park(struct io_sq_data *sqd)
6663{
6664 return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
6665}
6666
6667static void io_sq_thread_parkme(struct io_sq_data *sqd)
6668{
6669 for (;;) {
6670 /*
6671 * TASK_PARKED is a special state; we must serialize against
6672 * possible pending wakeups to avoid store-store collisions on
6673 * task->state.
6674 *
6675 * Such a collision might possibly result in the task state
6676 * changin from TASK_PARKED and us failing the
6677 * wait_task_inactive() in kthread_park().
6678 */
6679 set_special_state(TASK_PARKED);
6680 if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state))
6681 break;
6682
6683 /*
6684 * Thread is going to call schedule(), do not preempt it,
6685 * or the caller of kthread_park() may spend more time in
6686 * wait_task_inactive().
6687 */
6688 preempt_disable();
6689 complete(&sqd->completion);
6690 schedule_preempt_disabled();
6691 preempt_enable();
6692 }
6693 __set_current_state(TASK_RUNNING);
6694}
6695
Jens Axboe6c271ce2019-01-10 11:22:30 -07006696static int io_sq_thread(void *data)
6697{
Jens Axboe69fb2132020-09-14 11:16:23 -06006698 struct io_sq_data *sqd = data;
6699 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006700 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006701 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006702 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006703
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006704 sprintf(buf, "iou-sqp-%d", sqd->task_pid);
6705 set_task_comm(current, buf);
6706 sqd->thread = current;
6707 current->pf_io_worker = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006708
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006709 if (sqd->sq_cpu != -1)
6710 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6711 else
6712 set_cpus_allowed_ptr(current, cpu_online_mask);
6713 current->flags |= PF_NO_SETAFFINITY;
6714
6715 complete(&sqd->completion);
6716
6717 wait_for_completion(&sqd->startup);
6718
6719 while (!io_sq_thread_should_stop(sqd)) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006720 int ret;
6721 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006722
6723 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006724 * Any changes to the sqd lists are synchronized through the
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006725 * thread parking. This synchronizes the thread vs users,
Jens Axboe69fb2132020-09-14 11:16:23 -06006726 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006727 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006728 if (io_sq_thread_should_park(sqd)) {
6729 io_sq_thread_parkme(sqd);
6730 continue;
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006731 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006732 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006733 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006734 timeout = jiffies + sqd->sq_thread_idle;
6735 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006736 if (fatal_signal_pending(current))
6737 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006738 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006739 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006740 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006741 ret = __io_sq_thread(ctx, cap_entries);
6742 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6743 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006744 }
6745
Xiaoguang Wang08369242020-11-03 14:15:59 +08006746 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006747 io_run_task_work();
6748 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006749 if (sqt_spin)
6750 timeout = jiffies + sqd->sq_thread_idle;
6751 continue;
6752 }
6753
Xiaoguang Wang08369242020-11-03 14:15:59 +08006754 needs_sched = true;
6755 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6756 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6757 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6758 !list_empty_careful(&ctx->iopoll_list)) {
6759 needs_sched = false;
6760 break;
6761 }
6762 if (io_sqring_entries(ctx)) {
6763 needs_sched = false;
6764 break;
6765 }
6766 }
6767
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006768 if (needs_sched && !io_sq_thread_should_park(sqd)) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006769 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6770 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006771
Jens Axboe69fb2132020-09-14 11:16:23 -06006772 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06006773 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6774 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006775 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006776
6777 finish_wait(&sqd->wait, &wait);
6778 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006779 }
6780
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006781 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6782 io_uring_cancel_sqpoll(ctx);
6783
Jens Axboe4c6e2772020-07-01 11:29:10 -06006784 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006785
Jens Axboe86293972021-02-26 13:46:49 -07006786 if (io_sq_thread_should_park(sqd))
6787 io_sq_thread_parkme(sqd);
6788
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006789 /*
6790 * Clear thread under lock so that concurrent parks work correctly
6791 */
Jens Axboe86293972021-02-26 13:46:49 -07006792 complete(&sqd->completion);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006793 mutex_lock(&sqd->lock);
6794 sqd->thread = NULL;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07006795 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6796 ctx->sqo_exec = 1;
6797 io_ring_set_wakeup_flag(ctx);
6798 }
Jens Axboe06058632019-04-13 09:26:03 -06006799
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006800 complete(&sqd->exited);
Jens Axboee54945a2021-02-26 11:27:15 -07006801 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006802 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006803}
6804
Jens Axboebda52162019-09-24 13:47:15 -06006805struct io_wait_queue {
6806 struct wait_queue_entry wq;
6807 struct io_ring_ctx *ctx;
6808 unsigned to_wait;
6809 unsigned nr_timeouts;
6810};
6811
Pavel Begunkov6c503152021-01-04 20:36:36 +00006812static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006813{
6814 struct io_ring_ctx *ctx = iowq->ctx;
6815
6816 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006817 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006818 * started waiting. For timeouts, we always want to return to userspace,
6819 * regardless of event count.
6820 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006821 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006822 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6823}
6824
6825static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6826 int wake_flags, void *key)
6827{
6828 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6829 wq);
6830
Pavel Begunkov6c503152021-01-04 20:36:36 +00006831 /*
6832 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6833 * the task, and the next invocation will do it.
6834 */
6835 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6836 return autoremove_wake_function(curr, mode, wake_flags, key);
6837 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006838}
6839
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006840static int io_run_task_work_sig(void)
6841{
6842 if (io_run_task_work())
6843 return 1;
6844 if (!signal_pending(current))
6845 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06006846 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
6847 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006848 return -EINTR;
6849}
6850
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006851/* when returns >0, the caller should retry */
6852static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6853 struct io_wait_queue *iowq,
6854 signed long *timeout)
6855{
6856 int ret;
6857
6858 /* make sure we run task_work before checking for signals */
6859 ret = io_run_task_work_sig();
6860 if (ret || io_should_wake(iowq))
6861 return ret;
6862 /* let the caller flush overflows, retry */
6863 if (test_bit(0, &ctx->cq_check_overflow))
6864 return 1;
6865
6866 *timeout = schedule_timeout(*timeout);
6867 return !*timeout ? -ETIME : 1;
6868}
6869
Jens Axboe2b188cc2019-01-07 10:46:33 -07006870/*
6871 * Wait until events become available, if we don't already have some. The
6872 * application must reap them itself, as they reside on the shared cq ring.
6873 */
6874static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006875 const sigset_t __user *sig, size_t sigsz,
6876 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006877{
Jens Axboebda52162019-09-24 13:47:15 -06006878 struct io_wait_queue iowq = {
6879 .wq = {
6880 .private = current,
6881 .func = io_wake_function,
6882 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6883 },
6884 .ctx = ctx,
6885 .to_wait = min_events,
6886 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006887 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006888 signed long timeout = MAX_SCHEDULE_TIMEOUT;
6889 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006890
Jens Axboeb41e9852020-02-17 09:52:41 -07006891 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006892 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6893 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006894 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006895 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006896 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006897 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006898
6899 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006900#ifdef CONFIG_COMPAT
6901 if (in_compat_syscall())
6902 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006903 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006904 else
6905#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006906 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006907
Jens Axboe2b188cc2019-01-07 10:46:33 -07006908 if (ret)
6909 return ret;
6910 }
6911
Hao Xuc73ebb62020-11-03 10:54:37 +08006912 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006913 struct timespec64 ts;
6914
Hao Xuc73ebb62020-11-03 10:54:37 +08006915 if (get_timespec64(&ts, uts))
6916 return -EFAULT;
6917 timeout = timespec64_to_jiffies(&ts);
6918 }
6919
Jens Axboebda52162019-09-24 13:47:15 -06006920 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006921 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006922 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006923 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06006924 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6925 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006926 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
6927 finish_wait(&ctx->wait, &iowq.wq);
6928 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06006929
Jens Axboeb7db41c2020-07-04 08:55:50 -06006930 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006931
Hristo Venev75b28af2019-08-26 17:23:46 +00006932 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006933}
6934
Jens Axboe6b063142019-01-10 22:13:58 -07006935static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6936{
6937#if defined(CONFIG_UNIX)
6938 if (ctx->ring_sock) {
6939 struct sock *sock = ctx->ring_sock->sk;
6940 struct sk_buff *skb;
6941
6942 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6943 kfree_skb(skb);
6944 }
6945#else
6946 int i;
6947
Jens Axboe65e19f52019-10-26 07:20:21 -06006948 for (i = 0; i < ctx->nr_user_files; i++) {
6949 struct file *file;
6950
6951 file = io_file_from_index(ctx, i);
6952 if (file)
6953 fput(file);
6954 }
Jens Axboe6b063142019-01-10 22:13:58 -07006955#endif
6956}
6957
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00006958static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006959{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006960 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006961
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006962 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006963 complete(&data->done);
6964}
6965
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006966static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00006967{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006968 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00006969}
6970
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006971static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07006972{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006973 spin_unlock_bh(&ctx->rsrc_ref_lock);
6974}
6975
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006976static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
6977 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006978 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07006979{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006980 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006981 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006982 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006983 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006984 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07006985}
6986
Hao Xu8bad28d2021-02-19 17:19:36 +08006987static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07006988{
Hao Xu8bad28d2021-02-19 17:19:36 +08006989 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006990
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006991 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00006992 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00006993 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006994 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006995 if (ref_node)
6996 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006997}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006998
Hao Xu8bad28d2021-02-19 17:19:36 +08006999static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7000 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007001 void (*rsrc_put)(struct io_ring_ctx *ctx,
7002 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08007003{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007004 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08007005 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007006
Hao Xu8bad28d2021-02-19 17:19:36 +08007007 if (data->quiesce)
7008 return -ENXIO;
7009
7010 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007011 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007012 ret = -ENOMEM;
7013 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7014 if (!backup_node)
7015 break;
7016 backup_node->rsrc_data = data;
7017 backup_node->rsrc_put = rsrc_put;
7018
Hao Xu8bad28d2021-02-19 17:19:36 +08007019 io_sqe_rsrc_kill_node(ctx, data);
7020 percpu_ref_kill(&data->refs);
7021 flush_delayed_work(&ctx->rsrc_put_work);
7022
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007023 ret = wait_for_completion_interruptible(&data->done);
7024 if (!ret)
7025 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007026
Jens Axboecb5e1b82021-02-25 07:37:35 -07007027 percpu_ref_resurrect(&data->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08007028 io_sqe_rsrc_set_node(ctx, data, backup_node);
7029 backup_node = NULL;
Jens Axboecb5e1b82021-02-25 07:37:35 -07007030 reinit_completion(&data->done);
Hao Xu8bad28d2021-02-19 17:19:36 +08007031 mutex_unlock(&ctx->uring_lock);
7032 ret = io_run_task_work_sig();
7033 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007034 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007035 data->quiesce = false;
7036
7037 if (backup_node)
7038 destroy_fixed_rsrc_ref_node(backup_node);
7039 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007040}
7041
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007042static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7043{
7044 struct fixed_rsrc_data *data;
7045
7046 data = kzalloc(sizeof(*data), GFP_KERNEL);
7047 if (!data)
7048 return NULL;
7049
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007050 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007051 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7052 kfree(data);
7053 return NULL;
7054 }
7055 data->ctx = ctx;
7056 init_completion(&data->done);
7057 return data;
7058}
7059
7060static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7061{
7062 percpu_ref_exit(&data->refs);
7063 kfree(data->table);
7064 kfree(data);
7065}
7066
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007067static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7068{
7069 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007070 unsigned nr_tables, i;
7071 int ret;
7072
Hao Xu8bad28d2021-02-19 17:19:36 +08007073 /*
7074 * percpu_ref_is_dying() is to stop parallel files unregister
7075 * Since we possibly drop uring lock later in this function to
7076 * run task work.
7077 */
7078 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007079 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007080 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007081 if (ret)
7082 return ret;
7083
Jens Axboe6b063142019-01-10 22:13:58 -07007084 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007085 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7086 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007087 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007088 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007089 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007090 ctx->nr_user_files = 0;
7091 return 0;
7092}
7093
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007094static void io_sq_thread_unpark(struct io_sq_data *sqd)
7095 __releases(&sqd->lock)
7096{
7097 if (!sqd->thread)
7098 return;
7099 if (sqd->thread == current)
7100 return;
7101 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7102 wake_up_state(sqd->thread, TASK_PARKED);
7103 mutex_unlock(&sqd->lock);
7104}
7105
7106static bool io_sq_thread_park(struct io_sq_data *sqd)
7107 __acquires(&sqd->lock)
7108{
7109 if (sqd->thread == current)
7110 return true;
7111 mutex_lock(&sqd->lock);
7112 if (!sqd->thread) {
7113 mutex_unlock(&sqd->lock);
7114 return false;
7115 }
7116 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7117 wake_up_process(sqd->thread);
7118 wait_for_completion(&sqd->completion);
7119 return true;
7120}
7121
7122static void io_sq_thread_stop(struct io_sq_data *sqd)
7123{
Jens Axboee54945a2021-02-26 11:27:15 -07007124 if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state))
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007125 return;
Jens Axboee54945a2021-02-26 11:27:15 -07007126 mutex_lock(&sqd->lock);
7127 if (sqd->thread) {
7128 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7129 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state));
7130 wake_up_process(sqd->thread);
7131 mutex_unlock(&sqd->lock);
7132 wait_for_completion(&sqd->exited);
7133 WARN_ON_ONCE(sqd->thread);
7134 } else {
7135 mutex_unlock(&sqd->lock);
7136 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007137}
7138
Jens Axboe534ca6d2020-09-02 13:52:19 -06007139static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007140{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007141 if (refcount_dec_and_test(&sqd->refs)) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007142 io_sq_thread_stop(sqd);
7143 kfree(sqd);
7144 }
7145}
7146
7147static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7148{
7149 struct io_sq_data *sqd = ctx->sq_data;
7150
7151 if (sqd) {
Jens Axboeeb858902021-02-25 10:13:29 -07007152 complete(&sqd->startup);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007153 if (sqd->thread) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007154 wait_for_completion(&ctx->sq_thread_comp);
7155 io_sq_thread_park(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007156 }
7157
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007158 mutex_lock(&sqd->ctx_lock);
7159 list_del(&ctx->sqd_list);
7160 io_sqd_update_thread_idle(sqd);
7161 mutex_unlock(&sqd->ctx_lock);
7162
7163 if (sqd->thread)
7164 io_sq_thread_unpark(sqd);
7165
7166 io_put_sq_data(sqd);
7167 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007168 }
7169}
7170
Jens Axboeaa061652020-09-02 14:50:27 -06007171static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7172{
7173 struct io_ring_ctx *ctx_attach;
7174 struct io_sq_data *sqd;
7175 struct fd f;
7176
7177 f = fdget(p->wq_fd);
7178 if (!f.file)
7179 return ERR_PTR(-ENXIO);
7180 if (f.file->f_op != &io_uring_fops) {
7181 fdput(f);
7182 return ERR_PTR(-EINVAL);
7183 }
7184
7185 ctx_attach = f.file->private_data;
7186 sqd = ctx_attach->sq_data;
7187 if (!sqd) {
7188 fdput(f);
7189 return ERR_PTR(-EINVAL);
7190 }
7191
7192 refcount_inc(&sqd->refs);
7193 fdput(f);
7194 return sqd;
7195}
7196
Jens Axboe534ca6d2020-09-02 13:52:19 -06007197static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7198{
7199 struct io_sq_data *sqd;
7200
Jens Axboeaa061652020-09-02 14:50:27 -06007201 if (p->flags & IORING_SETUP_ATTACH_WQ)
7202 return io_attach_sq_data(p);
7203
Jens Axboe534ca6d2020-09-02 13:52:19 -06007204 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7205 if (!sqd)
7206 return ERR_PTR(-ENOMEM);
7207
7208 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007209 INIT_LIST_HEAD(&sqd->ctx_list);
7210 INIT_LIST_HEAD(&sqd->ctx_new_list);
7211 mutex_init(&sqd->ctx_lock);
7212 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007213 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007214 init_completion(&sqd->startup);
7215 init_completion(&sqd->completion);
7216 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007217 return sqd;
7218}
7219
Jens Axboe6b063142019-01-10 22:13:58 -07007220#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007221/*
7222 * Ensure the UNIX gc is aware of our file set, so we are certain that
7223 * the io_uring can be safely unregistered on process exit, even if we have
7224 * loops in the file referencing.
7225 */
7226static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7227{
7228 struct sock *sk = ctx->ring_sock->sk;
7229 struct scm_fp_list *fpl;
7230 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007231 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007232
Jens Axboe6b063142019-01-10 22:13:58 -07007233 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7234 if (!fpl)
7235 return -ENOMEM;
7236
7237 skb = alloc_skb(0, GFP_KERNEL);
7238 if (!skb) {
7239 kfree(fpl);
7240 return -ENOMEM;
7241 }
7242
7243 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007244
Jens Axboe08a45172019-10-03 08:11:03 -06007245 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007246 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007247 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007248 struct file *file = io_file_from_index(ctx, i + offset);
7249
7250 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007251 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007252 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007253 unix_inflight(fpl->user, fpl->fp[nr_files]);
7254 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007255 }
7256
Jens Axboe08a45172019-10-03 08:11:03 -06007257 if (nr_files) {
7258 fpl->max = SCM_MAX_FD;
7259 fpl->count = nr_files;
7260 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007261 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007262 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7263 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007264
Jens Axboe08a45172019-10-03 08:11:03 -06007265 for (i = 0; i < nr_files; i++)
7266 fput(fpl->fp[i]);
7267 } else {
7268 kfree_skb(skb);
7269 kfree(fpl);
7270 }
Jens Axboe6b063142019-01-10 22:13:58 -07007271
7272 return 0;
7273}
7274
7275/*
7276 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7277 * causes regular reference counting to break down. We rely on the UNIX
7278 * garbage collection to take care of this problem for us.
7279 */
7280static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7281{
7282 unsigned left, total;
7283 int ret = 0;
7284
7285 total = 0;
7286 left = ctx->nr_user_files;
7287 while (left) {
7288 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007289
7290 ret = __io_sqe_files_scm(ctx, this_files, total);
7291 if (ret)
7292 break;
7293 left -= this_files;
7294 total += this_files;
7295 }
7296
7297 if (!ret)
7298 return 0;
7299
7300 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007301 struct file *file = io_file_from_index(ctx, total);
7302
7303 if (file)
7304 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007305 total++;
7306 }
7307
7308 return ret;
7309}
7310#else
7311static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7312{
7313 return 0;
7314}
7315#endif
7316
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007317static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007318 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007319{
7320 int i;
7321
7322 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007323 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007324 unsigned this_files;
7325
7326 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7327 table->files = kcalloc(this_files, sizeof(struct file *),
7328 GFP_KERNEL);
7329 if (!table->files)
7330 break;
7331 nr_files -= this_files;
7332 }
7333
7334 if (i == nr_tables)
7335 return 0;
7336
7337 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007338 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007339 kfree(table->files);
7340 }
7341 return 1;
7342}
7343
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007344static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007345{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007346 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007347#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007348 struct sock *sock = ctx->ring_sock->sk;
7349 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7350 struct sk_buff *skb;
7351 int i;
7352
7353 __skb_queue_head_init(&list);
7354
7355 /*
7356 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7357 * remove this entry and rearrange the file array.
7358 */
7359 skb = skb_dequeue(head);
7360 while (skb) {
7361 struct scm_fp_list *fp;
7362
7363 fp = UNIXCB(skb).fp;
7364 for (i = 0; i < fp->count; i++) {
7365 int left;
7366
7367 if (fp->fp[i] != file)
7368 continue;
7369
7370 unix_notinflight(fp->user, fp->fp[i]);
7371 left = fp->count - 1 - i;
7372 if (left) {
7373 memmove(&fp->fp[i], &fp->fp[i + 1],
7374 left * sizeof(struct file *));
7375 }
7376 fp->count--;
7377 if (!fp->count) {
7378 kfree_skb(skb);
7379 skb = NULL;
7380 } else {
7381 __skb_queue_tail(&list, skb);
7382 }
7383 fput(file);
7384 file = NULL;
7385 break;
7386 }
7387
7388 if (!file)
7389 break;
7390
7391 __skb_queue_tail(&list, skb);
7392
7393 skb = skb_dequeue(head);
7394 }
7395
7396 if (skb_peek(&list)) {
7397 spin_lock_irq(&head->lock);
7398 while ((skb = __skb_dequeue(&list)) != NULL)
7399 __skb_queue_tail(head, skb);
7400 spin_unlock_irq(&head->lock);
7401 }
7402#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007403 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007404#endif
7405}
7406
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007407static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007408{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007409 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7410 struct io_ring_ctx *ctx = rsrc_data->ctx;
7411 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007412
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007413 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7414 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007415 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007416 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007417 }
7418
Xiaoguang Wang05589552020-03-31 14:05:18 +08007419 percpu_ref_exit(&ref_node->refs);
7420 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007421 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007422}
7423
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007424static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007425{
7426 struct io_ring_ctx *ctx;
7427 struct llist_node *node;
7428
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007429 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7430 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007431
7432 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007433 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007434 struct llist_node *next = node->next;
7435
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007436 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7437 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007438 node = next;
7439 }
7440}
7441
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007442static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7443 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007444{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007445 struct fixed_rsrc_table *table;
7446
7447 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7448 return &table->files[i & IORING_FILE_TABLE_MASK];
7449}
7450
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007451static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007452{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007453 struct fixed_rsrc_ref_node *ref_node;
7454 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007455 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007456 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007457 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007458
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007459 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7460 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007461 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007462
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007463 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007464 ref_node->done = true;
7465
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007466 while (!list_empty(&ctx->rsrc_ref_list)) {
7467 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007468 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007469 /* recycle ref nodes in order */
7470 if (!ref_node->done)
7471 break;
7472 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007473 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007474 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007475 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007476
7477 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007478 delay = 0;
7479
Jens Axboe4a38aed22020-05-14 17:21:15 -06007480 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007481 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007482 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007483 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007484}
7485
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007486static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007487 struct io_ring_ctx *ctx)
7488{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007489 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007490
7491 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7492 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007493 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007494
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007495 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007496 0, GFP_KERNEL)) {
7497 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007498 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007499 }
7500 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007501 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007502 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007503 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007504}
7505
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007506static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7507 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007508{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007509 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007510 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007511}
7512
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007513static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007514{
7515 percpu_ref_exit(&ref_node->refs);
7516 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007517}
7518
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007519
Jens Axboe05f3fb32019-12-09 11:22:50 -07007520static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7521 unsigned nr_args)
7522{
7523 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007524 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007525 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007526 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007527 struct fixed_rsrc_ref_node *ref_node;
7528 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007529
7530 if (ctx->file_data)
7531 return -EBUSY;
7532 if (!nr_args)
7533 return -EINVAL;
7534 if (nr_args > IORING_MAX_FIXED_FILES)
7535 return -EMFILE;
7536
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007537 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007538 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007539 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007540 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007541
7542 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007543 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007544 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007545 if (!file_data->table)
7546 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007547
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007548 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007549 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007550
Jens Axboe05f3fb32019-12-09 11:22:50 -07007551 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007552 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7553 ret = -EFAULT;
7554 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007555 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007556 /* allow sparse sets */
7557 if (fd == -1)
7558 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007559
Jens Axboe05f3fb32019-12-09 11:22:50 -07007560 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007561 ret = -EBADF;
7562 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007563 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007564
7565 /*
7566 * Don't allow io_uring instances to be registered. If UNIX
7567 * isn't enabled, then this causes a reference cycle and this
7568 * instance can never get freed. If UNIX is enabled we'll
7569 * handle it just fine, but there's still no point in allowing
7570 * a ring fd as it doesn't support regular read/write anyway.
7571 */
7572 if (file->f_op == &io_uring_fops) {
7573 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007574 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007575 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007576 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007577 }
7578
Jens Axboe05f3fb32019-12-09 11:22:50 -07007579 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007580 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007581 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007582 return ret;
7583 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007584
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007585 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007586 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007587 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007588 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007589 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007590 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007591
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007592 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007593 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007594out_fput:
7595 for (i = 0; i < ctx->nr_user_files; i++) {
7596 file = io_file_from_index(ctx, i);
7597 if (file)
7598 fput(file);
7599 }
7600 for (i = 0; i < nr_tables; i++)
7601 kfree(file_data->table[i].files);
7602 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007603out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007604 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007605 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007606 return ret;
7607}
7608
Jens Axboec3a31e62019-10-03 13:59:56 -06007609static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7610 int index)
7611{
7612#if defined(CONFIG_UNIX)
7613 struct sock *sock = ctx->ring_sock->sk;
7614 struct sk_buff_head *head = &sock->sk_receive_queue;
7615 struct sk_buff *skb;
7616
7617 /*
7618 * See if we can merge this file into an existing skb SCM_RIGHTS
7619 * file set. If there's no room, fall back to allocating a new skb
7620 * and filling it in.
7621 */
7622 spin_lock_irq(&head->lock);
7623 skb = skb_peek(head);
7624 if (skb) {
7625 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7626
7627 if (fpl->count < SCM_MAX_FD) {
7628 __skb_unlink(skb, head);
7629 spin_unlock_irq(&head->lock);
7630 fpl->fp[fpl->count] = get_file(file);
7631 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7632 fpl->count++;
7633 spin_lock_irq(&head->lock);
7634 __skb_queue_head(head, skb);
7635 } else {
7636 skb = NULL;
7637 }
7638 }
7639 spin_unlock_irq(&head->lock);
7640
7641 if (skb) {
7642 fput(file);
7643 return 0;
7644 }
7645
7646 return __io_sqe_files_scm(ctx, 1, index);
7647#else
7648 return 0;
7649#endif
7650}
7651
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007652static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007653{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007654 struct io_rsrc_put *prsrc;
7655 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007656
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007657 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7658 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007659 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007660
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007661 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007662 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007663
Hillf Dantona5318d32020-03-23 17:47:15 +08007664 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007665}
7666
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007667static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7668 struct file *file)
7669{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007670 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007671}
7672
Jens Axboe05f3fb32019-12-09 11:22:50 -07007673static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007674 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007675 unsigned nr_args)
7676{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007677 struct fixed_rsrc_data *data = ctx->file_data;
7678 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007679 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007680 __s32 __user *fds;
7681 int fd, i, err;
7682 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007683 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007684
Jens Axboe05f3fb32019-12-09 11:22:50 -07007685 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007686 return -EOVERFLOW;
7687 if (done > ctx->nr_user_files)
7688 return -EINVAL;
7689
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007690 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007691 if (!ref_node)
7692 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007693 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007694
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007695 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007696 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007697 err = 0;
7698 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7699 err = -EFAULT;
7700 break;
7701 }
noah4e0377a2021-01-26 15:23:28 -05007702 if (fd == IORING_REGISTER_FILES_SKIP)
7703 continue;
7704
Pavel Begunkov67973b92021-01-26 13:51:09 +00007705 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007706 file_slot = io_fixed_file_slot(ctx->file_data, i);
7707
7708 if (*file_slot) {
7709 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007710 if (err)
7711 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007712 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007713 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007714 }
7715 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007716 file = fget(fd);
7717 if (!file) {
7718 err = -EBADF;
7719 break;
7720 }
7721 /*
7722 * Don't allow io_uring instances to be registered. If
7723 * UNIX isn't enabled, then this causes a reference
7724 * cycle and this instance can never get freed. If UNIX
7725 * is enabled we'll handle it just fine, but there's
7726 * still no point in allowing a ring fd as it doesn't
7727 * support regular read/write anyway.
7728 */
7729 if (file->f_op == &io_uring_fops) {
7730 fput(file);
7731 err = -EBADF;
7732 break;
7733 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07007734 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007735 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007736 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07007737 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007738 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007739 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007740 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007741 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007742 }
7743
Xiaoguang Wang05589552020-03-31 14:05:18 +08007744 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007745 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007746 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007747 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007748 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007749
7750 return done ? done : err;
7751}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007752
Jens Axboe05f3fb32019-12-09 11:22:50 -07007753static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7754 unsigned nr_args)
7755{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007756 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007757
7758 if (!ctx->file_data)
7759 return -ENXIO;
7760 if (!nr_args)
7761 return -EINVAL;
7762 if (copy_from_user(&up, arg, sizeof(up)))
7763 return -EFAULT;
7764 if (up.resv)
7765 return -EINVAL;
7766
7767 return __io_sqe_files_update(ctx, &up, nr_args);
7768}
Jens Axboec3a31e62019-10-03 13:59:56 -06007769
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007770static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007771{
7772 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7773
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007774 req = io_put_req_find_next(req);
7775 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007776}
7777
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007778static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007779{
Jens Axboee9418942021-02-19 12:33:30 -07007780 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007781 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007782 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007783
Jens Axboee9418942021-02-19 12:33:30 -07007784 hash = ctx->hash_map;
7785 if (!hash) {
7786 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
7787 if (!hash)
7788 return ERR_PTR(-ENOMEM);
7789 refcount_set(&hash->refs, 1);
7790 init_waitqueue_head(&hash->wait);
7791 ctx->hash_map = hash;
7792 }
7793
7794 data.hash = hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007795 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007796 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007797
Jens Axboed25e3a32021-02-16 11:41:41 -07007798 /* Do QD, or 4 * CPUS, whatever is smallest */
7799 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007800
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007801 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007802}
7803
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007804static int io_uring_alloc_task_context(struct task_struct *task,
7805 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007806{
7807 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007808 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007809
7810 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7811 if (unlikely(!tctx))
7812 return -ENOMEM;
7813
Jens Axboed8a6df12020-10-15 16:24:45 -06007814 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7815 if (unlikely(ret)) {
7816 kfree(tctx);
7817 return ret;
7818 }
7819
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007820 tctx->io_wq = io_init_wq_offload(ctx);
7821 if (IS_ERR(tctx->io_wq)) {
7822 ret = PTR_ERR(tctx->io_wq);
7823 percpu_counter_destroy(&tctx->inflight);
7824 kfree(tctx);
7825 return ret;
7826 }
7827
Jens Axboe0f212202020-09-13 13:09:39 -06007828 xa_init(&tctx->xa);
7829 init_waitqueue_head(&tctx->wait);
7830 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007831 atomic_set(&tctx->in_idle, 0);
7832 tctx->sqpoll = false;
Jens Axboe0f212202020-09-13 13:09:39 -06007833 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007834 spin_lock_init(&tctx->task_lock);
7835 INIT_WQ_LIST(&tctx->task_list);
7836 tctx->task_state = 0;
7837 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007838 return 0;
7839}
7840
7841void __io_uring_free(struct task_struct *tsk)
7842{
7843 struct io_uring_task *tctx = tsk->io_uring;
7844
7845 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboed8a6df12020-10-15 16:24:45 -06007846 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007847 kfree(tctx);
7848 tsk->io_uring = NULL;
7849}
7850
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007851static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx)
7852{
7853 int ret;
7854
7855 clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7856 reinit_completion(&sqd->completion);
7857 ctx->sqo_dead = ctx->sqo_exec = 0;
7858 sqd->task_pid = current->pid;
7859 current->flags |= PF_IO_WORKER;
7860 ret = io_wq_fork_thread(io_sq_thread, sqd);
7861 current->flags &= ~PF_IO_WORKER;
7862 if (ret < 0) {
7863 sqd->thread = NULL;
7864 return ret;
7865 }
7866 wait_for_completion(&sqd->completion);
7867 return io_uring_alloc_task_context(sqd->thread, ctx);
7868}
7869
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007870static int io_sq_offload_create(struct io_ring_ctx *ctx,
7871 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007872{
7873 int ret;
7874
Jens Axboed25e3a32021-02-16 11:41:41 -07007875 /* Retain compatibility with failing for an invalid attach attempt */
7876 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7877 IORING_SETUP_ATTACH_WQ) {
7878 struct fd f;
7879
7880 f = fdget(p->wq_fd);
7881 if (!f.file)
7882 return -ENXIO;
7883 if (f.file->f_op != &io_uring_fops) {
7884 fdput(f);
7885 return -EINVAL;
7886 }
7887 fdput(f);
7888 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007889 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007890 struct io_sq_data *sqd;
7891
Jens Axboe3ec482d2019-04-08 10:51:01 -06007892 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007893 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007894 goto err;
7895
Jens Axboe534ca6d2020-09-02 13:52:19 -06007896 sqd = io_get_sq_data(p);
7897 if (IS_ERR(sqd)) {
7898 ret = PTR_ERR(sqd);
7899 goto err;
7900 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007901
Jens Axboe534ca6d2020-09-02 13:52:19 -06007902 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007903 io_sq_thread_park(sqd);
7904 mutex_lock(&sqd->ctx_lock);
7905 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7906 mutex_unlock(&sqd->ctx_lock);
7907 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007908
Jens Axboe917257d2019-04-13 09:28:55 -06007909 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7910 if (!ctx->sq_thread_idle)
7911 ctx->sq_thread_idle = HZ;
7912
Jens Axboeaa061652020-09-02 14:50:27 -06007913 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007914 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007915
Jens Axboe6c271ce2019-01-10 11:22:30 -07007916 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007917 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007918
Jens Axboe917257d2019-04-13 09:28:55 -06007919 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007920 if (cpu >= nr_cpu_ids)
7921 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007922 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007923 goto err;
7924
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007925 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007926 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007927 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007928 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007929
7930 sqd->task_pid = current->pid;
7931 current->flags |= PF_IO_WORKER;
7932 ret = io_wq_fork_thread(io_sq_thread, sqd);
7933 current->flags &= ~PF_IO_WORKER;
7934 if (ret < 0) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007935 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007936 goto err;
7937 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007938 wait_for_completion(&sqd->completion);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007939 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06007940 if (ret)
7941 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007942 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7943 /* Can't have SQ_AFF without SQPOLL */
7944 ret = -EINVAL;
7945 goto err;
7946 }
7947
Jens Axboe2b188cc2019-01-07 10:46:33 -07007948 return 0;
7949err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007950 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007951 return ret;
7952}
7953
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007954static void io_sq_offload_start(struct io_ring_ctx *ctx)
7955{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007956 struct io_sq_data *sqd = ctx->sq_data;
7957
Jens Axboeeb858902021-02-25 10:13:29 -07007958 if (ctx->flags & IORING_SETUP_SQPOLL)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007959 complete(&sqd->startup);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007960}
7961
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007962static inline void __io_unaccount_mem(struct user_struct *user,
7963 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007964{
7965 atomic_long_sub(nr_pages, &user->locked_vm);
7966}
7967
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007968static inline int __io_account_mem(struct user_struct *user,
7969 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007970{
7971 unsigned long page_limit, cur_pages, new_pages;
7972
7973 /* Don't allow more pages than we can safely lock */
7974 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7975
7976 do {
7977 cur_pages = atomic_long_read(&user->locked_vm);
7978 new_pages = cur_pages + nr_pages;
7979 if (new_pages > page_limit)
7980 return -ENOMEM;
7981 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7982 new_pages) != cur_pages);
7983
7984 return 0;
7985}
7986
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007987static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007988{
Jens Axboe62e398b2021-02-21 16:19:37 -07007989 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007990 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007991
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007992 if (ctx->mm_account)
7993 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007994}
7995
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007996static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007997{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007998 int ret;
7999
Jens Axboe62e398b2021-02-21 16:19:37 -07008000 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008001 ret = __io_account_mem(ctx->user, nr_pages);
8002 if (ret)
8003 return ret;
8004 }
8005
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008006 if (ctx->mm_account)
8007 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008008
8009 return 0;
8010}
8011
Jens Axboe2b188cc2019-01-07 10:46:33 -07008012static void io_mem_free(void *ptr)
8013{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008014 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008015
Mark Rutland52e04ef2019-04-30 17:30:21 +01008016 if (!ptr)
8017 return;
8018
8019 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008020 if (put_page_testzero(page))
8021 free_compound_page(page);
8022}
8023
8024static void *io_mem_alloc(size_t size)
8025{
8026 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008027 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008028
8029 return (void *) __get_free_pages(gfp_flags, get_order(size));
8030}
8031
Hristo Venev75b28af2019-08-26 17:23:46 +00008032static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8033 size_t *sq_offset)
8034{
8035 struct io_rings *rings;
8036 size_t off, sq_array_size;
8037
8038 off = struct_size(rings, cqes, cq_entries);
8039 if (off == SIZE_MAX)
8040 return SIZE_MAX;
8041
8042#ifdef CONFIG_SMP
8043 off = ALIGN(off, SMP_CACHE_BYTES);
8044 if (off == 0)
8045 return SIZE_MAX;
8046#endif
8047
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008048 if (sq_offset)
8049 *sq_offset = off;
8050
Hristo Venev75b28af2019-08-26 17:23:46 +00008051 sq_array_size = array_size(sizeof(u32), sq_entries);
8052 if (sq_array_size == SIZE_MAX)
8053 return SIZE_MAX;
8054
8055 if (check_add_overflow(off, sq_array_size, &off))
8056 return SIZE_MAX;
8057
Hristo Venev75b28af2019-08-26 17:23:46 +00008058 return off;
8059}
8060
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008061static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008062{
8063 int i, j;
8064
8065 if (!ctx->user_bufs)
8066 return -ENXIO;
8067
8068 for (i = 0; i < ctx->nr_user_bufs; i++) {
8069 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8070
8071 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008072 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008073
Jens Axboede293932020-09-17 16:19:16 -06008074 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008075 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008076 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008077 imu->nr_bvecs = 0;
8078 }
8079
8080 kfree(ctx->user_bufs);
8081 ctx->user_bufs = NULL;
8082 ctx->nr_user_bufs = 0;
8083 return 0;
8084}
8085
8086static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8087 void __user *arg, unsigned index)
8088{
8089 struct iovec __user *src;
8090
8091#ifdef CONFIG_COMPAT
8092 if (ctx->compat) {
8093 struct compat_iovec __user *ciovs;
8094 struct compat_iovec ciov;
8095
8096 ciovs = (struct compat_iovec __user *) arg;
8097 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8098 return -EFAULT;
8099
Jens Axboed55e5f52019-12-11 16:12:15 -07008100 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008101 dst->iov_len = ciov.iov_len;
8102 return 0;
8103 }
8104#endif
8105 src = (struct iovec __user *) arg;
8106 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8107 return -EFAULT;
8108 return 0;
8109}
8110
Jens Axboede293932020-09-17 16:19:16 -06008111/*
8112 * Not super efficient, but this is just a registration time. And we do cache
8113 * the last compound head, so generally we'll only do a full search if we don't
8114 * match that one.
8115 *
8116 * We check if the given compound head page has already been accounted, to
8117 * avoid double accounting it. This allows us to account the full size of the
8118 * page, not just the constituent pages of a huge page.
8119 */
8120static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8121 int nr_pages, struct page *hpage)
8122{
8123 int i, j;
8124
8125 /* check current page array */
8126 for (i = 0; i < nr_pages; i++) {
8127 if (!PageCompound(pages[i]))
8128 continue;
8129 if (compound_head(pages[i]) == hpage)
8130 return true;
8131 }
8132
8133 /* check previously registered pages */
8134 for (i = 0; i < ctx->nr_user_bufs; i++) {
8135 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8136
8137 for (j = 0; j < imu->nr_bvecs; j++) {
8138 if (!PageCompound(imu->bvec[j].bv_page))
8139 continue;
8140 if (compound_head(imu->bvec[j].bv_page) == hpage)
8141 return true;
8142 }
8143 }
8144
8145 return false;
8146}
8147
8148static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8149 int nr_pages, struct io_mapped_ubuf *imu,
8150 struct page **last_hpage)
8151{
8152 int i, ret;
8153
8154 for (i = 0; i < nr_pages; i++) {
8155 if (!PageCompound(pages[i])) {
8156 imu->acct_pages++;
8157 } else {
8158 struct page *hpage;
8159
8160 hpage = compound_head(pages[i]);
8161 if (hpage == *last_hpage)
8162 continue;
8163 *last_hpage = hpage;
8164 if (headpage_already_acct(ctx, pages, i, hpage))
8165 continue;
8166 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8167 }
8168 }
8169
8170 if (!imu->acct_pages)
8171 return 0;
8172
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008173 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008174 if (ret)
8175 imu->acct_pages = 0;
8176 return ret;
8177}
8178
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008179static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8180 struct io_mapped_ubuf *imu,
8181 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008182{
8183 struct vm_area_struct **vmas = NULL;
8184 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008185 unsigned long off, start, end, ubuf;
8186 size_t size;
8187 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008188
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008189 ubuf = (unsigned long) iov->iov_base;
8190 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8191 start = ubuf >> PAGE_SHIFT;
8192 nr_pages = end - start;
8193
8194 ret = -ENOMEM;
8195
8196 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8197 if (!pages)
8198 goto done;
8199
8200 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8201 GFP_KERNEL);
8202 if (!vmas)
8203 goto done;
8204
8205 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8206 GFP_KERNEL);
8207 if (!imu->bvec)
8208 goto done;
8209
8210 ret = 0;
8211 mmap_read_lock(current->mm);
8212 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8213 pages, vmas);
8214 if (pret == nr_pages) {
8215 /* don't support file backed memory */
8216 for (i = 0; i < nr_pages; i++) {
8217 struct vm_area_struct *vma = vmas[i];
8218
8219 if (vma->vm_file &&
8220 !is_file_hugepages(vma->vm_file)) {
8221 ret = -EOPNOTSUPP;
8222 break;
8223 }
8224 }
8225 } else {
8226 ret = pret < 0 ? pret : -EFAULT;
8227 }
8228 mmap_read_unlock(current->mm);
8229 if (ret) {
8230 /*
8231 * if we did partial map, or found file backed vmas,
8232 * release any pages we did get
8233 */
8234 if (pret > 0)
8235 unpin_user_pages(pages, pret);
8236 kvfree(imu->bvec);
8237 goto done;
8238 }
8239
8240 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8241 if (ret) {
8242 unpin_user_pages(pages, pret);
8243 kvfree(imu->bvec);
8244 goto done;
8245 }
8246
8247 off = ubuf & ~PAGE_MASK;
8248 size = iov->iov_len;
8249 for (i = 0; i < nr_pages; i++) {
8250 size_t vec_len;
8251
8252 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8253 imu->bvec[i].bv_page = pages[i];
8254 imu->bvec[i].bv_len = vec_len;
8255 imu->bvec[i].bv_offset = off;
8256 off = 0;
8257 size -= vec_len;
8258 }
8259 /* store original address for later verification */
8260 imu->ubuf = ubuf;
8261 imu->len = iov->iov_len;
8262 imu->nr_bvecs = nr_pages;
8263 ret = 0;
8264done:
8265 kvfree(pages);
8266 kvfree(vmas);
8267 return ret;
8268}
8269
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008270static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008271{
Jens Axboeedafcce2019-01-09 09:16:05 -07008272 if (ctx->user_bufs)
8273 return -EBUSY;
8274 if (!nr_args || nr_args > UIO_MAXIOV)
8275 return -EINVAL;
8276
8277 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8278 GFP_KERNEL);
8279 if (!ctx->user_bufs)
8280 return -ENOMEM;
8281
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008282 return 0;
8283}
8284
8285static int io_buffer_validate(struct iovec *iov)
8286{
8287 /*
8288 * Don't impose further limits on the size and buffer
8289 * constraints here, we'll -EINVAL later when IO is
8290 * submitted if they are wrong.
8291 */
8292 if (!iov->iov_base || !iov->iov_len)
8293 return -EFAULT;
8294
8295 /* arbitrary limit, but we need something */
8296 if (iov->iov_len > SZ_1G)
8297 return -EFAULT;
8298
8299 return 0;
8300}
8301
8302static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8303 unsigned int nr_args)
8304{
8305 int i, ret;
8306 struct iovec iov;
8307 struct page *last_hpage = NULL;
8308
8309 ret = io_buffers_map_alloc(ctx, nr_args);
8310 if (ret)
8311 return ret;
8312
Jens Axboeedafcce2019-01-09 09:16:05 -07008313 for (i = 0; i < nr_args; i++) {
8314 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008315
8316 ret = io_copy_iov(ctx, &iov, arg, i);
8317 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008318 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008319
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008320 ret = io_buffer_validate(&iov);
8321 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008322 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008323
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008324 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8325 if (ret)
8326 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008327
8328 ctx->nr_user_bufs++;
8329 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008330
8331 if (ret)
8332 io_sqe_buffers_unregister(ctx);
8333
Jens Axboeedafcce2019-01-09 09:16:05 -07008334 return ret;
8335}
8336
Jens Axboe9b402842019-04-11 11:45:41 -06008337static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8338{
8339 __s32 __user *fds = arg;
8340 int fd;
8341
8342 if (ctx->cq_ev_fd)
8343 return -EBUSY;
8344
8345 if (copy_from_user(&fd, fds, sizeof(*fds)))
8346 return -EFAULT;
8347
8348 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8349 if (IS_ERR(ctx->cq_ev_fd)) {
8350 int ret = PTR_ERR(ctx->cq_ev_fd);
8351 ctx->cq_ev_fd = NULL;
8352 return ret;
8353 }
8354
8355 return 0;
8356}
8357
8358static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8359{
8360 if (ctx->cq_ev_fd) {
8361 eventfd_ctx_put(ctx->cq_ev_fd);
8362 ctx->cq_ev_fd = NULL;
8363 return 0;
8364 }
8365
8366 return -ENXIO;
8367}
8368
Jens Axboe5a2e7452020-02-23 16:23:11 -07008369static int __io_destroy_buffers(int id, void *p, void *data)
8370{
8371 struct io_ring_ctx *ctx = data;
8372 struct io_buffer *buf = p;
8373
Jens Axboe067524e2020-03-02 16:32:28 -07008374 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008375 return 0;
8376}
8377
8378static void io_destroy_buffers(struct io_ring_ctx *ctx)
8379{
8380 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8381 idr_destroy(&ctx->io_buffer_idr);
8382}
8383
Jens Axboe68e68ee2021-02-13 09:00:02 -07008384static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008385{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008386 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008387
Jens Axboe68e68ee2021-02-13 09:00:02 -07008388 list_for_each_entry_safe(req, nxt, list, compl.list) {
8389 if (tsk && req->task != tsk)
8390 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008391 list_del(&req->compl.list);
8392 kmem_cache_free(req_cachep, req);
8393 }
8394}
8395
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008396static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008397{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008398 struct io_submit_state *submit_state = &ctx->submit_state;
Pavel Begunkove5547d22021-02-23 22:17:20 +00008399 struct io_comp_state *cs = &ctx->submit_state.comp;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008400
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008401 mutex_lock(&ctx->uring_lock);
8402
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008403 if (submit_state->free_reqs) {
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008404 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8405 submit_state->reqs);
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008406 submit_state->free_reqs = 0;
8407 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008408
8409 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkove5547d22021-02-23 22:17:20 +00008410 list_splice_init(&cs->locked_free_list, &cs->free_list);
8411 cs->locked_free_nr = 0;
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008412 spin_unlock_irq(&ctx->completion_lock);
8413
Pavel Begunkove5547d22021-02-23 22:17:20 +00008414 io_req_cache_free(&cs->free_list, NULL);
8415
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008416 mutex_unlock(&ctx->uring_lock);
8417}
8418
Jens Axboe2b188cc2019-01-07 10:46:33 -07008419static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8420{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008421 /*
8422 * Some may use context even when all refs and requests have been put,
8423 * and they are free to do so while still holding uring_lock, see
8424 * __io_req_task_submit(). Wait for them to finish.
8425 */
8426 mutex_lock(&ctx->uring_lock);
8427 mutex_unlock(&ctx->uring_lock);
8428
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008429 io_sq_thread_finish(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008430 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008431
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008432 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008433 mmdrop(ctx->mm_account);
8434 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008435 }
Jens Axboedef596e2019-01-09 08:59:42 -07008436
Hao Xu8bad28d2021-02-19 17:19:36 +08008437 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008438 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008439 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008440 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008441 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008442 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008443
Jens Axboe2b188cc2019-01-07 10:46:33 -07008444#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008445 if (ctx->ring_sock) {
8446 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008447 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008448 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008449#endif
8450
Hristo Venev75b28af2019-08-26 17:23:46 +00008451 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008452 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008453
8454 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008455 free_uid(ctx->user);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008456 io_req_caches_free(ctx, NULL);
Jens Axboee9418942021-02-19 12:33:30 -07008457 if (ctx->hash_map)
8458 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008459 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008460 kfree(ctx);
8461}
8462
8463static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8464{
8465 struct io_ring_ctx *ctx = file->private_data;
8466 __poll_t mask = 0;
8467
8468 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008469 /*
8470 * synchronizes with barrier from wq_has_sleeper call in
8471 * io_commit_cqring
8472 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008473 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008474 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008475 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008476
8477 /*
8478 * Don't flush cqring overflow list here, just do a simple check.
8479 * Otherwise there could possible be ABBA deadlock:
8480 * CPU0 CPU1
8481 * ---- ----
8482 * lock(&ctx->uring_lock);
8483 * lock(&ep->mtx);
8484 * lock(&ctx->uring_lock);
8485 * lock(&ep->mtx);
8486 *
8487 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8488 * pushs them to do the flush.
8489 */
8490 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008491 mask |= EPOLLIN | EPOLLRDNORM;
8492
8493 return mask;
8494}
8495
8496static int io_uring_fasync(int fd, struct file *file, int on)
8497{
8498 struct io_ring_ctx *ctx = file->private_data;
8499
8500 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8501}
8502
Yejune Deng0bead8c2020-12-24 11:02:20 +08008503static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008504{
Jens Axboe4379bf82021-02-15 13:40:22 -07008505 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008506
Jens Axboe4379bf82021-02-15 13:40:22 -07008507 creds = idr_remove(&ctx->personality_idr, id);
8508 if (creds) {
8509 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008510 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008511 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008512
8513 return -EINVAL;
8514}
8515
8516static int io_remove_personalities(int id, void *p, void *data)
8517{
8518 struct io_ring_ctx *ctx = data;
8519
8520 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008521 return 0;
8522}
8523
Pavel Begunkovba50a032021-02-26 15:47:56 +00008524static bool io_run_ctx_fallback(struct io_ring_ctx *ctx)
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008525{
8526 struct callback_head *work, *head, *next;
Pavel Begunkovba50a032021-02-26 15:47:56 +00008527 bool executed = false;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008528
8529 do {
8530 do {
8531 head = NULL;
8532 work = READ_ONCE(ctx->exit_task_work);
8533 } while (cmpxchg(&ctx->exit_task_work, work, head) != work);
8534
8535 if (!work)
8536 break;
8537
8538 do {
8539 next = work->next;
8540 work->func(work);
8541 work = next;
8542 cond_resched();
8543 } while (work);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008544 executed = true;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008545 } while (1);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008546
8547 return executed;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008548}
8549
Jens Axboe85faa7b2020-04-09 18:14:00 -06008550static void io_ring_exit_work(struct work_struct *work)
8551{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008552 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8553 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008554
Jens Axboe56952e92020-06-17 15:00:04 -06008555 /*
8556 * If we're doing polled IO and end up having requests being
8557 * submitted async (out-of-line), then completions can come in while
8558 * we're waiting for refs to drop. We need to reap these manually,
8559 * as nobody else will be looking for them.
8560 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008561 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008562 io_uring_try_cancel_requests(ctx, NULL, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008563 io_run_ctx_fallback(ctx);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008564 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008565 io_ring_ctx_free(ctx);
8566}
8567
Jens Axboe2b188cc2019-01-07 10:46:33 -07008568static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8569{
8570 mutex_lock(&ctx->uring_lock);
8571 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008572
8573 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8574 ctx->sqo_dead = 1;
8575
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008576 /* if force is set, the ring is going away. always drop after that */
8577 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008578 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008579 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008580 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008581 mutex_unlock(&ctx->uring_lock);
8582
Pavel Begunkov6b819282020-11-06 13:00:25 +00008583 io_kill_timeouts(ctx, NULL, NULL);
8584 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008585
Jens Axboe15dff282019-11-13 09:09:23 -07008586 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008587 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008588
Jens Axboe85faa7b2020-04-09 18:14:00 -06008589 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008590 /*
8591 * Use system_unbound_wq to avoid spawning tons of event kworkers
8592 * if we're exiting a ton of rings at the same time. It just adds
8593 * noise and overhead, there's no discernable change in runtime
8594 * over using system_wq.
8595 */
8596 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008597}
8598
8599static int io_uring_release(struct inode *inode, struct file *file)
8600{
8601 struct io_ring_ctx *ctx = file->private_data;
8602
8603 file->private_data = NULL;
8604 io_ring_ctx_wait_and_kill(ctx);
8605 return 0;
8606}
8607
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008608struct io_task_cancel {
8609 struct task_struct *task;
8610 struct files_struct *files;
8611};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008612
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008613static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008614{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008615 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008616 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008617 bool ret;
8618
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008619 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008620 unsigned long flags;
8621 struct io_ring_ctx *ctx = req->ctx;
8622
8623 /* protect against races with linked timeouts */
8624 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008625 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008626 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8627 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008628 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008629 }
8630 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008631}
8632
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008633static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008634 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008635 struct files_struct *files)
8636{
8637 struct io_defer_entry *de = NULL;
8638 LIST_HEAD(list);
8639
8640 spin_lock_irq(&ctx->completion_lock);
8641 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008642 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008643 list_cut_position(&list, &ctx->defer_list, &de->list);
8644 break;
8645 }
8646 }
8647 spin_unlock_irq(&ctx->completion_lock);
8648
8649 while (!list_empty(&list)) {
8650 de = list_first_entry(&list, struct io_defer_entry, list);
8651 list_del_init(&de->list);
8652 req_set_fail_links(de->req);
8653 io_put_req(de->req);
8654 io_req_complete(de->req, -ECANCELED);
8655 kfree(de);
8656 }
8657}
8658
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008659static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8660 struct task_struct *task,
8661 struct files_struct *files)
8662{
8663 struct io_task_cancel cancel = { .task = task, .files = files, };
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008664 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008665
8666 while (1) {
8667 enum io_wq_cancel cret;
8668 bool ret = false;
8669
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008670 if (tctx && tctx->io_wq) {
8671 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008672 &cancel, true);
8673 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8674 }
8675
8676 /* SQPOLL thread does its own polling */
8677 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8678 while (!list_empty_careful(&ctx->iopoll_list)) {
8679 io_iopoll_try_reap_events(ctx);
8680 ret = true;
8681 }
8682 }
8683
8684 ret |= io_poll_remove_all(ctx, task, files);
8685 ret |= io_kill_timeouts(ctx, task, files);
8686 ret |= io_run_task_work();
Pavel Begunkovba50a032021-02-26 15:47:56 +00008687 ret |= io_run_ctx_fallback(ctx);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008688 io_cqring_overflow_flush(ctx, true, task, files);
8689 if (!ret)
8690 break;
8691 cond_resched();
8692 }
8693}
8694
Pavel Begunkovca70f002021-01-26 15:28:27 +00008695static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8696 struct task_struct *task,
8697 struct files_struct *files)
8698{
8699 struct io_kiocb *req;
8700 int cnt = 0;
8701
8702 spin_lock_irq(&ctx->inflight_lock);
8703 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8704 cnt += io_match_task(req, task, files);
8705 spin_unlock_irq(&ctx->inflight_lock);
8706 return cnt;
8707}
8708
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008709static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008710 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008711 struct files_struct *files)
8712{
Jens Axboefcb323c2019-10-24 12:39:47 -06008713 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008714 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008715 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008716
Pavel Begunkovca70f002021-01-26 15:28:27 +00008717 inflight = io_uring_count_inflight(ctx, task, files);
8718 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008719 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008720
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008721 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008722
Pavel Begunkov34343782021-02-10 11:45:42 +00008723 if (ctx->sq_data)
8724 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008725 prepare_to_wait(&task->io_uring->wait, &wait,
8726 TASK_UNINTERRUPTIBLE);
8727 if (inflight == io_uring_count_inflight(ctx, task, files))
8728 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008729 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00008730 if (ctx->sq_data)
8731 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06008732 }
Jens Axboe0f212202020-09-13 13:09:39 -06008733}
8734
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008735static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8736{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008737 mutex_lock(&ctx->uring_lock);
8738 ctx->sqo_dead = 1;
8739 mutex_unlock(&ctx->uring_lock);
8740
8741 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008742 if (ctx->rings)
8743 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008744}
8745
Jens Axboe0f212202020-09-13 13:09:39 -06008746/*
8747 * We need to iteratively cancel requests, in case a request has dependent
8748 * hard links. These persist even for failure of cancelations, hence keep
8749 * looping until none are found.
8750 */
8751static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8752 struct files_struct *files)
8753{
8754 struct task_struct *task = current;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008755 bool did_park = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008756
Jens Axboefdaf0832020-10-30 09:37:30 -06008757 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008758 io_disable_sqo_submit(ctx);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008759 did_park = io_sq_thread_park(ctx->sq_data);
8760 if (did_park) {
8761 task = ctx->sq_data->thread;
8762 atomic_inc(&task->io_uring->in_idle);
8763 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008764 }
Jens Axboe0f212202020-09-13 13:09:39 -06008765
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008766 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008767
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008768 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008769 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008770 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008771
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008772 if (did_park) {
Jens Axboefdaf0832020-10-30 09:37:30 -06008773 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008774 io_sq_thread_unpark(ctx->sq_data);
8775 }
Jens Axboe0f212202020-09-13 13:09:39 -06008776}
8777
8778/*
8779 * Note that this task has used io_uring. We use it for cancelation purposes.
8780 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008781static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008782{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008783 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008784 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008785
8786 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008787 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008788 if (unlikely(ret))
8789 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008790 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008791 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008792 if (tctx->last != file) {
8793 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008794
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008795 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008796 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008797 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8798 file, GFP_KERNEL));
8799 if (ret) {
8800 fput(file);
8801 return ret;
8802 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008803
8804 /* one and only SQPOLL file note, held by sqo_task */
8805 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8806 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008807 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008808 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008809 }
8810
Jens Axboefdaf0832020-10-30 09:37:30 -06008811 /*
8812 * This is race safe in that the task itself is doing this, hence it
8813 * cannot be going through the exit/cancel paths at the same time.
8814 * This cannot be modified while exit/cancel is running.
8815 */
8816 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8817 tctx->sqpoll = true;
8818
Jens Axboe0f212202020-09-13 13:09:39 -06008819 return 0;
8820}
8821
8822/*
8823 * Remove this io_uring_file -> task mapping.
8824 */
8825static void io_uring_del_task_file(struct file *file)
8826{
8827 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008828
8829 if (tctx->last == file)
8830 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008831 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008832 if (file)
8833 fput(file);
8834}
8835
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008836static void io_uring_remove_task_files(struct io_uring_task *tctx)
8837{
8838 struct file *file;
8839 unsigned long index;
8840
8841 xa_for_each(&tctx->xa, index, file)
8842 io_uring_del_task_file(file);
8843}
8844
Jens Axboe0f212202020-09-13 13:09:39 -06008845void __io_uring_files_cancel(struct files_struct *files)
8846{
8847 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008848 struct file *file;
8849 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008850
8851 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008852 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008853 xa_for_each(&tctx->xa, index, file)
8854 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008855 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008856
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008857 if (files) {
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008858 io_uring_remove_task_files(tctx);
Jens Axboe8a378fb2021-02-23 12:27:49 -07008859 if (tctx->io_wq) {
Jens Axboeafcc4012021-02-26 13:48:19 -07008860 io_wq_put_and_exit(tctx->io_wq);
Jens Axboe8a378fb2021-02-23 12:27:49 -07008861 tctx->io_wq = NULL;
8862 }
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008863 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008864}
8865
8866static s64 tctx_inflight(struct io_uring_task *tctx)
8867{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008868 return percpu_counter_sum(&tctx->inflight);
8869}
8870
8871static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
8872{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008873 struct io_sq_data *sqd = ctx->sq_data;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008874 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06008875 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008876 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008877
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008878 if (!sqd)
8879 return;
8880 io_disable_sqo_submit(ctx);
8881 if (!io_sq_thread_park(sqd))
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008882 return;
8883 tctx = ctx->sq_data->thread->io_uring;
Jens Axboee54945a2021-02-26 11:27:15 -07008884 /* can happen on fork/alloc failure, just ignore that state */
8885 if (!tctx) {
8886 io_sq_thread_unpark(sqd);
8887 return;
8888 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008889
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008890 atomic_inc(&tctx->in_idle);
8891 do {
8892 /* read completions before cancelations */
8893 inflight = tctx_inflight(tctx);
8894 if (!inflight)
8895 break;
8896 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008897
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008898 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8899 /*
8900 * If we've seen completions, retry without waiting. This
8901 * avoids a race where a completion comes in before we did
8902 * prepare_to_wait().
8903 */
8904 if (inflight == tctx_inflight(tctx))
8905 schedule();
8906 finish_wait(&tctx->wait, &wait);
8907 } while (1);
8908 atomic_dec(&tctx->in_idle);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008909 io_sq_thread_unpark(sqd);
Jens Axboe0f212202020-09-13 13:09:39 -06008910}
8911
Jens Axboe0f212202020-09-13 13:09:39 -06008912/*
8913 * Find any io_uring fd that this task has registered or done IO on, and cancel
8914 * requests.
8915 */
8916void __io_uring_task_cancel(void)
8917{
8918 struct io_uring_task *tctx = current->io_uring;
8919 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008920 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008921
8922 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008923 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008924
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008925 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008926 if (tctx->sqpoll) {
8927 struct file *file;
8928 unsigned long index;
8929
8930 xa_for_each(&tctx->xa, index, file)
8931 io_uring_cancel_sqpoll(file->private_data);
8932 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008933
Jens Axboed8a6df12020-10-15 16:24:45 -06008934 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008935 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008936 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008937 if (!inflight)
8938 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008939 __io_uring_files_cancel(NULL);
8940
8941 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8942
8943 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008944 * If we've seen completions, retry without waiting. This
8945 * avoids a race where a completion comes in before we did
8946 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008947 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008948 if (inflight == tctx_inflight(tctx))
8949 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00008950 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008951 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008952
Jens Axboefdaf0832020-10-30 09:37:30 -06008953 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008954
8955 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008956}
8957
Jens Axboefcb323c2019-10-24 12:39:47 -06008958static int io_uring_flush(struct file *file, void *data)
8959{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008960 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008961 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008962
Jens Axboe3bfe6102021-02-16 14:15:30 -07008963 /* Ignore helper thread files exit */
8964 if (current->flags & PF_IO_WORKER)
8965 return 0;
8966
Jens Axboe41be53e2021-02-13 09:11:04 -07008967 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07008968 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07008969 io_req_caches_free(ctx, current);
8970 }
Jens Axboe84965ff2021-01-23 15:51:11 -07008971
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008972 io_run_ctx_fallback(ctx);
8973
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008974 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008975 return 0;
8976
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008977 /* we should have cancelled and erased it before PF_EXITING */
8978 WARN_ON_ONCE((current->flags & PF_EXITING) &&
8979 xa_load(&tctx->xa, (unsigned long)file));
8980
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008981 /*
8982 * fput() is pending, will be 2 if the only other ref is our potential
8983 * task file note. If the task is exiting, drop regardless of count.
8984 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008985 if (atomic_long_read(&file->f_count) != 2)
8986 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008987
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008988 if (ctx->flags & IORING_SETUP_SQPOLL) {
8989 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00008990 WARN_ON_ONCE(ctx->sqo_task != current &&
8991 xa_load(&tctx->xa, (unsigned long)file));
8992 /* sqo_dead check is for when this happens after cancellation */
8993 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008994 !xa_load(&tctx->xa, (unsigned long)file));
8995
8996 io_disable_sqo_submit(ctx);
8997 }
8998
8999 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9000 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009001 return 0;
9002}
9003
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009004static void *io_uring_validate_mmap_request(struct file *file,
9005 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009006{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009007 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009008 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009009 struct page *page;
9010 void *ptr;
9011
9012 switch (offset) {
9013 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009014 case IORING_OFF_CQ_RING:
9015 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009016 break;
9017 case IORING_OFF_SQES:
9018 ptr = ctx->sq_sqes;
9019 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009020 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009021 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009022 }
9023
9024 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009025 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009026 return ERR_PTR(-EINVAL);
9027
9028 return ptr;
9029}
9030
9031#ifdef CONFIG_MMU
9032
9033static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9034{
9035 size_t sz = vma->vm_end - vma->vm_start;
9036 unsigned long pfn;
9037 void *ptr;
9038
9039 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9040 if (IS_ERR(ptr))
9041 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009042
9043 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9044 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9045}
9046
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009047#else /* !CONFIG_MMU */
9048
9049static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9050{
9051 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9052}
9053
9054static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9055{
9056 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9057}
9058
9059static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9060 unsigned long addr, unsigned long len,
9061 unsigned long pgoff, unsigned long flags)
9062{
9063 void *ptr;
9064
9065 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9066 if (IS_ERR(ptr))
9067 return PTR_ERR(ptr);
9068
9069 return (unsigned long) ptr;
9070}
9071
9072#endif /* !CONFIG_MMU */
9073
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009074static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009075{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009076 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009077 DEFINE_WAIT(wait);
9078
9079 do {
9080 if (!io_sqring_full(ctx))
9081 break;
9082
9083 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9084
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009085 if (unlikely(ctx->sqo_dead)) {
9086 ret = -EOWNERDEAD;
9087 goto out;
9088 }
9089
Jens Axboe90554202020-09-03 12:12:41 -06009090 if (!io_sqring_full(ctx))
9091 break;
9092
9093 schedule();
9094 } while (!signal_pending(current));
9095
9096 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009097out:
9098 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009099}
9100
Hao Xuc73ebb62020-11-03 10:54:37 +08009101static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9102 struct __kernel_timespec __user **ts,
9103 const sigset_t __user **sig)
9104{
9105 struct io_uring_getevents_arg arg;
9106
9107 /*
9108 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9109 * is just a pointer to the sigset_t.
9110 */
9111 if (!(flags & IORING_ENTER_EXT_ARG)) {
9112 *sig = (const sigset_t __user *) argp;
9113 *ts = NULL;
9114 return 0;
9115 }
9116
9117 /*
9118 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9119 * timespec and sigset_t pointers if good.
9120 */
9121 if (*argsz != sizeof(arg))
9122 return -EINVAL;
9123 if (copy_from_user(&arg, argp, sizeof(arg)))
9124 return -EFAULT;
9125 *sig = u64_to_user_ptr(arg.sigmask);
9126 *argsz = arg.sigmask_sz;
9127 *ts = u64_to_user_ptr(arg.ts);
9128 return 0;
9129}
9130
Jens Axboe2b188cc2019-01-07 10:46:33 -07009131SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009132 u32, min_complete, u32, flags, const void __user *, argp,
9133 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009134{
9135 struct io_ring_ctx *ctx;
9136 long ret = -EBADF;
9137 int submitted = 0;
9138 struct fd f;
9139
Jens Axboe4c6e2772020-07-01 11:29:10 -06009140 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009141
Jens Axboe90554202020-09-03 12:12:41 -06009142 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009143 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009144 return -EINVAL;
9145
9146 f = fdget(fd);
9147 if (!f.file)
9148 return -EBADF;
9149
9150 ret = -EOPNOTSUPP;
9151 if (f.file->f_op != &io_uring_fops)
9152 goto out_fput;
9153
9154 ret = -ENXIO;
9155 ctx = f.file->private_data;
9156 if (!percpu_ref_tryget(&ctx->refs))
9157 goto out_fput;
9158
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009159 ret = -EBADFD;
9160 if (ctx->flags & IORING_SETUP_R_DISABLED)
9161 goto out;
9162
Jens Axboe6c271ce2019-01-10 11:22:30 -07009163 /*
9164 * For SQ polling, the thread will do all submissions and completions.
9165 * Just return the requested submit count, and wake the thread if
9166 * we were asked to.
9167 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009168 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009169 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009170 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009171
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009172 if (unlikely(ctx->sqo_exec)) {
9173 ret = io_sq_thread_fork(ctx->sq_data, ctx);
9174 if (ret)
9175 goto out;
9176 ctx->sqo_exec = 0;
9177 }
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009178 ret = -EOWNERDEAD;
9179 if (unlikely(ctx->sqo_dead))
9180 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009181 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009182 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009183 if (flags & IORING_ENTER_SQ_WAIT) {
9184 ret = io_sqpoll_wait_sq(ctx);
9185 if (ret)
9186 goto out;
9187 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009188 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009189 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009190 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009191 if (unlikely(ret))
9192 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009193 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009194 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009195 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009196
9197 if (submitted != to_submit)
9198 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009199 }
9200 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009201 const sigset_t __user *sig;
9202 struct __kernel_timespec __user *ts;
9203
9204 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9205 if (unlikely(ret))
9206 goto out;
9207
Jens Axboe2b188cc2019-01-07 10:46:33 -07009208 min_complete = min(min_complete, ctx->cq_entries);
9209
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009210 /*
9211 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9212 * space applications don't need to do io completion events
9213 * polling again, they can rely on io_sq_thread to do polling
9214 * work, which can reduce cpu usage and uring_lock contention.
9215 */
9216 if (ctx->flags & IORING_SETUP_IOPOLL &&
9217 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009218 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009219 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009220 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009221 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009222 }
9223
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009224out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009225 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009226out_fput:
9227 fdput(f);
9228 return submitted ? submitted : ret;
9229}
9230
Tobias Klauserbebdb652020-02-26 18:38:32 +01009231#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009232static int io_uring_show_cred(int id, void *p, void *data)
9233{
Jens Axboe4379bf82021-02-15 13:40:22 -07009234 const struct cred *cred = p;
Jens Axboe87ce9552020-01-30 08:25:34 -07009235 struct seq_file *m = data;
9236 struct user_namespace *uns = seq_user_ns(m);
9237 struct group_info *gi;
9238 kernel_cap_t cap;
9239 unsigned __capi;
9240 int g;
9241
9242 seq_printf(m, "%5d\n", id);
9243 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9244 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9245 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9246 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9247 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9248 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9249 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9250 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9251 seq_puts(m, "\n\tGroups:\t");
9252 gi = cred->group_info;
9253 for (g = 0; g < gi->ngroups; g++) {
9254 seq_put_decimal_ull(m, g ? " " : "",
9255 from_kgid_munged(uns, gi->gid[g]));
9256 }
9257 seq_puts(m, "\n\tCapEff:\t");
9258 cap = cred->cap_effective;
9259 CAP_FOR_EACH_U32(__capi)
9260 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9261 seq_putc(m, '\n');
9262 return 0;
9263}
9264
9265static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9266{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009267 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009268 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009269 int i;
9270
Jens Axboefad8e0d2020-09-28 08:57:48 -06009271 /*
9272 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9273 * since fdinfo case grabs it in the opposite direction of normal use
9274 * cases. If we fail to get the lock, we just don't iterate any
9275 * structures that could be going away outside the io_uring mutex.
9276 */
9277 has_lock = mutex_trylock(&ctx->uring_lock);
9278
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009279 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009280 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009281 if (!sq->thread)
9282 sq = NULL;
9283 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009284
9285 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9286 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009287 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009288 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009289 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009290
Jens Axboe87ce9552020-01-30 08:25:34 -07009291 if (f)
9292 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9293 else
9294 seq_printf(m, "%5u: <none>\n", i);
9295 }
9296 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009297 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009298 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9299
9300 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9301 (unsigned int) buf->len);
9302 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009303 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009304 seq_printf(m, "Personalities:\n");
9305 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9306 }
Jens Axboed7718a92020-02-14 22:23:12 -07009307 seq_printf(m, "PollList:\n");
9308 spin_lock_irq(&ctx->completion_lock);
9309 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9310 struct hlist_head *list = &ctx->cancel_hash[i];
9311 struct io_kiocb *req;
9312
9313 hlist_for_each_entry(req, list, hash_node)
9314 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9315 req->task->task_works != NULL);
9316 }
9317 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009318 if (has_lock)
9319 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009320}
9321
9322static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9323{
9324 struct io_ring_ctx *ctx = f->private_data;
9325
9326 if (percpu_ref_tryget(&ctx->refs)) {
9327 __io_uring_show_fdinfo(ctx, m);
9328 percpu_ref_put(&ctx->refs);
9329 }
9330}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009331#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009332
Jens Axboe2b188cc2019-01-07 10:46:33 -07009333static const struct file_operations io_uring_fops = {
9334 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009335 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009336 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009337#ifndef CONFIG_MMU
9338 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9339 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9340#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009341 .poll = io_uring_poll,
9342 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009343#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009344 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009345#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009346};
9347
9348static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9349 struct io_uring_params *p)
9350{
Hristo Venev75b28af2019-08-26 17:23:46 +00009351 struct io_rings *rings;
9352 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009353
Jens Axboebd740482020-08-05 12:58:23 -06009354 /* make sure these are sane, as we already accounted them */
9355 ctx->sq_entries = p->sq_entries;
9356 ctx->cq_entries = p->cq_entries;
9357
Hristo Venev75b28af2019-08-26 17:23:46 +00009358 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9359 if (size == SIZE_MAX)
9360 return -EOVERFLOW;
9361
9362 rings = io_mem_alloc(size);
9363 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009364 return -ENOMEM;
9365
Hristo Venev75b28af2019-08-26 17:23:46 +00009366 ctx->rings = rings;
9367 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9368 rings->sq_ring_mask = p->sq_entries - 1;
9369 rings->cq_ring_mask = p->cq_entries - 1;
9370 rings->sq_ring_entries = p->sq_entries;
9371 rings->cq_ring_entries = p->cq_entries;
9372 ctx->sq_mask = rings->sq_ring_mask;
9373 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009374
9375 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009376 if (size == SIZE_MAX) {
9377 io_mem_free(ctx->rings);
9378 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009379 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009380 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009381
9382 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009383 if (!ctx->sq_sqes) {
9384 io_mem_free(ctx->rings);
9385 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009387 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009388
Jens Axboe2b188cc2019-01-07 10:46:33 -07009389 return 0;
9390}
9391
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009392static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9393{
9394 int ret, fd;
9395
9396 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9397 if (fd < 0)
9398 return fd;
9399
9400 ret = io_uring_add_task_file(ctx, file);
9401 if (ret) {
9402 put_unused_fd(fd);
9403 return ret;
9404 }
9405 fd_install(fd, file);
9406 return fd;
9407}
9408
Jens Axboe2b188cc2019-01-07 10:46:33 -07009409/*
9410 * Allocate an anonymous fd, this is what constitutes the application
9411 * visible backing of an io_uring instance. The application mmaps this
9412 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9413 * we have to tie this fd to a socket for file garbage collection purposes.
9414 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009415static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009416{
9417 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009418#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009419 int ret;
9420
Jens Axboe2b188cc2019-01-07 10:46:33 -07009421 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9422 &ctx->ring_sock);
9423 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009424 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009425#endif
9426
Jens Axboe2b188cc2019-01-07 10:46:33 -07009427 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9428 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009429#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009430 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009431 sock_release(ctx->ring_sock);
9432 ctx->ring_sock = NULL;
9433 } else {
9434 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009435 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009436#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009437 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009438}
9439
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009440static int io_uring_create(unsigned entries, struct io_uring_params *p,
9441 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009442{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009443 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009444 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009445 int ret;
9446
Jens Axboe8110c1a2019-12-28 15:39:54 -07009447 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009448 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009449 if (entries > IORING_MAX_ENTRIES) {
9450 if (!(p->flags & IORING_SETUP_CLAMP))
9451 return -EINVAL;
9452 entries = IORING_MAX_ENTRIES;
9453 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009454
9455 /*
9456 * Use twice as many entries for the CQ ring. It's possible for the
9457 * application to drive a higher depth than the size of the SQ ring,
9458 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009459 * some flexibility in overcommitting a bit. If the application has
9460 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9461 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009462 */
9463 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009464 if (p->flags & IORING_SETUP_CQSIZE) {
9465 /*
9466 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9467 * to a power-of-two, if it isn't already. We do NOT impose
9468 * any cq vs sq ring sizing.
9469 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009470 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009471 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009472 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9473 if (!(p->flags & IORING_SETUP_CLAMP))
9474 return -EINVAL;
9475 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9476 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009477 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9478 if (p->cq_entries < p->sq_entries)
9479 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009480 } else {
9481 p->cq_entries = 2 * p->sq_entries;
9482 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009483
Jens Axboe2b188cc2019-01-07 10:46:33 -07009484 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009485 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009486 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009487 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009488 if (!capable(CAP_IPC_LOCK))
9489 ctx->user = get_uid(current_user());
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009490 ctx->sqo_task = current;
Jens Axboe2aede0e2020-09-14 10:45:53 -06009491
9492 /*
9493 * This is just grabbed for accounting purposes. When a process exits,
9494 * the mm is exited and dropped before the files, hence we need to hang
9495 * on to this mm purely for the purposes of being able to unaccount
9496 * memory (locked/pinned vm). It's not used for anything else.
9497 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009498 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009499 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009500
Jens Axboe2b188cc2019-01-07 10:46:33 -07009501 ret = io_allocate_scq_urings(ctx, p);
9502 if (ret)
9503 goto err;
9504
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009505 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009506 if (ret)
9507 goto err;
9508
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009509 if (!(p->flags & IORING_SETUP_R_DISABLED))
9510 io_sq_offload_start(ctx);
9511
Jens Axboe2b188cc2019-01-07 10:46:33 -07009512 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009513 p->sq_off.head = offsetof(struct io_rings, sq.head);
9514 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9515 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9516 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9517 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9518 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9519 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009520
9521 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009522 p->cq_off.head = offsetof(struct io_rings, cq.head);
9523 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9524 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9525 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9526 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9527 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009528 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009529
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009530 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9531 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009532 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009533 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Jens Axboe1c0aa1f2021-02-20 11:55:28 -07009534 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009535
9536 if (copy_to_user(params, p, sizeof(*p))) {
9537 ret = -EFAULT;
9538 goto err;
9539 }
Jens Axboed1719f72020-07-30 13:43:53 -06009540
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009541 file = io_uring_get_file(ctx);
9542 if (IS_ERR(file)) {
9543 ret = PTR_ERR(file);
9544 goto err;
9545 }
9546
Jens Axboed1719f72020-07-30 13:43:53 -06009547 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009548 * Install ring fd as the very last thing, so we don't risk someone
9549 * having closed it before we finish setup
9550 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009551 ret = io_uring_install_fd(ctx, file);
9552 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009553 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009554 /* fput will clean it up */
9555 fput(file);
9556 return ret;
9557 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009558
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009559 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009560 return ret;
9561err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009562 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009563 io_ring_ctx_wait_and_kill(ctx);
9564 return ret;
9565}
9566
9567/*
9568 * Sets up an aio uring context, and returns the fd. Applications asks for a
9569 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9570 * params structure passed in.
9571 */
9572static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9573{
9574 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009575 int i;
9576
9577 if (copy_from_user(&p, params, sizeof(p)))
9578 return -EFAULT;
9579 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9580 if (p.resv[i])
9581 return -EINVAL;
9582 }
9583
Jens Axboe6c271ce2019-01-10 11:22:30 -07009584 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009585 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009586 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9587 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009588 return -EINVAL;
9589
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009590 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009591}
9592
9593SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9594 struct io_uring_params __user *, params)
9595{
9596 return io_uring_setup(entries, params);
9597}
9598
Jens Axboe66f4af92020-01-16 15:36:52 -07009599static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9600{
9601 struct io_uring_probe *p;
9602 size_t size;
9603 int i, ret;
9604
9605 size = struct_size(p, ops, nr_args);
9606 if (size == SIZE_MAX)
9607 return -EOVERFLOW;
9608 p = kzalloc(size, GFP_KERNEL);
9609 if (!p)
9610 return -ENOMEM;
9611
9612 ret = -EFAULT;
9613 if (copy_from_user(p, arg, size))
9614 goto out;
9615 ret = -EINVAL;
9616 if (memchr_inv(p, 0, size))
9617 goto out;
9618
9619 p->last_op = IORING_OP_LAST - 1;
9620 if (nr_args > IORING_OP_LAST)
9621 nr_args = IORING_OP_LAST;
9622
9623 for (i = 0; i < nr_args; i++) {
9624 p->ops[i].op = i;
9625 if (!io_op_defs[i].not_supported)
9626 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9627 }
9628 p->ops_len = i;
9629
9630 ret = 0;
9631 if (copy_to_user(arg, p, size))
9632 ret = -EFAULT;
9633out:
9634 kfree(p);
9635 return ret;
9636}
9637
Jens Axboe071698e2020-01-28 10:04:42 -07009638static int io_register_personality(struct io_ring_ctx *ctx)
9639{
Jens Axboe4379bf82021-02-15 13:40:22 -07009640 const struct cred *creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009641 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009642
Jens Axboe4379bf82021-02-15 13:40:22 -07009643 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009644
Jens Axboe4379bf82021-02-15 13:40:22 -07009645 ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9646 USHRT_MAX, GFP_KERNEL);
9647 if (ret < 0)
9648 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009649 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009650}
9651
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009652static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9653 unsigned int nr_args)
9654{
9655 struct io_uring_restriction *res;
9656 size_t size;
9657 int i, ret;
9658
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009659 /* Restrictions allowed only if rings started disabled */
9660 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9661 return -EBADFD;
9662
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009663 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009664 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009665 return -EBUSY;
9666
9667 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9668 return -EINVAL;
9669
9670 size = array_size(nr_args, sizeof(*res));
9671 if (size == SIZE_MAX)
9672 return -EOVERFLOW;
9673
9674 res = memdup_user(arg, size);
9675 if (IS_ERR(res))
9676 return PTR_ERR(res);
9677
9678 ret = 0;
9679
9680 for (i = 0; i < nr_args; i++) {
9681 switch (res[i].opcode) {
9682 case IORING_RESTRICTION_REGISTER_OP:
9683 if (res[i].register_op >= IORING_REGISTER_LAST) {
9684 ret = -EINVAL;
9685 goto out;
9686 }
9687
9688 __set_bit(res[i].register_op,
9689 ctx->restrictions.register_op);
9690 break;
9691 case IORING_RESTRICTION_SQE_OP:
9692 if (res[i].sqe_op >= IORING_OP_LAST) {
9693 ret = -EINVAL;
9694 goto out;
9695 }
9696
9697 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9698 break;
9699 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9700 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9701 break;
9702 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9703 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9704 break;
9705 default:
9706 ret = -EINVAL;
9707 goto out;
9708 }
9709 }
9710
9711out:
9712 /* Reset all restrictions if an error happened */
9713 if (ret != 0)
9714 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9715 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009716 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009717
9718 kfree(res);
9719 return ret;
9720}
9721
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009722static int io_register_enable_rings(struct io_ring_ctx *ctx)
9723{
9724 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9725 return -EBADFD;
9726
9727 if (ctx->restrictions.registered)
9728 ctx->restricted = 1;
9729
9730 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9731
9732 io_sq_offload_start(ctx);
9733
9734 return 0;
9735}
9736
Jens Axboe071698e2020-01-28 10:04:42 -07009737static bool io_register_op_must_quiesce(int op)
9738{
9739 switch (op) {
9740 case IORING_UNREGISTER_FILES:
9741 case IORING_REGISTER_FILES_UPDATE:
9742 case IORING_REGISTER_PROBE:
9743 case IORING_REGISTER_PERSONALITY:
9744 case IORING_UNREGISTER_PERSONALITY:
9745 return false;
9746 default:
9747 return true;
9748 }
9749}
9750
Jens Axboeedafcce2019-01-09 09:16:05 -07009751static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9752 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009753 __releases(ctx->uring_lock)
9754 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009755{
9756 int ret;
9757
Jens Axboe35fa71a2019-04-22 10:23:23 -06009758 /*
9759 * We're inside the ring mutex, if the ref is already dying, then
9760 * someone else killed the ctx or is already going through
9761 * io_uring_register().
9762 */
9763 if (percpu_ref_is_dying(&ctx->refs))
9764 return -ENXIO;
9765
Jens Axboe071698e2020-01-28 10:04:42 -07009766 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009767 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009768
Jens Axboe05f3fb32019-12-09 11:22:50 -07009769 /*
9770 * Drop uring mutex before waiting for references to exit. If
9771 * another thread is currently inside io_uring_enter() it might
9772 * need to grab the uring_lock to make progress. If we hold it
9773 * here across the drain wait, then we can deadlock. It's safe
9774 * to drop the mutex here, since no new references will come in
9775 * after we've killed the percpu ref.
9776 */
9777 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009778 do {
9779 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9780 if (!ret)
9781 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009782 ret = io_run_task_work_sig();
9783 if (ret < 0)
9784 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009785 } while (1);
9786
Jens Axboe05f3fb32019-12-09 11:22:50 -07009787 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009788
Jens Axboec1503682020-01-08 08:26:07 -07009789 if (ret) {
9790 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009791 goto out_quiesce;
9792 }
9793 }
9794
9795 if (ctx->restricted) {
9796 if (opcode >= IORING_REGISTER_LAST) {
9797 ret = -EINVAL;
9798 goto out;
9799 }
9800
9801 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9802 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009803 goto out;
9804 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009805 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009806
9807 switch (opcode) {
9808 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009809 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -07009810 break;
9811 case IORING_UNREGISTER_BUFFERS:
9812 ret = -EINVAL;
9813 if (arg || nr_args)
9814 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009815 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07009816 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009817 case IORING_REGISTER_FILES:
9818 ret = io_sqe_files_register(ctx, arg, nr_args);
9819 break;
9820 case IORING_UNREGISTER_FILES:
9821 ret = -EINVAL;
9822 if (arg || nr_args)
9823 break;
9824 ret = io_sqe_files_unregister(ctx);
9825 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009826 case IORING_REGISTER_FILES_UPDATE:
9827 ret = io_sqe_files_update(ctx, arg, nr_args);
9828 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009829 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009830 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009831 ret = -EINVAL;
9832 if (nr_args != 1)
9833 break;
9834 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009835 if (ret)
9836 break;
9837 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9838 ctx->eventfd_async = 1;
9839 else
9840 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009841 break;
9842 case IORING_UNREGISTER_EVENTFD:
9843 ret = -EINVAL;
9844 if (arg || nr_args)
9845 break;
9846 ret = io_eventfd_unregister(ctx);
9847 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009848 case IORING_REGISTER_PROBE:
9849 ret = -EINVAL;
9850 if (!arg || nr_args > 256)
9851 break;
9852 ret = io_probe(ctx, arg, nr_args);
9853 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009854 case IORING_REGISTER_PERSONALITY:
9855 ret = -EINVAL;
9856 if (arg || nr_args)
9857 break;
9858 ret = io_register_personality(ctx);
9859 break;
9860 case IORING_UNREGISTER_PERSONALITY:
9861 ret = -EINVAL;
9862 if (arg)
9863 break;
9864 ret = io_unregister_personality(ctx, nr_args);
9865 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009866 case IORING_REGISTER_ENABLE_RINGS:
9867 ret = -EINVAL;
9868 if (arg || nr_args)
9869 break;
9870 ret = io_register_enable_rings(ctx);
9871 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009872 case IORING_REGISTER_RESTRICTIONS:
9873 ret = io_register_restrictions(ctx, arg, nr_args);
9874 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009875 default:
9876 ret = -EINVAL;
9877 break;
9878 }
9879
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009880out:
Jens Axboe071698e2020-01-28 10:04:42 -07009881 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009882 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009883 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009884out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009885 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009886 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009887 return ret;
9888}
9889
9890SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9891 void __user *, arg, unsigned int, nr_args)
9892{
9893 struct io_ring_ctx *ctx;
9894 long ret = -EBADF;
9895 struct fd f;
9896
9897 f = fdget(fd);
9898 if (!f.file)
9899 return -EBADF;
9900
9901 ret = -EOPNOTSUPP;
9902 if (f.file->f_op != &io_uring_fops)
9903 goto out_fput;
9904
9905 ctx = f.file->private_data;
9906
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +00009907 io_run_task_work();
9908
Jens Axboeedafcce2019-01-09 09:16:05 -07009909 mutex_lock(&ctx->uring_lock);
9910 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9911 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009912 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9913 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009914out_fput:
9915 fdput(f);
9916 return ret;
9917}
9918
Jens Axboe2b188cc2019-01-07 10:46:33 -07009919static int __init io_uring_init(void)
9920{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009921#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9922 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9923 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9924} while (0)
9925
9926#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9927 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9928 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9929 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9930 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9931 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9932 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9933 BUILD_BUG_SQE_ELEM(8, __u64, off);
9934 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9935 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009936 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009937 BUILD_BUG_SQE_ELEM(24, __u32, len);
9938 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9939 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9940 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9941 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009942 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9943 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009944 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9945 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9946 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9947 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9948 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9949 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9950 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9951 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009952 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009953 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9954 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9955 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009956 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009957
Jens Axboed3656342019-12-18 09:50:26 -07009958 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009959 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -07009960 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
9961 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009962 return 0;
9963};
9964__initcall(io_uring_init);