blob: afa0e91488e6c1e50509461aec0d05479ce1b299 [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;
Jens Axboe5f3f26f2021-02-25 10:17:46 -0700341 unsigned int sqo_exec: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342
Hristo Venev75b28af2019-08-26 17:23:46 +0000343 /*
344 * Ring buffer of indices into array of io_uring_sqe, which is
345 * mmapped by the application using the IORING_OFF_SQES offset.
346 *
347 * This indirection could e.g. be used to assign fixed
348 * io_uring_sqe entries to operations and only submit them to
349 * the queue when needed.
350 *
351 * The kernel modifies neither the indices array nor the entries
352 * array.
353 */
354 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700355 unsigned cached_sq_head;
356 unsigned sq_entries;
357 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700358 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600359 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100360 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700361 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600362
Jens Axboee9418942021-02-19 12:33:30 -0700363 /* hashed buffered write serialization */
364 struct io_wq_hash *hash_map;
365
Jens Axboede0617e2019-04-06 21:51:27 -0600366 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600367 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700368 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369
Jens Axboead3eb2c2019-12-18 17:12:20 -0700370 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700371 } ____cacheline_aligned_in_smp;
372
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700373 struct {
374 struct mutex uring_lock;
375 wait_queue_head_t wait;
376 } ____cacheline_aligned_in_smp;
377
378 struct io_submit_state submit_state;
379
Hristo Venev75b28af2019-08-26 17:23:46 +0000380 struct io_rings *rings;
381
Jens Axboe2aede0e2020-09-14 10:45:53 -0600382 /* Only used for accounting purposes */
383 struct mm_struct *mm_account;
384
Jens Axboe534ca6d2020-09-02 13:52:19 -0600385 struct io_sq_data *sq_data; /* if using sq thread polling */
386
Jens Axboe90554202020-09-03 12:12:41 -0600387 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600388 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700389
Jens Axboe6b063142019-01-10 22:13:58 -0700390 /*
391 * If used, fixed file set. Writers must ensure that ->refs is dead,
392 * readers must ensure that ->refs is alive as long as the file* is
393 * used. Only updated through io_uring_register(2).
394 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000395 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700396 unsigned nr_user_files;
397
Jens Axboeedafcce2019-01-09 09:16:05 -0700398 /* if used, fixed mapped user buffers */
399 unsigned nr_user_bufs;
400 struct io_mapped_ubuf *user_bufs;
401
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402 struct user_struct *user;
403
Jens Axboe0f158b42020-05-14 17:18:39 -0600404 struct completion ref_comp;
405 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700406
407#if defined(CONFIG_UNIX)
408 struct socket *ring_sock;
409#endif
410
Jens Axboe5a2e7452020-02-23 16:23:11 -0700411 struct idr io_buffer_idr;
412
Jens Axboe071698e2020-01-28 10:04:42 -0700413 struct idr personality_idr;
414
Jens Axboe206aefd2019-11-07 18:27:42 -0700415 struct {
416 unsigned cached_cq_tail;
417 unsigned cq_entries;
418 unsigned cq_mask;
419 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500420 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700421 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700422 struct wait_queue_head cq_wait;
423 struct fasync_struct *cq_fasync;
424 struct eventfd_ctx *cq_ev_fd;
425 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700426
427 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700428 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700429
Jens Axboedef596e2019-01-09 08:59:42 -0700430 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300431 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700432 * io_uring instances that don't use IORING_SETUP_SQPOLL.
433 * For SQPOLL, only the single threaded io_sq_thread() will
434 * manipulate the list, hence no extra locking is needed there.
435 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300436 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700437 struct hlist_head *cancel_hash;
438 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700439 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600440
441 spinlock_t inflight_lock;
442 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700443 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600444
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000445 struct delayed_work rsrc_put_work;
446 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000447 struct list_head rsrc_ref_list;
448 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600449
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200450 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700451
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700452 /* exit task_work */
453 struct callback_head *exit_task_work;
454
Jens Axboee9418942021-02-19 12:33:30 -0700455 struct wait_queue_head hash_wait;
456
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700457 /* Keep this last, we don't need it for the fast path */
458 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459};
460
Jens Axboe09bb8392019-03-13 12:39:28 -0600461/*
462 * First field must be the file pointer in all the
463 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
464 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700465struct io_poll_iocb {
466 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000467 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700468 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600469 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700470 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700471 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700472};
473
Pavel Begunkov018043b2020-10-27 23:17:18 +0000474struct io_poll_remove {
475 struct file *file;
476 u64 addr;
477};
478
Jens Axboeb5dba592019-12-11 14:02:38 -0700479struct io_close {
480 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700481 int fd;
482};
483
Jens Axboead8a48a2019-11-15 08:49:11 -0700484struct io_timeout_data {
485 struct io_kiocb *req;
486 struct hrtimer timer;
487 struct timespec64 ts;
488 enum hrtimer_mode mode;
489};
490
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700491struct io_accept {
492 struct file *file;
493 struct sockaddr __user *addr;
494 int __user *addr_len;
495 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600496 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700497};
498
499struct io_sync {
500 struct file *file;
501 loff_t len;
502 loff_t off;
503 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700504 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700505};
506
Jens Axboefbf23842019-12-17 18:45:56 -0700507struct io_cancel {
508 struct file *file;
509 u64 addr;
510};
511
Jens Axboeb29472e2019-12-17 18:50:29 -0700512struct io_timeout {
513 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300514 u32 off;
515 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300516 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000517 /* head of the link, used by linked timeouts only */
518 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700519};
520
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100521struct io_timeout_rem {
522 struct file *file;
523 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000524
525 /* timeout update */
526 struct timespec64 ts;
527 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100528};
529
Jens Axboe9adbd452019-12-20 08:45:55 -0700530struct io_rw {
531 /* NOTE: kiocb has the file as the first member, so don't do it here */
532 struct kiocb kiocb;
533 u64 addr;
534 u64 len;
535};
536
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700537struct io_connect {
538 struct file *file;
539 struct sockaddr __user *addr;
540 int addr_len;
541};
542
Jens Axboee47293f2019-12-20 08:58:21 -0700543struct io_sr_msg {
544 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700545 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300546 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700547 void __user *buf;
548 };
Jens Axboee47293f2019-12-20 08:58:21 -0700549 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700550 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700551 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700552 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700553};
554
Jens Axboe15b71ab2019-12-11 11:20:36 -0700555struct io_open {
556 struct file *file;
557 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700558 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700559 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600560 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700561};
562
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000563struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700564 struct file *file;
565 u64 arg;
566 u32 nr_args;
567 u32 offset;
568};
569
Jens Axboe4840e412019-12-25 22:03:45 -0700570struct io_fadvise {
571 struct file *file;
572 u64 offset;
573 u32 len;
574 u32 advice;
575};
576
Jens Axboec1ca7572019-12-25 22:18:28 -0700577struct io_madvise {
578 struct file *file;
579 u64 addr;
580 u32 len;
581 u32 advice;
582};
583
Jens Axboe3e4827b2020-01-08 15:18:09 -0700584struct io_epoll {
585 struct file *file;
586 int epfd;
587 int op;
588 int fd;
589 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700590};
591
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300592struct io_splice {
593 struct file *file_out;
594 struct file *file_in;
595 loff_t off_out;
596 loff_t off_in;
597 u64 len;
598 unsigned int flags;
599};
600
Jens Axboeddf0322d2020-02-23 16:41:33 -0700601struct io_provide_buf {
602 struct file *file;
603 __u64 addr;
604 __s32 len;
605 __u32 bgid;
606 __u16 nbufs;
607 __u16 bid;
608};
609
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700610struct io_statx {
611 struct file *file;
612 int dfd;
613 unsigned int mask;
614 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700615 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700616 struct statx __user *buffer;
617};
618
Jens Axboe36f4fa62020-09-05 11:14:22 -0600619struct io_shutdown {
620 struct file *file;
621 int how;
622};
623
Jens Axboe80a261f2020-09-28 14:23:58 -0600624struct io_rename {
625 struct file *file;
626 int old_dfd;
627 int new_dfd;
628 struct filename *oldpath;
629 struct filename *newpath;
630 int flags;
631};
632
Jens Axboe14a11432020-09-28 14:27:37 -0600633struct io_unlink {
634 struct file *file;
635 int dfd;
636 int flags;
637 struct filename *filename;
638};
639
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300640struct io_completion {
641 struct file *file;
642 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300643 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300644};
645
Jens Axboef499a022019-12-02 16:28:46 -0700646struct io_async_connect {
647 struct sockaddr_storage address;
648};
649
Jens Axboe03b12302019-12-02 18:50:25 -0700650struct io_async_msghdr {
651 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000652 /* points to an allocated iov, if NULL we use fast_iov instead */
653 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700654 struct sockaddr __user *uaddr;
655 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700656 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700657};
658
Jens Axboef67676d2019-12-02 11:03:47 -0700659struct io_async_rw {
660 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600661 const struct iovec *free_iovec;
662 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600663 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600664 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700665};
666
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300667enum {
668 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
669 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
670 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
671 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
672 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700673 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300674
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300675 REQ_F_FAIL_LINK_BIT,
676 REQ_F_INFLIGHT_BIT,
677 REQ_F_CUR_POS_BIT,
678 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300679 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300681 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700682 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700683 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600684 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100685 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000686 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700687
688 /* not a real bit, just to check we're not overflowing the space */
689 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300690};
691
692enum {
693 /* ctx owns file */
694 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
695 /* drain existing IO first */
696 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
697 /* linked sqes */
698 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
699 /* doesn't sever on completion < 0 */
700 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
701 /* IOSQE_ASYNC */
702 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700703 /* IOSQE_BUFFER_SELECT */
704 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300705
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300706 /* fail rest of links */
707 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
708 /* on inflight list */
709 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
710 /* read/write uses file position */
711 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
712 /* must not punt to workers */
713 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100714 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300715 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300716 /* regular file */
717 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300718 /* needs cleanup */
719 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700720 /* already went through poll handler */
721 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700722 /* buffer already selected */
723 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600724 /* doesn't need file table for this request */
725 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100726 /* linked timeout is active, i.e. prepared by link's head */
727 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000728 /* completion is deferred through io_comp_state */
729 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700730};
731
732struct async_poll {
733 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600734 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300735};
736
Jens Axboe7cbf1722021-02-10 00:03:20 +0000737struct io_task_work {
738 struct io_wq_work_node node;
739 task_work_func_t func;
740};
741
Jens Axboe09bb8392019-03-13 12:39:28 -0600742/*
743 * NOTE! Each of the iocb union members has the file pointer
744 * as the first entry in their struct definition. So you can
745 * access the file pointer through any of the sub-structs,
746 * or directly as just 'ki_filp' in this struct.
747 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700748struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700749 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600750 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700751 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700752 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000753 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700754 struct io_accept accept;
755 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700756 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700757 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100758 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700759 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700760 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700761 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700762 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000763 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700764 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700765 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700766 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300767 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700768 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700769 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600770 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600771 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600772 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300773 /* use only after cleaning per-op data, see io_clean_op() */
774 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700775 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700776
Jens Axboee8c2bc12020-08-15 18:44:09 -0700777 /* opcode allocated if it needs to store data for async defer */
778 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700779 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800780 /* polled IO has completed */
781 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700783 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300784 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700785
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300786 struct io_ring_ctx *ctx;
787 unsigned int flags;
788 refcount_t refs;
789 struct task_struct *task;
790 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000792 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000793 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700794
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300795 /*
796 * 1. used with ctx->iopoll_list with reads/writes
797 * 2. to track reqs with ->files (see io_op_def::file_table)
798 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300799 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000800 union {
801 struct io_task_work io_task_work;
802 struct callback_head task_work;
803 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300804 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
805 struct hlist_node hash_node;
806 struct async_poll *apoll;
807 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700808};
809
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300810struct io_defer_entry {
811 struct list_head list;
812 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300813 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300814};
815
Jens Axboed3656342019-12-18 09:50:26 -0700816struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700817 /* needs req->file assigned */
818 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700819 /* hash wq insertion if file is a regular file */
820 unsigned hash_reg_file : 1;
821 /* unbound wq insertion if file is a non-regular file */
822 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700823 /* opcode is not supported by this kernel */
824 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700825 /* set if opcode supports polled "wait" */
826 unsigned pollin : 1;
827 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700828 /* op supports buffer selection */
829 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700830 /* must always have async data allocated */
831 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600832 /* should block plug */
833 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700834 /* size of async data needed, if any */
835 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700836};
837
Jens Axboe09186822020-10-13 15:01:40 -0600838static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_NOP] = {},
840 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_file = 1,
842 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700843 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700844 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700845 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600846 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700847 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700848 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300849 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_file = 1,
851 .hash_reg_file = 1,
852 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700853 .pollout = 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_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700859 .needs_file = 1,
860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700862 .needs_file = 1,
863 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700864 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600865 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700866 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700867 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300868 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700869 .needs_file = 1,
870 .hash_reg_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700872 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600873 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700874 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700875 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300876 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700877 .needs_file = 1,
878 .unbound_nonreg_file = 1,
879 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300880 [IORING_OP_POLL_REMOVE] = {},
881 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700882 .needs_file = 1,
883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700885 .needs_file = 1,
886 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700887 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700888 .needs_async_data = 1,
889 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700890 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300891 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700895 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700896 .needs_async_data = 1,
897 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700898 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300899 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700900 .needs_async_data = 1,
901 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700902 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000903 [IORING_OP_TIMEOUT_REMOVE] = {
904 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700907 .needs_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700909 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700910 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300911 [IORING_OP_ASYNC_CANCEL] = {},
912 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700913 .needs_async_data = 1,
914 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700915 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300916 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700917 .needs_file = 1,
918 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700919 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700920 .needs_async_data = 1,
921 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700922 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300923 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700924 .needs_file = 1,
925 },
Jens Axboe44526be2021-02-15 13:32:18 -0700926 [IORING_OP_OPENAT] = {},
927 [IORING_OP_CLOSE] = {},
928 [IORING_OP_FILES_UPDATE] = {},
929 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300930 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700931 .needs_file = 1,
932 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700933 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700934 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600935 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700936 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700937 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300938 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700939 .needs_file = 1,
940 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700941 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600942 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700943 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700944 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300945 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700946 .needs_file = 1,
947 },
Jens Axboe44526be2021-02-15 13:32:18 -0700948 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700950 .needs_file = 1,
951 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700952 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700955 .needs_file = 1,
956 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700957 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700958 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700961 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700962 [IORING_OP_EPOLL_CTL] = {
963 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700964 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300965 [IORING_OP_SPLICE] = {
966 .needs_file = 1,
967 .hash_reg_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700969 },
970 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700971 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300972 [IORING_OP_TEE] = {
973 .needs_file = 1,
974 .hash_reg_file = 1,
975 .unbound_nonreg_file = 1,
976 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600977 [IORING_OP_SHUTDOWN] = {
978 .needs_file = 1,
979 },
Jens Axboe44526be2021-02-15 13:32:18 -0700980 [IORING_OP_RENAMEAT] = {},
981 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700982};
983
Pavel Begunkov9936c7c2021-02-04 13:51:56 +0000984static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
985 struct task_struct *task,
986 struct files_struct *files);
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700987static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000988static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +0000989static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000990 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +0000991static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000992
Pavel Begunkov23faba32021-02-11 18:28:22 +0000993static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700994static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800995static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +0100996static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -0600997static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -0700998static void io_dismantle_req(struct io_kiocb *req);
999static void io_put_task(struct task_struct *task, int nr);
1000static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001001static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001002static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001003static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001004static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001005 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001006 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001007static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001008static struct file *io_file_get(struct io_submit_state *state,
1009 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001010static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001011static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001012
Pavel Begunkov847595d2021-02-04 13:52:06 +00001013static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1014 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001015static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1016 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001017 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001018static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001019static void io_submit_flush_completions(struct io_comp_state *cs,
1020 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001021
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022static struct kmem_cache *req_cachep;
1023
Jens Axboe09186822020-10-13 15:01:40 -06001024static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001025
1026struct sock *io_uring_get_socket(struct file *file)
1027{
1028#if defined(CONFIG_UNIX)
1029 if (file->f_op == &io_uring_fops) {
1030 struct io_ring_ctx *ctx = file->private_data;
1031
1032 return ctx->ring_sock->sk;
1033 }
1034#endif
1035 return NULL;
1036}
1037EXPORT_SYMBOL(io_uring_get_socket);
1038
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001039#define io_for_each_link(pos, head) \
1040 for (pos = (head); pos; pos = pos->link)
1041
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001042static inline void io_clean_op(struct io_kiocb *req)
1043{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001044 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001045 __io_clean_op(req);
1046}
1047
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001048static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001049{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001050 struct io_ring_ctx *ctx = req->ctx;
1051
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001052 if (!req->fixed_rsrc_refs) {
1053 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1054 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001055 }
1056}
1057
Pavel Begunkov08d23632020-11-06 13:00:22 +00001058static bool io_match_task(struct io_kiocb *head,
1059 struct task_struct *task,
1060 struct files_struct *files)
1061{
1062 struct io_kiocb *req;
1063
Jens Axboe84965ff2021-01-23 15:51:11 -07001064 if (task && head->task != task) {
1065 /* in terms of cancelation, always match if req task is dead */
1066 if (head->task->flags & PF_EXITING)
1067 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001068 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001069 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001070 if (!files)
1071 return true;
1072
1073 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001074 if (req->file && req->file->f_op == &io_uring_fops)
1075 return true;
Jens Axboe4379bf82021-02-15 13:40:22 -07001076 if (req->task->files == files)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001077 return true;
1078 }
1079 return false;
1080}
1081
Jens Axboec40f6372020-06-25 15:39:59 -06001082static inline void req_set_fail_links(struct io_kiocb *req)
1083{
1084 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1085 req->flags |= REQ_F_FAIL_LINK;
1086}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001087
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1089{
1090 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1091
Jens Axboe0f158b42020-05-14 17:18:39 -06001092 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001093}
1094
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001095static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1096{
1097 return !req->timeout.off;
1098}
1099
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1101{
1102 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001103 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104
1105 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1106 if (!ctx)
1107 return NULL;
1108
Jens Axboe78076bb2019-12-04 19:56:40 -07001109 /*
1110 * Use 5 bits less than the max cq entries, that should give us around
1111 * 32 entries per hash list if totally full and uniformly spread.
1112 */
1113 hash_bits = ilog2(p->cq_entries);
1114 hash_bits -= 5;
1115 if (hash_bits <= 0)
1116 hash_bits = 1;
1117 ctx->cancel_hash_bits = hash_bits;
1118 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1119 GFP_KERNEL);
1120 if (!ctx->cancel_hash)
1121 goto err;
1122 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1123
Roman Gushchin21482892019-05-07 10:01:48 -07001124 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001125 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1126 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001127
1128 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001129 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001130 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001132 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001133 init_completion(&ctx->ref_comp);
1134 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001135 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001136 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 mutex_init(&ctx->uring_lock);
1138 init_waitqueue_head(&ctx->wait);
1139 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001140 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001141 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001142 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001143 spin_lock_init(&ctx->inflight_lock);
1144 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001145 spin_lock_init(&ctx->rsrc_ref_lock);
1146 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001147 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1148 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001149 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001150 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001152err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001153 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001154 kfree(ctx);
1155 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156}
1157
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001158static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001159{
Jens Axboe2bc99302020-07-09 09:43:27 -06001160 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1161 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001162
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001163 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001164 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001165 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001166
Bob Liu9d858b22019-11-13 18:06:25 +08001167 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001168}
1169
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001170static void io_req_track_inflight(struct io_kiocb *req)
1171{
1172 struct io_ring_ctx *ctx = req->ctx;
1173
1174 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001175 req->flags |= REQ_F_INFLIGHT;
1176
1177 spin_lock_irq(&ctx->inflight_lock);
1178 list_add(&req->inflight_entry, &ctx->inflight_list);
1179 spin_unlock_irq(&ctx->inflight_lock);
1180 }
1181}
1182
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001183static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001184{
Jens Axboed3656342019-12-18 09:50:26 -07001185 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001186 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001187
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001188 if (req->flags & REQ_F_FORCE_ASYNC)
1189 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1190
Jens Axboed3656342019-12-18 09:50:26 -07001191 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001192 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001193 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001194 } else {
1195 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001196 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001197 }
Jens Axboe561fb042019-10-24 07:25:42 -06001198}
1199
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001200static void io_prep_async_link(struct io_kiocb *req)
1201{
1202 struct io_kiocb *cur;
1203
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001204 io_for_each_link(cur, req)
1205 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001206}
1207
Jens Axboe7271ef32020-08-10 09:55:22 -06001208static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001209{
Jackie Liua197f662019-11-08 08:09:12 -07001210 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001211 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001212 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001213
Jens Axboe3bfe6102021-02-16 14:15:30 -07001214 BUG_ON(!tctx);
1215 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001216
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001217 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1218 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001219 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001220 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001221}
1222
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001223static void io_queue_async_work(struct io_kiocb *req)
1224{
Jens Axboe7271ef32020-08-10 09:55:22 -06001225 struct io_kiocb *link;
1226
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001227 /* init ->work of the whole link before punting */
1228 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001229 link = __io_queue_async_work(req);
1230
1231 if (link)
1232 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001233}
1234
Jens Axboe5262f562019-09-17 12:26:57 -06001235static void io_kill_timeout(struct io_kiocb *req)
1236{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001237 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001238 int ret;
1239
Jens Axboee8c2bc12020-08-15 18:44:09 -07001240 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001241 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001242 atomic_set(&req->ctx->cq_timeouts,
1243 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001244 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001245 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001246 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001247 }
1248}
1249
Jens Axboe76e1b642020-09-26 15:05:03 -06001250/*
1251 * Returns true if we found and killed one or more timeouts
1252 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001253static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1254 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001255{
1256 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001257 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001258
1259 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001260 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001261 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001262 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001263 canceled++;
1264 }
Jens Axboef3606e32020-09-22 08:18:24 -06001265 }
Jens Axboe5262f562019-09-17 12:26:57 -06001266 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001267 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001268}
1269
Pavel Begunkov04518942020-05-26 20:34:05 +03001270static void __io_queue_deferred(struct io_ring_ctx *ctx)
1271{
1272 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001273 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1274 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001275
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001276 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001277 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001278 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001279 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001280 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001281 } while (!list_empty(&ctx->defer_list));
1282}
1283
Pavel Begunkov360428f2020-05-30 14:54:17 +03001284static void io_flush_timeouts(struct io_ring_ctx *ctx)
1285{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001286 u32 seq;
1287
1288 if (list_empty(&ctx->timeout_list))
1289 return;
1290
1291 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1292
1293 do {
1294 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001295 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001296 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001297
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001298 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001299 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001300
1301 /*
1302 * Since seq can easily wrap around over time, subtract
1303 * the last seq at which timeouts were flushed before comparing.
1304 * Assuming not more than 2^31-1 events have happened since,
1305 * these subtractions won't have wrapped, so we can check if
1306 * target is in [last_seq, current_seq] by comparing the two.
1307 */
1308 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1309 events_got = seq - ctx->cq_last_tm_flush;
1310 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001311 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001312
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001313 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001314 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001315 } while (!list_empty(&ctx->timeout_list));
1316
1317 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001318}
1319
Jens Axboede0617e2019-04-06 21:51:27 -06001320static void io_commit_cqring(struct io_ring_ctx *ctx)
1321{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001322 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001323
1324 /* order cqe stores with ring update */
1325 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001326
Pavel Begunkov04518942020-05-26 20:34:05 +03001327 if (unlikely(!list_empty(&ctx->defer_list)))
1328 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001329}
1330
Jens Axboe90554202020-09-03 12:12:41 -06001331static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1332{
1333 struct io_rings *r = ctx->rings;
1334
1335 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1336}
1337
Pavel Begunkov888aae22021-01-19 13:32:39 +00001338static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1339{
1340 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1341}
1342
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1344{
Hristo Venev75b28af2019-08-26 17:23:46 +00001345 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001346 unsigned tail;
1347
Stefan Bühler115e12e2019-04-24 23:54:18 +02001348 /*
1349 * writes to the cq entry need to come after reading head; the
1350 * control dependency is enough as we're using WRITE_ONCE to
1351 * fill the cq entry
1352 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001353 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001354 return NULL;
1355
Pavel Begunkov888aae22021-01-19 13:32:39 +00001356 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001357 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001358}
1359
Jens Axboef2842ab2020-01-08 11:04:00 -07001360static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1361{
Jens Axboef0b493e2020-02-01 21:30:11 -07001362 if (!ctx->cq_ev_fd)
1363 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001364 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1365 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001366 if (!ctx->eventfd_async)
1367 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001368 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001369}
1370
Jens Axboeb41e9852020-02-17 09:52:41 -07001371static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001372{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001373 /* see waitqueue_active() comment */
1374 smp_mb();
1375
Jens Axboe8c838782019-03-12 15:48:16 -06001376 if (waitqueue_active(&ctx->wait))
1377 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001378 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1379 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001380 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001381 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001382 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001383 wake_up_interruptible(&ctx->cq_wait);
1384 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1385 }
Jens Axboe8c838782019-03-12 15:48:16 -06001386}
1387
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001388static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1389{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001390 /* see waitqueue_active() comment */
1391 smp_mb();
1392
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001393 if (ctx->flags & IORING_SETUP_SQPOLL) {
1394 if (waitqueue_active(&ctx->wait))
1395 wake_up(&ctx->wait);
1396 }
1397 if (io_should_trigger_evfd(ctx))
1398 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001399 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001400 wake_up_interruptible(&ctx->cq_wait);
1401 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1402 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001403}
1404
Jens Axboec4a2ed72019-11-21 21:01:26 -07001405/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001406static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1407 struct task_struct *tsk,
1408 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001410 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001411 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001412 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001413 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001414 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001415 LIST_HEAD(list);
1416
Pavel Begunkove23de152020-12-17 00:24:37 +00001417 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1418 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001419
Jens Axboeb18032b2021-01-24 16:58:56 -07001420 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001421 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001422 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001423 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001424 continue;
1425
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426 cqe = io_get_cqring(ctx);
1427 if (!cqe && !force)
1428 break;
1429
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001430 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 if (cqe) {
1432 WRITE_ONCE(cqe->user_data, req->user_data);
1433 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001434 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001436 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001437 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001438 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001439 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001440 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001441 }
1442
Pavel Begunkov09e88402020-12-17 00:24:38 +00001443 all_flushed = list_empty(&ctx->cq_overflow_list);
1444 if (all_flushed) {
1445 clear_bit(0, &ctx->sq_check_overflow);
1446 clear_bit(0, &ctx->cq_check_overflow);
1447 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1448 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001449
Jens Axboeb18032b2021-01-24 16:58:56 -07001450 if (posted)
1451 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001452 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001453 if (posted)
1454 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001455
1456 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001457 req = list_first_entry(&list, struct io_kiocb, compl.list);
1458 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001459 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001460 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001461
Pavel Begunkov09e88402020-12-17 00:24:38 +00001462 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001463}
1464
Pavel Begunkov6c503152021-01-04 20:36:36 +00001465static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1466 struct task_struct *tsk,
1467 struct files_struct *files)
1468{
1469 if (test_bit(0, &ctx->cq_check_overflow)) {
1470 /* iopoll syncs against uring_lock, not completion_lock */
1471 if (ctx->flags & IORING_SETUP_IOPOLL)
1472 mutex_lock(&ctx->uring_lock);
1473 __io_cqring_overflow_flush(ctx, force, tsk, files);
1474 if (ctx->flags & IORING_SETUP_IOPOLL)
1475 mutex_unlock(&ctx->uring_lock);
1476 }
1477}
1478
Jens Axboebcda7ba2020-02-23 16:42:51 -07001479static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001481 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482 struct io_uring_cqe *cqe;
1483
Jens Axboe78e19bb2019-11-06 15:21:34 -07001484 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001485
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486 /*
1487 * If we can't get a cq entry, userspace overflowed the
1488 * submission (by quite a lot). Increment the overflow count in
1489 * the ring.
1490 */
1491 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001492 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001493 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001495 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001496 } else if (ctx->cq_overflow_flushed ||
1497 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001498 /*
1499 * If we're in ring overflow flush mode, or in task cancel mode,
1500 * then we cannot store the request for later flushing, we need
1501 * to drop it on the floor.
1502 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001503 ctx->cached_cq_overflow++;
1504 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001505 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001506 if (list_empty(&ctx->cq_overflow_list)) {
1507 set_bit(0, &ctx->sq_check_overflow);
1508 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001509 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001510 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001511 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001512 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001513 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001514 refcount_inc(&req->refs);
1515 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516 }
1517}
1518
Jens Axboebcda7ba2020-02-23 16:42:51 -07001519static void io_cqring_fill_event(struct io_kiocb *req, long res)
1520{
1521 __io_cqring_fill_event(req, res, 0);
1522}
1523
Jens Axboec7dae4b2021-02-09 19:53:37 -07001524static inline void io_req_complete_post(struct io_kiocb *req, long res,
1525 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001527 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528 unsigned long flags;
1529
1530 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001531 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001532 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001533 /*
1534 * If we're the last reference to this request, add to our locked
1535 * free_list cache.
1536 */
1537 if (refcount_dec_and_test(&req->refs)) {
1538 struct io_comp_state *cs = &ctx->submit_state.comp;
1539
1540 io_dismantle_req(req);
1541 io_put_task(req->task, 1);
1542 list_add(&req->compl.list, &cs->locked_free_list);
1543 cs->locked_free_nr++;
1544 } else
1545 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001546 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1547
Jens Axboe8c838782019-03-12 15:48:16 -06001548 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001549 if (req) {
1550 io_queue_next(req);
1551 percpu_ref_put(&ctx->refs);
1552 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553}
1554
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001555static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001556 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001557{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001558 io_clean_op(req);
1559 req->result = res;
1560 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001561 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001562}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563
Pavel Begunkov889fca72021-02-10 00:03:09 +00001564static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1565 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001566{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001567 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1568 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001569 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001570 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001571}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001572
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001573static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001574{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001575 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001576}
1577
Jens Axboec7dae4b2021-02-09 19:53:37 -07001578static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001579{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001580 struct io_submit_state *state = &ctx->submit_state;
1581 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001582 struct io_kiocb *req = NULL;
1583
Jens Axboec7dae4b2021-02-09 19:53:37 -07001584 /*
1585 * If we have more than a batch's worth of requests in our IRQ side
1586 * locked cache, grab the lock and move them over to our submission
1587 * side cache.
1588 */
1589 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1590 spin_lock_irq(&ctx->completion_lock);
1591 list_splice_init(&cs->locked_free_list, &cs->free_list);
1592 cs->locked_free_nr = 0;
1593 spin_unlock_irq(&ctx->completion_lock);
1594 }
1595
1596 while (!list_empty(&cs->free_list)) {
1597 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001598 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001599 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001600 state->reqs[state->free_reqs++] = req;
1601 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1602 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001604
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001605 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606}
1607
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001608static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001609{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001610 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001612 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001613
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001614 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001615 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001616 int ret;
1617
Jens Axboec7dae4b2021-02-09 19:53:37 -07001618 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001619 goto got_req;
1620
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001621 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1622 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001623
1624 /*
1625 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1626 * retry single alloc to be on the safe side.
1627 */
1628 if (unlikely(ret <= 0)) {
1629 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1630 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001631 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001632 ret = 1;
1633 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001634 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001636got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001637 state->free_reqs--;
1638 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639}
1640
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001641static inline void io_put_file(struct io_kiocb *req, struct file *file,
1642 bool fixed)
1643{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001644 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001645 fput(file);
1646}
1647
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001648static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001650 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001651
Jens Axboee8c2bc12020-08-15 18:44:09 -07001652 if (req->async_data)
1653 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001654 if (req->file)
1655 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001656 if (req->fixed_rsrc_refs)
1657 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkovf85c3102021-03-01 18:20:46 +00001658
1659 if (req->flags & REQ_F_INFLIGHT) {
1660 struct io_ring_ctx *ctx = req->ctx;
1661 struct io_uring_task *tctx = req->task->io_uring;
1662 unsigned long flags;
1663
1664 spin_lock_irqsave(&ctx->inflight_lock, flags);
1665 list_del(&req->inflight_entry);
1666 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1667 req->flags &= ~REQ_F_INFLIGHT;
1668 if (atomic_read(&tctx->in_idle))
1669 wake_up(&tctx->wait);
1670 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001671}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001672
Pavel Begunkov7c660732021-01-25 11:42:21 +00001673static inline void io_put_task(struct task_struct *task, int nr)
1674{
1675 struct io_uring_task *tctx = task->io_uring;
1676
1677 percpu_counter_sub(&tctx->inflight, nr);
1678 if (unlikely(atomic_read(&tctx->in_idle)))
1679 wake_up(&tctx->wait);
1680 put_task_struct_many(task, nr);
1681}
1682
Pavel Begunkov216578e2020-10-13 09:44:00 +01001683static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001684{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001685 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001686
Pavel Begunkov216578e2020-10-13 09:44:00 +01001687 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001688 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001689
Pavel Begunkov3893f392021-02-10 00:03:15 +00001690 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001691 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001692}
1693
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001694static inline void io_remove_next_linked(struct io_kiocb *req)
1695{
1696 struct io_kiocb *nxt = req->link;
1697
1698 req->link = nxt->link;
1699 nxt->link = NULL;
1700}
1701
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001702static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001703{
Jackie Liua197f662019-11-08 08:09:12 -07001704 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001705 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001706 bool cancelled = false;
1707 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001708
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001709 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001710 link = req->link;
1711
Pavel Begunkov900fad42020-10-19 16:39:16 +01001712 /*
1713 * Can happen if a linked timeout fired and link had been like
1714 * req -> link t-out -> link t-out [-> ...]
1715 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001716 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1717 struct io_timeout_data *io = link->async_data;
1718 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001719
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001720 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001721 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001722 ret = hrtimer_try_to_cancel(&io->timer);
1723 if (ret != -1) {
1724 io_cqring_fill_event(link, -ECANCELED);
1725 io_commit_cqring(ctx);
1726 cancelled = true;
1727 }
1728 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001729 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001730 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001731
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001732 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001733 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001734 io_put_req(link);
1735 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001736}
1737
Jens Axboe4d7dd462019-11-20 13:03:52 -07001738
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001739static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001740{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001741 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001742 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001743 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001744
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001745 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001746 link = req->link;
1747 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06001748
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001749 while (link) {
1750 nxt = link->link;
1751 link->link = NULL;
1752
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001753 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001754 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001755
Jens Axboe1575f212021-02-27 15:20:49 -07001756 io_put_req_deferred(link, 2);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001757 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001758 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001759 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001760 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001761
Jens Axboe2665abf2019-11-05 12:40:47 -07001762 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001763}
1764
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001765static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001766{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001767 if (req->flags & REQ_F_LINK_TIMEOUT)
1768 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001769
Jens Axboe9e645e112019-05-10 16:07:28 -06001770 /*
1771 * If LINK is set, we have dependent requests in this chain. If we
1772 * didn't fail this request, queue the first one up, moving any other
1773 * dependencies to the next request. In case of failure, fail the rest
1774 * of the chain.
1775 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001776 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
1777 struct io_kiocb *nxt = req->link;
1778
1779 req->link = NULL;
1780 return nxt;
1781 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001782 io_fail_links(req);
1783 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001784}
Jens Axboe2665abf2019-11-05 12:40:47 -07001785
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001786static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001787{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001788 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001789 return NULL;
1790 return __io_req_find_next(req);
1791}
1792
Pavel Begunkov2c323952021-02-28 22:04:53 +00001793static void ctx_flush_and_put(struct io_ring_ctx *ctx)
1794{
1795 if (!ctx)
1796 return;
1797 if (ctx->submit_state.comp.nr) {
1798 mutex_lock(&ctx->uring_lock);
1799 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1800 mutex_unlock(&ctx->uring_lock);
1801 }
1802 percpu_ref_put(&ctx->refs);
1803}
1804
Jens Axboe7cbf1722021-02-10 00:03:20 +00001805static bool __tctx_task_work(struct io_uring_task *tctx)
1806{
Jens Axboe65453d12021-02-10 00:03:21 +00001807 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001808 struct io_wq_work_list list;
1809 struct io_wq_work_node *node;
1810
1811 if (wq_list_empty(&tctx->task_list))
1812 return false;
1813
Jens Axboe0b81e802021-02-16 10:33:53 -07001814 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001815 list = tctx->task_list;
1816 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001817 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001818
1819 node = list.first;
1820 while (node) {
1821 struct io_wq_work_node *next = node->next;
1822 struct io_kiocb *req;
1823
1824 req = container_of(node, struct io_kiocb, io_task_work.node);
Pavel Begunkov2c323952021-02-28 22:04:53 +00001825 if (req->ctx != ctx) {
1826 ctx_flush_and_put(ctx);
1827 ctx = req->ctx;
1828 percpu_ref_get(&ctx->refs);
1829 }
1830
Jens Axboe7cbf1722021-02-10 00:03:20 +00001831 req->task_work.func(&req->task_work);
1832 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001833 }
1834
Pavel Begunkov2c323952021-02-28 22:04:53 +00001835 ctx_flush_and_put(ctx);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001836 return list.first != NULL;
1837}
1838
1839static void tctx_task_work(struct callback_head *cb)
1840{
1841 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
1842
Jens Axboe1d5f3602021-02-26 14:54:16 -07001843 clear_bit(0, &tctx->task_state);
1844
Jens Axboe7cbf1722021-02-10 00:03:20 +00001845 while (__tctx_task_work(tctx))
1846 cond_resched();
Jens Axboe7cbf1722021-02-10 00:03:20 +00001847}
1848
1849static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
1850 enum task_work_notify_mode notify)
1851{
1852 struct io_uring_task *tctx = tsk->io_uring;
1853 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07001854 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001855 int ret;
1856
1857 WARN_ON_ONCE(!tctx);
1858
Jens Axboe0b81e802021-02-16 10:33:53 -07001859 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001860 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001861 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001862
1863 /* task_work already pending, we're done */
1864 if (test_bit(0, &tctx->task_state) ||
1865 test_and_set_bit(0, &tctx->task_state))
1866 return 0;
1867
1868 if (!task_work_add(tsk, &tctx->task_work, notify))
1869 return 0;
1870
1871 /*
1872 * Slow path - we failed, find and delete work. if the work is not
1873 * in the list, it got run and we're fine.
1874 */
1875 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07001876 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001877 wq_list_for_each(node, prev, &tctx->task_list) {
1878 if (&req->io_task_work.node == node) {
1879 wq_list_del(&tctx->task_list, node, prev);
1880 ret = 1;
1881 break;
1882 }
1883 }
Jens Axboe0b81e802021-02-16 10:33:53 -07001884 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001885 clear_bit(0, &tctx->task_state);
1886 return ret;
1887}
1888
Jens Axboe355fb9e2020-10-22 20:19:35 -06001889static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06001890{
1891 struct task_struct *tsk = req->task;
1892 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06001893 enum task_work_notify_mode notify;
1894 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06001895
Jens Axboe6200b0a2020-09-13 14:38:30 -06001896 if (tsk->flags & PF_EXITING)
1897 return -ESRCH;
1898
Jens Axboec2c4c832020-07-01 15:37:11 -06001899 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001900 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1901 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1902 * processing task_work. There's no reliable way to tell if TWA_RESUME
1903 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001904 */
Jens Axboe91989c72020-10-16 09:02:26 -06001905 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001906 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06001907 notify = TWA_SIGNAL;
1908
Jens Axboe7cbf1722021-02-10 00:03:20 +00001909 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001910 if (!ret)
1911 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001912
Jens Axboec2c4c832020-07-01 15:37:11 -06001913 return ret;
1914}
1915
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001916static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00001917 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001918{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001919 struct io_ring_ctx *ctx = req->ctx;
1920 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001921
1922 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001923 do {
1924 head = READ_ONCE(ctx->exit_task_work);
1925 req->task_work.next = head;
1926 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001927}
1928
Jens Axboec40f6372020-06-25 15:39:59 -06001929static void __io_req_task_cancel(struct io_kiocb *req, int error)
1930{
1931 struct io_ring_ctx *ctx = req->ctx;
1932
1933 spin_lock_irq(&ctx->completion_lock);
1934 io_cqring_fill_event(req, error);
1935 io_commit_cqring(ctx);
1936 spin_unlock_irq(&ctx->completion_lock);
1937
1938 io_cqring_ev_posted(ctx);
1939 req_set_fail_links(req);
1940 io_double_put_req(req);
1941}
1942
1943static void io_req_task_cancel(struct callback_head *cb)
1944{
1945 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001946 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001947
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00001948 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00001949 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00001950 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001951 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001952}
1953
1954static void __io_req_task_submit(struct io_kiocb *req)
1955{
1956 struct io_ring_ctx *ctx = req->ctx;
1957
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00001958 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001959 mutex_lock(&ctx->uring_lock);
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00001960 if (!(current->flags & PF_EXITING) && !current->in_execve)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001961 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001962 else
Jens Axboec40f6372020-06-25 15:39:59 -06001963 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001964 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06001965}
1966
Jens Axboec40f6372020-06-25 15:39:59 -06001967static void io_req_task_submit(struct callback_head *cb)
1968{
1969 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1970
1971 __io_req_task_submit(req);
1972}
1973
1974static void io_req_task_queue(struct io_kiocb *req)
1975{
Jens Axboec40f6372020-06-25 15:39:59 -06001976 int ret;
1977
Jens Axboe7cbf1722021-02-10 00:03:20 +00001978 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001979 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06001980 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00001981 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00001982 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001983 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06001984 }
Jens Axboec40f6372020-06-25 15:39:59 -06001985}
1986
Pavel Begunkova3df76982021-02-18 22:32:52 +00001987static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
1988{
1989 percpu_ref_get(&req->ctx->refs);
1990 req->result = ret;
1991 req->task_work.func = io_req_task_cancel;
1992
1993 if (unlikely(io_req_task_work_add(req)))
1994 io_req_task_work_add_fallback(req, io_req_task_cancel);
1995}
1996
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001997static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001998{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001999 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002000
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002001 if (nxt)
2002 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002003}
2004
Jens Axboe9e645e112019-05-10 16:07:28 -06002005static void io_free_req(struct io_kiocb *req)
2006{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002007 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002008 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002009}
2010
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002011struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002012 struct task_struct *task;
2013 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002014 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002015};
2016
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002017static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002018{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002019 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002020 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002021 rb->task = NULL;
2022}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002023
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002024static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2025 struct req_batch *rb)
2026{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002027 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002028 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002029 if (rb->ctx_refs)
2030 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002031}
2032
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002033static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2034 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002035{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002036 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002037
Jens Axboee3bc8e92020-09-24 08:45:57 -06002038 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002039 if (rb->task)
2040 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002041 rb->task = req->task;
2042 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002043 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002044 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002045 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002046
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002047 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002048 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002049 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002050 else
2051 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002052}
2053
Pavel Begunkov905c1722021-02-10 00:03:14 +00002054static void io_submit_flush_completions(struct io_comp_state *cs,
2055 struct io_ring_ctx *ctx)
2056{
2057 int i, nr = cs->nr;
2058 struct io_kiocb *req;
2059 struct req_batch rb;
2060
2061 io_init_req_batch(&rb);
2062 spin_lock_irq(&ctx->completion_lock);
2063 for (i = 0; i < nr; i++) {
2064 req = cs->reqs[i];
2065 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2066 }
2067 io_commit_cqring(ctx);
2068 spin_unlock_irq(&ctx->completion_lock);
2069
2070 io_cqring_ev_posted(ctx);
2071 for (i = 0; i < nr; i++) {
2072 req = cs->reqs[i];
2073
2074 /* submission and completion refs */
2075 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002076 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002077 }
2078
2079 io_req_free_batch_finish(ctx, &rb);
2080 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002081}
2082
Jens Axboeba816ad2019-09-28 11:36:45 -06002083/*
2084 * Drop reference to request, return next in chain (if there is one) if this
2085 * was the last reference to this request.
2086 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002087static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002088{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002089 struct io_kiocb *nxt = NULL;
2090
Jens Axboe2a44f462020-02-25 13:25:41 -07002091 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002092 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002093 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002094 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002095 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002096}
2097
Jens Axboe2b188cc2019-01-07 10:46:33 -07002098static void io_put_req(struct io_kiocb *req)
2099{
Jens Axboedef596e2019-01-09 08:59:42 -07002100 if (refcount_dec_and_test(&req->refs))
2101 io_free_req(req);
2102}
2103
Pavel Begunkov216578e2020-10-13 09:44:00 +01002104static void io_put_req_deferred_cb(struct callback_head *cb)
2105{
2106 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2107
2108 io_free_req(req);
2109}
2110
2111static void io_free_req_deferred(struct io_kiocb *req)
2112{
2113 int ret;
2114
Jens Axboe7cbf1722021-02-10 00:03:20 +00002115 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002116 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002117 if (unlikely(ret))
2118 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002119}
2120
2121static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2122{
2123 if (refcount_sub_and_test(refs, &req->refs))
2124 io_free_req_deferred(req);
2125}
2126
Jens Axboe978db572019-11-14 22:39:04 -07002127static void io_double_put_req(struct io_kiocb *req)
2128{
2129 /* drop both submit and complete references */
2130 if (refcount_sub_and_test(2, &req->refs))
2131 io_free_req(req);
2132}
2133
Pavel Begunkov6c503152021-01-04 20:36:36 +00002134static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002135{
2136 /* See comment at the top of this file */
2137 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002138 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002139}
2140
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002141static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2142{
2143 struct io_rings *rings = ctx->rings;
2144
2145 /* make sure SQ entry isn't read before tail */
2146 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2147}
2148
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002149static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002150{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002151 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002152
Jens Axboebcda7ba2020-02-23 16:42:51 -07002153 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2154 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002155 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002156 kfree(kbuf);
2157 return cflags;
2158}
2159
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002160static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2161{
2162 struct io_buffer *kbuf;
2163
2164 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2165 return io_put_kbuf(req, kbuf);
2166}
2167
Jens Axboe4c6e2772020-07-01 11:29:10 -06002168static inline bool io_run_task_work(void)
2169{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002170 /*
2171 * Not safe to run on exiting task, and the task_work handling will
2172 * not add work to such a task.
2173 */
2174 if (unlikely(current->flags & PF_EXITING))
2175 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002176 if (current->task_works) {
2177 __set_current_state(TASK_RUNNING);
2178 task_work_run();
2179 return true;
2180 }
2181
2182 return false;
2183}
2184
Jens Axboedef596e2019-01-09 08:59:42 -07002185/*
2186 * Find and free completed poll iocbs
2187 */
2188static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2189 struct list_head *done)
2190{
Jens Axboe8237e042019-12-28 10:48:22 -07002191 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002192 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002193
2194 /* order with ->result store in io_complete_rw_iopoll() */
2195 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002196
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002197 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002198 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002199 int cflags = 0;
2200
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002201 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002202 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002203
Pavel Begunkovf1613402021-02-11 18:28:21 +00002204 if (READ_ONCE(req->result) == -EAGAIN) {
2205 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002206 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002207 continue;
2208 }
2209
Jens Axboebcda7ba2020-02-23 16:42:51 -07002210 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002211 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002212
2213 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002214 (*nr_events)++;
2215
Pavel Begunkovc3524382020-06-28 12:52:32 +03002216 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002217 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002218 }
Jens Axboedef596e2019-01-09 08:59:42 -07002219
Jens Axboe09bb8392019-03-13 12:39:28 -06002220 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002221 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002222 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002223}
2224
Jens Axboedef596e2019-01-09 08:59:42 -07002225static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2226 long min)
2227{
2228 struct io_kiocb *req, *tmp;
2229 LIST_HEAD(done);
2230 bool spin;
2231 int ret;
2232
2233 /*
2234 * Only spin for completions if we don't have multiple devices hanging
2235 * off our complete list, and we're under the requested amount.
2236 */
2237 spin = !ctx->poll_multi_file && *nr_events < min;
2238
2239 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002240 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002241 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002242
2243 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002244 * Move completed and retryable entries to our local lists.
2245 * If we find a request that requires polling, break out
2246 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002247 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002248 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002249 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002250 continue;
2251 }
2252 if (!list_empty(&done))
2253 break;
2254
2255 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2256 if (ret < 0)
2257 break;
2258
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002259 /* iopoll may have completed current req */
2260 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002261 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002262
Jens Axboedef596e2019-01-09 08:59:42 -07002263 if (ret && spin)
2264 spin = false;
2265 ret = 0;
2266 }
2267
2268 if (!list_empty(&done))
2269 io_iopoll_complete(ctx, nr_events, &done);
2270
2271 return ret;
2272}
2273
2274/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002275 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002276 * non-spinning poll check - we'll still enter the driver poll loop, but only
2277 * as a non-spinning completion check.
2278 */
2279static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2280 long min)
2281{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002282 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002283 int ret;
2284
2285 ret = io_do_iopoll(ctx, nr_events, min);
2286 if (ret < 0)
2287 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002288 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002289 return 0;
2290 }
2291
2292 return 1;
2293}
2294
2295/*
2296 * We can't just wait for polled events to come to us, we have to actively
2297 * find and complete them.
2298 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002299static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002300{
2301 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2302 return;
2303
2304 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002305 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002306 unsigned int nr_events = 0;
2307
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002308 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002309
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002310 /* let it sleep and repeat later if can't complete a request */
2311 if (nr_events == 0)
2312 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002313 /*
2314 * Ensure we allow local-to-the-cpu processing to take place,
2315 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002316 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002317 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002318 if (need_resched()) {
2319 mutex_unlock(&ctx->uring_lock);
2320 cond_resched();
2321 mutex_lock(&ctx->uring_lock);
2322 }
Jens Axboedef596e2019-01-09 08:59:42 -07002323 }
2324 mutex_unlock(&ctx->uring_lock);
2325}
2326
Pavel Begunkov7668b922020-07-07 16:36:21 +03002327static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002328{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002329 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002330 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002331
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002332 /*
2333 * We disallow the app entering submit/complete with polling, but we
2334 * still need to lock the ring to prevent racing with polled issue
2335 * that got punted to a workqueue.
2336 */
2337 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002338 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002339 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002340 * Don't enter poll loop if we already have events pending.
2341 * If we do, we can potentially be spinning for commands that
2342 * already triggered a CQE (eg in error).
2343 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002344 if (test_bit(0, &ctx->cq_check_overflow))
2345 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2346 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002347 break;
2348
2349 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002350 * If a submit got punted to a workqueue, we can have the
2351 * application entering polling for a command before it gets
2352 * issued. That app will hold the uring_lock for the duration
2353 * of the poll right here, so we need to take a breather every
2354 * now and then to ensure that the issue has a chance to add
2355 * the poll to the issued list. Otherwise we can spin here
2356 * forever, while the workqueue is stuck trying to acquire the
2357 * very same mutex.
2358 */
2359 if (!(++iters & 7)) {
2360 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002361 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002362 mutex_lock(&ctx->uring_lock);
2363 }
2364
Pavel Begunkov7668b922020-07-07 16:36:21 +03002365 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002366 if (ret <= 0)
2367 break;
2368 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002369 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002370
Jens Axboe500f9fb2019-08-19 12:15:59 -06002371 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002372 return ret;
2373}
2374
Jens Axboe491381ce2019-10-17 09:20:46 -06002375static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376{
Jens Axboe491381ce2019-10-17 09:20:46 -06002377 /*
2378 * Tell lockdep we inherited freeze protection from submission
2379 * thread.
2380 */
2381 if (req->flags & REQ_F_ISREG) {
2382 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383
Jens Axboe491381ce2019-10-17 09:20:46 -06002384 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002385 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002386 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002387}
2388
Jens Axboeb63534c2020-06-04 11:28:00 -06002389#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002390static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002391{
2392 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002393 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002394 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002395
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002396 /* already prepared */
2397 if (req->async_data)
2398 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002399
2400 switch (req->opcode) {
2401 case IORING_OP_READV:
2402 case IORING_OP_READ_FIXED:
2403 case IORING_OP_READ:
2404 rw = READ;
2405 break;
2406 case IORING_OP_WRITEV:
2407 case IORING_OP_WRITE_FIXED:
2408 case IORING_OP_WRITE:
2409 rw = WRITE;
2410 break;
2411 default:
2412 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2413 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002414 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002415 }
2416
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002417 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2418 if (ret < 0)
2419 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002420 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002421}
Jens Axboeb63534c2020-06-04 11:28:00 -06002422
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002423static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002424{
Jens Axboe355afae2020-09-02 09:30:31 -06002425 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002426 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002427
Jens Axboe355afae2020-09-02 09:30:31 -06002428 if (!S_ISBLK(mode) && !S_ISREG(mode))
2429 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002430 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2431 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002432 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002433 /*
2434 * If ref is dying, we might be running poll reap from the exit work.
2435 * Don't attempt to reissue from that path, just let it fail with
2436 * -EAGAIN.
2437 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002438 if (percpu_ref_is_dying(&ctx->refs))
2439 return false;
2440 return true;
2441}
2442#endif
2443
2444static bool io_rw_reissue(struct io_kiocb *req)
2445{
2446#ifdef CONFIG_BLOCK
2447 if (!io_rw_should_reissue(req))
Jens Axboe7c977a52021-02-23 19:17:35 -07002448 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002449
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002450 lockdep_assert_held(&req->ctx->uring_lock);
2451
Jens Axboe37d1e2e2021-02-17 21:03:43 -07002452 if (io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002453 refcount_inc(&req->refs);
2454 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002455 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002456 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002457 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002458#endif
2459 return false;
2460}
2461
Jens Axboea1d7c392020-06-22 11:09:46 -06002462static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002463 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002464{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002465 int cflags = 0;
2466
Pavel Begunkov23faba32021-02-11 18:28:22 +00002467 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2468 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002469 if (res != req->result)
2470 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002471
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002472 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2473 kiocb_end_write(req);
2474 if (req->flags & REQ_F_BUFFER_SELECTED)
2475 cflags = io_put_rw_kbuf(req);
2476 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002477}
2478
2479static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2480{
Jens Axboe9adbd452019-12-20 08:45:55 -07002481 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002482
Pavel Begunkov889fca72021-02-10 00:03:09 +00002483 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002484}
2485
Jens Axboedef596e2019-01-09 08:59:42 -07002486static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2487{
Jens Axboe9adbd452019-12-20 08:45:55 -07002488 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002489
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002490#ifdef CONFIG_BLOCK
2491 /* Rewind iter, if we have one. iopoll path resubmits as usual */
2492 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2493 struct io_async_rw *rw = req->async_data;
2494
2495 if (rw)
2496 iov_iter_revert(&rw->iter,
2497 req->result - iov_iter_count(&rw->iter));
2498 else if (!io_resubmit_prep(req))
2499 res = -EIO;
2500 }
2501#endif
2502
Jens Axboe491381ce2019-10-17 09:20:46 -06002503 if (kiocb->ki_flags & IOCB_WRITE)
2504 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002505
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002506 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002507 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002508
2509 WRITE_ONCE(req->result, res);
2510 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002511 smp_wmb();
2512 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002513}
2514
2515/*
2516 * After the iocb has been issued, it's safe to be found on the poll list.
2517 * Adding the kiocb to the list AFTER submission ensures that we don't
2518 * find it from a io_iopoll_getevents() thread before the issuer is done
2519 * accessing the kiocb cookie.
2520 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002521static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002522{
2523 struct io_ring_ctx *ctx = req->ctx;
2524
2525 /*
2526 * Track whether we have multiple files in our lists. This will impact
2527 * how we do polling eventually, not spinning if we're on potentially
2528 * different devices.
2529 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002530 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002531 ctx->poll_multi_file = false;
2532 } else if (!ctx->poll_multi_file) {
2533 struct io_kiocb *list_req;
2534
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002535 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002536 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002537 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002538 ctx->poll_multi_file = true;
2539 }
2540
2541 /*
2542 * For fast devices, IO may have already completed. If it has, add
2543 * it to the front so we find it first.
2544 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002545 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002546 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002547 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002548 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002549
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002550 /*
2551 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2552 * task context or in io worker task context. If current task context is
2553 * sq thread, we don't need to check whether should wake up sq thread.
2554 */
2555 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002556 wq_has_sleeper(&ctx->sq_data->wait))
2557 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002558}
2559
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002560static inline void io_state_file_put(struct io_submit_state *state)
2561{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002562 if (state->file_refs) {
2563 fput_many(state->file, state->file_refs);
2564 state->file_refs = 0;
2565 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002566}
2567
2568/*
2569 * Get as many references to a file as we have IOs left in this submission,
2570 * assuming most submissions are for one file, or at least that each file
2571 * has more than one submission.
2572 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002573static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002574{
2575 if (!state)
2576 return fget(fd);
2577
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002578 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002579 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002580 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002581 return state->file;
2582 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002583 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002584 }
2585 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002586 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002587 return NULL;
2588
2589 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002590 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002591 return state->file;
2592}
2593
Jens Axboe4503b762020-06-01 10:00:27 -06002594static bool io_bdev_nowait(struct block_device *bdev)
2595{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002596 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002597}
2598
Jens Axboe2b188cc2019-01-07 10:46:33 -07002599/*
2600 * If we tracked the file through the SCM inflight mechanism, we could support
2601 * any file. For now, just ensure that anything potentially problematic is done
2602 * inline.
2603 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002604static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605{
2606 umode_t mode = file_inode(file)->i_mode;
2607
Jens Axboe4503b762020-06-01 10:00:27 -06002608 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002609 if (IS_ENABLED(CONFIG_BLOCK) &&
2610 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002611 return true;
2612 return false;
2613 }
2614 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002615 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002616 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002617 if (IS_ENABLED(CONFIG_BLOCK) &&
2618 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002619 file->f_op != &io_uring_fops)
2620 return true;
2621 return false;
2622 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623
Jens Axboec5b85622020-06-09 19:23:05 -06002624 /* any ->read/write should understand O_NONBLOCK */
2625 if (file->f_flags & O_NONBLOCK)
2626 return true;
2627
Jens Axboeaf197f52020-04-28 13:15:06 -06002628 if (!(file->f_mode & FMODE_NOWAIT))
2629 return false;
2630
2631 if (rw == READ)
2632 return file->f_op->read_iter != NULL;
2633
2634 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635}
2636
Pavel Begunkova88fc402020-09-30 22:57:53 +03002637static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638{
Jens Axboedef596e2019-01-09 08:59:42 -07002639 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002640 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002641 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002642 unsigned ioprio;
2643 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002645 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002646 req->flags |= REQ_F_ISREG;
2647
Jens Axboe2b188cc2019-01-07 10:46:33 -07002648 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002649 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002650 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002651 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002652 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002653 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002654 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2655 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2656 if (unlikely(ret))
2657 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002658
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002659 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2660 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2661 req->flags |= REQ_F_NOWAIT;
2662
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663 ioprio = READ_ONCE(sqe->ioprio);
2664 if (ioprio) {
2665 ret = ioprio_check_cap(ioprio);
2666 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002667 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002668
2669 kiocb->ki_ioprio = ioprio;
2670 } else
2671 kiocb->ki_ioprio = get_current_ioprio();
2672
Jens Axboedef596e2019-01-09 08:59:42 -07002673 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002674 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2675 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002676 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677
Jens Axboedef596e2019-01-09 08:59:42 -07002678 kiocb->ki_flags |= IOCB_HIPRI;
2679 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002680 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002681 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002682 if (kiocb->ki_flags & IOCB_HIPRI)
2683 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002684 kiocb->ki_complete = io_complete_rw;
2685 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002686
Jens Axboe3529d8c2019-12-19 18:24:38 -07002687 req->rw.addr = READ_ONCE(sqe->addr);
2688 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002689 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002690 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691}
2692
2693static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2694{
2695 switch (ret) {
2696 case -EIOCBQUEUED:
2697 break;
2698 case -ERESTARTSYS:
2699 case -ERESTARTNOINTR:
2700 case -ERESTARTNOHAND:
2701 case -ERESTART_RESTARTBLOCK:
2702 /*
2703 * We can't just restart the syscall, since previously
2704 * submitted sqes may already be in progress. Just fail this
2705 * IO with EINTR.
2706 */
2707 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002708 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002709 default:
2710 kiocb->ki_complete(kiocb, ret, 0);
2711 }
2712}
2713
Jens Axboea1d7c392020-06-22 11:09:46 -06002714static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002715 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002716{
Jens Axboeba042912019-12-25 16:33:42 -07002717 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002718 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002719
Jens Axboe227c0c92020-08-13 11:51:40 -06002720 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002721 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002722 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002723 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002724 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002725 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002726 }
2727
Jens Axboeba042912019-12-25 16:33:42 -07002728 if (req->flags & REQ_F_CUR_POS)
2729 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002730 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002731 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002732 else
2733 io_rw_done(kiocb, ret);
2734}
2735
Pavel Begunkov847595d2021-02-04 13:52:06 +00002736static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002737{
Jens Axboe9adbd452019-12-20 08:45:55 -07002738 struct io_ring_ctx *ctx = req->ctx;
2739 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002740 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002741 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002742 size_t offset;
2743 u64 buf_addr;
2744
Jens Axboeedafcce2019-01-09 09:16:05 -07002745 if (unlikely(buf_index >= ctx->nr_user_bufs))
2746 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002747 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2748 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002749 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002750
2751 /* overflow */
2752 if (buf_addr + len < buf_addr)
2753 return -EFAULT;
2754 /* not inside the mapped region */
2755 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2756 return -EFAULT;
2757
2758 /*
2759 * May not be a start of buffer, set size appropriately
2760 * and advance us to the beginning.
2761 */
2762 offset = buf_addr - imu->ubuf;
2763 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002764
2765 if (offset) {
2766 /*
2767 * Don't use iov_iter_advance() here, as it's really slow for
2768 * using the latter parts of a big fixed buffer - it iterates
2769 * over each segment manually. We can cheat a bit here, because
2770 * we know that:
2771 *
2772 * 1) it's a BVEC iter, we set it up
2773 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2774 * first and last bvec
2775 *
2776 * So just find our index, and adjust the iterator afterwards.
2777 * If the offset is within the first bvec (or the whole first
2778 * bvec, just use iov_iter_advance(). This makes it easier
2779 * since we can just skip the first segment, which may not
2780 * be PAGE_SIZE aligned.
2781 */
2782 const struct bio_vec *bvec = imu->bvec;
2783
2784 if (offset <= bvec->bv_len) {
2785 iov_iter_advance(iter, offset);
2786 } else {
2787 unsigned long seg_skip;
2788
2789 /* skip first vec */
2790 offset -= bvec->bv_len;
2791 seg_skip = 1 + (offset >> PAGE_SHIFT);
2792
2793 iter->bvec = bvec + seg_skip;
2794 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002795 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002796 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002797 }
2798 }
2799
Pavel Begunkov847595d2021-02-04 13:52:06 +00002800 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002801}
2802
Jens Axboebcda7ba2020-02-23 16:42:51 -07002803static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2804{
2805 if (needs_lock)
2806 mutex_unlock(&ctx->uring_lock);
2807}
2808
2809static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2810{
2811 /*
2812 * "Normal" inline submissions always hold the uring_lock, since we
2813 * grab it from the system call. Same is true for the SQPOLL offload.
2814 * The only exception is when we've detached the request and issue it
2815 * from an async worker thread, grab the lock for that case.
2816 */
2817 if (needs_lock)
2818 mutex_lock(&ctx->uring_lock);
2819}
2820
2821static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2822 int bgid, struct io_buffer *kbuf,
2823 bool needs_lock)
2824{
2825 struct io_buffer *head;
2826
2827 if (req->flags & REQ_F_BUFFER_SELECTED)
2828 return kbuf;
2829
2830 io_ring_submit_lock(req->ctx, needs_lock);
2831
2832 lockdep_assert_held(&req->ctx->uring_lock);
2833
2834 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2835 if (head) {
2836 if (!list_empty(&head->list)) {
2837 kbuf = list_last_entry(&head->list, struct io_buffer,
2838 list);
2839 list_del(&kbuf->list);
2840 } else {
2841 kbuf = head;
2842 idr_remove(&req->ctx->io_buffer_idr, bgid);
2843 }
2844 if (*len > kbuf->len)
2845 *len = kbuf->len;
2846 } else {
2847 kbuf = ERR_PTR(-ENOBUFS);
2848 }
2849
2850 io_ring_submit_unlock(req->ctx, needs_lock);
2851
2852 return kbuf;
2853}
2854
Jens Axboe4d954c22020-02-27 07:31:19 -07002855static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2856 bool needs_lock)
2857{
2858 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002859 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002860
2861 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002862 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002863 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2864 if (IS_ERR(kbuf))
2865 return kbuf;
2866 req->rw.addr = (u64) (unsigned long) kbuf;
2867 req->flags |= REQ_F_BUFFER_SELECTED;
2868 return u64_to_user_ptr(kbuf->addr);
2869}
2870
2871#ifdef CONFIG_COMPAT
2872static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2873 bool needs_lock)
2874{
2875 struct compat_iovec __user *uiov;
2876 compat_ssize_t clen;
2877 void __user *buf;
2878 ssize_t len;
2879
2880 uiov = u64_to_user_ptr(req->rw.addr);
2881 if (!access_ok(uiov, sizeof(*uiov)))
2882 return -EFAULT;
2883 if (__get_user(clen, &uiov->iov_len))
2884 return -EFAULT;
2885 if (clen < 0)
2886 return -EINVAL;
2887
2888 len = clen;
2889 buf = io_rw_buffer_select(req, &len, needs_lock);
2890 if (IS_ERR(buf))
2891 return PTR_ERR(buf);
2892 iov[0].iov_base = buf;
2893 iov[0].iov_len = (compat_size_t) len;
2894 return 0;
2895}
2896#endif
2897
2898static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2899 bool needs_lock)
2900{
2901 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2902 void __user *buf;
2903 ssize_t len;
2904
2905 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2906 return -EFAULT;
2907
2908 len = iov[0].iov_len;
2909 if (len < 0)
2910 return -EINVAL;
2911 buf = io_rw_buffer_select(req, &len, needs_lock);
2912 if (IS_ERR(buf))
2913 return PTR_ERR(buf);
2914 iov[0].iov_base = buf;
2915 iov[0].iov_len = len;
2916 return 0;
2917}
2918
2919static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2920 bool needs_lock)
2921{
Jens Axboedddb3e22020-06-04 11:27:01 -06002922 if (req->flags & REQ_F_BUFFER_SELECTED) {
2923 struct io_buffer *kbuf;
2924
2925 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2926 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2927 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002928 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002929 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00002930 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07002931 return -EINVAL;
2932
2933#ifdef CONFIG_COMPAT
2934 if (req->ctx->compat)
2935 return io_compat_import(req, iov, needs_lock);
2936#endif
2937
2938 return __io_iov_buffer_select(req, iov, needs_lock);
2939}
2940
Pavel Begunkov847595d2021-02-04 13:52:06 +00002941static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
2942 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002943{
Jens Axboe9adbd452019-12-20 08:45:55 -07002944 void __user *buf = u64_to_user_ptr(req->rw.addr);
2945 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002946 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07002947 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002948
Pavel Begunkov7d009162019-11-25 23:14:40 +03002949 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002950 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002951 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002952 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002953
Jens Axboebcda7ba2020-02-23 16:42:51 -07002954 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002955 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002956 return -EINVAL;
2957
Jens Axboe3a6820f2019-12-22 15:19:35 -07002958 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002959 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002960 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002961 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002962 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002963 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002964 }
2965
Jens Axboe3a6820f2019-12-22 15:19:35 -07002966 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2967 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00002968 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002969 }
2970
Jens Axboe4d954c22020-02-27 07:31:19 -07002971 if (req->flags & REQ_F_BUFFER_SELECT) {
2972 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00002973 if (!ret)
2974 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07002975 *iovec = NULL;
2976 return ret;
2977 }
2978
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002979 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
2980 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002981}
2982
Jens Axboe0fef9482020-08-26 10:36:20 -06002983static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2984{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03002985 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06002986}
2987
Jens Axboe32960612019-09-23 11:05:34 -06002988/*
2989 * For files that don't have ->read_iter() and ->write_iter(), handle them
2990 * by looping over ->read() or ->write() manually.
2991 */
Jens Axboe4017eb92020-10-22 14:14:12 -06002992static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06002993{
Jens Axboe4017eb92020-10-22 14:14:12 -06002994 struct kiocb *kiocb = &req->rw.kiocb;
2995 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06002996 ssize_t ret = 0;
2997
2998 /*
2999 * Don't support polled IO through this interface, and we can't
3000 * support non-blocking either. For the latter, this just causes
3001 * the kiocb to be handled from an async context.
3002 */
3003 if (kiocb->ki_flags & IOCB_HIPRI)
3004 return -EOPNOTSUPP;
3005 if (kiocb->ki_flags & IOCB_NOWAIT)
3006 return -EAGAIN;
3007
3008 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003009 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003010 ssize_t nr;
3011
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003012 if (!iov_iter_is_bvec(iter)) {
3013 iovec = iov_iter_iovec(iter);
3014 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003015 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3016 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003017 }
3018
Jens Axboe32960612019-09-23 11:05:34 -06003019 if (rw == READ) {
3020 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003021 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003022 } else {
3023 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003024 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003025 }
3026
3027 if (nr < 0) {
3028 if (!ret)
3029 ret = nr;
3030 break;
3031 }
3032 ret += nr;
3033 if (nr != iovec.iov_len)
3034 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003035 req->rw.len -= nr;
3036 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003037 iov_iter_advance(iter, nr);
3038 }
3039
3040 return ret;
3041}
3042
Jens Axboeff6165b2020-08-13 09:47:43 -06003043static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3044 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003045{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003046 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003047
Jens Axboeff6165b2020-08-13 09:47:43 -06003048 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003049 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003050 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003051 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003052 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003053 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003054 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003055 unsigned iov_off = 0;
3056
3057 rw->iter.iov = rw->fast_iov;
3058 if (iter->iov != fast_iov) {
3059 iov_off = iter->iov - fast_iov;
3060 rw->iter.iov += iov_off;
3061 }
3062 if (rw->fast_iov != fast_iov)
3063 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003064 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003065 } else {
3066 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003067 }
3068}
3069
Jens Axboee8c2bc12020-08-15 18:44:09 -07003070static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003071{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003072 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3073 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3074 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003075}
3076
Jens Axboee8c2bc12020-08-15 18:44:09 -07003077static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003078{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003079 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003080 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003081
Jens Axboee8c2bc12020-08-15 18:44:09 -07003082 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003083}
3084
Jens Axboeff6165b2020-08-13 09:47:43 -06003085static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3086 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003087 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003088{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003089 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003090 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003091 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003092 if (__io_alloc_async_data(req)) {
3093 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003094 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003095 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003096
Jens Axboeff6165b2020-08-13 09:47:43 -06003097 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003098 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003099 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003100}
3101
Pavel Begunkov73debe62020-09-30 22:57:54 +03003102static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003103{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003104 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003105 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003106 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003107
Pavel Begunkov2846c482020-11-07 13:16:27 +00003108 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003109 if (unlikely(ret < 0))
3110 return ret;
3111
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003112 iorw->bytes_done = 0;
3113 iorw->free_iovec = iov;
3114 if (iov)
3115 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003116 return 0;
3117}
3118
Pavel Begunkov73debe62020-09-30 22:57:54 +03003119static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003120{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003121 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3122 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003123 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003124}
3125
Jens Axboec1dd91d2020-08-03 16:43:59 -06003126/*
3127 * This is our waitqueue callback handler, registered through lock_page_async()
3128 * when we initially tried to do the IO with the iocb armed our waitqueue.
3129 * This gets called when the page is unlocked, and we generally expect that to
3130 * happen when the page IO is completed and the page is now uptodate. This will
3131 * queue a task_work based retry of the operation, attempting to copy the data
3132 * again. If the latter fails because the page was NOT uptodate, then we will
3133 * do a thread based blocking retry of the operation. That's the unexpected
3134 * slow path.
3135 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003136static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3137 int sync, void *arg)
3138{
3139 struct wait_page_queue *wpq;
3140 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003141 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003142
3143 wpq = container_of(wait, struct wait_page_queue, wait);
3144
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003145 if (!wake_page_match(wpq, key))
3146 return 0;
3147
Hao Xuc8d317a2020-09-29 20:00:45 +08003148 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003149 list_del_init(&wait->entry);
3150
Jens Axboebcf5a062020-05-22 09:24:42 -06003151 /* submit ref gets dropped, acquire a new one */
3152 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003153 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003154 return 1;
3155}
3156
Jens Axboec1dd91d2020-08-03 16:43:59 -06003157/*
3158 * This controls whether a given IO request should be armed for async page
3159 * based retry. If we return false here, the request is handed to the async
3160 * worker threads for retry. If we're doing buffered reads on a regular file,
3161 * we prepare a private wait_page_queue entry and retry the operation. This
3162 * will either succeed because the page is now uptodate and unlocked, or it
3163 * will register a callback when the page is unlocked at IO completion. Through
3164 * that callback, io_uring uses task_work to setup a retry of the operation.
3165 * That retry will attempt the buffered read again. The retry will generally
3166 * succeed, or in rare cases where it fails, we then fall back to using the
3167 * async worker threads for a blocking retry.
3168 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003169static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003170{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003171 struct io_async_rw *rw = req->async_data;
3172 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003173 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003174
3175 /* never retry for NOWAIT, we just complete with -EAGAIN */
3176 if (req->flags & REQ_F_NOWAIT)
3177 return false;
3178
Jens Axboe227c0c92020-08-13 11:51:40 -06003179 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003180 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003181 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003182
Jens Axboebcf5a062020-05-22 09:24:42 -06003183 /*
3184 * just use poll if we can, and don't attempt if the fs doesn't
3185 * support callback based unlocks
3186 */
3187 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3188 return false;
3189
Jens Axboe3b2a4432020-08-16 10:58:43 -07003190 wait->wait.func = io_async_buf_func;
3191 wait->wait.private = req;
3192 wait->wait.flags = 0;
3193 INIT_LIST_HEAD(&wait->wait.entry);
3194 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003195 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003196 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003197 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003198}
3199
3200static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3201{
3202 if (req->file->f_op->read_iter)
3203 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003204 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003205 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003206 else
3207 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003208}
3209
Pavel Begunkov889fca72021-02-10 00:03:09 +00003210static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003211{
3212 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003213 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003214 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003215 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003216 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003217 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003218
Pavel Begunkov2846c482020-11-07 13:16:27 +00003219 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003220 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003221 iovec = NULL;
3222 } else {
3223 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3224 if (ret < 0)
3225 return ret;
3226 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003227 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003228 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003229
Jens Axboefd6c2e42019-12-18 12:19:41 -07003230 /* Ensure we clear previously set non-block flag */
3231 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003232 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003233 else
3234 kiocb->ki_flags |= IOCB_NOWAIT;
3235
Pavel Begunkov24c74672020-06-21 13:09:51 +03003236 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003237 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3238 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003239 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003240 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003241
Pavel Begunkov632546c2020-11-07 13:16:26 +00003242 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003243 if (unlikely(ret)) {
3244 kfree(iovec);
3245 return ret;
3246 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003247
Jens Axboe227c0c92020-08-13 11:51:40 -06003248 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003249
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003250 if (ret == -EIOCBQUEUED) {
Jens Axboe3e6a0d32021-03-01 13:56:00 -07003251 if (req->async_data)
3252 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003253 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003254 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003255 /* IOPOLL retry should happen for io-wq threads */
3256 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003257 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003258 /* no retry on NONBLOCK nor RWF_NOWAIT */
3259 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003260 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003261 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003262 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003263 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003264 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003265 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003266 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003267 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003268 }
3269
Jens Axboe227c0c92020-08-13 11:51:40 -06003270 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003271 if (ret2)
3272 return ret2;
3273
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003274 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003275 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003276 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003277 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003278
Pavel Begunkovb23df912021-02-04 13:52:04 +00003279 do {
3280 io_size -= ret;
3281 rw->bytes_done += ret;
3282 /* if we can retry, do so with the callbacks armed */
3283 if (!io_rw_should_retry(req)) {
3284 kiocb->ki_flags &= ~IOCB_WAITQ;
3285 return -EAGAIN;
3286 }
3287
3288 /*
3289 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3290 * we get -EIOCBQUEUED, then we'll get a notification when the
3291 * desired page gets unlocked. We can also get a partial read
3292 * here, and if we do, then just retry at the new offset.
3293 */
3294 ret = io_iter_do_read(req, iter);
3295 if (ret == -EIOCBQUEUED)
3296 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003297 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003298 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003299done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003300 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003301out_free:
3302 /* it's faster to check here then delegate to kfree */
3303 if (iovec)
3304 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003305 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003306}
3307
Pavel Begunkov73debe62020-09-30 22:57:54 +03003308static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003309{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003310 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3311 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003312 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003313}
3314
Pavel Begunkov889fca72021-02-10 00:03:09 +00003315static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003316{
3317 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003318 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003319 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003320 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003321 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003322 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003323
Pavel Begunkov2846c482020-11-07 13:16:27 +00003324 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003325 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003326 iovec = NULL;
3327 } else {
3328 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3329 if (ret < 0)
3330 return ret;
3331 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003332 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003333 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003334
Jens Axboefd6c2e42019-12-18 12:19:41 -07003335 /* Ensure we clear previously set non-block flag */
3336 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003337 kiocb->ki_flags &= ~IOCB_NOWAIT;
3338 else
3339 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003340
Pavel Begunkov24c74672020-06-21 13:09:51 +03003341 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003342 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003343 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003344
Jens Axboe10d59342019-12-09 20:16:22 -07003345 /* file path doesn't support NOWAIT for non-direct_IO */
3346 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3347 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003348 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003349
Pavel Begunkov632546c2020-11-07 13:16:26 +00003350 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003351 if (unlikely(ret))
3352 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003353
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003354 /*
3355 * Open-code file_start_write here to grab freeze protection,
3356 * which will be released by another thread in
3357 * io_complete_rw(). Fool lockdep by telling it the lock got
3358 * released so that it doesn't complain about the held lock when
3359 * we return to userspace.
3360 */
3361 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003362 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003363 __sb_writers_release(file_inode(req->file)->i_sb,
3364 SB_FREEZE_WRITE);
3365 }
3366 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003367
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003368 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003369 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003370 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003371 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003372 else
3373 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003374
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003375 /*
3376 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3377 * retry them without IOCB_NOWAIT.
3378 */
3379 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3380 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003381 /* no retry on NONBLOCK nor RWF_NOWAIT */
3382 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003383 goto done;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07003384 if (ret2 == -EIOCBQUEUED && req->async_data)
3385 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003386 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003387 /* IOPOLL retry should happen for io-wq threads */
3388 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3389 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003390done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003391 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003392 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003393copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003394 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003395 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003396 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003397 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398 }
Jens Axboe31b51512019-01-18 22:56:34 -07003399out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003400 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003401 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003402 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003403 return ret;
3404}
3405
Jens Axboe80a261f2020-09-28 14:23:58 -06003406static int io_renameat_prep(struct io_kiocb *req,
3407 const struct io_uring_sqe *sqe)
3408{
3409 struct io_rename *ren = &req->rename;
3410 const char __user *oldf, *newf;
3411
3412 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3413 return -EBADF;
3414
3415 ren->old_dfd = READ_ONCE(sqe->fd);
3416 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3417 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3418 ren->new_dfd = READ_ONCE(sqe->len);
3419 ren->flags = READ_ONCE(sqe->rename_flags);
3420
3421 ren->oldpath = getname(oldf);
3422 if (IS_ERR(ren->oldpath))
3423 return PTR_ERR(ren->oldpath);
3424
3425 ren->newpath = getname(newf);
3426 if (IS_ERR(ren->newpath)) {
3427 putname(ren->oldpath);
3428 return PTR_ERR(ren->newpath);
3429 }
3430
3431 req->flags |= REQ_F_NEED_CLEANUP;
3432 return 0;
3433}
3434
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003435static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003436{
3437 struct io_rename *ren = &req->rename;
3438 int ret;
3439
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003440 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003441 return -EAGAIN;
3442
3443 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3444 ren->newpath, ren->flags);
3445
3446 req->flags &= ~REQ_F_NEED_CLEANUP;
3447 if (ret < 0)
3448 req_set_fail_links(req);
3449 io_req_complete(req, ret);
3450 return 0;
3451}
3452
Jens Axboe14a11432020-09-28 14:27:37 -06003453static int io_unlinkat_prep(struct io_kiocb *req,
3454 const struct io_uring_sqe *sqe)
3455{
3456 struct io_unlink *un = &req->unlink;
3457 const char __user *fname;
3458
3459 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3460 return -EBADF;
3461
3462 un->dfd = READ_ONCE(sqe->fd);
3463
3464 un->flags = READ_ONCE(sqe->unlink_flags);
3465 if (un->flags & ~AT_REMOVEDIR)
3466 return -EINVAL;
3467
3468 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3469 un->filename = getname(fname);
3470 if (IS_ERR(un->filename))
3471 return PTR_ERR(un->filename);
3472
3473 req->flags |= REQ_F_NEED_CLEANUP;
3474 return 0;
3475}
3476
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003477static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003478{
3479 struct io_unlink *un = &req->unlink;
3480 int ret;
3481
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003482 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003483 return -EAGAIN;
3484
3485 if (un->flags & AT_REMOVEDIR)
3486 ret = do_rmdir(un->dfd, un->filename);
3487 else
3488 ret = do_unlinkat(un->dfd, un->filename);
3489
3490 req->flags &= ~REQ_F_NEED_CLEANUP;
3491 if (ret < 0)
3492 req_set_fail_links(req);
3493 io_req_complete(req, ret);
3494 return 0;
3495}
3496
Jens Axboe36f4fa62020-09-05 11:14:22 -06003497static int io_shutdown_prep(struct io_kiocb *req,
3498 const struct io_uring_sqe *sqe)
3499{
3500#if defined(CONFIG_NET)
3501 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3502 return -EINVAL;
3503 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3504 sqe->buf_index)
3505 return -EINVAL;
3506
3507 req->shutdown.how = READ_ONCE(sqe->len);
3508 return 0;
3509#else
3510 return -EOPNOTSUPP;
3511#endif
3512}
3513
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003514static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003515{
3516#if defined(CONFIG_NET)
3517 struct socket *sock;
3518 int ret;
3519
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003520 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003521 return -EAGAIN;
3522
Linus Torvalds48aba792020-12-16 12:44:05 -08003523 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003524 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003525 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003526
3527 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003528 if (ret < 0)
3529 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003530 io_req_complete(req, ret);
3531 return 0;
3532#else
3533 return -EOPNOTSUPP;
3534#endif
3535}
3536
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003537static int __io_splice_prep(struct io_kiocb *req,
3538 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003539{
3540 struct io_splice* sp = &req->splice;
3541 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003542
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003543 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3544 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003545
3546 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003547 sp->len = READ_ONCE(sqe->len);
3548 sp->flags = READ_ONCE(sqe->splice_flags);
3549
3550 if (unlikely(sp->flags & ~valid_flags))
3551 return -EINVAL;
3552
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003553 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3554 (sp->flags & SPLICE_F_FD_IN_FIXED));
3555 if (!sp->file_in)
3556 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003557 req->flags |= REQ_F_NEED_CLEANUP;
3558
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003559 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3560 /*
3561 * Splice operation will be punted aync, and here need to
3562 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3563 */
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003564 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003565 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003566
3567 return 0;
3568}
3569
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003570static int io_tee_prep(struct io_kiocb *req,
3571 const struct io_uring_sqe *sqe)
3572{
3573 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3574 return -EINVAL;
3575 return __io_splice_prep(req, sqe);
3576}
3577
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003578static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003579{
3580 struct io_splice *sp = &req->splice;
3581 struct file *in = sp->file_in;
3582 struct file *out = sp->file_out;
3583 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3584 long ret = 0;
3585
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003586 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003587 return -EAGAIN;
3588 if (sp->len)
3589 ret = do_tee(in, out, sp->len, flags);
3590
3591 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3592 req->flags &= ~REQ_F_NEED_CLEANUP;
3593
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003594 if (ret != sp->len)
3595 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003596 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003597 return 0;
3598}
3599
3600static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3601{
3602 struct io_splice* sp = &req->splice;
3603
3604 sp->off_in = READ_ONCE(sqe->splice_off_in);
3605 sp->off_out = READ_ONCE(sqe->off);
3606 return __io_splice_prep(req, sqe);
3607}
3608
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003609static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003610{
3611 struct io_splice *sp = &req->splice;
3612 struct file *in = sp->file_in;
3613 struct file *out = sp->file_out;
3614 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3615 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003616 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003617
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003618 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003619 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003620
3621 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3622 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003623
Jens Axboe948a7742020-05-17 14:21:38 -06003624 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003625 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003626
3627 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3628 req->flags &= ~REQ_F_NEED_CLEANUP;
3629
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003630 if (ret != sp->len)
3631 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003632 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003633 return 0;
3634}
3635
Jens Axboe2b188cc2019-01-07 10:46:33 -07003636/*
3637 * IORING_OP_NOP just posts a completion event, nothing else.
3638 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003639static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003640{
3641 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003642
Jens Axboedef596e2019-01-09 08:59:42 -07003643 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3644 return -EINVAL;
3645
Pavel Begunkov889fca72021-02-10 00:03:09 +00003646 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003647 return 0;
3648}
3649
Pavel Begunkov1155c762021-02-18 18:29:38 +00003650static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003651{
Jens Axboe6b063142019-01-10 22:13:58 -07003652 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003653
Jens Axboe09bb8392019-03-13 12:39:28 -06003654 if (!req->file)
3655 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003656
Jens Axboe6b063142019-01-10 22:13:58 -07003657 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003658 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003659 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003660 return -EINVAL;
3661
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003662 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3663 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3664 return -EINVAL;
3665
3666 req->sync.off = READ_ONCE(sqe->off);
3667 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003668 return 0;
3669}
3670
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003671static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003672{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003673 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003674 int ret;
3675
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003676 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003677 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003678 return -EAGAIN;
3679
Jens Axboe9adbd452019-12-20 08:45:55 -07003680 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003681 end > 0 ? end : LLONG_MAX,
3682 req->sync.flags & IORING_FSYNC_DATASYNC);
3683 if (ret < 0)
3684 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003685 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003686 return 0;
3687}
3688
Jens Axboed63d1b52019-12-10 10:38:56 -07003689static int io_fallocate_prep(struct io_kiocb *req,
3690 const struct io_uring_sqe *sqe)
3691{
3692 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3693 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003694 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3695 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003696
3697 req->sync.off = READ_ONCE(sqe->off);
3698 req->sync.len = READ_ONCE(sqe->addr);
3699 req->sync.mode = READ_ONCE(sqe->len);
3700 return 0;
3701}
3702
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003703static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003704{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003705 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003706
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003707 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003708 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003709 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003710 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3711 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003712 if (ret < 0)
3713 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003714 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003715 return 0;
3716}
3717
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003718static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003719{
Jens Axboef8748882020-01-08 17:47:02 -07003720 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003721 int ret;
3722
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003723 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003724 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003725 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003726 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003727
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003728 /* open.how should be already initialised */
3729 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003730 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003731
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003732 req->open.dfd = READ_ONCE(sqe->fd);
3733 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003734 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003735 if (IS_ERR(req->open.filename)) {
3736 ret = PTR_ERR(req->open.filename);
3737 req->open.filename = NULL;
3738 return ret;
3739 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003740 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003741 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003742 return 0;
3743}
3744
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003745static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3746{
3747 u64 flags, mode;
3748
Jens Axboe14587a462020-09-05 11:36:08 -06003749 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003750 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003751 mode = READ_ONCE(sqe->len);
3752 flags = READ_ONCE(sqe->open_flags);
3753 req->open.how = build_open_how(flags, mode);
3754 return __io_openat_prep(req, sqe);
3755}
3756
Jens Axboecebdb982020-01-08 17:59:24 -07003757static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3758{
3759 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003760 size_t len;
3761 int ret;
3762
Jens Axboe14587a462020-09-05 11:36:08 -06003763 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003764 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003765 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3766 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003767 if (len < OPEN_HOW_SIZE_VER0)
3768 return -EINVAL;
3769
3770 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3771 len);
3772 if (ret)
3773 return ret;
3774
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003775 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003776}
3777
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003778static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003779{
3780 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003781 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003782 bool nonblock_set;
3783 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003784 int ret;
3785
Jens Axboecebdb982020-01-08 17:59:24 -07003786 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003787 if (ret)
3788 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003789 nonblock_set = op.open_flag & O_NONBLOCK;
3790 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003791 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003792 /*
3793 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3794 * it'll always -EAGAIN
3795 */
3796 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3797 return -EAGAIN;
3798 op.lookup_flags |= LOOKUP_CACHED;
3799 op.open_flag |= O_NONBLOCK;
3800 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003801
Jens Axboe4022e7a2020-03-19 19:23:18 -06003802 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003803 if (ret < 0)
3804 goto err;
3805
3806 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003807 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003808 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3809 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003810 /*
3811 * We could hang on to this 'fd', but seems like marginal
3812 * gain for something that is now known to be a slower path.
3813 * So just put it, and we'll get a new one when we retry.
3814 */
3815 put_unused_fd(ret);
3816 return -EAGAIN;
3817 }
3818
Jens Axboe15b71ab2019-12-11 11:20:36 -07003819 if (IS_ERR(file)) {
3820 put_unused_fd(ret);
3821 ret = PTR_ERR(file);
3822 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003823 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003824 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003825 fsnotify_open(file);
3826 fd_install(ret, file);
3827 }
3828err:
3829 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003830 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003831 if (ret < 0)
3832 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003833 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003834 return 0;
3835}
3836
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003837static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003838{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003839 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07003840}
3841
Jens Axboe067524e2020-03-02 16:32:28 -07003842static int io_remove_buffers_prep(struct io_kiocb *req,
3843 const struct io_uring_sqe *sqe)
3844{
3845 struct io_provide_buf *p = &req->pbuf;
3846 u64 tmp;
3847
3848 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3849 return -EINVAL;
3850
3851 tmp = READ_ONCE(sqe->fd);
3852 if (!tmp || tmp > USHRT_MAX)
3853 return -EINVAL;
3854
3855 memset(p, 0, sizeof(*p));
3856 p->nbufs = tmp;
3857 p->bgid = READ_ONCE(sqe->buf_group);
3858 return 0;
3859}
3860
3861static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3862 int bgid, unsigned nbufs)
3863{
3864 unsigned i = 0;
3865
3866 /* shouldn't happen */
3867 if (!nbufs)
3868 return 0;
3869
3870 /* the head kbuf is the list itself */
3871 while (!list_empty(&buf->list)) {
3872 struct io_buffer *nxt;
3873
3874 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3875 list_del(&nxt->list);
3876 kfree(nxt);
3877 if (++i == nbufs)
3878 return i;
3879 }
3880 i++;
3881 kfree(buf);
3882 idr_remove(&ctx->io_buffer_idr, bgid);
3883
3884 return i;
3885}
3886
Pavel Begunkov889fca72021-02-10 00:03:09 +00003887static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07003888{
3889 struct io_provide_buf *p = &req->pbuf;
3890 struct io_ring_ctx *ctx = req->ctx;
3891 struct io_buffer *head;
3892 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003893 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07003894
3895 io_ring_submit_lock(ctx, !force_nonblock);
3896
3897 lockdep_assert_held(&ctx->uring_lock);
3898
3899 ret = -ENOENT;
3900 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3901 if (head)
3902 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003903 if (ret < 0)
3904 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003905
3906 /* need to hold the lock to complete IOPOLL requests */
3907 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003908 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003909 io_ring_submit_unlock(ctx, !force_nonblock);
3910 } else {
3911 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003912 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003913 }
Jens Axboe067524e2020-03-02 16:32:28 -07003914 return 0;
3915}
3916
Jens Axboeddf0322d2020-02-23 16:41:33 -07003917static int io_provide_buffers_prep(struct io_kiocb *req,
3918 const struct io_uring_sqe *sqe)
3919{
3920 struct io_provide_buf *p = &req->pbuf;
3921 u64 tmp;
3922
3923 if (sqe->ioprio || sqe->rw_flags)
3924 return -EINVAL;
3925
3926 tmp = READ_ONCE(sqe->fd);
3927 if (!tmp || tmp > USHRT_MAX)
3928 return -E2BIG;
3929 p->nbufs = tmp;
3930 p->addr = READ_ONCE(sqe->addr);
3931 p->len = READ_ONCE(sqe->len);
3932
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003933 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003934 return -EFAULT;
3935
3936 p->bgid = READ_ONCE(sqe->buf_group);
3937 tmp = READ_ONCE(sqe->off);
3938 if (tmp > USHRT_MAX)
3939 return -E2BIG;
3940 p->bid = tmp;
3941 return 0;
3942}
3943
3944static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3945{
3946 struct io_buffer *buf;
3947 u64 addr = pbuf->addr;
3948 int i, bid = pbuf->bid;
3949
3950 for (i = 0; i < pbuf->nbufs; i++) {
3951 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3952 if (!buf)
3953 break;
3954
3955 buf->addr = addr;
3956 buf->len = pbuf->len;
3957 buf->bid = bid;
3958 addr += pbuf->len;
3959 bid++;
3960 if (!*head) {
3961 INIT_LIST_HEAD(&buf->list);
3962 *head = buf;
3963 } else {
3964 list_add_tail(&buf->list, &(*head)->list);
3965 }
3966 }
3967
3968 return i ? i : -ENOMEM;
3969}
3970
Pavel Begunkov889fca72021-02-10 00:03:09 +00003971static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003972{
3973 struct io_provide_buf *p = &req->pbuf;
3974 struct io_ring_ctx *ctx = req->ctx;
3975 struct io_buffer *head, *list;
3976 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003977 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07003978
3979 io_ring_submit_lock(ctx, !force_nonblock);
3980
3981 lockdep_assert_held(&ctx->uring_lock);
3982
3983 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3984
3985 ret = io_add_buffers(p, &head);
3986 if (ret < 0)
3987 goto out;
3988
3989 if (!list) {
3990 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3991 GFP_KERNEL);
3992 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003993 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003994 goto out;
3995 }
3996 }
3997out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07003998 if (ret < 0)
3999 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004000
4001 /* need to hold the lock to complete IOPOLL requests */
4002 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004003 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004004 io_ring_submit_unlock(ctx, !force_nonblock);
4005 } else {
4006 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004007 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004008 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004009 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004010}
4011
Jens Axboe3e4827b2020-01-08 15:18:09 -07004012static int io_epoll_ctl_prep(struct io_kiocb *req,
4013 const struct io_uring_sqe *sqe)
4014{
4015#if defined(CONFIG_EPOLL)
4016 if (sqe->ioprio || sqe->buf_index)
4017 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004018 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004019 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004020
4021 req->epoll.epfd = READ_ONCE(sqe->fd);
4022 req->epoll.op = READ_ONCE(sqe->len);
4023 req->epoll.fd = READ_ONCE(sqe->off);
4024
4025 if (ep_op_has_event(req->epoll.op)) {
4026 struct epoll_event __user *ev;
4027
4028 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4029 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4030 return -EFAULT;
4031 }
4032
4033 return 0;
4034#else
4035 return -EOPNOTSUPP;
4036#endif
4037}
4038
Pavel Begunkov889fca72021-02-10 00:03:09 +00004039static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004040{
4041#if defined(CONFIG_EPOLL)
4042 struct io_epoll *ie = &req->epoll;
4043 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004044 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004045
4046 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4047 if (force_nonblock && ret == -EAGAIN)
4048 return -EAGAIN;
4049
4050 if (ret < 0)
4051 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004052 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004053 return 0;
4054#else
4055 return -EOPNOTSUPP;
4056#endif
4057}
4058
Jens Axboec1ca7572019-12-25 22:18:28 -07004059static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4060{
4061#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4062 if (sqe->ioprio || sqe->buf_index || sqe->off)
4063 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004064 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4065 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004066
4067 req->madvise.addr = READ_ONCE(sqe->addr);
4068 req->madvise.len = READ_ONCE(sqe->len);
4069 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4070 return 0;
4071#else
4072 return -EOPNOTSUPP;
4073#endif
4074}
4075
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004076static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004077{
4078#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4079 struct io_madvise *ma = &req->madvise;
4080 int ret;
4081
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004082 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004083 return -EAGAIN;
4084
Minchan Kim0726b012020-10-17 16:14:50 -07004085 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004086 if (ret < 0)
4087 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004088 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004089 return 0;
4090#else
4091 return -EOPNOTSUPP;
4092#endif
4093}
4094
Jens Axboe4840e412019-12-25 22:03:45 -07004095static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4096{
4097 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4098 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004099 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4100 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004101
4102 req->fadvise.offset = READ_ONCE(sqe->off);
4103 req->fadvise.len = READ_ONCE(sqe->len);
4104 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4105 return 0;
4106}
4107
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004108static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004109{
4110 struct io_fadvise *fa = &req->fadvise;
4111 int ret;
4112
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004113 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004114 switch (fa->advice) {
4115 case POSIX_FADV_NORMAL:
4116 case POSIX_FADV_RANDOM:
4117 case POSIX_FADV_SEQUENTIAL:
4118 break;
4119 default:
4120 return -EAGAIN;
4121 }
4122 }
Jens Axboe4840e412019-12-25 22:03:45 -07004123
4124 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4125 if (ret < 0)
4126 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004127 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004128 return 0;
4129}
4130
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004131static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4132{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004133 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004134 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004135 if (sqe->ioprio || sqe->buf_index)
4136 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004137 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004138 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004139
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004140 req->statx.dfd = READ_ONCE(sqe->fd);
4141 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004142 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004143 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4144 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004145
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004146 return 0;
4147}
4148
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004149static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004150{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004151 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004152 int ret;
4153
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004154 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004155 /* only need file table for an actual valid fd */
4156 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4157 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004158 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004159 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004160
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004161 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4162 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004163
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004164 if (ret < 0)
4165 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004166 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004167 return 0;
4168}
4169
Jens Axboeb5dba592019-12-11 14:02:38 -07004170static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4171{
Jens Axboe14587a462020-09-05 11:36:08 -06004172 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004173 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004174 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4175 sqe->rw_flags || sqe->buf_index)
4176 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004177 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004178 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004179
4180 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004181 return 0;
4182}
4183
Pavel Begunkov889fca72021-02-10 00:03:09 +00004184static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004185{
Jens Axboe9eac1902021-01-19 15:50:37 -07004186 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004187 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004188 struct fdtable *fdt;
4189 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004190 int ret;
4191
Jens Axboe9eac1902021-01-19 15:50:37 -07004192 file = NULL;
4193 ret = -EBADF;
4194 spin_lock(&files->file_lock);
4195 fdt = files_fdtable(files);
4196 if (close->fd >= fdt->max_fds) {
4197 spin_unlock(&files->file_lock);
4198 goto err;
4199 }
4200 file = fdt->fd[close->fd];
4201 if (!file) {
4202 spin_unlock(&files->file_lock);
4203 goto err;
4204 }
4205
4206 if (file->f_op == &io_uring_fops) {
4207 spin_unlock(&files->file_lock);
4208 file = NULL;
4209 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004210 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004211
4212 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004213 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004214 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004215 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004216 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004217
Jens Axboe9eac1902021-01-19 15:50:37 -07004218 ret = __close_fd_get_file(close->fd, &file);
4219 spin_unlock(&files->file_lock);
4220 if (ret < 0) {
4221 if (ret == -ENOENT)
4222 ret = -EBADF;
4223 goto err;
4224 }
4225
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004226 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004227 ret = filp_close(file, current->files);
4228err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004229 if (ret < 0)
4230 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004231 if (file)
4232 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004233 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004234 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004235}
4236
Pavel Begunkov1155c762021-02-18 18:29:38 +00004237static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004238{
4239 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004240
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004241 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4242 return -EINVAL;
4243 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4244 return -EINVAL;
4245
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004246 req->sync.off = READ_ONCE(sqe->off);
4247 req->sync.len = READ_ONCE(sqe->len);
4248 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004249 return 0;
4250}
4251
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004252static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004253{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004254 int ret;
4255
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004256 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004257 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004258 return -EAGAIN;
4259
Jens Axboe9adbd452019-12-20 08:45:55 -07004260 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004261 req->sync.flags);
4262 if (ret < 0)
4263 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004264 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004265 return 0;
4266}
4267
YueHaibing469956e2020-03-04 15:53:52 +08004268#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004269static int io_setup_async_msg(struct io_kiocb *req,
4270 struct io_async_msghdr *kmsg)
4271{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004272 struct io_async_msghdr *async_msg = req->async_data;
4273
4274 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004275 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004276 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004277 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004278 return -ENOMEM;
4279 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004280 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004281 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004282 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004283 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004284 /* if were using fast_iov, set it to the new one */
4285 if (!async_msg->free_iov)
4286 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4287
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004288 return -EAGAIN;
4289}
4290
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004291static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4292 struct io_async_msghdr *iomsg)
4293{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004294 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004295 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004296 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004297 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004298}
4299
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004300static int io_sendmsg_prep_async(struct io_kiocb *req)
4301{
4302 int ret;
4303
4304 if (!io_op_defs[req->opcode].needs_async_data)
4305 return 0;
4306 ret = io_sendmsg_copy_hdr(req, req->async_data);
4307 if (!ret)
4308 req->flags |= REQ_F_NEED_CLEANUP;
4309 return ret;
4310}
4311
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004313{
Jens Axboee47293f2019-12-20 08:58:21 -07004314 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004315
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004316 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4317 return -EINVAL;
4318
Jens Axboee47293f2019-12-20 08:58:21 -07004319 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004320 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004321 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322
Jens Axboed8768362020-02-27 14:17:49 -07004323#ifdef CONFIG_COMPAT
4324 if (req->ctx->compat)
4325 sr->msg_flags |= MSG_CMSG_COMPAT;
4326#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004327 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004328}
4329
Pavel Begunkov889fca72021-02-10 00:03:09 +00004330static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004331{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004332 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004333 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004334 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004335 int ret;
4336
Florent Revestdba4a922020-12-04 12:36:04 +01004337 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004338 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004339 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004340
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004341 kmsg = req->async_data;
4342 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004343 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004344 if (ret)
4345 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004346 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004347 }
4348
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004349 flags = req->sr_msg.msg_flags;
4350 if (flags & MSG_DONTWAIT)
4351 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004352 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004353 flags |= MSG_DONTWAIT;
4354
4355 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004356 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004357 return io_setup_async_msg(req, kmsg);
4358 if (ret == -ERESTARTSYS)
4359 ret = -EINTR;
4360
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004361 /* fast path, check for non-NULL to avoid function call */
4362 if (kmsg->free_iov)
4363 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004364 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004365 if (ret < 0)
4366 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004367 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004368 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004369}
4370
Pavel Begunkov889fca72021-02-10 00:03:09 +00004371static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004372{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004373 struct io_sr_msg *sr = &req->sr_msg;
4374 struct msghdr msg;
4375 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004376 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004377 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004378 int ret;
4379
Florent Revestdba4a922020-12-04 12:36:04 +01004380 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004381 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004382 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004383
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004384 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4385 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004386 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004387
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004388 msg.msg_name = NULL;
4389 msg.msg_control = NULL;
4390 msg.msg_controllen = 0;
4391 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004392
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004393 flags = req->sr_msg.msg_flags;
4394 if (flags & MSG_DONTWAIT)
4395 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004396 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004397 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004398
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004399 msg.msg_flags = flags;
4400 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004401 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004402 return -EAGAIN;
4403 if (ret == -ERESTARTSYS)
4404 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004405
Jens Axboe03b12302019-12-02 18:50:25 -07004406 if (ret < 0)
4407 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004408 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004409 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004410}
4411
Pavel Begunkov1400e692020-07-12 20:41:05 +03004412static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4413 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004414{
4415 struct io_sr_msg *sr = &req->sr_msg;
4416 struct iovec __user *uiov;
4417 size_t iov_len;
4418 int ret;
4419
Pavel Begunkov1400e692020-07-12 20:41:05 +03004420 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4421 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004422 if (ret)
4423 return ret;
4424
4425 if (req->flags & REQ_F_BUFFER_SELECT) {
4426 if (iov_len > 1)
4427 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004428 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004429 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004430 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004431 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004432 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004433 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004434 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004435 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004436 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004437 if (ret > 0)
4438 ret = 0;
4439 }
4440
4441 return ret;
4442}
4443
4444#ifdef CONFIG_COMPAT
4445static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004446 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004447{
4448 struct compat_msghdr __user *msg_compat;
4449 struct io_sr_msg *sr = &req->sr_msg;
4450 struct compat_iovec __user *uiov;
4451 compat_uptr_t ptr;
4452 compat_size_t len;
4453 int ret;
4454
Pavel Begunkov270a5942020-07-12 20:41:04 +03004455 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004456 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004457 &ptr, &len);
4458 if (ret)
4459 return ret;
4460
4461 uiov = compat_ptr(ptr);
4462 if (req->flags & REQ_F_BUFFER_SELECT) {
4463 compat_ssize_t clen;
4464
4465 if (len > 1)
4466 return -EINVAL;
4467 if (!access_ok(uiov, sizeof(*uiov)))
4468 return -EFAULT;
4469 if (__get_user(clen, &uiov->iov_len))
4470 return -EFAULT;
4471 if (clen < 0)
4472 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004473 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004474 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004475 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004476 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004477 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004478 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004479 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004480 if (ret < 0)
4481 return ret;
4482 }
4483
4484 return 0;
4485}
Jens Axboe03b12302019-12-02 18:50:25 -07004486#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004487
Pavel Begunkov1400e692020-07-12 20:41:05 +03004488static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4489 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004490{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004491 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004492
4493#ifdef CONFIG_COMPAT
4494 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004495 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004496#endif
4497
Pavel Begunkov1400e692020-07-12 20:41:05 +03004498 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004499}
4500
Jens Axboebcda7ba2020-02-23 16:42:51 -07004501static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004502 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004503{
4504 struct io_sr_msg *sr = &req->sr_msg;
4505 struct io_buffer *kbuf;
4506
Jens Axboebcda7ba2020-02-23 16:42:51 -07004507 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4508 if (IS_ERR(kbuf))
4509 return kbuf;
4510
4511 sr->kbuf = kbuf;
4512 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004513 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004514}
4515
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004516static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4517{
4518 return io_put_kbuf(req, req->sr_msg.kbuf);
4519}
4520
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004521static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004522{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004523 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004524
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004525 if (!io_op_defs[req->opcode].needs_async_data)
4526 return 0;
4527 ret = io_recvmsg_copy_hdr(req, req->async_data);
4528 if (!ret)
4529 req->flags |= REQ_F_NEED_CLEANUP;
4530 return ret;
4531}
4532
4533static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4534{
4535 struct io_sr_msg *sr = &req->sr_msg;
4536
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004537 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4538 return -EINVAL;
4539
Jens Axboe3529d8c2019-12-19 18:24:38 -07004540 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004541 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004542 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004543 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004544
Jens Axboed8768362020-02-27 14:17:49 -07004545#ifdef CONFIG_COMPAT
4546 if (req->ctx->compat)
4547 sr->msg_flags |= MSG_CMSG_COMPAT;
4548#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004549 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004550}
4551
Pavel Begunkov889fca72021-02-10 00:03:09 +00004552static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004553{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004554 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004555 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004556 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004557 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004558 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004559 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004560
Florent Revestdba4a922020-12-04 12:36:04 +01004561 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004562 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004563 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004564
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004565 kmsg = req->async_data;
4566 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004567 ret = io_recvmsg_copy_hdr(req, &iomsg);
4568 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004569 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004570 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004571 }
4572
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004573 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004574 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004575 if (IS_ERR(kbuf))
4576 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004577 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004578 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4579 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004580 1, req->sr_msg.len);
4581 }
4582
4583 flags = req->sr_msg.msg_flags;
4584 if (flags & MSG_DONTWAIT)
4585 req->flags |= REQ_F_NOWAIT;
4586 else if (force_nonblock)
4587 flags |= MSG_DONTWAIT;
4588
4589 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4590 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004591 if (force_nonblock && ret == -EAGAIN)
4592 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004593 if (ret == -ERESTARTSYS)
4594 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004595
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004596 if (req->flags & REQ_F_BUFFER_SELECTED)
4597 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004598 /* fast path, check for non-NULL to avoid function call */
4599 if (kmsg->free_iov)
4600 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004601 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004602 if (ret < 0)
4603 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004604 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004605 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004606}
4607
Pavel Begunkov889fca72021-02-10 00:03:09 +00004608static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004609{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004610 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004611 struct io_sr_msg *sr = &req->sr_msg;
4612 struct msghdr msg;
4613 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004614 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004615 struct iovec iov;
4616 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004617 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004618 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004619
Florent Revestdba4a922020-12-04 12:36:04 +01004620 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004621 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004622 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004623
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004624 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004625 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004626 if (IS_ERR(kbuf))
4627 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004629 }
4630
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004632 if (unlikely(ret))
4633 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004634
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004635 msg.msg_name = NULL;
4636 msg.msg_control = NULL;
4637 msg.msg_controllen = 0;
4638 msg.msg_namelen = 0;
4639 msg.msg_iocb = NULL;
4640 msg.msg_flags = 0;
4641
4642 flags = req->sr_msg.msg_flags;
4643 if (flags & MSG_DONTWAIT)
4644 req->flags |= REQ_F_NOWAIT;
4645 else if (force_nonblock)
4646 flags |= MSG_DONTWAIT;
4647
4648 ret = sock_recvmsg(sock, &msg, flags);
4649 if (force_nonblock && ret == -EAGAIN)
4650 return -EAGAIN;
4651 if (ret == -ERESTARTSYS)
4652 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004653out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004654 if (req->flags & REQ_F_BUFFER_SELECTED)
4655 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004656 if (ret < 0)
4657 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004658 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004659 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004660}
4661
Jens Axboe3529d8c2019-12-19 18:24:38 -07004662static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004663{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004664 struct io_accept *accept = &req->accept;
4665
Jens Axboe14587a462020-09-05 11:36:08 -06004666 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004667 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004668 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004669 return -EINVAL;
4670
Jens Axboed55e5f52019-12-11 16:12:15 -07004671 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4672 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004673 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004674 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004675 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004676}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004677
Pavel Begunkov889fca72021-02-10 00:03:09 +00004678static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004679{
4680 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004681 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004682 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004683 int ret;
4684
Jiufei Xuee697dee2020-06-10 13:41:59 +08004685 if (req->file->f_flags & O_NONBLOCK)
4686 req->flags |= REQ_F_NOWAIT;
4687
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004688 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004689 accept->addr_len, accept->flags,
4690 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004691 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004692 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004693 if (ret < 0) {
4694 if (ret == -ERESTARTSYS)
4695 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004696 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004697 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004698 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004699 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004700}
4701
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004702static int io_connect_prep_async(struct io_kiocb *req)
4703{
4704 struct io_async_connect *io = req->async_data;
4705 struct io_connect *conn = &req->connect;
4706
4707 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4708}
4709
Jens Axboe3529d8c2019-12-19 18:24:38 -07004710static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004711{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004712 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004713
Jens Axboe14587a462020-09-05 11:36:08 -06004714 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004715 return -EINVAL;
4716 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4717 return -EINVAL;
4718
Jens Axboe3529d8c2019-12-19 18:24:38 -07004719 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4720 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004721 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004722}
4723
Pavel Begunkov889fca72021-02-10 00:03:09 +00004724static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004725{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004726 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004727 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004728 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004729 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004730
Jens Axboee8c2bc12020-08-15 18:44:09 -07004731 if (req->async_data) {
4732 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004733 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004734 ret = move_addr_to_kernel(req->connect.addr,
4735 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004736 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004737 if (ret)
4738 goto out;
4739 io = &__io;
4740 }
4741
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004742 file_flags = force_nonblock ? O_NONBLOCK : 0;
4743
Jens Axboee8c2bc12020-08-15 18:44:09 -07004744 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004745 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004746 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004747 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004748 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004749 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004750 ret = -ENOMEM;
4751 goto out;
4752 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004753 io = req->async_data;
4754 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004755 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004756 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004757 if (ret == -ERESTARTSYS)
4758 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004759out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004760 if (ret < 0)
4761 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004762 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004763 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004764}
YueHaibing469956e2020-03-04 15:53:52 +08004765#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004766#define IO_NETOP_FN(op) \
4767static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4768{ \
4769 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004770}
4771
Jens Axboe99a10082021-02-19 09:35:19 -07004772#define IO_NETOP_PREP(op) \
4773IO_NETOP_FN(op) \
4774static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4775{ \
4776 return -EOPNOTSUPP; \
4777} \
4778
4779#define IO_NETOP_PREP_ASYNC(op) \
4780IO_NETOP_PREP(op) \
4781static int io_##op##_prep_async(struct io_kiocb *req) \
4782{ \
4783 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004784}
4785
Jens Axboe99a10082021-02-19 09:35:19 -07004786IO_NETOP_PREP_ASYNC(sendmsg);
4787IO_NETOP_PREP_ASYNC(recvmsg);
4788IO_NETOP_PREP_ASYNC(connect);
4789IO_NETOP_PREP(accept);
4790IO_NETOP_FN(send);
4791IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004792#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004793
Jens Axboed7718a92020-02-14 22:23:12 -07004794struct io_poll_table {
4795 struct poll_table_struct pt;
4796 struct io_kiocb *req;
4797 int error;
4798};
4799
Jens Axboed7718a92020-02-14 22:23:12 -07004800static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4801 __poll_t mask, task_work_func_t func)
4802{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004803 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004804
4805 /* for instances that support it check for an event match first: */
4806 if (mask && !(mask & poll->events))
4807 return 0;
4808
4809 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4810
4811 list_del_init(&poll->wait.entry);
4812
Jens Axboed7718a92020-02-14 22:23:12 -07004813 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004814 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004815 percpu_ref_get(&req->ctx->refs);
4816
Jens Axboed7718a92020-02-14 22:23:12 -07004817 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004818 * If this fails, then the task is exiting. When a task exits, the
4819 * work gets canceled, so just cancel this request as well instead
4820 * of executing it. We can't safely execute it anyway, as we may not
4821 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004822 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004823 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004824 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004825 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004826 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004827 }
Jens Axboed7718a92020-02-14 22:23:12 -07004828 return 1;
4829}
4830
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004831static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4832 __acquires(&req->ctx->completion_lock)
4833{
4834 struct io_ring_ctx *ctx = req->ctx;
4835
4836 if (!req->result && !READ_ONCE(poll->canceled)) {
4837 struct poll_table_struct pt = { ._key = poll->events };
4838
4839 req->result = vfs_poll(req->file, &pt) & poll->events;
4840 }
4841
4842 spin_lock_irq(&ctx->completion_lock);
4843 if (!req->result && !READ_ONCE(poll->canceled)) {
4844 add_wait_queue(poll->head, &poll->wait);
4845 return true;
4846 }
4847
4848 return false;
4849}
4850
Jens Axboed4e7cd32020-08-15 11:44:50 -07004851static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004852{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004853 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004854 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004855 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004856 return req->apoll->double_poll;
4857}
4858
4859static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4860{
4861 if (req->opcode == IORING_OP_POLL_ADD)
4862 return &req->poll;
4863 return &req->apoll->poll;
4864}
4865
4866static void io_poll_remove_double(struct io_kiocb *req)
4867{
4868 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004869
4870 lockdep_assert_held(&req->ctx->completion_lock);
4871
4872 if (poll && poll->head) {
4873 struct wait_queue_head *head = poll->head;
4874
4875 spin_lock(&head->lock);
4876 list_del_init(&poll->wait.entry);
4877 if (poll->wait.private)
4878 refcount_dec(&req->refs);
4879 poll->head = NULL;
4880 spin_unlock(&head->lock);
4881 }
4882}
4883
4884static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4885{
4886 struct io_ring_ctx *ctx = req->ctx;
4887
Jens Axboed4e7cd32020-08-15 11:44:50 -07004888 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004889 req->poll.done = true;
4890 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4891 io_commit_cqring(ctx);
4892}
4893
Jens Axboe18bceab2020-05-15 11:56:54 -06004894static void io_poll_task_func(struct callback_head *cb)
4895{
4896 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004897 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004898 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004899
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004900 if (io_poll_rewait(req, &req->poll)) {
4901 spin_unlock_irq(&ctx->completion_lock);
4902 } else {
4903 hash_del(&req->hash_node);
4904 io_poll_complete(req, req->result, 0);
4905 spin_unlock_irq(&ctx->completion_lock);
4906
4907 nxt = io_put_req_find_next(req);
4908 io_cqring_ev_posted(ctx);
4909 if (nxt)
4910 __io_req_task_submit(nxt);
4911 }
4912
Jens Axboe6d816e02020-08-11 08:04:14 -06004913 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004914}
4915
4916static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4917 int sync, void *key)
4918{
4919 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004920 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004921 __poll_t mask = key_to_poll(key);
4922
4923 /* for instances that support it check for an event match first: */
4924 if (mask && !(mask & poll->events))
4925 return 0;
4926
Jens Axboe8706e042020-09-28 08:38:54 -06004927 list_del_init(&wait->entry);
4928
Jens Axboe807abcb2020-07-17 17:09:27 -06004929 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004930 bool done;
4931
Jens Axboe807abcb2020-07-17 17:09:27 -06004932 spin_lock(&poll->head->lock);
4933 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004934 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004935 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004936 /* make sure double remove sees this as being gone */
4937 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004938 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06004939 if (!done) {
4940 /* use wait func handler, so it matches the rq type */
4941 poll->wait.func(&poll->wait, mode, sync, key);
4942 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004943 }
4944 refcount_dec(&req->refs);
4945 return 1;
4946}
4947
4948static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4949 wait_queue_func_t wake_func)
4950{
4951 poll->head = NULL;
4952 poll->done = false;
4953 poll->canceled = false;
4954 poll->events = events;
4955 INIT_LIST_HEAD(&poll->wait.entry);
4956 init_waitqueue_func_entry(&poll->wait, wake_func);
4957}
4958
4959static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004960 struct wait_queue_head *head,
4961 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004962{
4963 struct io_kiocb *req = pt->req;
4964
4965 /*
4966 * If poll->head is already set, it's because the file being polled
4967 * uses multiple waitqueues for poll handling (eg one for read, one
4968 * for write). Setup a separate io_poll_iocb if this happens.
4969 */
4970 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01004971 struct io_poll_iocb *poll_one = poll;
4972
Jens Axboe18bceab2020-05-15 11:56:54 -06004973 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004974 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004975 pt->error = -EINVAL;
4976 return;
4977 }
Jens Axboe1c3b3e62021-02-28 16:07:30 -07004978 /* double add on the same waitqueue head, ignore */
4979 if (poll->head == head)
4980 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06004981 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4982 if (!poll) {
4983 pt->error = -ENOMEM;
4984 return;
4985 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01004986 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06004987 refcount_inc(&req->refs);
4988 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004989 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004990 }
4991
4992 pt->error = 0;
4993 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004994
4995 if (poll->events & EPOLLEXCLUSIVE)
4996 add_wait_queue_exclusive(head, &poll->wait);
4997 else
4998 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004999}
5000
5001static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5002 struct poll_table_struct *p)
5003{
5004 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005005 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005006
Jens Axboe807abcb2020-07-17 17:09:27 -06005007 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005008}
5009
Jens Axboed7718a92020-02-14 22:23:12 -07005010static void io_async_task_func(struct callback_head *cb)
5011{
5012 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5013 struct async_poll *apoll = req->apoll;
5014 struct io_ring_ctx *ctx = req->ctx;
5015
5016 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5017
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005018 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005019 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005020 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005021 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005022 }
5023
Jens Axboe31067252020-05-17 17:43:31 -06005024 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005025 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005026 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005027
Jens Axboed4e7cd32020-08-15 11:44:50 -07005028 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005029 spin_unlock_irq(&ctx->completion_lock);
5030
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005031 if (!READ_ONCE(apoll->poll.canceled))
5032 __io_req_task_submit(req);
5033 else
5034 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005035
Jens Axboe6d816e02020-08-11 08:04:14 -06005036 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005037 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005038 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005039}
5040
5041static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5042 void *key)
5043{
5044 struct io_kiocb *req = wait->private;
5045 struct io_poll_iocb *poll = &req->apoll->poll;
5046
5047 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5048 key_to_poll(key));
5049
5050 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5051}
5052
5053static void io_poll_req_insert(struct io_kiocb *req)
5054{
5055 struct io_ring_ctx *ctx = req->ctx;
5056 struct hlist_head *list;
5057
5058 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5059 hlist_add_head(&req->hash_node, list);
5060}
5061
5062static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5063 struct io_poll_iocb *poll,
5064 struct io_poll_table *ipt, __poll_t mask,
5065 wait_queue_func_t wake_func)
5066 __acquires(&ctx->completion_lock)
5067{
5068 struct io_ring_ctx *ctx = req->ctx;
5069 bool cancel = false;
5070
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005071 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005072 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005073 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005074 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005075
5076 ipt->pt._key = mask;
5077 ipt->req = req;
5078 ipt->error = -EINVAL;
5079
Jens Axboed7718a92020-02-14 22:23:12 -07005080 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5081
5082 spin_lock_irq(&ctx->completion_lock);
5083 if (likely(poll->head)) {
5084 spin_lock(&poll->head->lock);
5085 if (unlikely(list_empty(&poll->wait.entry))) {
5086 if (ipt->error)
5087 cancel = true;
5088 ipt->error = 0;
5089 mask = 0;
5090 }
5091 if (mask || ipt->error)
5092 list_del_init(&poll->wait.entry);
5093 else if (cancel)
5094 WRITE_ONCE(poll->canceled, true);
5095 else if (!poll->done) /* actually waiting for an event */
5096 io_poll_req_insert(req);
5097 spin_unlock(&poll->head->lock);
5098 }
5099
5100 return mask;
5101}
5102
5103static bool io_arm_poll_handler(struct io_kiocb *req)
5104{
5105 const struct io_op_def *def = &io_op_defs[req->opcode];
5106 struct io_ring_ctx *ctx = req->ctx;
5107 struct async_poll *apoll;
5108 struct io_poll_table ipt;
5109 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005110 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005111
5112 if (!req->file || !file_can_poll(req->file))
5113 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005114 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005115 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005116 if (def->pollin)
5117 rw = READ;
5118 else if (def->pollout)
5119 rw = WRITE;
5120 else
5121 return false;
5122 /* if we can't nonblock try, then no point in arming a poll handler */
5123 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005124 return false;
5125
5126 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5127 if (unlikely(!apoll))
5128 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005129 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005130
5131 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005132 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005133
Nathan Chancellor8755d972020-03-02 16:01:19 -07005134 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005135 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005136 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005137 if (def->pollout)
5138 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005139
5140 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5141 if ((req->opcode == IORING_OP_RECVMSG) &&
5142 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5143 mask &= ~POLLIN;
5144
Jens Axboed7718a92020-02-14 22:23:12 -07005145 mask |= POLLERR | POLLPRI;
5146
5147 ipt.pt._qproc = io_async_queue_proc;
5148
5149 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5150 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005151 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005152 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005153 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005154 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005155 kfree(apoll);
5156 return false;
5157 }
5158 spin_unlock_irq(&ctx->completion_lock);
5159 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5160 apoll->poll.events);
5161 return true;
5162}
5163
5164static bool __io_poll_remove_one(struct io_kiocb *req,
5165 struct io_poll_iocb *poll)
5166{
Jens Axboeb41e9852020-02-17 09:52:41 -07005167 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005168
5169 spin_lock(&poll->head->lock);
5170 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005171 if (!list_empty(&poll->wait.entry)) {
5172 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005173 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005174 }
5175 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005176 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005177 return do_complete;
5178}
5179
5180static bool io_poll_remove_one(struct io_kiocb *req)
5181{
5182 bool do_complete;
5183
Jens Axboed4e7cd32020-08-15 11:44:50 -07005184 io_poll_remove_double(req);
5185
Jens Axboed7718a92020-02-14 22:23:12 -07005186 if (req->opcode == IORING_OP_POLL_ADD) {
5187 do_complete = __io_poll_remove_one(req, &req->poll);
5188 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005189 struct async_poll *apoll = req->apoll;
5190
Jens Axboed7718a92020-02-14 22:23:12 -07005191 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005192 do_complete = __io_poll_remove_one(req, &apoll->poll);
5193 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005194 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005195 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005196 kfree(apoll);
5197 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005198 }
5199
Jens Axboeb41e9852020-02-17 09:52:41 -07005200 if (do_complete) {
5201 io_cqring_fill_event(req, -ECANCELED);
5202 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005203 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005204 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005205 }
5206
5207 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005208}
5209
Jens Axboe76e1b642020-09-26 15:05:03 -06005210/*
5211 * Returns true if we found and killed one or more poll requests
5212 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005213static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5214 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005215{
Jens Axboe78076bb2019-12-04 19:56:40 -07005216 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005217 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005218 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005219
5220 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005221 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5222 struct hlist_head *list;
5223
5224 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005225 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005226 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005227 posted += io_poll_remove_one(req);
5228 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005229 }
5230 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005231
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005232 if (posted)
5233 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005234
5235 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005236}
5237
Jens Axboe47f46762019-11-09 17:43:02 -07005238static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5239{
Jens Axboe78076bb2019-12-04 19:56:40 -07005240 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005241 struct io_kiocb *req;
5242
Jens Axboe78076bb2019-12-04 19:56:40 -07005243 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5244 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005245 if (sqe_addr != req->user_data)
5246 continue;
5247 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005248 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005249 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005250 }
5251
5252 return -ENOENT;
5253}
5254
Jens Axboe3529d8c2019-12-19 18:24:38 -07005255static int io_poll_remove_prep(struct io_kiocb *req,
5256 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005257{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005258 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5259 return -EINVAL;
5260 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5261 sqe->poll_events)
5262 return -EINVAL;
5263
Pavel Begunkov018043b2020-10-27 23:17:18 +00005264 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005265 return 0;
5266}
5267
5268/*
5269 * Find a running poll command that matches one specified in sqe->addr,
5270 * and remove it if found.
5271 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005272static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005273{
5274 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005275 int ret;
5276
Jens Axboe221c5eb2019-01-17 09:41:58 -07005277 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005278 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005279 spin_unlock_irq(&ctx->completion_lock);
5280
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005281 if (ret < 0)
5282 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005283 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005284 return 0;
5285}
5286
Jens Axboe221c5eb2019-01-17 09:41:58 -07005287static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5288 void *key)
5289{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005290 struct io_kiocb *req = wait->private;
5291 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005292
Jens Axboed7718a92020-02-14 22:23:12 -07005293 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005294}
5295
Jens Axboe221c5eb2019-01-17 09:41:58 -07005296static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5297 struct poll_table_struct *p)
5298{
5299 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5300
Jens Axboee8c2bc12020-08-15 18:44:09 -07005301 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005302}
5303
Jens Axboe3529d8c2019-12-19 18:24:38 -07005304static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005305{
5306 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005307 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005308
5309 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5310 return -EINVAL;
5311 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5312 return -EINVAL;
5313
Jiufei Xue5769a352020-06-17 17:53:55 +08005314 events = READ_ONCE(sqe->poll32_events);
5315#ifdef __BIG_ENDIAN
5316 events = swahw32(events);
5317#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005318 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5319 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005320 return 0;
5321}
5322
Pavel Begunkov61e98202021-02-10 00:03:08 +00005323static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005324{
5325 struct io_poll_iocb *poll = &req->poll;
5326 struct io_ring_ctx *ctx = req->ctx;
5327 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005328 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005329
Jens Axboed7718a92020-02-14 22:23:12 -07005330 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005331
Jens Axboed7718a92020-02-14 22:23:12 -07005332 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5333 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005334
Jens Axboe8c838782019-03-12 15:48:16 -06005335 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005336 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005337 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005338 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005339 spin_unlock_irq(&ctx->completion_lock);
5340
Jens Axboe8c838782019-03-12 15:48:16 -06005341 if (mask) {
5342 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005343 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005344 }
Jens Axboe8c838782019-03-12 15:48:16 -06005345 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005346}
5347
Jens Axboe5262f562019-09-17 12:26:57 -06005348static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5349{
Jens Axboead8a48a2019-11-15 08:49:11 -07005350 struct io_timeout_data *data = container_of(timer,
5351 struct io_timeout_data, timer);
5352 struct io_kiocb *req = data->req;
5353 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005354 unsigned long flags;
5355
Jens Axboe5262f562019-09-17 12:26:57 -06005356 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005357 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005358 atomic_set(&req->ctx->cq_timeouts,
5359 atomic_read(&req->ctx->cq_timeouts) + 1);
5360
Jens Axboe78e19bb2019-11-06 15:21:34 -07005361 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005362 io_commit_cqring(ctx);
5363 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5364
5365 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005366 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005367 io_put_req(req);
5368 return HRTIMER_NORESTART;
5369}
5370
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005371static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5372 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005373{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005374 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005375 struct io_kiocb *req;
5376 int ret = -ENOENT;
5377
5378 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5379 if (user_data == req->user_data) {
5380 ret = 0;
5381 break;
5382 }
5383 }
5384
5385 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005386 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005387
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005388 io = req->async_data;
5389 ret = hrtimer_try_to_cancel(&io->timer);
5390 if (ret == -1)
5391 return ERR_PTR(-EALREADY);
5392 list_del_init(&req->timeout.list);
5393 return req;
5394}
5395
5396static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5397{
5398 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5399
5400 if (IS_ERR(req))
5401 return PTR_ERR(req);
5402
5403 req_set_fail_links(req);
5404 io_cqring_fill_event(req, -ECANCELED);
5405 io_put_req_deferred(req, 1);
5406 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005407}
5408
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005409static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5410 struct timespec64 *ts, enum hrtimer_mode mode)
5411{
5412 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5413 struct io_timeout_data *data;
5414
5415 if (IS_ERR(req))
5416 return PTR_ERR(req);
5417
5418 req->timeout.off = 0; /* noseq */
5419 data = req->async_data;
5420 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5421 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5422 data->timer.function = io_timeout_fn;
5423 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5424 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005425}
5426
Jens Axboe3529d8c2019-12-19 18:24:38 -07005427static int io_timeout_remove_prep(struct io_kiocb *req,
5428 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005429{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005430 struct io_timeout_rem *tr = &req->timeout_rem;
5431
Jens Axboeb29472e2019-12-17 18:50:29 -07005432 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5433 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005434 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5435 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005436 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005437 return -EINVAL;
5438
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005439 tr->addr = READ_ONCE(sqe->addr);
5440 tr->flags = READ_ONCE(sqe->timeout_flags);
5441 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5442 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5443 return -EINVAL;
5444 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5445 return -EFAULT;
5446 } else if (tr->flags) {
5447 /* timeout removal doesn't support flags */
5448 return -EINVAL;
5449 }
5450
Jens Axboeb29472e2019-12-17 18:50:29 -07005451 return 0;
5452}
5453
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005454static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5455{
5456 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5457 : HRTIMER_MODE_REL;
5458}
5459
Jens Axboe11365042019-10-16 09:08:32 -06005460/*
5461 * Remove or update an existing timeout command
5462 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005463static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005464{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005465 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005466 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005467 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005468
Jens Axboe11365042019-10-16 09:08:32 -06005469 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005470 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005471 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005472 else
5473 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5474 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005475
Jens Axboe47f46762019-11-09 17:43:02 -07005476 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005477 io_commit_cqring(ctx);
5478 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005479 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005480 if (ret < 0)
5481 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005482 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005483 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005484}
5485
Jens Axboe3529d8c2019-12-19 18:24:38 -07005486static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005487 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005488{
Jens Axboead8a48a2019-11-15 08:49:11 -07005489 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005490 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005491 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005492
Jens Axboead8a48a2019-11-15 08:49:11 -07005493 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005494 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005495 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005496 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005497 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005498 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005499 flags = READ_ONCE(sqe->timeout_flags);
5500 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005501 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005502
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005503 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005504
Jens Axboee8c2bc12020-08-15 18:44:09 -07005505 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005506 return -ENOMEM;
5507
Jens Axboee8c2bc12020-08-15 18:44:09 -07005508 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005509 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005510
5511 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005512 return -EFAULT;
5513
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005514 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005515 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5516 return 0;
5517}
5518
Pavel Begunkov61e98202021-02-10 00:03:08 +00005519static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005520{
Jens Axboead8a48a2019-11-15 08:49:11 -07005521 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005522 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005523 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005524 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005525
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005526 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005527
Jens Axboe5262f562019-09-17 12:26:57 -06005528 /*
5529 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005530 * timeout event to be satisfied. If it isn't set, then this is
5531 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005532 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005533 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005534 entry = ctx->timeout_list.prev;
5535 goto add;
5536 }
Jens Axboe5262f562019-09-17 12:26:57 -06005537
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005538 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5539 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005540
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005541 /* Update the last seq here in case io_flush_timeouts() hasn't.
5542 * This is safe because ->completion_lock is held, and submissions
5543 * and completions are never mixed in the same ->completion_lock section.
5544 */
5545 ctx->cq_last_tm_flush = tail;
5546
Jens Axboe5262f562019-09-17 12:26:57 -06005547 /*
5548 * Insertion sort, ensuring the first entry in the list is always
5549 * the one we need first.
5550 */
Jens Axboe5262f562019-09-17 12:26:57 -06005551 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005552 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5553 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005554
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005555 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005556 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005557 /* nxt.seq is behind @tail, otherwise would've been completed */
5558 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005559 break;
5560 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005561add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005562 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005563 data->timer.function = io_timeout_fn;
5564 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005565 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005566 return 0;
5567}
5568
Jens Axboe62755e32019-10-28 21:49:21 -06005569static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005570{
Jens Axboe62755e32019-10-28 21:49:21 -06005571 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005572
Jens Axboe62755e32019-10-28 21:49:21 -06005573 return req->user_data == (unsigned long) data;
5574}
5575
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005576static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005577{
Jens Axboe62755e32019-10-28 21:49:21 -06005578 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005579 int ret = 0;
5580
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005581 if (!tctx->io_wq)
5582 return -ENOENT;
5583
5584 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005585 switch (cancel_ret) {
5586 case IO_WQ_CANCEL_OK:
5587 ret = 0;
5588 break;
5589 case IO_WQ_CANCEL_RUNNING:
5590 ret = -EALREADY;
5591 break;
5592 case IO_WQ_CANCEL_NOTFOUND:
5593 ret = -ENOENT;
5594 break;
5595 }
5596
Jens Axboee977d6d2019-11-05 12:39:45 -07005597 return ret;
5598}
5599
Jens Axboe47f46762019-11-09 17:43:02 -07005600static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5601 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005602 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005603{
5604 unsigned long flags;
5605 int ret;
5606
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005607 ret = io_async_cancel_one(req->task->io_uring,
5608 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005609 if (ret != -ENOENT) {
5610 spin_lock_irqsave(&ctx->completion_lock, flags);
5611 goto done;
5612 }
5613
5614 spin_lock_irqsave(&ctx->completion_lock, flags);
5615 ret = io_timeout_cancel(ctx, sqe_addr);
5616 if (ret != -ENOENT)
5617 goto done;
5618 ret = io_poll_cancel(ctx, sqe_addr);
5619done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005620 if (!ret)
5621 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005622 io_cqring_fill_event(req, ret);
5623 io_commit_cqring(ctx);
5624 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5625 io_cqring_ev_posted(ctx);
5626
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005627 if (ret < 0)
5628 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005629 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005630}
5631
Jens Axboe3529d8c2019-12-19 18:24:38 -07005632static int io_async_cancel_prep(struct io_kiocb *req,
5633 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005634{
Jens Axboefbf23842019-12-17 18:45:56 -07005635 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005636 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005637 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5638 return -EINVAL;
5639 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005640 return -EINVAL;
5641
Jens Axboefbf23842019-12-17 18:45:56 -07005642 req->cancel.addr = READ_ONCE(sqe->addr);
5643 return 0;
5644}
5645
Pavel Begunkov61e98202021-02-10 00:03:08 +00005646static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005647{
5648 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005649
Pavel Begunkov014db002020-03-03 21:33:12 +03005650 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005651 return 0;
5652}
5653
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005654static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005655 const struct io_uring_sqe *sqe)
5656{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005657 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5658 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005659 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5660 return -EINVAL;
5661 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005662 return -EINVAL;
5663
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005664 req->rsrc_update.offset = READ_ONCE(sqe->off);
5665 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5666 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005667 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005668 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005669 return 0;
5670}
5671
Pavel Begunkov889fca72021-02-10 00:03:09 +00005672static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005673{
5674 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005675 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005676 int ret;
5677
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005678 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005679 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005680
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005681 up.offset = req->rsrc_update.offset;
5682 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005683
5684 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005685 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005686 mutex_unlock(&ctx->uring_lock);
5687
5688 if (ret < 0)
5689 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005690 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005691 return 0;
5692}
5693
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005694static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005695{
Jens Axboed625c6e2019-12-17 19:53:05 -07005696 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005697 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005698 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005699 case IORING_OP_READV:
5700 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005701 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005702 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005703 case IORING_OP_WRITEV:
5704 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005705 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005706 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005707 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005708 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005709 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005710 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005711 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005712 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005713 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005714 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005715 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005716 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005717 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005718 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005719 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005720 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005721 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005722 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005723 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005724 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005725 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005726 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005727 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005728 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005729 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005730 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005731 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005732 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005733 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005734 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005735 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005736 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005737 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005738 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005739 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005740 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005741 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005742 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005743 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005744 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005745 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005746 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005747 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005748 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005749 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005750 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005751 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005752 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005753 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005754 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005755 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005756 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005757 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005758 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005759 case IORING_OP_SHUTDOWN:
5760 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005761 case IORING_OP_RENAMEAT:
5762 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005763 case IORING_OP_UNLINKAT:
5764 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005765 }
5766
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005767 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5768 req->opcode);
5769 return-EINVAL;
5770}
5771
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005772static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005773{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005774 switch (req->opcode) {
5775 case IORING_OP_READV:
5776 case IORING_OP_READ_FIXED:
5777 case IORING_OP_READ:
5778 return io_rw_prep_async(req, READ);
5779 case IORING_OP_WRITEV:
5780 case IORING_OP_WRITE_FIXED:
5781 case IORING_OP_WRITE:
5782 return io_rw_prep_async(req, WRITE);
5783 case IORING_OP_SENDMSG:
5784 case IORING_OP_SEND:
5785 return io_sendmsg_prep_async(req);
5786 case IORING_OP_RECVMSG:
5787 case IORING_OP_RECV:
5788 return io_recvmsg_prep_async(req);
5789 case IORING_OP_CONNECT:
5790 return io_connect_prep_async(req);
5791 }
5792 return 0;
5793}
5794
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005795static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005796{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005797 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005798 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005799 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005800 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005801 return 0;
5802 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005803 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005804 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805}
5806
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005807static u32 io_get_sequence(struct io_kiocb *req)
5808{
5809 struct io_kiocb *pos;
5810 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005811 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005812
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005813 io_for_each_link(pos, req)
5814 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005815
5816 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5817 return total_submitted - nr_reqs;
5818}
5819
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005820static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005821{
5822 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005823 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005824 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005825 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005826
5827 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005828 if (likely(list_empty_careful(&ctx->defer_list) &&
5829 !(req->flags & REQ_F_IO_DRAIN)))
5830 return 0;
5831
5832 seq = io_get_sequence(req);
5833 /* Still a chance to pass the sequence check */
5834 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005835 return 0;
5836
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005837 ret = io_req_defer_prep(req);
5838 if (ret)
5839 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005840 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005841 de = kmalloc(sizeof(*de), GFP_KERNEL);
5842 if (!de)
5843 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005844
5845 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005846 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005847 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005848 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005849 io_queue_async_work(req);
5850 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005851 }
5852
5853 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005854 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005855 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005856 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005857 spin_unlock_irq(&ctx->completion_lock);
5858 return -EIOCBQUEUED;
5859}
Jens Axboeedafcce2019-01-09 09:16:05 -07005860
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005861static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005862{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005863 if (req->flags & REQ_F_BUFFER_SELECTED) {
5864 switch (req->opcode) {
5865 case IORING_OP_READV:
5866 case IORING_OP_READ_FIXED:
5867 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005868 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005869 break;
5870 case IORING_OP_RECVMSG:
5871 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005872 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005873 break;
5874 }
5875 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005876 }
5877
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005878 if (req->flags & REQ_F_NEED_CLEANUP) {
5879 switch (req->opcode) {
5880 case IORING_OP_READV:
5881 case IORING_OP_READ_FIXED:
5882 case IORING_OP_READ:
5883 case IORING_OP_WRITEV:
5884 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005885 case IORING_OP_WRITE: {
5886 struct io_async_rw *io = req->async_data;
5887 if (io->free_iovec)
5888 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005889 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005890 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005891 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005892 case IORING_OP_SENDMSG: {
5893 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005894
5895 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005896 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005897 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005898 case IORING_OP_SPLICE:
5899 case IORING_OP_TEE:
5900 io_put_file(req, req->splice.file_in,
5901 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5902 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005903 case IORING_OP_OPENAT:
5904 case IORING_OP_OPENAT2:
5905 if (req->open.filename)
5906 putname(req->open.filename);
5907 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06005908 case IORING_OP_RENAMEAT:
5909 putname(req->rename.oldpath);
5910 putname(req->rename.newpath);
5911 break;
Jens Axboe14a11432020-09-28 14:27:37 -06005912 case IORING_OP_UNLINKAT:
5913 putname(req->unlink.filename);
5914 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005915 }
5916 req->flags &= ~REQ_F_NEED_CLEANUP;
5917 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005918}
5919
Pavel Begunkov889fca72021-02-10 00:03:09 +00005920static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07005921{
Jens Axboeedafcce2019-01-09 09:16:05 -07005922 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07005923 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07005924 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005925
Jens Axboe5730b272021-02-27 15:57:30 -07005926 if (req->work.personality) {
5927 const struct cred *new_creds;
5928
5929 if (!(issue_flags & IO_URING_F_NONBLOCK))
5930 mutex_lock(&ctx->uring_lock);
5931 new_creds = idr_find(&ctx->personality_idr, req->work.personality);
5932 if (!(issue_flags & IO_URING_F_NONBLOCK))
5933 mutex_unlock(&ctx->uring_lock);
5934 if (!new_creds)
5935 return -EINVAL;
5936 creds = override_creds(new_creds);
5937 }
5938
Jens Axboed625c6e2019-12-17 19:53:05 -07005939 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005940 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005941 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07005942 break;
5943 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005944 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005945 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005946 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005947 break;
5948 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005949 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005950 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005951 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005952 break;
5953 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005954 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005955 break;
5956 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005957 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958 break;
5959 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005960 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005961 break;
5962 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005963 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005964 break;
5965 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005966 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005967 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005968 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005969 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005970 break;
5971 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005972 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005973 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005974 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005975 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005976 break;
5977 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005978 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005979 break;
5980 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005981 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005982 break;
5983 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005984 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005985 break;
5986 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005987 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005988 break;
5989 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005990 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005991 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005992 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005993 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07005994 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005995 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005996 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005997 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005998 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005999 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006000 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006001 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006002 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006003 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006004 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006005 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006006 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006007 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006008 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006009 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006010 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006011 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006012 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006013 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006014 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006015 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006016 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006017 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006018 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006019 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006020 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006021 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006022 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006023 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006024 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006025 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006026 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006027 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006028 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006029 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006030 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006031 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006032 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006033 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006034 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006035 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006036 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006037 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006038 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006039 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006040 default:
6041 ret = -EINVAL;
6042 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006043 }
6044
Jens Axboe5730b272021-02-27 15:57:30 -07006045 if (creds)
6046 revert_creds(creds);
6047
Jens Axboe2b188cc2019-01-07 10:46:33 -07006048 if (ret)
6049 return ret;
6050
Jens Axboeb5325762020-05-19 21:20:27 -06006051 /* If the op doesn't have a file, we're not polling for it */
6052 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006053 const bool in_async = io_wq_current_is_worker();
6054
Jens Axboe11ba8202020-01-15 21:51:17 -07006055 /* workqueue context doesn't hold uring_lock, grab it now */
6056 if (in_async)
6057 mutex_lock(&ctx->uring_lock);
6058
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006059 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006060
6061 if (in_async)
6062 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063 }
6064
6065 return 0;
6066}
6067
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006068static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006069{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006071 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006072 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006073
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006074 timeout = io_prep_linked_timeout(req);
6075 if (timeout)
6076 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006077
Jens Axboe4014d942021-01-19 15:53:54 -07006078 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006079 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006080
Jens Axboe561fb042019-10-24 07:25:42 -06006081 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006082 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006083 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006084 /*
6085 * We can get EAGAIN for polled IO even though we're
6086 * forcing a sync submission from here, since we can't
6087 * wait for request slots on the block side.
6088 */
6089 if (ret != -EAGAIN)
6090 break;
6091 cond_resched();
6092 } while (1);
6093 }
Jens Axboe31b51512019-01-18 22:56:34 -07006094
Pavel Begunkova3df76982021-02-18 22:32:52 +00006095 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006096 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006097 /* io-wq is going to take one down */
6098 refcount_inc(&req->refs);
6099 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006100 }
Jens Axboe31b51512019-01-18 22:56:34 -07006101}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006102
Jens Axboe65e19f52019-10-26 07:20:21 -06006103static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6104 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006105{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006106 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006107
Jens Axboe05f3fb32019-12-09 11:22:50 -07006108 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006109 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006110}
6111
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006112static struct file *io_file_get(struct io_submit_state *state,
6113 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006114{
6115 struct io_ring_ctx *ctx = req->ctx;
6116 struct file *file;
6117
6118 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006119 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006120 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006121 fd = array_index_nospec(fd, ctx->nr_user_files);
6122 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006123 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006124 } else {
6125 trace_io_uring_file_get(ctx, fd);
6126 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006127 }
6128
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006129 if (file && unlikely(file->f_op == &io_uring_fops))
6130 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006131 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006132}
6133
Jens Axboe2665abf2019-11-05 12:40:47 -07006134static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6135{
Jens Axboead8a48a2019-11-15 08:49:11 -07006136 struct io_timeout_data *data = container_of(timer,
6137 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006138 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006139 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006140 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006141
6142 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006143 prev = req->timeout.head;
6144 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006145
6146 /*
6147 * We don't expect the list to be empty, that will only happen if we
6148 * race with the completion of the linked work.
6149 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006150 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006151 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006152 else
6153 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006154 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6155
6156 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006157 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006158 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006159 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006160 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006161 io_req_complete_post(req, -ETIME, 0);
6162 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006163 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006164 return HRTIMER_NORESTART;
6165}
6166
Jens Axboe7271ef32020-08-10 09:55:22 -06006167static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006168{
Jens Axboe76a46e02019-11-10 23:34:16 -07006169 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006170 * If the back reference is NULL, then our linked request finished
6171 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006172 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006173 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006174 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006175
Jens Axboead8a48a2019-11-15 08:49:11 -07006176 data->timer.function = io_link_timeout_fn;
6177 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6178 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006179 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006180}
6181
6182static void io_queue_linked_timeout(struct io_kiocb *req)
6183{
6184 struct io_ring_ctx *ctx = req->ctx;
6185
6186 spin_lock_irq(&ctx->completion_lock);
6187 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006188 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006189
Jens Axboe2665abf2019-11-05 12:40:47 -07006190 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006191 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006192}
6193
Jens Axboead8a48a2019-11-15 08:49:11 -07006194static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006195{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006196 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006198 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6199 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006200 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006201
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006202 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006203 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006204 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006205 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006206}
6207
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006208static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006210 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006211 int ret;
6212
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006213 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006214
6215 /*
6216 * We async punt it if the file wasn't marked NOWAIT, or if the file
6217 * doesn't support non-blocking read/write attempts
6218 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006219 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006220 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006221 /*
6222 * Queued up for async execution, worker will release
6223 * submit reference when the iocb is actually submitted.
6224 */
6225 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006227 } else if (likely(!ret)) {
6228 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006229 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006230 struct io_ring_ctx *ctx = req->ctx;
6231 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006232
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006233 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006234 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006235 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006236 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006237 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006238 }
6239 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006240 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006241 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006242 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006243 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006244 if (linked_timeout)
6245 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246}
6247
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006248static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006249{
6250 int ret;
6251
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006252 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006253 if (ret) {
6254 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006255fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006256 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006257 io_put_req(req);
6258 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006259 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006260 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006261 ret = io_req_defer_prep(req);
6262 if (unlikely(ret))
6263 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006264 io_queue_async_work(req);
6265 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006266 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006267 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006268}
6269
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006270/*
6271 * Check SQE restrictions (opcode and flags).
6272 *
6273 * Returns 'true' if SQE is allowed, 'false' otherwise.
6274 */
6275static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6276 struct io_kiocb *req,
6277 unsigned int sqe_flags)
6278{
6279 if (!ctx->restricted)
6280 return true;
6281
6282 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6283 return false;
6284
6285 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6286 ctx->restrictions.sqe_flags_required)
6287 return false;
6288
6289 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6290 ctx->restrictions.sqe_flags_required))
6291 return false;
6292
6293 return true;
6294}
6295
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006296static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006297 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006298{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006299 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006300 unsigned int sqe_flags;
Jens Axboe5730b272021-02-27 15:57:30 -07006301 int ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006302
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006303 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006304 /* same numerical values with corresponding REQ_F_*, safe to copy */
6305 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006306 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006307 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006308 req->file = NULL;
6309 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006310 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006311 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006312 /* one is dropped after submission, the other at completion */
6313 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006314 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006315 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006316
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006317 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006318 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6319 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006320 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006321 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006322
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006323 if (unlikely(req->opcode >= IORING_OP_LAST))
6324 return -EINVAL;
6325
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006326 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6327 return -EACCES;
6328
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006329 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6330 !io_op_defs[req->opcode].buffer_select)
6331 return -EOPNOTSUPP;
6332
Jens Axboe5730b272021-02-27 15:57:30 -07006333 req->work.list.next = NULL;
6334 req->work.flags = 0;
6335 req->work.personality = READ_ONCE(sqe->personality);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006336 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006337
Jens Axboe27926b62020-10-28 09:33:23 -06006338 /*
6339 * Plug now if we have more than 1 IO left after this, and the target
6340 * is potentially a read/write to block based storage.
6341 */
6342 if (!state->plug_started && state->ios_left > 1 &&
6343 io_op_defs[req->opcode].plug) {
6344 blk_start_plug(&state->plug);
6345 state->plug_started = true;
6346 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006347
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006348 if (io_op_defs[req->opcode].needs_file) {
6349 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006350
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006351 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006352 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006353 ret = -EBADF;
6354 }
6355
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006356 state->ios_left--;
6357 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006358}
6359
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006360static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006361 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006362{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006363 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006364 int ret;
6365
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006366 ret = io_init_req(ctx, req, sqe);
6367 if (unlikely(ret)) {
6368fail_req:
6369 io_put_req(req);
6370 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006371 if (link->head) {
6372 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006373 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006374 io_put_req(link->head);
6375 io_req_complete(link->head, -ECANCELED);
6376 link->head = NULL;
6377 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006378 return ret;
6379 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006380 ret = io_req_prep(req, sqe);
6381 if (unlikely(ret))
6382 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006383
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006384 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006385 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6386 true, ctx->flags & IORING_SETUP_SQPOLL);
6387
Jens Axboe6c271ce2019-01-10 11:22:30 -07006388 /*
6389 * If we already have a head request, queue this one for async
6390 * submittal once the head completes. If we don't have a head but
6391 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6392 * submitted sync once the chain is complete. If none of those
6393 * conditions are true (normal request), then just queue it.
6394 */
6395 if (link->head) {
6396 struct io_kiocb *head = link->head;
6397
6398 /*
6399 * Taking sequential execution of a link, draining both sides
6400 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6401 * requests in the link. So, it drains the head and the
6402 * next after the link request. The last one is done via
6403 * drain_next flag to persist the effect across calls.
6404 */
6405 if (req->flags & REQ_F_IO_DRAIN) {
6406 head->flags |= REQ_F_IO_DRAIN;
6407 ctx->drain_next = 1;
6408 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006409 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006410 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006411 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006412 trace_io_uring_link(ctx, req, head);
6413 link->last->link = req;
6414 link->last = req;
6415
6416 /* last request of a link, enqueue the link */
6417 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006418 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006419 link->head = NULL;
6420 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006421 } else {
6422 if (unlikely(ctx->drain_next)) {
6423 req->flags |= REQ_F_IO_DRAIN;
6424 ctx->drain_next = 0;
6425 }
6426 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006427 link->head = req;
6428 link->last = req;
6429 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006430 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006431 }
6432 }
6433
6434 return 0;
6435}
6436
6437/*
6438 * Batched submission is done, ensure local IO is flushed out.
6439 */
6440static void io_submit_state_end(struct io_submit_state *state,
6441 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006442{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006443 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006444 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006445 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006446 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006447 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006448 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006449 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006450}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006451
Jens Axboe9e645e112019-05-10 16:07:28 -06006452/*
6453 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006454 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006455static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006456 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006457{
6458 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006459 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006460 /* set only head, no need to init link_last in advance */
6461 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006462}
6463
Jens Axboe193155c2020-02-22 23:22:19 -07006464static void io_commit_sqring(struct io_ring_ctx *ctx)
6465{
Jens Axboe75c6a032020-01-28 10:15:23 -07006466 struct io_rings *rings = ctx->rings;
6467
6468 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006469 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006470 * since once we write the new head, the application could
6471 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006472 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006473 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006474}
6475
Jens Axboe9e645e112019-05-10 16:07:28 -06006476/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006477 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006478 * that is mapped by userspace. This means that care needs to be taken to
6479 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006480 * being a good citizen. If members of the sqe are validated and then later
6481 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006482 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006483 */
6484static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006485{
6486 u32 *sq_array = ctx->sq_array;
6487 unsigned head;
6488
6489 /*
6490 * The cached sq head (or cq tail) serves two purposes:
6491 *
6492 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006493 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006494 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006495 * though the application is the one updating it.
6496 */
6497 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6498 if (likely(head < ctx->sq_entries))
6499 return &ctx->sq_sqes[head];
6500
6501 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006502 ctx->cached_sq_dropped++;
6503 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6504 return NULL;
6505}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006506
Jens Axboe0f212202020-09-13 13:09:39 -06006507static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006508{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006509 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006510
Jens Axboec4a2ed72019-11-21 21:01:26 -07006511 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006512 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006513 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006514 return -EBUSY;
6515 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006516
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006517 /* make sure SQ entry isn't read before tail */
6518 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006519
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006520 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6521 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006522
Jens Axboed8a6df12020-10-15 16:24:45 -06006523 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006524 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006525 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006526
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006527 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006528 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006529 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006530
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006531 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006532 if (unlikely(!req)) {
6533 if (!submitted)
6534 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006535 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006536 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006537 sqe = io_get_sqe(ctx);
6538 if (unlikely(!sqe)) {
6539 kmem_cache_free(req_cachep, req);
6540 break;
6541 }
Jens Axboed3656342019-12-18 09:50:26 -07006542 /* will complete beyond this point, count as submitted */
6543 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006544 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006545 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006546 }
6547
Pavel Begunkov9466f432020-01-25 22:34:01 +03006548 if (unlikely(submitted != nr)) {
6549 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006550 struct io_uring_task *tctx = current->io_uring;
6551 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006552
Jens Axboed8a6df12020-10-15 16:24:45 -06006553 percpu_ref_put_many(&ctx->refs, unused);
6554 percpu_counter_sub(&tctx->inflight, unused);
6555 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006556 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006557
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006558 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006559 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6560 io_commit_sqring(ctx);
6561
Jens Axboe6c271ce2019-01-10 11:22:30 -07006562 return submitted;
6563}
6564
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006565static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6566{
6567 /* Tell userspace we may need a wakeup call */
6568 spin_lock_irq(&ctx->completion_lock);
6569 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6570 spin_unlock_irq(&ctx->completion_lock);
6571}
6572
6573static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6574{
6575 spin_lock_irq(&ctx->completion_lock);
6576 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6577 spin_unlock_irq(&ctx->completion_lock);
6578}
6579
Xiaoguang Wang08369242020-11-03 14:15:59 +08006580static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006581{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006582 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006583 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584
Jens Axboec8d1ba52020-09-14 11:07:26 -06006585 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006586 /* if we're handling multiple rings, cap submit size for fairness */
6587 if (cap_entries && to_submit > 8)
6588 to_submit = 8;
6589
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006590 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6591 unsigned nr_events = 0;
6592
Xiaoguang Wang08369242020-11-03 14:15:59 +08006593 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006594 if (!list_empty(&ctx->iopoll_list))
6595 io_do_iopoll(ctx, &nr_events, 0);
6596
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00006597 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006598 ret = io_submit_sqes(ctx, to_submit);
6599 mutex_unlock(&ctx->uring_lock);
6600 }
Jens Axboe90554202020-09-03 12:12:41 -06006601
6602 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6603 wake_up(&ctx->sqo_sq_wait);
6604
Xiaoguang Wang08369242020-11-03 14:15:59 +08006605 return ret;
6606}
6607
6608static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6609{
6610 struct io_ring_ctx *ctx;
6611 unsigned sq_thread_idle = 0;
6612
6613 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6614 if (sq_thread_idle < ctx->sq_thread_idle)
6615 sq_thread_idle = ctx->sq_thread_idle;
6616 }
6617
6618 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006619}
6620
Jens Axboe69fb2132020-09-14 11:16:23 -06006621static void io_sqd_init_new(struct io_sq_data *sqd)
6622{
6623 struct io_ring_ctx *ctx;
6624
6625 while (!list_empty(&sqd->ctx_new_list)) {
6626 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006627 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6628 complete(&ctx->sq_thread_comp);
6629 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006630
6631 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006632}
6633
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006634static bool io_sq_thread_should_stop(struct io_sq_data *sqd)
6635{
6636 return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6637}
6638
6639static bool io_sq_thread_should_park(struct io_sq_data *sqd)
6640{
6641 return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
6642}
6643
6644static void io_sq_thread_parkme(struct io_sq_data *sqd)
6645{
6646 for (;;) {
6647 /*
6648 * TASK_PARKED is a special state; we must serialize against
6649 * possible pending wakeups to avoid store-store collisions on
6650 * task->state.
6651 *
6652 * Such a collision might possibly result in the task state
6653 * changin from TASK_PARKED and us failing the
6654 * wait_task_inactive() in kthread_park().
6655 */
6656 set_special_state(TASK_PARKED);
6657 if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state))
6658 break;
6659
6660 /*
6661 * Thread is going to call schedule(), do not preempt it,
6662 * or the caller of kthread_park() may spend more time in
6663 * wait_task_inactive().
6664 */
6665 preempt_disable();
6666 complete(&sqd->completion);
6667 schedule_preempt_disabled();
6668 preempt_enable();
6669 }
6670 __set_current_state(TASK_RUNNING);
6671}
6672
Jens Axboe6c271ce2019-01-10 11:22:30 -07006673static int io_sq_thread(void *data)
6674{
Jens Axboe69fb2132020-09-14 11:16:23 -06006675 struct io_sq_data *sqd = data;
6676 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006677 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006678 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006679 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006680
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006681 sprintf(buf, "iou-sqp-%d", sqd->task_pid);
6682 set_task_comm(current, buf);
6683 sqd->thread = current;
6684 current->pf_io_worker = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006685
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006686 if (sqd->sq_cpu != -1)
6687 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6688 else
6689 set_cpus_allowed_ptr(current, cpu_online_mask);
6690 current->flags |= PF_NO_SETAFFINITY;
6691
6692 complete(&sqd->completion);
6693
6694 wait_for_completion(&sqd->startup);
6695
6696 while (!io_sq_thread_should_stop(sqd)) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006697 int ret;
6698 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006699
6700 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006701 * Any changes to the sqd lists are synchronized through the
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006702 * thread parking. This synchronizes the thread vs users,
Jens Axboe69fb2132020-09-14 11:16:23 -06006703 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006704 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006705 if (io_sq_thread_should_park(sqd)) {
6706 io_sq_thread_parkme(sqd);
6707 continue;
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006708 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006709 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006710 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006711 timeout = jiffies + sqd->sq_thread_idle;
6712 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006713 if (fatal_signal_pending(current))
6714 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006715 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006716 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006717 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006718 ret = __io_sq_thread(ctx, cap_entries);
6719 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6720 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006721 }
6722
Xiaoguang Wang08369242020-11-03 14:15:59 +08006723 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006724 io_run_task_work();
6725 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006726 if (sqt_spin)
6727 timeout = jiffies + sqd->sq_thread_idle;
6728 continue;
6729 }
6730
Xiaoguang Wang08369242020-11-03 14:15:59 +08006731 needs_sched = true;
6732 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6733 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6734 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6735 !list_empty_careful(&ctx->iopoll_list)) {
6736 needs_sched = false;
6737 break;
6738 }
6739 if (io_sqring_entries(ctx)) {
6740 needs_sched = false;
6741 break;
6742 }
6743 }
6744
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006745 if (needs_sched && !io_sq_thread_should_park(sqd)) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006746 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6747 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006748
Jens Axboe69fb2132020-09-14 11:16:23 -06006749 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06006750 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6751 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006752 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006753
6754 finish_wait(&sqd->wait, &wait);
6755 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006756 }
6757
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006758 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6759 io_uring_cancel_sqpoll(ctx);
6760
Jens Axboe4c6e2772020-07-01 11:29:10 -06006761 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006762
Jens Axboe86293972021-02-26 13:46:49 -07006763 if (io_sq_thread_should_park(sqd))
6764 io_sq_thread_parkme(sqd);
6765
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006766 /*
6767 * Clear thread under lock so that concurrent parks work correctly
6768 */
Jens Axboe86293972021-02-26 13:46:49 -07006769 complete(&sqd->completion);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006770 mutex_lock(&sqd->lock);
6771 sqd->thread = NULL;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07006772 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6773 ctx->sqo_exec = 1;
6774 io_ring_set_wakeup_flag(ctx);
6775 }
Jens Axboe06058632019-04-13 09:26:03 -06006776
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006777 complete(&sqd->exited);
Jens Axboee54945a2021-02-26 11:27:15 -07006778 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006779 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006780}
6781
Jens Axboebda52162019-09-24 13:47:15 -06006782struct io_wait_queue {
6783 struct wait_queue_entry wq;
6784 struct io_ring_ctx *ctx;
6785 unsigned to_wait;
6786 unsigned nr_timeouts;
6787};
6788
Pavel Begunkov6c503152021-01-04 20:36:36 +00006789static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006790{
6791 struct io_ring_ctx *ctx = iowq->ctx;
6792
6793 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006794 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006795 * started waiting. For timeouts, we always want to return to userspace,
6796 * regardless of event count.
6797 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006798 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006799 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6800}
6801
6802static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6803 int wake_flags, void *key)
6804{
6805 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6806 wq);
6807
Pavel Begunkov6c503152021-01-04 20:36:36 +00006808 /*
6809 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6810 * the task, and the next invocation will do it.
6811 */
6812 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6813 return autoremove_wake_function(curr, mode, wake_flags, key);
6814 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006815}
6816
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006817static int io_run_task_work_sig(void)
6818{
6819 if (io_run_task_work())
6820 return 1;
6821 if (!signal_pending(current))
6822 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06006823 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
6824 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006825 return -EINTR;
6826}
6827
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006828/* when returns >0, the caller should retry */
6829static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6830 struct io_wait_queue *iowq,
6831 signed long *timeout)
6832{
6833 int ret;
6834
6835 /* make sure we run task_work before checking for signals */
6836 ret = io_run_task_work_sig();
6837 if (ret || io_should_wake(iowq))
6838 return ret;
6839 /* let the caller flush overflows, retry */
6840 if (test_bit(0, &ctx->cq_check_overflow))
6841 return 1;
6842
6843 *timeout = schedule_timeout(*timeout);
6844 return !*timeout ? -ETIME : 1;
6845}
6846
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847/*
6848 * Wait until events become available, if we don't already have some. The
6849 * application must reap them itself, as they reside on the shared cq ring.
6850 */
6851static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006852 const sigset_t __user *sig, size_t sigsz,
6853 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006854{
Jens Axboebda52162019-09-24 13:47:15 -06006855 struct io_wait_queue iowq = {
6856 .wq = {
6857 .private = current,
6858 .func = io_wake_function,
6859 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6860 },
6861 .ctx = ctx,
6862 .to_wait = min_events,
6863 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006864 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006865 signed long timeout = MAX_SCHEDULE_TIMEOUT;
6866 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006867
Jens Axboeb41e9852020-02-17 09:52:41 -07006868 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006869 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6870 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006871 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006872 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006873 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006874 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006875
6876 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006877#ifdef CONFIG_COMPAT
6878 if (in_compat_syscall())
6879 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006880 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006881 else
6882#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006883 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006884
Jens Axboe2b188cc2019-01-07 10:46:33 -07006885 if (ret)
6886 return ret;
6887 }
6888
Hao Xuc73ebb62020-11-03 10:54:37 +08006889 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006890 struct timespec64 ts;
6891
Hao Xuc73ebb62020-11-03 10:54:37 +08006892 if (get_timespec64(&ts, uts))
6893 return -EFAULT;
6894 timeout = timespec64_to_jiffies(&ts);
6895 }
6896
Jens Axboebda52162019-09-24 13:47:15 -06006897 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006898 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006899 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006900 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06006901 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6902 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006903 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
6904 finish_wait(&ctx->wait, &iowq.wq);
6905 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06006906
Jens Axboeb7db41c2020-07-04 08:55:50 -06006907 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006908
Hristo Venev75b28af2019-08-26 17:23:46 +00006909 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006910}
6911
Jens Axboe6b063142019-01-10 22:13:58 -07006912static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6913{
6914#if defined(CONFIG_UNIX)
6915 if (ctx->ring_sock) {
6916 struct sock *sock = ctx->ring_sock->sk;
6917 struct sk_buff *skb;
6918
6919 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6920 kfree_skb(skb);
6921 }
6922#else
6923 int i;
6924
Jens Axboe65e19f52019-10-26 07:20:21 -06006925 for (i = 0; i < ctx->nr_user_files; i++) {
6926 struct file *file;
6927
6928 file = io_file_from_index(ctx, i);
6929 if (file)
6930 fput(file);
6931 }
Jens Axboe6b063142019-01-10 22:13:58 -07006932#endif
6933}
6934
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00006935static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006936{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006937 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006938
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006939 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006940 complete(&data->done);
6941}
6942
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006943static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00006944{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006945 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00006946}
6947
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006948static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07006949{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006950 spin_unlock_bh(&ctx->rsrc_ref_lock);
6951}
6952
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006953static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
6954 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006955 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07006956{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006957 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006958 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006959 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006960 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006961 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07006962}
6963
Hao Xu8bad28d2021-02-19 17:19:36 +08006964static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07006965{
Hao Xu8bad28d2021-02-19 17:19:36 +08006966 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006967
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006968 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00006969 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00006970 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006971 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006972 if (ref_node)
6973 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006974}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006975
Hao Xu8bad28d2021-02-19 17:19:36 +08006976static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
6977 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006978 void (*rsrc_put)(struct io_ring_ctx *ctx,
6979 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08006980{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006981 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08006982 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006983
Hao Xu8bad28d2021-02-19 17:19:36 +08006984 if (data->quiesce)
6985 return -ENXIO;
6986
6987 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00006988 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006989 ret = -ENOMEM;
6990 backup_node = alloc_fixed_rsrc_ref_node(ctx);
6991 if (!backup_node)
6992 break;
6993 backup_node->rsrc_data = data;
6994 backup_node->rsrc_put = rsrc_put;
6995
Hao Xu8bad28d2021-02-19 17:19:36 +08006996 io_sqe_rsrc_kill_node(ctx, data);
6997 percpu_ref_kill(&data->refs);
6998 flush_delayed_work(&ctx->rsrc_put_work);
6999
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007000 ret = wait_for_completion_interruptible(&data->done);
7001 if (!ret)
7002 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007003
Jens Axboecb5e1b82021-02-25 07:37:35 -07007004 percpu_ref_resurrect(&data->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08007005 io_sqe_rsrc_set_node(ctx, data, backup_node);
7006 backup_node = NULL;
Jens Axboecb5e1b82021-02-25 07:37:35 -07007007 reinit_completion(&data->done);
Hao Xu8bad28d2021-02-19 17:19:36 +08007008 mutex_unlock(&ctx->uring_lock);
7009 ret = io_run_task_work_sig();
7010 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007011 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007012 data->quiesce = false;
7013
7014 if (backup_node)
7015 destroy_fixed_rsrc_ref_node(backup_node);
7016 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007017}
7018
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007019static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7020{
7021 struct fixed_rsrc_data *data;
7022
7023 data = kzalloc(sizeof(*data), GFP_KERNEL);
7024 if (!data)
7025 return NULL;
7026
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007027 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007028 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7029 kfree(data);
7030 return NULL;
7031 }
7032 data->ctx = ctx;
7033 init_completion(&data->done);
7034 return data;
7035}
7036
7037static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7038{
7039 percpu_ref_exit(&data->refs);
7040 kfree(data->table);
7041 kfree(data);
7042}
7043
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007044static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7045{
7046 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007047 unsigned nr_tables, i;
7048 int ret;
7049
Hao Xu8bad28d2021-02-19 17:19:36 +08007050 /*
7051 * percpu_ref_is_dying() is to stop parallel files unregister
7052 * Since we possibly drop uring lock later in this function to
7053 * run task work.
7054 */
7055 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007056 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007057 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007058 if (ret)
7059 return ret;
7060
Jens Axboe6b063142019-01-10 22:13:58 -07007061 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007062 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7063 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007064 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007065 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007066 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007067 ctx->nr_user_files = 0;
7068 return 0;
7069}
7070
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007071static void io_sq_thread_unpark(struct io_sq_data *sqd)
7072 __releases(&sqd->lock)
7073{
7074 if (!sqd->thread)
7075 return;
7076 if (sqd->thread == current)
7077 return;
7078 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7079 wake_up_state(sqd->thread, TASK_PARKED);
7080 mutex_unlock(&sqd->lock);
7081}
7082
7083static bool io_sq_thread_park(struct io_sq_data *sqd)
7084 __acquires(&sqd->lock)
7085{
7086 if (sqd->thread == current)
7087 return true;
7088 mutex_lock(&sqd->lock);
7089 if (!sqd->thread) {
7090 mutex_unlock(&sqd->lock);
7091 return false;
7092 }
7093 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7094 wake_up_process(sqd->thread);
7095 wait_for_completion(&sqd->completion);
7096 return true;
7097}
7098
7099static void io_sq_thread_stop(struct io_sq_data *sqd)
7100{
Jens Axboee54945a2021-02-26 11:27:15 -07007101 if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state))
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007102 return;
Jens Axboee54945a2021-02-26 11:27:15 -07007103 mutex_lock(&sqd->lock);
7104 if (sqd->thread) {
7105 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7106 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state));
7107 wake_up_process(sqd->thread);
7108 mutex_unlock(&sqd->lock);
7109 wait_for_completion(&sqd->exited);
7110 WARN_ON_ONCE(sqd->thread);
7111 } else {
7112 mutex_unlock(&sqd->lock);
7113 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007114}
7115
Jens Axboe534ca6d2020-09-02 13:52:19 -06007116static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007117{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007118 if (refcount_dec_and_test(&sqd->refs)) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007119 io_sq_thread_stop(sqd);
7120 kfree(sqd);
7121 }
7122}
7123
7124static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7125{
7126 struct io_sq_data *sqd = ctx->sq_data;
7127
7128 if (sqd) {
Jens Axboeeb858902021-02-25 10:13:29 -07007129 complete(&sqd->startup);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007130 if (sqd->thread) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007131 wait_for_completion(&ctx->sq_thread_comp);
7132 io_sq_thread_park(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007133 }
7134
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007135 mutex_lock(&sqd->ctx_lock);
7136 list_del(&ctx->sqd_list);
7137 io_sqd_update_thread_idle(sqd);
7138 mutex_unlock(&sqd->ctx_lock);
7139
7140 if (sqd->thread)
7141 io_sq_thread_unpark(sqd);
7142
7143 io_put_sq_data(sqd);
7144 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007145 }
7146}
7147
Jens Axboeaa061652020-09-02 14:50:27 -06007148static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7149{
7150 struct io_ring_ctx *ctx_attach;
7151 struct io_sq_data *sqd;
7152 struct fd f;
7153
7154 f = fdget(p->wq_fd);
7155 if (!f.file)
7156 return ERR_PTR(-ENXIO);
7157 if (f.file->f_op != &io_uring_fops) {
7158 fdput(f);
7159 return ERR_PTR(-EINVAL);
7160 }
7161
7162 ctx_attach = f.file->private_data;
7163 sqd = ctx_attach->sq_data;
7164 if (!sqd) {
7165 fdput(f);
7166 return ERR_PTR(-EINVAL);
7167 }
7168
7169 refcount_inc(&sqd->refs);
7170 fdput(f);
7171 return sqd;
7172}
7173
Jens Axboe534ca6d2020-09-02 13:52:19 -06007174static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7175{
7176 struct io_sq_data *sqd;
7177
Jens Axboeaa061652020-09-02 14:50:27 -06007178 if (p->flags & IORING_SETUP_ATTACH_WQ)
7179 return io_attach_sq_data(p);
7180
Jens Axboe534ca6d2020-09-02 13:52:19 -06007181 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7182 if (!sqd)
7183 return ERR_PTR(-ENOMEM);
7184
7185 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007186 INIT_LIST_HEAD(&sqd->ctx_list);
7187 INIT_LIST_HEAD(&sqd->ctx_new_list);
7188 mutex_init(&sqd->ctx_lock);
7189 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007190 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007191 init_completion(&sqd->startup);
7192 init_completion(&sqd->completion);
7193 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007194 return sqd;
7195}
7196
Jens Axboe6b063142019-01-10 22:13:58 -07007197#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007198/*
7199 * Ensure the UNIX gc is aware of our file set, so we are certain that
7200 * the io_uring can be safely unregistered on process exit, even if we have
7201 * loops in the file referencing.
7202 */
7203static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7204{
7205 struct sock *sk = ctx->ring_sock->sk;
7206 struct scm_fp_list *fpl;
7207 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007208 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007209
Jens Axboe6b063142019-01-10 22:13:58 -07007210 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7211 if (!fpl)
7212 return -ENOMEM;
7213
7214 skb = alloc_skb(0, GFP_KERNEL);
7215 if (!skb) {
7216 kfree(fpl);
7217 return -ENOMEM;
7218 }
7219
7220 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007221
Jens Axboe08a45172019-10-03 08:11:03 -06007222 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007223 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007224 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007225 struct file *file = io_file_from_index(ctx, i + offset);
7226
7227 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007228 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007229 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007230 unix_inflight(fpl->user, fpl->fp[nr_files]);
7231 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007232 }
7233
Jens Axboe08a45172019-10-03 08:11:03 -06007234 if (nr_files) {
7235 fpl->max = SCM_MAX_FD;
7236 fpl->count = nr_files;
7237 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007238 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007239 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7240 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007241
Jens Axboe08a45172019-10-03 08:11:03 -06007242 for (i = 0; i < nr_files; i++)
7243 fput(fpl->fp[i]);
7244 } else {
7245 kfree_skb(skb);
7246 kfree(fpl);
7247 }
Jens Axboe6b063142019-01-10 22:13:58 -07007248
7249 return 0;
7250}
7251
7252/*
7253 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7254 * causes regular reference counting to break down. We rely on the UNIX
7255 * garbage collection to take care of this problem for us.
7256 */
7257static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7258{
7259 unsigned left, total;
7260 int ret = 0;
7261
7262 total = 0;
7263 left = ctx->nr_user_files;
7264 while (left) {
7265 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007266
7267 ret = __io_sqe_files_scm(ctx, this_files, total);
7268 if (ret)
7269 break;
7270 left -= this_files;
7271 total += this_files;
7272 }
7273
7274 if (!ret)
7275 return 0;
7276
7277 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007278 struct file *file = io_file_from_index(ctx, total);
7279
7280 if (file)
7281 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007282 total++;
7283 }
7284
7285 return ret;
7286}
7287#else
7288static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7289{
7290 return 0;
7291}
7292#endif
7293
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007294static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007295 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007296{
7297 int i;
7298
7299 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007300 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007301 unsigned this_files;
7302
7303 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7304 table->files = kcalloc(this_files, sizeof(struct file *),
7305 GFP_KERNEL);
7306 if (!table->files)
7307 break;
7308 nr_files -= this_files;
7309 }
7310
7311 if (i == nr_tables)
7312 return 0;
7313
7314 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007315 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007316 kfree(table->files);
7317 }
7318 return 1;
7319}
7320
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007321static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007322{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007323 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007324#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007325 struct sock *sock = ctx->ring_sock->sk;
7326 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7327 struct sk_buff *skb;
7328 int i;
7329
7330 __skb_queue_head_init(&list);
7331
7332 /*
7333 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7334 * remove this entry and rearrange the file array.
7335 */
7336 skb = skb_dequeue(head);
7337 while (skb) {
7338 struct scm_fp_list *fp;
7339
7340 fp = UNIXCB(skb).fp;
7341 for (i = 0; i < fp->count; i++) {
7342 int left;
7343
7344 if (fp->fp[i] != file)
7345 continue;
7346
7347 unix_notinflight(fp->user, fp->fp[i]);
7348 left = fp->count - 1 - i;
7349 if (left) {
7350 memmove(&fp->fp[i], &fp->fp[i + 1],
7351 left * sizeof(struct file *));
7352 }
7353 fp->count--;
7354 if (!fp->count) {
7355 kfree_skb(skb);
7356 skb = NULL;
7357 } else {
7358 __skb_queue_tail(&list, skb);
7359 }
7360 fput(file);
7361 file = NULL;
7362 break;
7363 }
7364
7365 if (!file)
7366 break;
7367
7368 __skb_queue_tail(&list, skb);
7369
7370 skb = skb_dequeue(head);
7371 }
7372
7373 if (skb_peek(&list)) {
7374 spin_lock_irq(&head->lock);
7375 while ((skb = __skb_dequeue(&list)) != NULL)
7376 __skb_queue_tail(head, skb);
7377 spin_unlock_irq(&head->lock);
7378 }
7379#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007380 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007381#endif
7382}
7383
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007384static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007385{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007386 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7387 struct io_ring_ctx *ctx = rsrc_data->ctx;
7388 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007389
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007390 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7391 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007392 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007393 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007394 }
7395
Xiaoguang Wang05589552020-03-31 14:05:18 +08007396 percpu_ref_exit(&ref_node->refs);
7397 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007398 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007399}
7400
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007401static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007402{
7403 struct io_ring_ctx *ctx;
7404 struct llist_node *node;
7405
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007406 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7407 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007408
7409 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007410 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007411 struct llist_node *next = node->next;
7412
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007413 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7414 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007415 node = next;
7416 }
7417}
7418
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007419static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7420 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007422 struct fixed_rsrc_table *table;
7423
7424 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7425 return &table->files[i & IORING_FILE_TABLE_MASK];
7426}
7427
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007428static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007429{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007430 struct fixed_rsrc_ref_node *ref_node;
7431 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007432 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007433 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007434 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007435
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007436 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7437 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007438 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007439
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007440 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007441 ref_node->done = true;
7442
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007443 while (!list_empty(&ctx->rsrc_ref_list)) {
7444 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007445 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007446 /* recycle ref nodes in order */
7447 if (!ref_node->done)
7448 break;
7449 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007450 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007451 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007452 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007453
7454 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007455 delay = 0;
7456
Jens Axboe4a38aed22020-05-14 17:21:15 -06007457 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007458 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007459 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007460 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007461}
7462
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007463static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007464 struct io_ring_ctx *ctx)
7465{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007466 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007467
7468 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7469 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007470 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007471
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007472 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007473 0, GFP_KERNEL)) {
7474 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007475 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007476 }
7477 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007478 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007479 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007480 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007481}
7482
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007483static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7484 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007485{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007486 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007487 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007488}
7489
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007490static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007491{
7492 percpu_ref_exit(&ref_node->refs);
7493 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007494}
7495
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007496
Jens Axboe05f3fb32019-12-09 11:22:50 -07007497static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7498 unsigned nr_args)
7499{
7500 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007501 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007502 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007503 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007504 struct fixed_rsrc_ref_node *ref_node;
7505 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007506
7507 if (ctx->file_data)
7508 return -EBUSY;
7509 if (!nr_args)
7510 return -EINVAL;
7511 if (nr_args > IORING_MAX_FIXED_FILES)
7512 return -EMFILE;
7513
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007514 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007515 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007516 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007517 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007518
7519 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007520 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007521 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007522 if (!file_data->table)
7523 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007524
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007525 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007526 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007527
Jens Axboe05f3fb32019-12-09 11:22:50 -07007528 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007529 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7530 ret = -EFAULT;
7531 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007532 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007533 /* allow sparse sets */
7534 if (fd == -1)
7535 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007536
Jens Axboe05f3fb32019-12-09 11:22:50 -07007537 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007538 ret = -EBADF;
7539 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007540 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007541
7542 /*
7543 * Don't allow io_uring instances to be registered. If UNIX
7544 * isn't enabled, then this causes a reference cycle and this
7545 * instance can never get freed. If UNIX is enabled we'll
7546 * handle it just fine, but there's still no point in allowing
7547 * a ring fd as it doesn't support regular read/write anyway.
7548 */
7549 if (file->f_op == &io_uring_fops) {
7550 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007551 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007552 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007553 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007554 }
7555
Jens Axboe05f3fb32019-12-09 11:22:50 -07007556 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007557 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007558 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007559 return ret;
7560 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007561
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007562 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007563 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007564 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007565 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007566 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007567 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007568
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007569 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007570 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007571out_fput:
7572 for (i = 0; i < ctx->nr_user_files; i++) {
7573 file = io_file_from_index(ctx, i);
7574 if (file)
7575 fput(file);
7576 }
7577 for (i = 0; i < nr_tables; i++)
7578 kfree(file_data->table[i].files);
7579 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007580out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007581 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007582 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007583 return ret;
7584}
7585
Jens Axboec3a31e62019-10-03 13:59:56 -06007586static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7587 int index)
7588{
7589#if defined(CONFIG_UNIX)
7590 struct sock *sock = ctx->ring_sock->sk;
7591 struct sk_buff_head *head = &sock->sk_receive_queue;
7592 struct sk_buff *skb;
7593
7594 /*
7595 * See if we can merge this file into an existing skb SCM_RIGHTS
7596 * file set. If there's no room, fall back to allocating a new skb
7597 * and filling it in.
7598 */
7599 spin_lock_irq(&head->lock);
7600 skb = skb_peek(head);
7601 if (skb) {
7602 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7603
7604 if (fpl->count < SCM_MAX_FD) {
7605 __skb_unlink(skb, head);
7606 spin_unlock_irq(&head->lock);
7607 fpl->fp[fpl->count] = get_file(file);
7608 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7609 fpl->count++;
7610 spin_lock_irq(&head->lock);
7611 __skb_queue_head(head, skb);
7612 } else {
7613 skb = NULL;
7614 }
7615 }
7616 spin_unlock_irq(&head->lock);
7617
7618 if (skb) {
7619 fput(file);
7620 return 0;
7621 }
7622
7623 return __io_sqe_files_scm(ctx, 1, index);
7624#else
7625 return 0;
7626#endif
7627}
7628
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007629static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007630{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007631 struct io_rsrc_put *prsrc;
7632 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007633
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007634 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7635 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007636 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007637
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007638 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007639 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007640
Hillf Dantona5318d32020-03-23 17:47:15 +08007641 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007642}
7643
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007644static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7645 struct file *file)
7646{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007647 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007648}
7649
Jens Axboe05f3fb32019-12-09 11:22:50 -07007650static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007651 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007652 unsigned nr_args)
7653{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007654 struct fixed_rsrc_data *data = ctx->file_data;
7655 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007656 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007657 __s32 __user *fds;
7658 int fd, i, err;
7659 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007660 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007661
Jens Axboe05f3fb32019-12-09 11:22:50 -07007662 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007663 return -EOVERFLOW;
7664 if (done > ctx->nr_user_files)
7665 return -EINVAL;
7666
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007667 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007668 if (!ref_node)
7669 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007670 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007671
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007672 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007673 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007674 err = 0;
7675 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7676 err = -EFAULT;
7677 break;
7678 }
noah4e0377a2021-01-26 15:23:28 -05007679 if (fd == IORING_REGISTER_FILES_SKIP)
7680 continue;
7681
Pavel Begunkov67973b92021-01-26 13:51:09 +00007682 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007683 file_slot = io_fixed_file_slot(ctx->file_data, i);
7684
7685 if (*file_slot) {
7686 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007687 if (err)
7688 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007689 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007690 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007691 }
7692 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007693 file = fget(fd);
7694 if (!file) {
7695 err = -EBADF;
7696 break;
7697 }
7698 /*
7699 * Don't allow io_uring instances to be registered. If
7700 * UNIX isn't enabled, then this causes a reference
7701 * cycle and this instance can never get freed. If UNIX
7702 * is enabled we'll handle it just fine, but there's
7703 * still no point in allowing a ring fd as it doesn't
7704 * support regular read/write anyway.
7705 */
7706 if (file->f_op == &io_uring_fops) {
7707 fput(file);
7708 err = -EBADF;
7709 break;
7710 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07007711 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007712 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007713 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07007714 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007715 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007716 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007717 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007718 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007719 }
7720
Xiaoguang Wang05589552020-03-31 14:05:18 +08007721 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007722 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007723 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007724 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007725 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007726
7727 return done ? done : err;
7728}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007729
Jens Axboe05f3fb32019-12-09 11:22:50 -07007730static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7731 unsigned nr_args)
7732{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007733 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007734
7735 if (!ctx->file_data)
7736 return -ENXIO;
7737 if (!nr_args)
7738 return -EINVAL;
7739 if (copy_from_user(&up, arg, sizeof(up)))
7740 return -EFAULT;
7741 if (up.resv)
7742 return -EINVAL;
7743
7744 return __io_sqe_files_update(ctx, &up, nr_args);
7745}
Jens Axboec3a31e62019-10-03 13:59:56 -06007746
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007747static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007748{
7749 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7750
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007751 req = io_put_req_find_next(req);
7752 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007753}
7754
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007755static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007756{
Jens Axboee9418942021-02-19 12:33:30 -07007757 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007758 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007759 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007760
Jens Axboee9418942021-02-19 12:33:30 -07007761 hash = ctx->hash_map;
7762 if (!hash) {
7763 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
7764 if (!hash)
7765 return ERR_PTR(-ENOMEM);
7766 refcount_set(&hash->refs, 1);
7767 init_waitqueue_head(&hash->wait);
7768 ctx->hash_map = hash;
7769 }
7770
7771 data.hash = hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007772 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007773 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007774
Jens Axboed25e3a32021-02-16 11:41:41 -07007775 /* Do QD, or 4 * CPUS, whatever is smallest */
7776 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007777
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007778 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007779}
7780
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007781static int io_uring_alloc_task_context(struct task_struct *task,
7782 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007783{
7784 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007785 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007786
7787 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7788 if (unlikely(!tctx))
7789 return -ENOMEM;
7790
Jens Axboed8a6df12020-10-15 16:24:45 -06007791 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7792 if (unlikely(ret)) {
7793 kfree(tctx);
7794 return ret;
7795 }
7796
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007797 tctx->io_wq = io_init_wq_offload(ctx);
7798 if (IS_ERR(tctx->io_wq)) {
7799 ret = PTR_ERR(tctx->io_wq);
7800 percpu_counter_destroy(&tctx->inflight);
7801 kfree(tctx);
7802 return ret;
7803 }
7804
Jens Axboe0f212202020-09-13 13:09:39 -06007805 xa_init(&tctx->xa);
7806 init_waitqueue_head(&tctx->wait);
7807 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007808 atomic_set(&tctx->in_idle, 0);
7809 tctx->sqpoll = false;
Jens Axboe0f212202020-09-13 13:09:39 -06007810 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007811 spin_lock_init(&tctx->task_lock);
7812 INIT_WQ_LIST(&tctx->task_list);
7813 tctx->task_state = 0;
7814 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007815 return 0;
7816}
7817
7818void __io_uring_free(struct task_struct *tsk)
7819{
7820 struct io_uring_task *tctx = tsk->io_uring;
7821
7822 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00007823 WARN_ON_ONCE(tctx->io_wq);
7824
Jens Axboed8a6df12020-10-15 16:24:45 -06007825 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007826 kfree(tctx);
7827 tsk->io_uring = NULL;
7828}
7829
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007830static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx)
7831{
7832 int ret;
7833
7834 clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7835 reinit_completion(&sqd->completion);
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00007836 ctx->sqo_exec = 0;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007837 sqd->task_pid = current->pid;
7838 current->flags |= PF_IO_WORKER;
7839 ret = io_wq_fork_thread(io_sq_thread, sqd);
7840 current->flags &= ~PF_IO_WORKER;
7841 if (ret < 0) {
7842 sqd->thread = NULL;
7843 return ret;
7844 }
7845 wait_for_completion(&sqd->completion);
7846 return io_uring_alloc_task_context(sqd->thread, ctx);
7847}
7848
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007849static int io_sq_offload_create(struct io_ring_ctx *ctx,
7850 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007851{
7852 int ret;
7853
Jens Axboed25e3a32021-02-16 11:41:41 -07007854 /* Retain compatibility with failing for an invalid attach attempt */
7855 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7856 IORING_SETUP_ATTACH_WQ) {
7857 struct fd f;
7858
7859 f = fdget(p->wq_fd);
7860 if (!f.file)
7861 return -ENXIO;
7862 if (f.file->f_op != &io_uring_fops) {
7863 fdput(f);
7864 return -EINVAL;
7865 }
7866 fdput(f);
7867 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007868 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007869 struct io_sq_data *sqd;
7870
Jens Axboe3ec482d2019-04-08 10:51:01 -06007871 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007872 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007873 goto err;
7874
Jens Axboe534ca6d2020-09-02 13:52:19 -06007875 sqd = io_get_sq_data(p);
7876 if (IS_ERR(sqd)) {
7877 ret = PTR_ERR(sqd);
7878 goto err;
7879 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007880
Jens Axboe534ca6d2020-09-02 13:52:19 -06007881 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007882 io_sq_thread_park(sqd);
7883 mutex_lock(&sqd->ctx_lock);
7884 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7885 mutex_unlock(&sqd->ctx_lock);
7886 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007887
Jens Axboe917257d2019-04-13 09:28:55 -06007888 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7889 if (!ctx->sq_thread_idle)
7890 ctx->sq_thread_idle = HZ;
7891
Jens Axboeaa061652020-09-02 14:50:27 -06007892 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007893 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007894
Jens Axboe6c271ce2019-01-10 11:22:30 -07007895 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007896 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007897
Jens Axboe917257d2019-04-13 09:28:55 -06007898 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007899 if (cpu >= nr_cpu_ids)
7900 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007901 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007902 goto err;
7903
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007904 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007905 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007906 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007907 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007908
7909 sqd->task_pid = current->pid;
7910 current->flags |= PF_IO_WORKER;
7911 ret = io_wq_fork_thread(io_sq_thread, sqd);
7912 current->flags &= ~PF_IO_WORKER;
7913 if (ret < 0) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007914 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007915 goto err;
7916 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007917 wait_for_completion(&sqd->completion);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007918 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06007919 if (ret)
7920 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007921 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7922 /* Can't have SQ_AFF without SQPOLL */
7923 ret = -EINVAL;
7924 goto err;
7925 }
7926
Jens Axboe2b188cc2019-01-07 10:46:33 -07007927 return 0;
7928err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007929 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007930 return ret;
7931}
7932
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007933static void io_sq_offload_start(struct io_ring_ctx *ctx)
7934{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007935 struct io_sq_data *sqd = ctx->sq_data;
7936
Jens Axboe3ebba792021-02-28 15:32:18 -07007937 ctx->flags &= ~IORING_SETUP_R_DISABLED;
Jens Axboeeb858902021-02-25 10:13:29 -07007938 if (ctx->flags & IORING_SETUP_SQPOLL)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007939 complete(&sqd->startup);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007940}
7941
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007942static inline void __io_unaccount_mem(struct user_struct *user,
7943 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007944{
7945 atomic_long_sub(nr_pages, &user->locked_vm);
7946}
7947
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007948static inline int __io_account_mem(struct user_struct *user,
7949 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007950{
7951 unsigned long page_limit, cur_pages, new_pages;
7952
7953 /* Don't allow more pages than we can safely lock */
7954 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7955
7956 do {
7957 cur_pages = atomic_long_read(&user->locked_vm);
7958 new_pages = cur_pages + nr_pages;
7959 if (new_pages > page_limit)
7960 return -ENOMEM;
7961 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7962 new_pages) != cur_pages);
7963
7964 return 0;
7965}
7966
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007967static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007968{
Jens Axboe62e398b2021-02-21 16:19:37 -07007969 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007970 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007971
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007972 if (ctx->mm_account)
7973 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007974}
7975
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007976static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007977{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007978 int ret;
7979
Jens Axboe62e398b2021-02-21 16:19:37 -07007980 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007981 ret = __io_account_mem(ctx->user, nr_pages);
7982 if (ret)
7983 return ret;
7984 }
7985
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007986 if (ctx->mm_account)
7987 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007988
7989 return 0;
7990}
7991
Jens Axboe2b188cc2019-01-07 10:46:33 -07007992static void io_mem_free(void *ptr)
7993{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007994 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007995
Mark Rutland52e04ef2019-04-30 17:30:21 +01007996 if (!ptr)
7997 return;
7998
7999 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008000 if (put_page_testzero(page))
8001 free_compound_page(page);
8002}
8003
8004static void *io_mem_alloc(size_t size)
8005{
8006 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008007 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008008
8009 return (void *) __get_free_pages(gfp_flags, get_order(size));
8010}
8011
Hristo Venev75b28af2019-08-26 17:23:46 +00008012static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8013 size_t *sq_offset)
8014{
8015 struct io_rings *rings;
8016 size_t off, sq_array_size;
8017
8018 off = struct_size(rings, cqes, cq_entries);
8019 if (off == SIZE_MAX)
8020 return SIZE_MAX;
8021
8022#ifdef CONFIG_SMP
8023 off = ALIGN(off, SMP_CACHE_BYTES);
8024 if (off == 0)
8025 return SIZE_MAX;
8026#endif
8027
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008028 if (sq_offset)
8029 *sq_offset = off;
8030
Hristo Venev75b28af2019-08-26 17:23:46 +00008031 sq_array_size = array_size(sizeof(u32), sq_entries);
8032 if (sq_array_size == SIZE_MAX)
8033 return SIZE_MAX;
8034
8035 if (check_add_overflow(off, sq_array_size, &off))
8036 return SIZE_MAX;
8037
Hristo Venev75b28af2019-08-26 17:23:46 +00008038 return off;
8039}
8040
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008041static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008042{
8043 int i, j;
8044
8045 if (!ctx->user_bufs)
8046 return -ENXIO;
8047
8048 for (i = 0; i < ctx->nr_user_bufs; i++) {
8049 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8050
8051 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008052 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008053
Jens Axboede293932020-09-17 16:19:16 -06008054 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008055 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008056 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008057 imu->nr_bvecs = 0;
8058 }
8059
8060 kfree(ctx->user_bufs);
8061 ctx->user_bufs = NULL;
8062 ctx->nr_user_bufs = 0;
8063 return 0;
8064}
8065
8066static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8067 void __user *arg, unsigned index)
8068{
8069 struct iovec __user *src;
8070
8071#ifdef CONFIG_COMPAT
8072 if (ctx->compat) {
8073 struct compat_iovec __user *ciovs;
8074 struct compat_iovec ciov;
8075
8076 ciovs = (struct compat_iovec __user *) arg;
8077 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8078 return -EFAULT;
8079
Jens Axboed55e5f52019-12-11 16:12:15 -07008080 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008081 dst->iov_len = ciov.iov_len;
8082 return 0;
8083 }
8084#endif
8085 src = (struct iovec __user *) arg;
8086 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8087 return -EFAULT;
8088 return 0;
8089}
8090
Jens Axboede293932020-09-17 16:19:16 -06008091/*
8092 * Not super efficient, but this is just a registration time. And we do cache
8093 * the last compound head, so generally we'll only do a full search if we don't
8094 * match that one.
8095 *
8096 * We check if the given compound head page has already been accounted, to
8097 * avoid double accounting it. This allows us to account the full size of the
8098 * page, not just the constituent pages of a huge page.
8099 */
8100static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8101 int nr_pages, struct page *hpage)
8102{
8103 int i, j;
8104
8105 /* check current page array */
8106 for (i = 0; i < nr_pages; i++) {
8107 if (!PageCompound(pages[i]))
8108 continue;
8109 if (compound_head(pages[i]) == hpage)
8110 return true;
8111 }
8112
8113 /* check previously registered pages */
8114 for (i = 0; i < ctx->nr_user_bufs; i++) {
8115 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8116
8117 for (j = 0; j < imu->nr_bvecs; j++) {
8118 if (!PageCompound(imu->bvec[j].bv_page))
8119 continue;
8120 if (compound_head(imu->bvec[j].bv_page) == hpage)
8121 return true;
8122 }
8123 }
8124
8125 return false;
8126}
8127
8128static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8129 int nr_pages, struct io_mapped_ubuf *imu,
8130 struct page **last_hpage)
8131{
8132 int i, ret;
8133
8134 for (i = 0; i < nr_pages; i++) {
8135 if (!PageCompound(pages[i])) {
8136 imu->acct_pages++;
8137 } else {
8138 struct page *hpage;
8139
8140 hpage = compound_head(pages[i]);
8141 if (hpage == *last_hpage)
8142 continue;
8143 *last_hpage = hpage;
8144 if (headpage_already_acct(ctx, pages, i, hpage))
8145 continue;
8146 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8147 }
8148 }
8149
8150 if (!imu->acct_pages)
8151 return 0;
8152
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008153 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008154 if (ret)
8155 imu->acct_pages = 0;
8156 return ret;
8157}
8158
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008159static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8160 struct io_mapped_ubuf *imu,
8161 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008162{
8163 struct vm_area_struct **vmas = NULL;
8164 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008165 unsigned long off, start, end, ubuf;
8166 size_t size;
8167 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008168
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008169 ubuf = (unsigned long) iov->iov_base;
8170 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8171 start = ubuf >> PAGE_SHIFT;
8172 nr_pages = end - start;
8173
8174 ret = -ENOMEM;
8175
8176 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8177 if (!pages)
8178 goto done;
8179
8180 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8181 GFP_KERNEL);
8182 if (!vmas)
8183 goto done;
8184
8185 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8186 GFP_KERNEL);
8187 if (!imu->bvec)
8188 goto done;
8189
8190 ret = 0;
8191 mmap_read_lock(current->mm);
8192 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8193 pages, vmas);
8194 if (pret == nr_pages) {
8195 /* don't support file backed memory */
8196 for (i = 0; i < nr_pages; i++) {
8197 struct vm_area_struct *vma = vmas[i];
8198
8199 if (vma->vm_file &&
8200 !is_file_hugepages(vma->vm_file)) {
8201 ret = -EOPNOTSUPP;
8202 break;
8203 }
8204 }
8205 } else {
8206 ret = pret < 0 ? pret : -EFAULT;
8207 }
8208 mmap_read_unlock(current->mm);
8209 if (ret) {
8210 /*
8211 * if we did partial map, or found file backed vmas,
8212 * release any pages we did get
8213 */
8214 if (pret > 0)
8215 unpin_user_pages(pages, pret);
8216 kvfree(imu->bvec);
8217 goto done;
8218 }
8219
8220 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8221 if (ret) {
8222 unpin_user_pages(pages, pret);
8223 kvfree(imu->bvec);
8224 goto done;
8225 }
8226
8227 off = ubuf & ~PAGE_MASK;
8228 size = iov->iov_len;
8229 for (i = 0; i < nr_pages; i++) {
8230 size_t vec_len;
8231
8232 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8233 imu->bvec[i].bv_page = pages[i];
8234 imu->bvec[i].bv_len = vec_len;
8235 imu->bvec[i].bv_offset = off;
8236 off = 0;
8237 size -= vec_len;
8238 }
8239 /* store original address for later verification */
8240 imu->ubuf = ubuf;
8241 imu->len = iov->iov_len;
8242 imu->nr_bvecs = nr_pages;
8243 ret = 0;
8244done:
8245 kvfree(pages);
8246 kvfree(vmas);
8247 return ret;
8248}
8249
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008250static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008251{
Jens Axboeedafcce2019-01-09 09:16:05 -07008252 if (ctx->user_bufs)
8253 return -EBUSY;
8254 if (!nr_args || nr_args > UIO_MAXIOV)
8255 return -EINVAL;
8256
8257 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8258 GFP_KERNEL);
8259 if (!ctx->user_bufs)
8260 return -ENOMEM;
8261
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008262 return 0;
8263}
8264
8265static int io_buffer_validate(struct iovec *iov)
8266{
8267 /*
8268 * Don't impose further limits on the size and buffer
8269 * constraints here, we'll -EINVAL later when IO is
8270 * submitted if they are wrong.
8271 */
8272 if (!iov->iov_base || !iov->iov_len)
8273 return -EFAULT;
8274
8275 /* arbitrary limit, but we need something */
8276 if (iov->iov_len > SZ_1G)
8277 return -EFAULT;
8278
8279 return 0;
8280}
8281
8282static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8283 unsigned int nr_args)
8284{
8285 int i, ret;
8286 struct iovec iov;
8287 struct page *last_hpage = NULL;
8288
8289 ret = io_buffers_map_alloc(ctx, nr_args);
8290 if (ret)
8291 return ret;
8292
Jens Axboeedafcce2019-01-09 09:16:05 -07008293 for (i = 0; i < nr_args; i++) {
8294 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008295
8296 ret = io_copy_iov(ctx, &iov, arg, i);
8297 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008298 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008299
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008300 ret = io_buffer_validate(&iov);
8301 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008302 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008303
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008304 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8305 if (ret)
8306 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008307
8308 ctx->nr_user_bufs++;
8309 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008310
8311 if (ret)
8312 io_sqe_buffers_unregister(ctx);
8313
Jens Axboeedafcce2019-01-09 09:16:05 -07008314 return ret;
8315}
8316
Jens Axboe9b402842019-04-11 11:45:41 -06008317static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8318{
8319 __s32 __user *fds = arg;
8320 int fd;
8321
8322 if (ctx->cq_ev_fd)
8323 return -EBUSY;
8324
8325 if (copy_from_user(&fd, fds, sizeof(*fds)))
8326 return -EFAULT;
8327
8328 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8329 if (IS_ERR(ctx->cq_ev_fd)) {
8330 int ret = PTR_ERR(ctx->cq_ev_fd);
8331 ctx->cq_ev_fd = NULL;
8332 return ret;
8333 }
8334
8335 return 0;
8336}
8337
8338static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8339{
8340 if (ctx->cq_ev_fd) {
8341 eventfd_ctx_put(ctx->cq_ev_fd);
8342 ctx->cq_ev_fd = NULL;
8343 return 0;
8344 }
8345
8346 return -ENXIO;
8347}
8348
Jens Axboe5a2e7452020-02-23 16:23:11 -07008349static int __io_destroy_buffers(int id, void *p, void *data)
8350{
8351 struct io_ring_ctx *ctx = data;
8352 struct io_buffer *buf = p;
8353
Jens Axboe067524e2020-03-02 16:32:28 -07008354 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008355 return 0;
8356}
8357
8358static void io_destroy_buffers(struct io_ring_ctx *ctx)
8359{
8360 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8361 idr_destroy(&ctx->io_buffer_idr);
8362}
8363
Jens Axboe68e68ee2021-02-13 09:00:02 -07008364static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008365{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008366 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008367
Jens Axboe68e68ee2021-02-13 09:00:02 -07008368 list_for_each_entry_safe(req, nxt, list, compl.list) {
8369 if (tsk && req->task != tsk)
8370 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008371 list_del(&req->compl.list);
8372 kmem_cache_free(req_cachep, req);
8373 }
8374}
8375
Jens Axboe4010fec2021-02-27 15:04:18 -07008376static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008377{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008378 struct io_submit_state *submit_state = &ctx->submit_state;
Pavel Begunkove5547d22021-02-23 22:17:20 +00008379 struct io_comp_state *cs = &ctx->submit_state.comp;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008380
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008381 mutex_lock(&ctx->uring_lock);
8382
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008383 if (submit_state->free_reqs) {
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008384 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8385 submit_state->reqs);
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008386 submit_state->free_reqs = 0;
8387 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008388
8389 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkove5547d22021-02-23 22:17:20 +00008390 list_splice_init(&cs->locked_free_list, &cs->free_list);
8391 cs->locked_free_nr = 0;
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008392 spin_unlock_irq(&ctx->completion_lock);
8393
Pavel Begunkove5547d22021-02-23 22:17:20 +00008394 io_req_cache_free(&cs->free_list, NULL);
8395
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008396 mutex_unlock(&ctx->uring_lock);
8397}
8398
Jens Axboe2b188cc2019-01-07 10:46:33 -07008399static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8400{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008401 /*
8402 * Some may use context even when all refs and requests have been put,
8403 * and they are free to do so while still holding uring_lock, see
8404 * __io_req_task_submit(). Wait for them to finish.
8405 */
8406 mutex_lock(&ctx->uring_lock);
8407 mutex_unlock(&ctx->uring_lock);
8408
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008409 io_sq_thread_finish(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008410 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008411
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008412 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008413 mmdrop(ctx->mm_account);
8414 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008415 }
Jens Axboedef596e2019-01-09 08:59:42 -07008416
Hao Xu8bad28d2021-02-19 17:19:36 +08008417 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008418 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008419 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008420 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008421 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008422 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008423
Jens Axboe2b188cc2019-01-07 10:46:33 -07008424#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008425 if (ctx->ring_sock) {
8426 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008427 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008428 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008429#endif
8430
Hristo Venev75b28af2019-08-26 17:23:46 +00008431 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008432 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008433
8434 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008435 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008436 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008437 if (ctx->hash_map)
8438 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008439 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008440 kfree(ctx);
8441}
8442
8443static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8444{
8445 struct io_ring_ctx *ctx = file->private_data;
8446 __poll_t mask = 0;
8447
8448 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008449 /*
8450 * synchronizes with barrier from wq_has_sleeper call in
8451 * io_commit_cqring
8452 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008453 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008454 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008455 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008456
8457 /*
8458 * Don't flush cqring overflow list here, just do a simple check.
8459 * Otherwise there could possible be ABBA deadlock:
8460 * CPU0 CPU1
8461 * ---- ----
8462 * lock(&ctx->uring_lock);
8463 * lock(&ep->mtx);
8464 * lock(&ctx->uring_lock);
8465 * lock(&ep->mtx);
8466 *
8467 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8468 * pushs them to do the flush.
8469 */
8470 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008471 mask |= EPOLLIN | EPOLLRDNORM;
8472
8473 return mask;
8474}
8475
8476static int io_uring_fasync(int fd, struct file *file, int on)
8477{
8478 struct io_ring_ctx *ctx = file->private_data;
8479
8480 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8481}
8482
Yejune Deng0bead8c2020-12-24 11:02:20 +08008483static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008484{
Jens Axboe4379bf82021-02-15 13:40:22 -07008485 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008486
Jens Axboe4379bf82021-02-15 13:40:22 -07008487 creds = idr_remove(&ctx->personality_idr, id);
8488 if (creds) {
8489 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008490 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008491 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008492
8493 return -EINVAL;
8494}
8495
8496static int io_remove_personalities(int id, void *p, void *data)
8497{
8498 struct io_ring_ctx *ctx = data;
8499
8500 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008501 return 0;
8502}
8503
Pavel Begunkovba50a032021-02-26 15:47:56 +00008504static bool io_run_ctx_fallback(struct io_ring_ctx *ctx)
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008505{
Pavel Begunkov28c47212021-02-28 22:04:54 +00008506 struct callback_head *work, *next;
Pavel Begunkovba50a032021-02-26 15:47:56 +00008507 bool executed = false;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008508
8509 do {
Pavel Begunkov28c47212021-02-28 22:04:54 +00008510 work = xchg(&ctx->exit_task_work, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008511 if (!work)
8512 break;
8513
8514 do {
8515 next = work->next;
8516 work->func(work);
8517 work = next;
8518 cond_resched();
8519 } while (work);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008520 executed = true;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008521 } while (1);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008522
8523 return executed;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008524}
8525
Jens Axboe85faa7b2020-04-09 18:14:00 -06008526static void io_ring_exit_work(struct work_struct *work)
8527{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008528 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8529 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008530
Jens Axboe56952e92020-06-17 15:00:04 -06008531 /*
8532 * If we're doing polled IO and end up having requests being
8533 * submitted async (out-of-line), then completions can come in while
8534 * we're waiting for refs to drop. We need to reap these manually,
8535 * as nobody else will be looking for them.
8536 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008537 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008538 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008539 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008540 io_ring_ctx_free(ctx);
8541}
8542
Jens Axboe2b188cc2019-01-07 10:46:33 -07008543static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8544{
8545 mutex_lock(&ctx->uring_lock);
8546 percpu_ref_kill(&ctx->refs);
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008547 /* if force is set, the ring is going away. always drop after that */
8548 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008549 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008550 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008551 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008552 mutex_unlock(&ctx->uring_lock);
8553
Pavel Begunkov6b819282020-11-06 13:00:25 +00008554 io_kill_timeouts(ctx, NULL, NULL);
8555 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008556
Jens Axboe15dff282019-11-13 09:09:23 -07008557 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008558 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008559
Jens Axboe85faa7b2020-04-09 18:14:00 -06008560 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008561 /*
8562 * Use system_unbound_wq to avoid spawning tons of event kworkers
8563 * if we're exiting a ton of rings at the same time. It just adds
8564 * noise and overhead, there's no discernable change in runtime
8565 * over using system_wq.
8566 */
8567 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008568}
8569
8570static int io_uring_release(struct inode *inode, struct file *file)
8571{
8572 struct io_ring_ctx *ctx = file->private_data;
8573
8574 file->private_data = NULL;
8575 io_ring_ctx_wait_and_kill(ctx);
8576 return 0;
8577}
8578
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008579struct io_task_cancel {
8580 struct task_struct *task;
8581 struct files_struct *files;
8582};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008583
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008584static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008585{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008586 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008587 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008588 bool ret;
8589
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008590 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008591 unsigned long flags;
8592 struct io_ring_ctx *ctx = req->ctx;
8593
8594 /* protect against races with linked timeouts */
8595 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008596 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008597 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8598 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008599 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008600 }
8601 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008602}
8603
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008604static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008605 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008606 struct files_struct *files)
8607{
8608 struct io_defer_entry *de = NULL;
8609 LIST_HEAD(list);
8610
8611 spin_lock_irq(&ctx->completion_lock);
8612 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008613 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008614 list_cut_position(&list, &ctx->defer_list, &de->list);
8615 break;
8616 }
8617 }
8618 spin_unlock_irq(&ctx->completion_lock);
8619
8620 while (!list_empty(&list)) {
8621 de = list_first_entry(&list, struct io_defer_entry, list);
8622 list_del_init(&de->list);
8623 req_set_fail_links(de->req);
8624 io_put_req(de->req);
8625 io_req_complete(de->req, -ECANCELED);
8626 kfree(de);
8627 }
8628}
8629
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008630static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8631 struct task_struct *task,
8632 struct files_struct *files)
8633{
8634 struct io_task_cancel cancel = { .task = task, .files = files, };
Pavel Begunkov64c72122021-03-01 18:20:45 +00008635 struct task_struct *tctx_task = task ?: current;
8636 struct io_uring_task *tctx = tctx_task->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008637
8638 while (1) {
8639 enum io_wq_cancel cret;
8640 bool ret = false;
8641
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008642 if (tctx && tctx->io_wq) {
8643 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008644 &cancel, true);
8645 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8646 }
8647
8648 /* SQPOLL thread does its own polling */
8649 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8650 while (!list_empty_careful(&ctx->iopoll_list)) {
8651 io_iopoll_try_reap_events(ctx);
8652 ret = true;
8653 }
8654 }
8655
8656 ret |= io_poll_remove_all(ctx, task, files);
8657 ret |= io_kill_timeouts(ctx, task, files);
8658 ret |= io_run_task_work();
Pavel Begunkovba50a032021-02-26 15:47:56 +00008659 ret |= io_run_ctx_fallback(ctx);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008660 io_cqring_overflow_flush(ctx, true, task, files);
8661 if (!ret)
8662 break;
8663 cond_resched();
8664 }
8665}
8666
Pavel Begunkovca70f002021-01-26 15:28:27 +00008667static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8668 struct task_struct *task,
8669 struct files_struct *files)
8670{
8671 struct io_kiocb *req;
8672 int cnt = 0;
8673
8674 spin_lock_irq(&ctx->inflight_lock);
8675 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8676 cnt += io_match_task(req, task, files);
8677 spin_unlock_irq(&ctx->inflight_lock);
8678 return cnt;
8679}
8680
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008681static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008682 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008683 struct files_struct *files)
8684{
Jens Axboefcb323c2019-10-24 12:39:47 -06008685 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008686 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008687 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008688
Pavel Begunkovca70f002021-01-26 15:28:27 +00008689 inflight = io_uring_count_inflight(ctx, task, files);
8690 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008691 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008692
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008693 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008694
Pavel Begunkov34343782021-02-10 11:45:42 +00008695 if (ctx->sq_data)
8696 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008697 prepare_to_wait(&task->io_uring->wait, &wait,
8698 TASK_UNINTERRUPTIBLE);
8699 if (inflight == io_uring_count_inflight(ctx, task, files))
8700 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008701 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00008702 if (ctx->sq_data)
8703 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06008704 }
Jens Axboe0f212202020-09-13 13:09:39 -06008705}
8706
8707/*
8708 * We need to iteratively cancel requests, in case a request has dependent
8709 * hard links. These persist even for failure of cancelations, hence keep
8710 * looping until none are found.
8711 */
8712static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8713 struct files_struct *files)
8714{
8715 struct task_struct *task = current;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008716 bool did_park = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008717
Jens Axboefdaf0832020-10-30 09:37:30 -06008718 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00008719 /* never started, nothing to cancel */
8720 if (ctx->flags & IORING_SETUP_R_DISABLED) {
8721 io_sq_offload_start(ctx);
8722 return;
8723 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008724 did_park = io_sq_thread_park(ctx->sq_data);
8725 if (did_park) {
8726 task = ctx->sq_data->thread;
8727 atomic_inc(&task->io_uring->in_idle);
8728 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008729 }
Jens Axboe0f212202020-09-13 13:09:39 -06008730
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008731 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008732
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008733 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008734 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008735 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008736
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008737 if (did_park) {
Jens Axboefdaf0832020-10-30 09:37:30 -06008738 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008739 io_sq_thread_unpark(ctx->sq_data);
8740 }
Jens Axboe0f212202020-09-13 13:09:39 -06008741}
8742
8743/*
8744 * Note that this task has used io_uring. We use it for cancelation purposes.
8745 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008746static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008747{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008748 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008749 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008750
8751 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008752 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008753 if (unlikely(ret))
8754 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008755 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008756 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008757 if (tctx->last != file) {
8758 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008759
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008760 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008761 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008762 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8763 file, GFP_KERNEL));
8764 if (ret) {
8765 fput(file);
8766 return ret;
8767 }
Jens Axboe0f212202020-09-13 13:09:39 -06008768 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008769 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008770 }
8771
Jens Axboefdaf0832020-10-30 09:37:30 -06008772 /*
8773 * This is race safe in that the task itself is doing this, hence it
8774 * cannot be going through the exit/cancel paths at the same time.
8775 * This cannot be modified while exit/cancel is running.
8776 */
8777 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8778 tctx->sqpoll = true;
8779
Jens Axboe0f212202020-09-13 13:09:39 -06008780 return 0;
8781}
8782
8783/*
8784 * Remove this io_uring_file -> task mapping.
8785 */
8786static void io_uring_del_task_file(struct file *file)
8787{
8788 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008789
8790 if (tctx->last == file)
8791 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008792 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008793 if (file)
8794 fput(file);
8795}
8796
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008797static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008798{
8799 struct file *file;
8800 unsigned long index;
8801
8802 xa_for_each(&tctx->xa, index, file)
8803 io_uring_del_task_file(file);
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008804 if (tctx->io_wq) {
8805 io_wq_put_and_exit(tctx->io_wq);
8806 tctx->io_wq = NULL;
8807 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008808}
8809
Jens Axboe0f212202020-09-13 13:09:39 -06008810void __io_uring_files_cancel(struct files_struct *files)
8811{
8812 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008813 struct file *file;
8814 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008815
8816 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008817 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008818 xa_for_each(&tctx->xa, index, file)
8819 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008820 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008821
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008822 if (files)
8823 io_uring_clean_tctx(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008824}
8825
8826static s64 tctx_inflight(struct io_uring_task *tctx)
8827{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008828 return percpu_counter_sum(&tctx->inflight);
8829}
8830
8831static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
8832{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008833 struct io_sq_data *sqd = ctx->sq_data;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008834 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06008835 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008836 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008837
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008838 if (!sqd)
8839 return;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008840 if (!io_sq_thread_park(sqd))
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008841 return;
8842 tctx = ctx->sq_data->thread->io_uring;
Jens Axboee54945a2021-02-26 11:27:15 -07008843 /* can happen on fork/alloc failure, just ignore that state */
8844 if (!tctx) {
8845 io_sq_thread_unpark(sqd);
8846 return;
8847 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008848
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008849 atomic_inc(&tctx->in_idle);
8850 do {
8851 /* read completions before cancelations */
8852 inflight = tctx_inflight(tctx);
8853 if (!inflight)
8854 break;
8855 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008856
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008857 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8858 /*
8859 * If we've seen completions, retry without waiting. This
8860 * avoids a race where a completion comes in before we did
8861 * prepare_to_wait().
8862 */
8863 if (inflight == tctx_inflight(tctx))
8864 schedule();
8865 finish_wait(&tctx->wait, &wait);
8866 } while (1);
8867 atomic_dec(&tctx->in_idle);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008868 io_sq_thread_unpark(sqd);
Jens Axboe0f212202020-09-13 13:09:39 -06008869}
8870
Jens Axboe0f212202020-09-13 13:09:39 -06008871/*
8872 * Find any io_uring fd that this task has registered or done IO on, and cancel
8873 * requests.
8874 */
8875void __io_uring_task_cancel(void)
8876{
8877 struct io_uring_task *tctx = current->io_uring;
8878 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008879 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008880
8881 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008882 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008883
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008884 if (tctx->sqpoll) {
8885 struct file *file;
8886 unsigned long index;
8887
8888 xa_for_each(&tctx->xa, index, file)
8889 io_uring_cancel_sqpoll(file->private_data);
8890 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008891
Jens Axboed8a6df12020-10-15 16:24:45 -06008892 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008893 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008894 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008895 if (!inflight)
8896 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008897 __io_uring_files_cancel(NULL);
8898
8899 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8900
8901 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008902 * If we've seen completions, retry without waiting. This
8903 * avoids a race where a completion comes in before we did
8904 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008905 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008906 if (inflight == tctx_inflight(tctx))
8907 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00008908 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008909 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008910
Jens Axboefdaf0832020-10-30 09:37:30 -06008911 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008912
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008913 io_uring_clean_tctx(tctx);
8914 /* all current's requests should be gone, we can kill tctx */
8915 __io_uring_free(current);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008916}
8917
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008918static void *io_uring_validate_mmap_request(struct file *file,
8919 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008920{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008921 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008922 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008923 struct page *page;
8924 void *ptr;
8925
8926 switch (offset) {
8927 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008928 case IORING_OFF_CQ_RING:
8929 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008930 break;
8931 case IORING_OFF_SQES:
8932 ptr = ctx->sq_sqes;
8933 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008934 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008935 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008936 }
8937
8938 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008939 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008940 return ERR_PTR(-EINVAL);
8941
8942 return ptr;
8943}
8944
8945#ifdef CONFIG_MMU
8946
8947static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8948{
8949 size_t sz = vma->vm_end - vma->vm_start;
8950 unsigned long pfn;
8951 void *ptr;
8952
8953 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8954 if (IS_ERR(ptr))
8955 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008956
8957 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8958 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8959}
8960
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008961#else /* !CONFIG_MMU */
8962
8963static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8964{
8965 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8966}
8967
8968static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8969{
8970 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8971}
8972
8973static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8974 unsigned long addr, unsigned long len,
8975 unsigned long pgoff, unsigned long flags)
8976{
8977 void *ptr;
8978
8979 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8980 if (IS_ERR(ptr))
8981 return PTR_ERR(ptr);
8982
8983 return (unsigned long) ptr;
8984}
8985
8986#endif /* !CONFIG_MMU */
8987
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008988static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06008989{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008990 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06008991 DEFINE_WAIT(wait);
8992
8993 do {
8994 if (!io_sqring_full(ctx))
8995 break;
Jens Axboe90554202020-09-03 12:12:41 -06008996 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8997
8998 if (!io_sqring_full(ctx))
8999 break;
Jens Axboe90554202020-09-03 12:12:41 -06009000 schedule();
9001 } while (!signal_pending(current));
9002
9003 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009004 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009005}
9006
Hao Xuc73ebb62020-11-03 10:54:37 +08009007static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9008 struct __kernel_timespec __user **ts,
9009 const sigset_t __user **sig)
9010{
9011 struct io_uring_getevents_arg arg;
9012
9013 /*
9014 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9015 * is just a pointer to the sigset_t.
9016 */
9017 if (!(flags & IORING_ENTER_EXT_ARG)) {
9018 *sig = (const sigset_t __user *) argp;
9019 *ts = NULL;
9020 return 0;
9021 }
9022
9023 /*
9024 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9025 * timespec and sigset_t pointers if good.
9026 */
9027 if (*argsz != sizeof(arg))
9028 return -EINVAL;
9029 if (copy_from_user(&arg, argp, sizeof(arg)))
9030 return -EFAULT;
9031 *sig = u64_to_user_ptr(arg.sigmask);
9032 *argsz = arg.sigmask_sz;
9033 *ts = u64_to_user_ptr(arg.ts);
9034 return 0;
9035}
9036
Jens Axboe2b188cc2019-01-07 10:46:33 -07009037SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009038 u32, min_complete, u32, flags, const void __user *, argp,
9039 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009040{
9041 struct io_ring_ctx *ctx;
9042 long ret = -EBADF;
9043 int submitted = 0;
9044 struct fd f;
9045
Jens Axboe4c6e2772020-07-01 11:29:10 -06009046 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009047
Jens Axboe90554202020-09-03 12:12:41 -06009048 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009049 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009050 return -EINVAL;
9051
9052 f = fdget(fd);
9053 if (!f.file)
9054 return -EBADF;
9055
9056 ret = -EOPNOTSUPP;
9057 if (f.file->f_op != &io_uring_fops)
9058 goto out_fput;
9059
9060 ret = -ENXIO;
9061 ctx = f.file->private_data;
9062 if (!percpu_ref_tryget(&ctx->refs))
9063 goto out_fput;
9064
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009065 ret = -EBADFD;
9066 if (ctx->flags & IORING_SETUP_R_DISABLED)
9067 goto out;
9068
Jens Axboe6c271ce2019-01-10 11:22:30 -07009069 /*
9070 * For SQ polling, the thread will do all submissions and completions.
9071 * Just return the requested submit count, and wake the thread if
9072 * we were asked to.
9073 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009074 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009075 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009076 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009077
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009078 if (unlikely(ctx->sqo_exec)) {
9079 ret = io_sq_thread_fork(ctx->sq_data, ctx);
9080 if (ret)
9081 goto out;
9082 ctx->sqo_exec = 0;
9083 }
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009084 ret = -EOWNERDEAD;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009085 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009086 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009087 if (flags & IORING_ENTER_SQ_WAIT) {
9088 ret = io_sqpoll_wait_sq(ctx);
9089 if (ret)
9090 goto out;
9091 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009092 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009093 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009094 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009095 if (unlikely(ret))
9096 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009097 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009098 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009099 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009100
9101 if (submitted != to_submit)
9102 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009103 }
9104 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009105 const sigset_t __user *sig;
9106 struct __kernel_timespec __user *ts;
9107
9108 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9109 if (unlikely(ret))
9110 goto out;
9111
Jens Axboe2b188cc2019-01-07 10:46:33 -07009112 min_complete = min(min_complete, ctx->cq_entries);
9113
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009114 /*
9115 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9116 * space applications don't need to do io completion events
9117 * polling again, they can rely on io_sq_thread to do polling
9118 * work, which can reduce cpu usage and uring_lock contention.
9119 */
9120 if (ctx->flags & IORING_SETUP_IOPOLL &&
9121 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009122 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009123 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009124 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009125 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009126 }
9127
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009128out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009129 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009130out_fput:
9131 fdput(f);
9132 return submitted ? submitted : ret;
9133}
9134
Tobias Klauserbebdb652020-02-26 18:38:32 +01009135#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009136static int io_uring_show_cred(int id, void *p, void *data)
9137{
Jens Axboe4379bf82021-02-15 13:40:22 -07009138 const struct cred *cred = p;
Jens Axboe87ce9552020-01-30 08:25:34 -07009139 struct seq_file *m = data;
9140 struct user_namespace *uns = seq_user_ns(m);
9141 struct group_info *gi;
9142 kernel_cap_t cap;
9143 unsigned __capi;
9144 int g;
9145
9146 seq_printf(m, "%5d\n", id);
9147 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9148 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9149 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9150 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9151 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9152 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9153 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9154 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9155 seq_puts(m, "\n\tGroups:\t");
9156 gi = cred->group_info;
9157 for (g = 0; g < gi->ngroups; g++) {
9158 seq_put_decimal_ull(m, g ? " " : "",
9159 from_kgid_munged(uns, gi->gid[g]));
9160 }
9161 seq_puts(m, "\n\tCapEff:\t");
9162 cap = cred->cap_effective;
9163 CAP_FOR_EACH_U32(__capi)
9164 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9165 seq_putc(m, '\n');
9166 return 0;
9167}
9168
9169static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9170{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009171 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009172 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009173 int i;
9174
Jens Axboefad8e0d2020-09-28 08:57:48 -06009175 /*
9176 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9177 * since fdinfo case grabs it in the opposite direction of normal use
9178 * cases. If we fail to get the lock, we just don't iterate any
9179 * structures that could be going away outside the io_uring mutex.
9180 */
9181 has_lock = mutex_trylock(&ctx->uring_lock);
9182
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009183 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009184 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009185 if (!sq->thread)
9186 sq = NULL;
9187 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009188
9189 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9190 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009191 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009192 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009193 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009194
Jens Axboe87ce9552020-01-30 08:25:34 -07009195 if (f)
9196 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9197 else
9198 seq_printf(m, "%5u: <none>\n", i);
9199 }
9200 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009201 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009202 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9203
9204 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9205 (unsigned int) buf->len);
9206 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009207 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009208 seq_printf(m, "Personalities:\n");
9209 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9210 }
Jens Axboed7718a92020-02-14 22:23:12 -07009211 seq_printf(m, "PollList:\n");
9212 spin_lock_irq(&ctx->completion_lock);
9213 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9214 struct hlist_head *list = &ctx->cancel_hash[i];
9215 struct io_kiocb *req;
9216
9217 hlist_for_each_entry(req, list, hash_node)
9218 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9219 req->task->task_works != NULL);
9220 }
9221 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009222 if (has_lock)
9223 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009224}
9225
9226static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9227{
9228 struct io_ring_ctx *ctx = f->private_data;
9229
9230 if (percpu_ref_tryget(&ctx->refs)) {
9231 __io_uring_show_fdinfo(ctx, m);
9232 percpu_ref_put(&ctx->refs);
9233 }
9234}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009235#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009236
Jens Axboe2b188cc2019-01-07 10:46:33 -07009237static const struct file_operations io_uring_fops = {
9238 .release = io_uring_release,
9239 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009240#ifndef CONFIG_MMU
9241 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9242 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9243#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009244 .poll = io_uring_poll,
9245 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009246#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009247 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009248#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009249};
9250
9251static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9252 struct io_uring_params *p)
9253{
Hristo Venev75b28af2019-08-26 17:23:46 +00009254 struct io_rings *rings;
9255 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009256
Jens Axboebd740482020-08-05 12:58:23 -06009257 /* make sure these are sane, as we already accounted them */
9258 ctx->sq_entries = p->sq_entries;
9259 ctx->cq_entries = p->cq_entries;
9260
Hristo Venev75b28af2019-08-26 17:23:46 +00009261 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9262 if (size == SIZE_MAX)
9263 return -EOVERFLOW;
9264
9265 rings = io_mem_alloc(size);
9266 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009267 return -ENOMEM;
9268
Hristo Venev75b28af2019-08-26 17:23:46 +00009269 ctx->rings = rings;
9270 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9271 rings->sq_ring_mask = p->sq_entries - 1;
9272 rings->cq_ring_mask = p->cq_entries - 1;
9273 rings->sq_ring_entries = p->sq_entries;
9274 rings->cq_ring_entries = p->cq_entries;
9275 ctx->sq_mask = rings->sq_ring_mask;
9276 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009277
9278 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009279 if (size == SIZE_MAX) {
9280 io_mem_free(ctx->rings);
9281 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009282 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009283 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009284
9285 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009286 if (!ctx->sq_sqes) {
9287 io_mem_free(ctx->rings);
9288 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009289 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009290 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009291
Jens Axboe2b188cc2019-01-07 10:46:33 -07009292 return 0;
9293}
9294
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009295static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9296{
9297 int ret, fd;
9298
9299 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9300 if (fd < 0)
9301 return fd;
9302
9303 ret = io_uring_add_task_file(ctx, file);
9304 if (ret) {
9305 put_unused_fd(fd);
9306 return ret;
9307 }
9308 fd_install(fd, file);
9309 return fd;
9310}
9311
Jens Axboe2b188cc2019-01-07 10:46:33 -07009312/*
9313 * Allocate an anonymous fd, this is what constitutes the application
9314 * visible backing of an io_uring instance. The application mmaps this
9315 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9316 * we have to tie this fd to a socket for file garbage collection purposes.
9317 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009318static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009319{
9320 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009321#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009322 int ret;
9323
Jens Axboe2b188cc2019-01-07 10:46:33 -07009324 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9325 &ctx->ring_sock);
9326 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009327 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009328#endif
9329
Jens Axboe2b188cc2019-01-07 10:46:33 -07009330 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9331 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009332#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009333 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009334 sock_release(ctx->ring_sock);
9335 ctx->ring_sock = NULL;
9336 } else {
9337 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009338 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009339#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009340 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009341}
9342
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009343static int io_uring_create(unsigned entries, struct io_uring_params *p,
9344 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009345{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009346 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009347 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009348 int ret;
9349
Jens Axboe8110c1a2019-12-28 15:39:54 -07009350 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009351 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009352 if (entries > IORING_MAX_ENTRIES) {
9353 if (!(p->flags & IORING_SETUP_CLAMP))
9354 return -EINVAL;
9355 entries = IORING_MAX_ENTRIES;
9356 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009357
9358 /*
9359 * Use twice as many entries for the CQ ring. It's possible for the
9360 * application to drive a higher depth than the size of the SQ ring,
9361 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009362 * some flexibility in overcommitting a bit. If the application has
9363 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9364 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009365 */
9366 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009367 if (p->flags & IORING_SETUP_CQSIZE) {
9368 /*
9369 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9370 * to a power-of-two, if it isn't already. We do NOT impose
9371 * any cq vs sq ring sizing.
9372 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009373 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009374 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009375 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9376 if (!(p->flags & IORING_SETUP_CLAMP))
9377 return -EINVAL;
9378 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9379 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009380 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9381 if (p->cq_entries < p->sq_entries)
9382 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009383 } else {
9384 p->cq_entries = 2 * p->sq_entries;
9385 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386
Jens Axboe2b188cc2019-01-07 10:46:33 -07009387 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009388 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009389 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009390 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009391 if (!capable(CAP_IPC_LOCK))
9392 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -06009393
9394 /*
9395 * This is just grabbed for accounting purposes. When a process exits,
9396 * the mm is exited and dropped before the files, hence we need to hang
9397 * on to this mm purely for the purposes of being able to unaccount
9398 * memory (locked/pinned vm). It's not used for anything else.
9399 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009400 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009401 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009402
Jens Axboe2b188cc2019-01-07 10:46:33 -07009403 ret = io_allocate_scq_urings(ctx, p);
9404 if (ret)
9405 goto err;
9406
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009407 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408 if (ret)
9409 goto err;
9410
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009411 if (!(p->flags & IORING_SETUP_R_DISABLED))
9412 io_sq_offload_start(ctx);
9413
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009415 p->sq_off.head = offsetof(struct io_rings, sq.head);
9416 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9417 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9418 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9419 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9420 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9421 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009422
9423 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009424 p->cq_off.head = offsetof(struct io_rings, cq.head);
9425 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9426 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9427 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9428 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9429 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009430 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009431
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009432 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9433 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009434 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009435 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Jens Axboe1c0aa1f2021-02-20 11:55:28 -07009436 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009437
9438 if (copy_to_user(params, p, sizeof(*p))) {
9439 ret = -EFAULT;
9440 goto err;
9441 }
Jens Axboed1719f72020-07-30 13:43:53 -06009442
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009443 file = io_uring_get_file(ctx);
9444 if (IS_ERR(file)) {
9445 ret = PTR_ERR(file);
9446 goto err;
9447 }
9448
Jens Axboed1719f72020-07-30 13:43:53 -06009449 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009450 * Install ring fd as the very last thing, so we don't risk someone
9451 * having closed it before we finish setup
9452 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009453 ret = io_uring_install_fd(ctx, file);
9454 if (ret < 0) {
9455 /* fput will clean it up */
9456 fput(file);
9457 return ret;
9458 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009459
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009460 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009461 return ret;
9462err:
9463 io_ring_ctx_wait_and_kill(ctx);
9464 return ret;
9465}
9466
9467/*
9468 * Sets up an aio uring context, and returns the fd. Applications asks for a
9469 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9470 * params structure passed in.
9471 */
9472static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9473{
9474 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009475 int i;
9476
9477 if (copy_from_user(&p, params, sizeof(p)))
9478 return -EFAULT;
9479 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9480 if (p.resv[i])
9481 return -EINVAL;
9482 }
9483
Jens Axboe6c271ce2019-01-10 11:22:30 -07009484 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009485 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009486 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9487 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009488 return -EINVAL;
9489
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009490 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009491}
9492
9493SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9494 struct io_uring_params __user *, params)
9495{
9496 return io_uring_setup(entries, params);
9497}
9498
Jens Axboe66f4af92020-01-16 15:36:52 -07009499static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9500{
9501 struct io_uring_probe *p;
9502 size_t size;
9503 int i, ret;
9504
9505 size = struct_size(p, ops, nr_args);
9506 if (size == SIZE_MAX)
9507 return -EOVERFLOW;
9508 p = kzalloc(size, GFP_KERNEL);
9509 if (!p)
9510 return -ENOMEM;
9511
9512 ret = -EFAULT;
9513 if (copy_from_user(p, arg, size))
9514 goto out;
9515 ret = -EINVAL;
9516 if (memchr_inv(p, 0, size))
9517 goto out;
9518
9519 p->last_op = IORING_OP_LAST - 1;
9520 if (nr_args > IORING_OP_LAST)
9521 nr_args = IORING_OP_LAST;
9522
9523 for (i = 0; i < nr_args; i++) {
9524 p->ops[i].op = i;
9525 if (!io_op_defs[i].not_supported)
9526 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9527 }
9528 p->ops_len = i;
9529
9530 ret = 0;
9531 if (copy_to_user(arg, p, size))
9532 ret = -EFAULT;
9533out:
9534 kfree(p);
9535 return ret;
9536}
9537
Jens Axboe071698e2020-01-28 10:04:42 -07009538static int io_register_personality(struct io_ring_ctx *ctx)
9539{
Jens Axboe4379bf82021-02-15 13:40:22 -07009540 const struct cred *creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009541 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009542
Jens Axboe4379bf82021-02-15 13:40:22 -07009543 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009544
Jens Axboe4379bf82021-02-15 13:40:22 -07009545 ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9546 USHRT_MAX, GFP_KERNEL);
9547 if (ret < 0)
9548 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009549 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009550}
9551
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009552static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9553 unsigned int nr_args)
9554{
9555 struct io_uring_restriction *res;
9556 size_t size;
9557 int i, ret;
9558
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009559 /* Restrictions allowed only if rings started disabled */
9560 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9561 return -EBADFD;
9562
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009563 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009564 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009565 return -EBUSY;
9566
9567 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9568 return -EINVAL;
9569
9570 size = array_size(nr_args, sizeof(*res));
9571 if (size == SIZE_MAX)
9572 return -EOVERFLOW;
9573
9574 res = memdup_user(arg, size);
9575 if (IS_ERR(res))
9576 return PTR_ERR(res);
9577
9578 ret = 0;
9579
9580 for (i = 0; i < nr_args; i++) {
9581 switch (res[i].opcode) {
9582 case IORING_RESTRICTION_REGISTER_OP:
9583 if (res[i].register_op >= IORING_REGISTER_LAST) {
9584 ret = -EINVAL;
9585 goto out;
9586 }
9587
9588 __set_bit(res[i].register_op,
9589 ctx->restrictions.register_op);
9590 break;
9591 case IORING_RESTRICTION_SQE_OP:
9592 if (res[i].sqe_op >= IORING_OP_LAST) {
9593 ret = -EINVAL;
9594 goto out;
9595 }
9596
9597 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9598 break;
9599 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9600 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9601 break;
9602 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9603 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9604 break;
9605 default:
9606 ret = -EINVAL;
9607 goto out;
9608 }
9609 }
9610
9611out:
9612 /* Reset all restrictions if an error happened */
9613 if (ret != 0)
9614 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9615 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009616 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009617
9618 kfree(res);
9619 return ret;
9620}
9621
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009622static int io_register_enable_rings(struct io_ring_ctx *ctx)
9623{
9624 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9625 return -EBADFD;
9626
9627 if (ctx->restrictions.registered)
9628 ctx->restricted = 1;
9629
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009630 io_sq_offload_start(ctx);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009631 return 0;
9632}
9633
Jens Axboe071698e2020-01-28 10:04:42 -07009634static bool io_register_op_must_quiesce(int op)
9635{
9636 switch (op) {
9637 case IORING_UNREGISTER_FILES:
9638 case IORING_REGISTER_FILES_UPDATE:
9639 case IORING_REGISTER_PROBE:
9640 case IORING_REGISTER_PERSONALITY:
9641 case IORING_UNREGISTER_PERSONALITY:
9642 return false;
9643 default:
9644 return true;
9645 }
9646}
9647
Jens Axboeedafcce2019-01-09 09:16:05 -07009648static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9649 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009650 __releases(ctx->uring_lock)
9651 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009652{
9653 int ret;
9654
Jens Axboe35fa71a2019-04-22 10:23:23 -06009655 /*
9656 * We're inside the ring mutex, if the ref is already dying, then
9657 * someone else killed the ctx or is already going through
9658 * io_uring_register().
9659 */
9660 if (percpu_ref_is_dying(&ctx->refs))
9661 return -ENXIO;
9662
Jens Axboe071698e2020-01-28 10:04:42 -07009663 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009664 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009665
Jens Axboe05f3fb32019-12-09 11:22:50 -07009666 /*
9667 * Drop uring mutex before waiting for references to exit. If
9668 * another thread is currently inside io_uring_enter() it might
9669 * need to grab the uring_lock to make progress. If we hold it
9670 * here across the drain wait, then we can deadlock. It's safe
9671 * to drop the mutex here, since no new references will come in
9672 * after we've killed the percpu ref.
9673 */
9674 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009675 do {
9676 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9677 if (!ret)
9678 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009679 ret = io_run_task_work_sig();
9680 if (ret < 0)
9681 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009682 } while (1);
9683
Jens Axboe05f3fb32019-12-09 11:22:50 -07009684 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009685
Jens Axboec1503682020-01-08 08:26:07 -07009686 if (ret) {
9687 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009688 goto out_quiesce;
9689 }
9690 }
9691
9692 if (ctx->restricted) {
9693 if (opcode >= IORING_REGISTER_LAST) {
9694 ret = -EINVAL;
9695 goto out;
9696 }
9697
9698 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9699 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009700 goto out;
9701 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009702 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009703
9704 switch (opcode) {
9705 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009706 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -07009707 break;
9708 case IORING_UNREGISTER_BUFFERS:
9709 ret = -EINVAL;
9710 if (arg || nr_args)
9711 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009712 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07009713 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009714 case IORING_REGISTER_FILES:
9715 ret = io_sqe_files_register(ctx, arg, nr_args);
9716 break;
9717 case IORING_UNREGISTER_FILES:
9718 ret = -EINVAL;
9719 if (arg || nr_args)
9720 break;
9721 ret = io_sqe_files_unregister(ctx);
9722 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009723 case IORING_REGISTER_FILES_UPDATE:
9724 ret = io_sqe_files_update(ctx, arg, nr_args);
9725 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009726 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009727 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009728 ret = -EINVAL;
9729 if (nr_args != 1)
9730 break;
9731 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009732 if (ret)
9733 break;
9734 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9735 ctx->eventfd_async = 1;
9736 else
9737 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009738 break;
9739 case IORING_UNREGISTER_EVENTFD:
9740 ret = -EINVAL;
9741 if (arg || nr_args)
9742 break;
9743 ret = io_eventfd_unregister(ctx);
9744 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009745 case IORING_REGISTER_PROBE:
9746 ret = -EINVAL;
9747 if (!arg || nr_args > 256)
9748 break;
9749 ret = io_probe(ctx, arg, nr_args);
9750 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009751 case IORING_REGISTER_PERSONALITY:
9752 ret = -EINVAL;
9753 if (arg || nr_args)
9754 break;
9755 ret = io_register_personality(ctx);
9756 break;
9757 case IORING_UNREGISTER_PERSONALITY:
9758 ret = -EINVAL;
9759 if (arg)
9760 break;
9761 ret = io_unregister_personality(ctx, nr_args);
9762 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009763 case IORING_REGISTER_ENABLE_RINGS:
9764 ret = -EINVAL;
9765 if (arg || nr_args)
9766 break;
9767 ret = io_register_enable_rings(ctx);
9768 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009769 case IORING_REGISTER_RESTRICTIONS:
9770 ret = io_register_restrictions(ctx, arg, nr_args);
9771 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009772 default:
9773 ret = -EINVAL;
9774 break;
9775 }
9776
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009777out:
Jens Axboe071698e2020-01-28 10:04:42 -07009778 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009779 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009780 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009781out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009782 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009783 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009784 return ret;
9785}
9786
9787SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9788 void __user *, arg, unsigned int, nr_args)
9789{
9790 struct io_ring_ctx *ctx;
9791 long ret = -EBADF;
9792 struct fd f;
9793
9794 f = fdget(fd);
9795 if (!f.file)
9796 return -EBADF;
9797
9798 ret = -EOPNOTSUPP;
9799 if (f.file->f_op != &io_uring_fops)
9800 goto out_fput;
9801
9802 ctx = f.file->private_data;
9803
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +00009804 io_run_task_work();
9805
Jens Axboeedafcce2019-01-09 09:16:05 -07009806 mutex_lock(&ctx->uring_lock);
9807 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9808 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009809 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9810 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009811out_fput:
9812 fdput(f);
9813 return ret;
9814}
9815
Jens Axboe2b188cc2019-01-07 10:46:33 -07009816static int __init io_uring_init(void)
9817{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009818#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9819 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9820 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9821} while (0)
9822
9823#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9824 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9825 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9826 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9827 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9828 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9829 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9830 BUILD_BUG_SQE_ELEM(8, __u64, off);
9831 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9832 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009833 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009834 BUILD_BUG_SQE_ELEM(24, __u32, len);
9835 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9836 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9837 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9838 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009839 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9840 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009841 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9842 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9843 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9844 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9845 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9846 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9847 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9848 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009849 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009850 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9851 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9852 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009853 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009854
Jens Axboed3656342019-12-18 09:50:26 -07009855 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009856 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -07009857 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
9858 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009859 return 0;
9860};
9861__initcall(io_uring_init);