blob: 6090a380e903a56312cda51c9c44d2f5771e851a [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 /*
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700383 * For SQPOLL usage
Jens Axboe2aede0e2020-09-14 10:45:53 -0600384 */
385 struct task_struct *sqo_task;
386
387 /* Only used for accounting purposes */
388 struct mm_struct *mm_account;
389
Jens Axboe534ca6d2020-09-02 13:52:19 -0600390 struct io_sq_data *sq_data; /* if using sq thread polling */
391
Jens Axboe90554202020-09-03 12:12:41 -0600392 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600393 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700394
Jens Axboe6b063142019-01-10 22:13:58 -0700395 /*
396 * If used, fixed file set. Writers must ensure that ->refs is dead,
397 * readers must ensure that ->refs is alive as long as the file* is
398 * used. Only updated through io_uring_register(2).
399 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000400 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700401 unsigned nr_user_files;
402
Jens Axboeedafcce2019-01-09 09:16:05 -0700403 /* if used, fixed mapped user buffers */
404 unsigned nr_user_bufs;
405 struct io_mapped_ubuf *user_bufs;
406
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 struct user_struct *user;
408
Jens Axboe0f158b42020-05-14 17:18:39 -0600409 struct completion ref_comp;
410 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700411
412#if defined(CONFIG_UNIX)
413 struct socket *ring_sock;
414#endif
415
Jens Axboe5a2e7452020-02-23 16:23:11 -0700416 struct idr io_buffer_idr;
417
Jens Axboe071698e2020-01-28 10:04:42 -0700418 struct idr personality_idr;
419
Jens Axboe206aefd2019-11-07 18:27:42 -0700420 struct {
421 unsigned cached_cq_tail;
422 unsigned cq_entries;
423 unsigned cq_mask;
424 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500425 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700426 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700427 struct wait_queue_head cq_wait;
428 struct fasync_struct *cq_fasync;
429 struct eventfd_ctx *cq_ev_fd;
430 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700431
432 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700434
Jens Axboedef596e2019-01-09 08:59:42 -0700435 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300436 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700437 * io_uring instances that don't use IORING_SETUP_SQPOLL.
438 * For SQPOLL, only the single threaded io_sq_thread() will
439 * manipulate the list, hence no extra locking is needed there.
440 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300441 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700442 struct hlist_head *cancel_hash;
443 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700444 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600445
446 spinlock_t inflight_lock;
447 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600449
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000450 struct delayed_work rsrc_put_work;
451 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000452 struct list_head rsrc_ref_list;
453 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600454
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200455 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700456
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700457 /* exit task_work */
458 struct callback_head *exit_task_work;
459
Jens Axboee9418942021-02-19 12:33:30 -0700460 struct wait_queue_head hash_wait;
461
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700462 /* Keep this last, we don't need it for the fast path */
463 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464};
465
Jens Axboe09bb8392019-03-13 12:39:28 -0600466/*
467 * First field must be the file pointer in all the
468 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
469 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700470struct io_poll_iocb {
471 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000472 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700473 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600474 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700475 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700476 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700477};
478
Pavel Begunkov018043b2020-10-27 23:17:18 +0000479struct io_poll_remove {
480 struct file *file;
481 u64 addr;
482};
483
Jens Axboeb5dba592019-12-11 14:02:38 -0700484struct io_close {
485 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700486 int fd;
487};
488
Jens Axboead8a48a2019-11-15 08:49:11 -0700489struct io_timeout_data {
490 struct io_kiocb *req;
491 struct hrtimer timer;
492 struct timespec64 ts;
493 enum hrtimer_mode mode;
494};
495
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700496struct io_accept {
497 struct file *file;
498 struct sockaddr __user *addr;
499 int __user *addr_len;
500 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600501 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700502};
503
504struct io_sync {
505 struct file *file;
506 loff_t len;
507 loff_t off;
508 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700509 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700510};
511
Jens Axboefbf23842019-12-17 18:45:56 -0700512struct io_cancel {
513 struct file *file;
514 u64 addr;
515};
516
Jens Axboeb29472e2019-12-17 18:50:29 -0700517struct io_timeout {
518 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300519 u32 off;
520 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300521 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000522 /* head of the link, used by linked timeouts only */
523 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700524};
525
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100526struct io_timeout_rem {
527 struct file *file;
528 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000529
530 /* timeout update */
531 struct timespec64 ts;
532 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100533};
534
Jens Axboe9adbd452019-12-20 08:45:55 -0700535struct io_rw {
536 /* NOTE: kiocb has the file as the first member, so don't do it here */
537 struct kiocb kiocb;
538 u64 addr;
539 u64 len;
540};
541
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700542struct io_connect {
543 struct file *file;
544 struct sockaddr __user *addr;
545 int addr_len;
546};
547
Jens Axboee47293f2019-12-20 08:58:21 -0700548struct io_sr_msg {
549 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700550 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300551 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700552 void __user *buf;
553 };
Jens Axboee47293f2019-12-20 08:58:21 -0700554 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700555 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700556 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700557 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700558};
559
Jens Axboe15b71ab2019-12-11 11:20:36 -0700560struct io_open {
561 struct file *file;
562 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700563 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700564 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600565 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700566};
567
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000568struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700569 struct file *file;
570 u64 arg;
571 u32 nr_args;
572 u32 offset;
573};
574
Jens Axboe4840e412019-12-25 22:03:45 -0700575struct io_fadvise {
576 struct file *file;
577 u64 offset;
578 u32 len;
579 u32 advice;
580};
581
Jens Axboec1ca7572019-12-25 22:18:28 -0700582struct io_madvise {
583 struct file *file;
584 u64 addr;
585 u32 len;
586 u32 advice;
587};
588
Jens Axboe3e4827b2020-01-08 15:18:09 -0700589struct io_epoll {
590 struct file *file;
591 int epfd;
592 int op;
593 int fd;
594 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595};
596
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300597struct io_splice {
598 struct file *file_out;
599 struct file *file_in;
600 loff_t off_out;
601 loff_t off_in;
602 u64 len;
603 unsigned int flags;
604};
605
Jens Axboeddf0322d2020-02-23 16:41:33 -0700606struct io_provide_buf {
607 struct file *file;
608 __u64 addr;
609 __s32 len;
610 __u32 bgid;
611 __u16 nbufs;
612 __u16 bid;
613};
614
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700615struct io_statx {
616 struct file *file;
617 int dfd;
618 unsigned int mask;
619 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700620 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700621 struct statx __user *buffer;
622};
623
Jens Axboe36f4fa62020-09-05 11:14:22 -0600624struct io_shutdown {
625 struct file *file;
626 int how;
627};
628
Jens Axboe80a261f2020-09-28 14:23:58 -0600629struct io_rename {
630 struct file *file;
631 int old_dfd;
632 int new_dfd;
633 struct filename *oldpath;
634 struct filename *newpath;
635 int flags;
636};
637
Jens Axboe14a11432020-09-28 14:27:37 -0600638struct io_unlink {
639 struct file *file;
640 int dfd;
641 int flags;
642 struct filename *filename;
643};
644
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300645struct io_completion {
646 struct file *file;
647 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300648 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300649};
650
Jens Axboef499a022019-12-02 16:28:46 -0700651struct io_async_connect {
652 struct sockaddr_storage address;
653};
654
Jens Axboe03b12302019-12-02 18:50:25 -0700655struct io_async_msghdr {
656 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000657 /* points to an allocated iov, if NULL we use fast_iov instead */
658 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700659 struct sockaddr __user *uaddr;
660 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700661 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700662};
663
Jens Axboef67676d2019-12-02 11:03:47 -0700664struct io_async_rw {
665 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600666 const struct iovec *free_iovec;
667 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600668 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600669 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700670};
671
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300672enum {
673 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
674 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
675 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
676 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
677 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700678 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300679
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680 REQ_F_FAIL_LINK_BIT,
681 REQ_F_INFLIGHT_BIT,
682 REQ_F_CUR_POS_BIT,
683 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300684 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300685 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300686 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700687 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700688 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600689 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100690 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000691 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700692
693 /* not a real bit, just to check we're not overflowing the space */
694 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300695};
696
697enum {
698 /* ctx owns file */
699 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
700 /* drain existing IO first */
701 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
702 /* linked sqes */
703 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
704 /* doesn't sever on completion < 0 */
705 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
706 /* IOSQE_ASYNC */
707 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700708 /* IOSQE_BUFFER_SELECT */
709 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300710
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300711 /* fail rest of links */
712 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
713 /* on inflight list */
714 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
715 /* read/write uses file position */
716 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
717 /* must not punt to workers */
718 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100719 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300720 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721 /* regular file */
722 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300723 /* needs cleanup */
724 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700725 /* already went through poll handler */
726 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700727 /* buffer already selected */
728 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600729 /* doesn't need file table for this request */
730 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100731 /* linked timeout is active, i.e. prepared by link's head */
732 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000733 /* completion is deferred through io_comp_state */
734 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700735};
736
737struct async_poll {
738 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600739 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300740};
741
Jens Axboe7cbf1722021-02-10 00:03:20 +0000742struct io_task_work {
743 struct io_wq_work_node node;
744 task_work_func_t func;
745};
746
Jens Axboe09bb8392019-03-13 12:39:28 -0600747/*
748 * NOTE! Each of the iocb union members has the file pointer
749 * as the first entry in their struct definition. So you can
750 * access the file pointer through any of the sub-structs,
751 * or directly as just 'ki_filp' in this struct.
752 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700753struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700754 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600755 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700756 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700757 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000758 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700759 struct io_accept accept;
760 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700761 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700762 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100763 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700764 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700765 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700766 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700767 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000768 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700769 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700770 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700771 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300772 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700773 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700774 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600775 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600776 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600777 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300778 /* use only after cleaning per-op data, see io_clean_op() */
779 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700780 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700781
Jens Axboee8c2bc12020-08-15 18:44:09 -0700782 /* opcode allocated if it needs to store data for async defer */
783 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700784 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800785 /* polled IO has completed */
786 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700788 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300789 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700790
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300791 struct io_ring_ctx *ctx;
792 unsigned int flags;
793 refcount_t refs;
794 struct task_struct *task;
795 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700796
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000797 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000798 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700799
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300800 /*
801 * 1. used with ctx->iopoll_list with reads/writes
802 * 2. to track reqs with ->files (see io_op_def::file_table)
803 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300804 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000805 union {
806 struct io_task_work io_task_work;
807 struct callback_head task_work;
808 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300809 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
810 struct hlist_node hash_node;
811 struct async_poll *apoll;
812 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813};
814
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300815struct io_defer_entry {
816 struct list_head list;
817 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300818 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300819};
820
Jens Axboed3656342019-12-18 09:50:26 -0700821struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700822 /* needs req->file assigned */
823 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700824 /* hash wq insertion if file is a regular file */
825 unsigned hash_reg_file : 1;
826 /* unbound wq insertion if file is a non-regular file */
827 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700828 /* opcode is not supported by this kernel */
829 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700830 /* set if opcode supports polled "wait" */
831 unsigned pollin : 1;
832 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700833 /* op supports buffer selection */
834 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 /* must always have async data allocated */
836 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600837 /* should block plug */
838 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 /* size of async data needed, if any */
840 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700841};
842
Jens Axboe09186822020-10-13 15:01:40 -0600843static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_NOP] = {},
845 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700846 .needs_file = 1,
847 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700848 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700849 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700850 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600851 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700852 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_file = 1,
856 .hash_reg_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700859 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600860 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700861 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700862 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300863 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700864 .needs_file = 1,
865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700867 .needs_file = 1,
868 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700869 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600870 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700871 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700872 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300873 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700874 .needs_file = 1,
875 .hash_reg_file = 1,
876 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700877 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600878 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700879 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700882 .needs_file = 1,
883 .unbound_nonreg_file = 1,
884 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300885 [IORING_OP_POLL_REMOVE] = {},
886 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700887 .needs_file = 1,
888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700892 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700893 .needs_async_data = 1,
894 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700897 .needs_file = 1,
898 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700899 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700900 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700901 .needs_async_data = 1,
902 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700903 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300904 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700907 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000908 [IORING_OP_TIMEOUT_REMOVE] = {
909 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000910 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300911 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700912 .needs_file = 1,
913 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700914 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700915 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300916 [IORING_OP_ASYNC_CANCEL] = {},
917 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700918 .needs_async_data = 1,
919 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700922 .needs_file = 1,
923 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700924 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700925 .needs_async_data = 1,
926 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700929 .needs_file = 1,
930 },
Jens Axboe44526be2021-02-15 13:32:18 -0700931 [IORING_OP_OPENAT] = {},
932 [IORING_OP_CLOSE] = {},
933 [IORING_OP_FILES_UPDATE] = {},
934 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700939 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700944 .needs_file = 1,
945 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700946 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600947 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700948 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700949 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300950 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700951 .needs_file = 1,
952 },
Jens Axboe44526be2021-02-15 13:32:18 -0700953 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700955 .needs_file = 1,
956 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700957 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700958 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300959 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700960 .needs_file = 1,
961 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700962 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700963 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700966 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700967 [IORING_OP_EPOLL_CTL] = {
968 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700969 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300970 [IORING_OP_SPLICE] = {
971 .needs_file = 1,
972 .hash_reg_file = 1,
973 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700974 },
975 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700976 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300977 [IORING_OP_TEE] = {
978 .needs_file = 1,
979 .hash_reg_file = 1,
980 .unbound_nonreg_file = 1,
981 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600982 [IORING_OP_SHUTDOWN] = {
983 .needs_file = 1,
984 },
Jens Axboe44526be2021-02-15 13:32:18 -0700985 [IORING_OP_RENAMEAT] = {},
986 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700987};
988
Pavel Begunkov9936c7c2021-02-04 13:51:56 +0000989static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
990 struct task_struct *task,
991 struct files_struct *files);
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700992static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000993static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +0000994static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000995 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +0000996static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000997
Pavel Begunkov23faba32021-02-11 18:28:22 +0000998static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700999static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001000static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001001static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001002static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001003static void io_dismantle_req(struct io_kiocb *req);
1004static void io_put_task(struct task_struct *task, int nr);
1005static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001006static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001007static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001008static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001009static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001010 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001011 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001012static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001013static struct file *io_file_get(struct io_submit_state *state,
1014 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001015static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001016static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001017
Pavel Begunkov847595d2021-02-04 13:52:06 +00001018static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1019 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001020static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1021 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001022 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001023static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001024static void io_submit_flush_completions(struct io_comp_state *cs,
1025 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001026
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027static struct kmem_cache *req_cachep;
1028
Jens Axboe09186822020-10-13 15:01:40 -06001029static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030
1031struct sock *io_uring_get_socket(struct file *file)
1032{
1033#if defined(CONFIG_UNIX)
1034 if (file->f_op == &io_uring_fops) {
1035 struct io_ring_ctx *ctx = file->private_data;
1036
1037 return ctx->ring_sock->sk;
1038 }
1039#endif
1040 return NULL;
1041}
1042EXPORT_SYMBOL(io_uring_get_socket);
1043
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001044#define io_for_each_link(pos, head) \
1045 for (pos = (head); pos; pos = pos->link)
1046
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001047static inline void io_clean_op(struct io_kiocb *req)
1048{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001049 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001050 __io_clean_op(req);
1051}
1052
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001053static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001054{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001055 struct io_ring_ctx *ctx = req->ctx;
1056
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001057 if (!req->fixed_rsrc_refs) {
1058 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1059 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001060 }
1061}
1062
Pavel Begunkov08d23632020-11-06 13:00:22 +00001063static bool io_match_task(struct io_kiocb *head,
1064 struct task_struct *task,
1065 struct files_struct *files)
1066{
1067 struct io_kiocb *req;
1068
Jens Axboe84965ff2021-01-23 15:51:11 -07001069 if (task && head->task != task) {
1070 /* in terms of cancelation, always match if req task is dead */
1071 if (head->task->flags & PF_EXITING)
1072 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001073 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001074 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001075 if (!files)
1076 return true;
1077
1078 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001079 if (req->file && req->file->f_op == &io_uring_fops)
1080 return true;
Jens Axboe4379bf82021-02-15 13:40:22 -07001081 if (req->task->files == files)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001082 return true;
1083 }
1084 return false;
1085}
1086
Jens Axboec40f6372020-06-25 15:39:59 -06001087static inline void req_set_fail_links(struct io_kiocb *req)
1088{
1089 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1090 req->flags |= REQ_F_FAIL_LINK;
1091}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001092
Jens Axboe2b188cc2019-01-07 10:46:33 -07001093static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1094{
1095 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1096
Jens Axboe0f158b42020-05-14 17:18:39 -06001097 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098}
1099
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001100static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1101{
1102 return !req->timeout.off;
1103}
1104
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1106{
1107 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001108 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109
1110 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1111 if (!ctx)
1112 return NULL;
1113
Jens Axboe78076bb2019-12-04 19:56:40 -07001114 /*
1115 * Use 5 bits less than the max cq entries, that should give us around
1116 * 32 entries per hash list if totally full and uniformly spread.
1117 */
1118 hash_bits = ilog2(p->cq_entries);
1119 hash_bits -= 5;
1120 if (hash_bits <= 0)
1121 hash_bits = 1;
1122 ctx->cancel_hash_bits = hash_bits;
1123 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1124 GFP_KERNEL);
1125 if (!ctx->cancel_hash)
1126 goto err;
1127 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1128
Roman Gushchin21482892019-05-07 10:01:48 -07001129 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001130 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1131 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132
1133 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001134 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001135 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001137 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001138 init_completion(&ctx->ref_comp);
1139 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001140 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001141 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001142 mutex_init(&ctx->uring_lock);
1143 init_waitqueue_head(&ctx->wait);
1144 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001145 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001146 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001147 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001148 spin_lock_init(&ctx->inflight_lock);
1149 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001150 spin_lock_init(&ctx->rsrc_ref_lock);
1151 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001152 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1153 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001154 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001155 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001157err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001158 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001159 kfree(ctx);
1160 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161}
1162
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001163static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001164{
Jens Axboe2bc99302020-07-09 09:43:27 -06001165 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1166 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001167
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001168 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001169 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001170 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001171
Bob Liu9d858b22019-11-13 18:06:25 +08001172 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001173}
1174
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001175static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001176{
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001177 if (req->flags & REQ_F_INFLIGHT) {
1178 struct io_ring_ctx *ctx = req->ctx;
1179 struct io_uring_task *tctx = req->task->io_uring;
1180 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001181
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001182 spin_lock_irqsave(&ctx->inflight_lock, flags);
1183 list_del(&req->inflight_entry);
1184 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1185 req->flags &= ~REQ_F_INFLIGHT;
1186 if (atomic_read(&tctx->in_idle))
1187 wake_up(&tctx->wait);
1188 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001189}
1190
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001191static void io_req_track_inflight(struct io_kiocb *req)
1192{
1193 struct io_ring_ctx *ctx = req->ctx;
1194
1195 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001196 req->flags |= REQ_F_INFLIGHT;
1197
1198 spin_lock_irq(&ctx->inflight_lock);
1199 list_add(&req->inflight_entry, &ctx->inflight_list);
1200 spin_unlock_irq(&ctx->inflight_lock);
1201 }
1202}
1203
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001204static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001205{
Jens Axboed3656342019-12-18 09:50:26 -07001206 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001207 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001208
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001209 if (req->flags & REQ_F_FORCE_ASYNC)
1210 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1211
Jens Axboed3656342019-12-18 09:50:26 -07001212 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001213 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001214 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001215 } else {
1216 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001217 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001218 }
Jens Axboe561fb042019-10-24 07:25:42 -06001219}
1220
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001221static void io_prep_async_link(struct io_kiocb *req)
1222{
1223 struct io_kiocb *cur;
1224
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001225 io_for_each_link(cur, req)
1226 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001227}
1228
Jens Axboe7271ef32020-08-10 09:55:22 -06001229static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001230{
Jackie Liua197f662019-11-08 08:09:12 -07001231 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001232 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001233 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001234
Jens Axboe3bfe6102021-02-16 14:15:30 -07001235 BUG_ON(!tctx);
1236 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001237
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001238 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1239 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001240 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001241 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001242}
1243
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001244static void io_queue_async_work(struct io_kiocb *req)
1245{
Jens Axboe7271ef32020-08-10 09:55:22 -06001246 struct io_kiocb *link;
1247
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001248 /* init ->work of the whole link before punting */
1249 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001250 link = __io_queue_async_work(req);
1251
1252 if (link)
1253 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001254}
1255
Jens Axboe5262f562019-09-17 12:26:57 -06001256static void io_kill_timeout(struct io_kiocb *req)
1257{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001258 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001259 int ret;
1260
Jens Axboee8c2bc12020-08-15 18:44:09 -07001261 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001262 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001263 atomic_set(&req->ctx->cq_timeouts,
1264 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001265 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001266 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001267 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001268 }
1269}
1270
Jens Axboe76e1b642020-09-26 15:05:03 -06001271/*
1272 * Returns true if we found and killed one or more timeouts
1273 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001274static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1275 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001276{
1277 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001278 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001279
1280 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001281 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001282 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001283 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001284 canceled++;
1285 }
Jens Axboef3606e32020-09-22 08:18:24 -06001286 }
Jens Axboe5262f562019-09-17 12:26:57 -06001287 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001288 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001289}
1290
Pavel Begunkov04518942020-05-26 20:34:05 +03001291static void __io_queue_deferred(struct io_ring_ctx *ctx)
1292{
1293 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001294 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1295 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001296
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001297 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001298 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001299 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001300 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001301 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001302 } while (!list_empty(&ctx->defer_list));
1303}
1304
Pavel Begunkov360428f2020-05-30 14:54:17 +03001305static void io_flush_timeouts(struct io_ring_ctx *ctx)
1306{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001307 u32 seq;
1308
1309 if (list_empty(&ctx->timeout_list))
1310 return;
1311
1312 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1313
1314 do {
1315 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001316 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001317 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001318
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001319 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001320 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001321
1322 /*
1323 * Since seq can easily wrap around over time, subtract
1324 * the last seq at which timeouts were flushed before comparing.
1325 * Assuming not more than 2^31-1 events have happened since,
1326 * these subtractions won't have wrapped, so we can check if
1327 * target is in [last_seq, current_seq] by comparing the two.
1328 */
1329 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1330 events_got = seq - ctx->cq_last_tm_flush;
1331 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001332 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001333
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001334 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001335 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001336 } while (!list_empty(&ctx->timeout_list));
1337
1338 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001339}
1340
Jens Axboede0617e2019-04-06 21:51:27 -06001341static void io_commit_cqring(struct io_ring_ctx *ctx)
1342{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001343 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001344
1345 /* order cqe stores with ring update */
1346 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001347
Pavel Begunkov04518942020-05-26 20:34:05 +03001348 if (unlikely(!list_empty(&ctx->defer_list)))
1349 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001350}
1351
Jens Axboe90554202020-09-03 12:12:41 -06001352static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1353{
1354 struct io_rings *r = ctx->rings;
1355
1356 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1357}
1358
Pavel Begunkov888aae22021-01-19 13:32:39 +00001359static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1360{
1361 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1362}
1363
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1365{
Hristo Venev75b28af2019-08-26 17:23:46 +00001366 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367 unsigned tail;
1368
Stefan Bühler115e12e2019-04-24 23:54:18 +02001369 /*
1370 * writes to the cq entry need to come after reading head; the
1371 * control dependency is enough as we're using WRITE_ONCE to
1372 * fill the cq entry
1373 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001374 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001375 return NULL;
1376
Pavel Begunkov888aae22021-01-19 13:32:39 +00001377 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001378 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379}
1380
Jens Axboef2842ab2020-01-08 11:04:00 -07001381static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1382{
Jens Axboef0b493e2020-02-01 21:30:11 -07001383 if (!ctx->cq_ev_fd)
1384 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001385 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1386 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001387 if (!ctx->eventfd_async)
1388 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001389 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001390}
1391
Jens Axboeb41e9852020-02-17 09:52:41 -07001392static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001393{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001394 /* see waitqueue_active() comment */
1395 smp_mb();
1396
Jens Axboe8c838782019-03-12 15:48:16 -06001397 if (waitqueue_active(&ctx->wait))
1398 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001399 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1400 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001401 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001402 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001403 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001404 wake_up_interruptible(&ctx->cq_wait);
1405 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1406 }
Jens Axboe8c838782019-03-12 15:48:16 -06001407}
1408
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001409static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1410{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001411 /* see waitqueue_active() comment */
1412 smp_mb();
1413
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001414 if (ctx->flags & IORING_SETUP_SQPOLL) {
1415 if (waitqueue_active(&ctx->wait))
1416 wake_up(&ctx->wait);
1417 }
1418 if (io_should_trigger_evfd(ctx))
1419 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001420 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001421 wake_up_interruptible(&ctx->cq_wait);
1422 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1423 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001424}
1425
Jens Axboec4a2ed72019-11-21 21:01:26 -07001426/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001427static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1428 struct task_struct *tsk,
1429 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001432 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001433 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001434 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001435 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436 LIST_HEAD(list);
1437
Pavel Begunkove23de152020-12-17 00:24:37 +00001438 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1439 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001440
Jens Axboeb18032b2021-01-24 16:58:56 -07001441 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001442 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001443 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001444 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001445 continue;
1446
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001447 cqe = io_get_cqring(ctx);
1448 if (!cqe && !force)
1449 break;
1450
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001451 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001452 if (cqe) {
1453 WRITE_ONCE(cqe->user_data, req->user_data);
1454 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001455 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001456 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001457 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001458 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001459 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001460 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001461 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001462 }
1463
Pavel Begunkov09e88402020-12-17 00:24:38 +00001464 all_flushed = list_empty(&ctx->cq_overflow_list);
1465 if (all_flushed) {
1466 clear_bit(0, &ctx->sq_check_overflow);
1467 clear_bit(0, &ctx->cq_check_overflow);
1468 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1469 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001470
Jens Axboeb18032b2021-01-24 16:58:56 -07001471 if (posted)
1472 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001473 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001474 if (posted)
1475 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001476
1477 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001478 req = list_first_entry(&list, struct io_kiocb, compl.list);
1479 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001480 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001481 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001482
Pavel Begunkov09e88402020-12-17 00:24:38 +00001483 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001484}
1485
Pavel Begunkov6c503152021-01-04 20:36:36 +00001486static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1487 struct task_struct *tsk,
1488 struct files_struct *files)
1489{
1490 if (test_bit(0, &ctx->cq_check_overflow)) {
1491 /* iopoll syncs against uring_lock, not completion_lock */
1492 if (ctx->flags & IORING_SETUP_IOPOLL)
1493 mutex_lock(&ctx->uring_lock);
1494 __io_cqring_overflow_flush(ctx, force, tsk, files);
1495 if (ctx->flags & IORING_SETUP_IOPOLL)
1496 mutex_unlock(&ctx->uring_lock);
1497 }
1498}
1499
Jens Axboebcda7ba2020-02-23 16:42:51 -07001500static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503 struct io_uring_cqe *cqe;
1504
Jens Axboe78e19bb2019-11-06 15:21:34 -07001505 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001506
Jens Axboe2b188cc2019-01-07 10:46:33 -07001507 /*
1508 * If we can't get a cq entry, userspace overflowed the
1509 * submission (by quite a lot). Increment the overflow count in
1510 * the ring.
1511 */
1512 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001513 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001514 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001515 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001516 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001517 } else if (ctx->cq_overflow_flushed ||
1518 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001519 /*
1520 * If we're in ring overflow flush mode, or in task cancel mode,
1521 * then we cannot store the request for later flushing, we need
1522 * to drop it on the floor.
1523 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001524 ctx->cached_cq_overflow++;
1525 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001526 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001527 if (list_empty(&ctx->cq_overflow_list)) {
1528 set_bit(0, &ctx->sq_check_overflow);
1529 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001530 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001531 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001532 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001533 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001534 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001535 refcount_inc(&req->refs);
1536 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001537 }
1538}
1539
Jens Axboebcda7ba2020-02-23 16:42:51 -07001540static void io_cqring_fill_event(struct io_kiocb *req, long res)
1541{
1542 __io_cqring_fill_event(req, res, 0);
1543}
1544
Jens Axboec7dae4b2021-02-09 19:53:37 -07001545static inline void io_req_complete_post(struct io_kiocb *req, long res,
1546 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001547{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001548 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549 unsigned long flags;
1550
1551 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001552 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001554 /*
1555 * If we're the last reference to this request, add to our locked
1556 * free_list cache.
1557 */
1558 if (refcount_dec_and_test(&req->refs)) {
1559 struct io_comp_state *cs = &ctx->submit_state.comp;
1560
1561 io_dismantle_req(req);
1562 io_put_task(req->task, 1);
1563 list_add(&req->compl.list, &cs->locked_free_list);
1564 cs->locked_free_nr++;
1565 } else
1566 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1568
Jens Axboe8c838782019-03-12 15:48:16 -06001569 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001570 if (req) {
1571 io_queue_next(req);
1572 percpu_ref_put(&ctx->refs);
1573 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574}
1575
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001576static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001577 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001578{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001579 io_clean_op(req);
1580 req->result = res;
1581 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001582 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001583}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584
Pavel Begunkov889fca72021-02-10 00:03:09 +00001585static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1586 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001587{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001588 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1589 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001590 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001591 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001592}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001593
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001594static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001595{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001596 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001597}
1598
Jens Axboec7dae4b2021-02-09 19:53:37 -07001599static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001600{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001601 struct io_submit_state *state = &ctx->submit_state;
1602 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001603 struct io_kiocb *req = NULL;
1604
Jens Axboec7dae4b2021-02-09 19:53:37 -07001605 /*
1606 * If we have more than a batch's worth of requests in our IRQ side
1607 * locked cache, grab the lock and move them over to our submission
1608 * side cache.
1609 */
1610 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1611 spin_lock_irq(&ctx->completion_lock);
1612 list_splice_init(&cs->locked_free_list, &cs->free_list);
1613 cs->locked_free_nr = 0;
1614 spin_unlock_irq(&ctx->completion_lock);
1615 }
1616
1617 while (!list_empty(&cs->free_list)) {
1618 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001619 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001620 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001621 state->reqs[state->free_reqs++] = req;
1622 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1623 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001624 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001625
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001626 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001627}
1628
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001629static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001630{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001631 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001632
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001633 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001634
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001635 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001636 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001637 int ret;
1638
Jens Axboec7dae4b2021-02-09 19:53:37 -07001639 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001640 goto got_req;
1641
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001642 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1643 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001644
1645 /*
1646 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1647 * retry single alloc to be on the safe side.
1648 */
1649 if (unlikely(ret <= 0)) {
1650 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1651 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001652 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001653 ret = 1;
1654 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001655 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001657got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001658 state->free_reqs--;
1659 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660}
1661
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001662static inline void io_put_file(struct io_kiocb *req, struct file *file,
1663 bool fixed)
1664{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001665 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001666 fput(file);
1667}
1668
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001669static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001671 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001672
Jens Axboee8c2bc12020-08-15 18:44:09 -07001673 if (req->async_data)
1674 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001675 if (req->file)
1676 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001677 if (req->fixed_rsrc_refs)
1678 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001679 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001680}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001681
Pavel Begunkov7c660732021-01-25 11:42:21 +00001682static inline void io_put_task(struct task_struct *task, int nr)
1683{
1684 struct io_uring_task *tctx = task->io_uring;
1685
1686 percpu_counter_sub(&tctx->inflight, nr);
1687 if (unlikely(atomic_read(&tctx->in_idle)))
1688 wake_up(&tctx->wait);
1689 put_task_struct_many(task, nr);
1690}
1691
Pavel Begunkov216578e2020-10-13 09:44:00 +01001692static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001693{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001694 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001695
Pavel Begunkov216578e2020-10-13 09:44:00 +01001696 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001697 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001698
Pavel Begunkov3893f392021-02-10 00:03:15 +00001699 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001700 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001701}
1702
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001703static inline void io_remove_next_linked(struct io_kiocb *req)
1704{
1705 struct io_kiocb *nxt = req->link;
1706
1707 req->link = nxt->link;
1708 nxt->link = NULL;
1709}
1710
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001711static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001712{
Jackie Liua197f662019-11-08 08:09:12 -07001713 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001714 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001715 bool cancelled = false;
1716 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001717
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001718 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001719 link = req->link;
1720
Pavel Begunkov900fad42020-10-19 16:39:16 +01001721 /*
1722 * Can happen if a linked timeout fired and link had been like
1723 * req -> link t-out -> link t-out [-> ...]
1724 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001725 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1726 struct io_timeout_data *io = link->async_data;
1727 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001728
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001729 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001730 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001731 ret = hrtimer_try_to_cancel(&io->timer);
1732 if (ret != -1) {
1733 io_cqring_fill_event(link, -ECANCELED);
1734 io_commit_cqring(ctx);
1735 cancelled = true;
1736 }
1737 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001738 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001739 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001740
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001741 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001742 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001743 io_put_req(link);
1744 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001745}
1746
Jens Axboe4d7dd462019-11-20 13:03:52 -07001747
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001748static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001749{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001750 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001751 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001752 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001753
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001754 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001755 link = req->link;
1756 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06001757
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001758 while (link) {
1759 nxt = link->link;
1760 link->link = NULL;
1761
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001762 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001763 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001764
Jens Axboe1575f212021-02-27 15:20:49 -07001765 io_put_req_deferred(link, 2);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001766 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001767 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001768 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001769 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001770
Jens Axboe2665abf2019-11-05 12:40:47 -07001771 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001772}
1773
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001774static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001775{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001776 if (req->flags & REQ_F_LINK_TIMEOUT)
1777 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001778
Jens Axboe9e645e112019-05-10 16:07:28 -06001779 /*
1780 * If LINK is set, we have dependent requests in this chain. If we
1781 * didn't fail this request, queue the first one up, moving any other
1782 * dependencies to the next request. In case of failure, fail the rest
1783 * of the chain.
1784 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001785 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
1786 struct io_kiocb *nxt = req->link;
1787
1788 req->link = NULL;
1789 return nxt;
1790 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001791 io_fail_links(req);
1792 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001793}
Jens Axboe2665abf2019-11-05 12:40:47 -07001794
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001795static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001796{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001797 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001798 return NULL;
1799 return __io_req_find_next(req);
1800}
1801
Pavel Begunkov2c323952021-02-28 22:04:53 +00001802static void ctx_flush_and_put(struct io_ring_ctx *ctx)
1803{
1804 if (!ctx)
1805 return;
1806 if (ctx->submit_state.comp.nr) {
1807 mutex_lock(&ctx->uring_lock);
1808 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1809 mutex_unlock(&ctx->uring_lock);
1810 }
1811 percpu_ref_put(&ctx->refs);
1812}
1813
Jens Axboe7cbf1722021-02-10 00:03:20 +00001814static bool __tctx_task_work(struct io_uring_task *tctx)
1815{
Jens Axboe65453d12021-02-10 00:03:21 +00001816 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001817 struct io_wq_work_list list;
1818 struct io_wq_work_node *node;
1819
1820 if (wq_list_empty(&tctx->task_list))
1821 return false;
1822
Jens Axboe0b81e802021-02-16 10:33:53 -07001823 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001824 list = tctx->task_list;
1825 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001826 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001827
1828 node = list.first;
1829 while (node) {
1830 struct io_wq_work_node *next = node->next;
1831 struct io_kiocb *req;
1832
1833 req = container_of(node, struct io_kiocb, io_task_work.node);
Pavel Begunkov2c323952021-02-28 22:04:53 +00001834 if (req->ctx != ctx) {
1835 ctx_flush_and_put(ctx);
1836 ctx = req->ctx;
1837 percpu_ref_get(&ctx->refs);
1838 }
1839
Jens Axboe7cbf1722021-02-10 00:03:20 +00001840 req->task_work.func(&req->task_work);
1841 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001842 }
1843
Pavel Begunkov2c323952021-02-28 22:04:53 +00001844 ctx_flush_and_put(ctx);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001845 return list.first != NULL;
1846}
1847
1848static void tctx_task_work(struct callback_head *cb)
1849{
1850 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
1851
Jens Axboe1d5f3602021-02-26 14:54:16 -07001852 clear_bit(0, &tctx->task_state);
1853
Jens Axboe7cbf1722021-02-10 00:03:20 +00001854 while (__tctx_task_work(tctx))
1855 cond_resched();
Jens Axboe7cbf1722021-02-10 00:03:20 +00001856}
1857
1858static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
1859 enum task_work_notify_mode notify)
1860{
1861 struct io_uring_task *tctx = tsk->io_uring;
1862 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07001863 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001864 int ret;
1865
1866 WARN_ON_ONCE(!tctx);
1867
Jens Axboe0b81e802021-02-16 10:33:53 -07001868 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001869 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001870 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001871
1872 /* task_work already pending, we're done */
1873 if (test_bit(0, &tctx->task_state) ||
1874 test_and_set_bit(0, &tctx->task_state))
1875 return 0;
1876
1877 if (!task_work_add(tsk, &tctx->task_work, notify))
1878 return 0;
1879
1880 /*
1881 * Slow path - we failed, find and delete work. if the work is not
1882 * in the list, it got run and we're fine.
1883 */
1884 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07001885 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001886 wq_list_for_each(node, prev, &tctx->task_list) {
1887 if (&req->io_task_work.node == node) {
1888 wq_list_del(&tctx->task_list, node, prev);
1889 ret = 1;
1890 break;
1891 }
1892 }
Jens Axboe0b81e802021-02-16 10:33:53 -07001893 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001894 clear_bit(0, &tctx->task_state);
1895 return ret;
1896}
1897
Jens Axboe355fb9e2020-10-22 20:19:35 -06001898static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06001899{
1900 struct task_struct *tsk = req->task;
1901 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06001902 enum task_work_notify_mode notify;
1903 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06001904
Jens Axboe6200b0a2020-09-13 14:38:30 -06001905 if (tsk->flags & PF_EXITING)
1906 return -ESRCH;
1907
Jens Axboec2c4c832020-07-01 15:37:11 -06001908 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001909 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1910 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1911 * processing task_work. There's no reliable way to tell if TWA_RESUME
1912 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001913 */
Jens Axboe91989c72020-10-16 09:02:26 -06001914 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001915 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06001916 notify = TWA_SIGNAL;
1917
Jens Axboe7cbf1722021-02-10 00:03:20 +00001918 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001919 if (!ret)
1920 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001921
Jens Axboec2c4c832020-07-01 15:37:11 -06001922 return ret;
1923}
1924
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001925static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00001926 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001927{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001928 struct io_ring_ctx *ctx = req->ctx;
1929 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001930
1931 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001932 do {
1933 head = READ_ONCE(ctx->exit_task_work);
1934 req->task_work.next = head;
1935 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001936}
1937
Jens Axboec40f6372020-06-25 15:39:59 -06001938static void __io_req_task_cancel(struct io_kiocb *req, int error)
1939{
1940 struct io_ring_ctx *ctx = req->ctx;
1941
1942 spin_lock_irq(&ctx->completion_lock);
1943 io_cqring_fill_event(req, error);
1944 io_commit_cqring(ctx);
1945 spin_unlock_irq(&ctx->completion_lock);
1946
1947 io_cqring_ev_posted(ctx);
1948 req_set_fail_links(req);
1949 io_double_put_req(req);
1950}
1951
1952static void io_req_task_cancel(struct callback_head *cb)
1953{
1954 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001955 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001956
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00001957 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00001958 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00001959 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001960 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001961}
1962
1963static void __io_req_task_submit(struct io_kiocb *req)
1964{
1965 struct io_ring_ctx *ctx = req->ctx;
1966
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00001967 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001968 mutex_lock(&ctx->uring_lock);
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00001969 if (!(current->flags & PF_EXITING) && !current->in_execve)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001970 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001971 else
Jens Axboec40f6372020-06-25 15:39:59 -06001972 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001973 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06001974}
1975
Jens Axboec40f6372020-06-25 15:39:59 -06001976static void io_req_task_submit(struct callback_head *cb)
1977{
1978 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1979
1980 __io_req_task_submit(req);
1981}
1982
1983static void io_req_task_queue(struct io_kiocb *req)
1984{
Jens Axboec40f6372020-06-25 15:39:59 -06001985 int ret;
1986
Jens Axboe7cbf1722021-02-10 00:03:20 +00001987 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001988 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06001989 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00001990 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00001991 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001992 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06001993 }
Jens Axboec40f6372020-06-25 15:39:59 -06001994}
1995
Pavel Begunkova3df76982021-02-18 22:32:52 +00001996static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
1997{
1998 percpu_ref_get(&req->ctx->refs);
1999 req->result = ret;
2000 req->task_work.func = io_req_task_cancel;
2001
2002 if (unlikely(io_req_task_work_add(req)))
2003 io_req_task_work_add_fallback(req, io_req_task_cancel);
2004}
2005
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002006static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002007{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002008 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002009
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002010 if (nxt)
2011 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002012}
2013
Jens Axboe9e645e112019-05-10 16:07:28 -06002014static void io_free_req(struct io_kiocb *req)
2015{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002016 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002017 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002018}
2019
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002020struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002021 struct task_struct *task;
2022 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002023 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002024};
2025
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002026static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002027{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002028 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002029 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002030 rb->task = NULL;
2031}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002032
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002033static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2034 struct req_batch *rb)
2035{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002036 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002037 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002038 if (rb->ctx_refs)
2039 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002040}
2041
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002042static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2043 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002044{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002045 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002046
Jens Axboee3bc8e92020-09-24 08:45:57 -06002047 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002048 if (rb->task)
2049 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002050 rb->task = req->task;
2051 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002052 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002053 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002054 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002055
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002056 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002057 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002058 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002059 else
2060 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002061}
2062
Pavel Begunkov905c1722021-02-10 00:03:14 +00002063static void io_submit_flush_completions(struct io_comp_state *cs,
2064 struct io_ring_ctx *ctx)
2065{
2066 int i, nr = cs->nr;
2067 struct io_kiocb *req;
2068 struct req_batch rb;
2069
2070 io_init_req_batch(&rb);
2071 spin_lock_irq(&ctx->completion_lock);
2072 for (i = 0; i < nr; i++) {
2073 req = cs->reqs[i];
2074 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2075 }
2076 io_commit_cqring(ctx);
2077 spin_unlock_irq(&ctx->completion_lock);
2078
2079 io_cqring_ev_posted(ctx);
2080 for (i = 0; i < nr; i++) {
2081 req = cs->reqs[i];
2082
2083 /* submission and completion refs */
2084 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002085 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002086 }
2087
2088 io_req_free_batch_finish(ctx, &rb);
2089 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002090}
2091
Jens Axboeba816ad2019-09-28 11:36:45 -06002092/*
2093 * Drop reference to request, return next in chain (if there is one) if this
2094 * was the last reference to this request.
2095 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002096static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002097{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002098 struct io_kiocb *nxt = NULL;
2099
Jens Axboe2a44f462020-02-25 13:25:41 -07002100 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002101 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002102 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002103 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002104 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002105}
2106
Jens Axboe2b188cc2019-01-07 10:46:33 -07002107static void io_put_req(struct io_kiocb *req)
2108{
Jens Axboedef596e2019-01-09 08:59:42 -07002109 if (refcount_dec_and_test(&req->refs))
2110 io_free_req(req);
2111}
2112
Pavel Begunkov216578e2020-10-13 09:44:00 +01002113static void io_put_req_deferred_cb(struct callback_head *cb)
2114{
2115 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2116
2117 io_free_req(req);
2118}
2119
2120static void io_free_req_deferred(struct io_kiocb *req)
2121{
2122 int ret;
2123
Jens Axboe7cbf1722021-02-10 00:03:20 +00002124 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002125 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002126 if (unlikely(ret))
2127 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002128}
2129
2130static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2131{
2132 if (refcount_sub_and_test(refs, &req->refs))
2133 io_free_req_deferred(req);
2134}
2135
Jens Axboe978db572019-11-14 22:39:04 -07002136static void io_double_put_req(struct io_kiocb *req)
2137{
2138 /* drop both submit and complete references */
2139 if (refcount_sub_and_test(2, &req->refs))
2140 io_free_req(req);
2141}
2142
Pavel Begunkov6c503152021-01-04 20:36:36 +00002143static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002144{
2145 /* See comment at the top of this file */
2146 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002147 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002148}
2149
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002150static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2151{
2152 struct io_rings *rings = ctx->rings;
2153
2154 /* make sure SQ entry isn't read before tail */
2155 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2156}
2157
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002158static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002159{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002160 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002161
Jens Axboebcda7ba2020-02-23 16:42:51 -07002162 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2163 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002164 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002165 kfree(kbuf);
2166 return cflags;
2167}
2168
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002169static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2170{
2171 struct io_buffer *kbuf;
2172
2173 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2174 return io_put_kbuf(req, kbuf);
2175}
2176
Jens Axboe4c6e2772020-07-01 11:29:10 -06002177static inline bool io_run_task_work(void)
2178{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002179 /*
2180 * Not safe to run on exiting task, and the task_work handling will
2181 * not add work to such a task.
2182 */
2183 if (unlikely(current->flags & PF_EXITING))
2184 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002185 if (current->task_works) {
2186 __set_current_state(TASK_RUNNING);
2187 task_work_run();
2188 return true;
2189 }
2190
2191 return false;
2192}
2193
Jens Axboedef596e2019-01-09 08:59:42 -07002194/*
2195 * Find and free completed poll iocbs
2196 */
2197static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2198 struct list_head *done)
2199{
Jens Axboe8237e042019-12-28 10:48:22 -07002200 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002201 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002202
2203 /* order with ->result store in io_complete_rw_iopoll() */
2204 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002205
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002206 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002207 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002208 int cflags = 0;
2209
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002210 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002211 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002212
Pavel Begunkovf1613402021-02-11 18:28:21 +00002213 if (READ_ONCE(req->result) == -EAGAIN) {
2214 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002215 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002216 continue;
2217 }
2218
Jens Axboebcda7ba2020-02-23 16:42:51 -07002219 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002220 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002221
2222 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002223 (*nr_events)++;
2224
Pavel Begunkovc3524382020-06-28 12:52:32 +03002225 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002226 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002227 }
Jens Axboedef596e2019-01-09 08:59:42 -07002228
Jens Axboe09bb8392019-03-13 12:39:28 -06002229 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002230 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002231 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002232}
2233
Jens Axboedef596e2019-01-09 08:59:42 -07002234static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2235 long min)
2236{
2237 struct io_kiocb *req, *tmp;
2238 LIST_HEAD(done);
2239 bool spin;
2240 int ret;
2241
2242 /*
2243 * Only spin for completions if we don't have multiple devices hanging
2244 * off our complete list, and we're under the requested amount.
2245 */
2246 spin = !ctx->poll_multi_file && *nr_events < min;
2247
2248 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002249 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002250 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002251
2252 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002253 * Move completed and retryable entries to our local lists.
2254 * If we find a request that requires polling, break out
2255 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002256 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002257 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002258 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002259 continue;
2260 }
2261 if (!list_empty(&done))
2262 break;
2263
2264 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2265 if (ret < 0)
2266 break;
2267
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002268 /* iopoll may have completed current req */
2269 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002270 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002271
Jens Axboedef596e2019-01-09 08:59:42 -07002272 if (ret && spin)
2273 spin = false;
2274 ret = 0;
2275 }
2276
2277 if (!list_empty(&done))
2278 io_iopoll_complete(ctx, nr_events, &done);
2279
2280 return ret;
2281}
2282
2283/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002284 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002285 * non-spinning poll check - we'll still enter the driver poll loop, but only
2286 * as a non-spinning completion check.
2287 */
2288static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2289 long min)
2290{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002291 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002292 int ret;
2293
2294 ret = io_do_iopoll(ctx, nr_events, min);
2295 if (ret < 0)
2296 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002297 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002298 return 0;
2299 }
2300
2301 return 1;
2302}
2303
2304/*
2305 * We can't just wait for polled events to come to us, we have to actively
2306 * find and complete them.
2307 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002308static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002309{
2310 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2311 return;
2312
2313 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002314 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002315 unsigned int nr_events = 0;
2316
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002317 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002318
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002319 /* let it sleep and repeat later if can't complete a request */
2320 if (nr_events == 0)
2321 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002322 /*
2323 * Ensure we allow local-to-the-cpu processing to take place,
2324 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002325 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002326 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002327 if (need_resched()) {
2328 mutex_unlock(&ctx->uring_lock);
2329 cond_resched();
2330 mutex_lock(&ctx->uring_lock);
2331 }
Jens Axboedef596e2019-01-09 08:59:42 -07002332 }
2333 mutex_unlock(&ctx->uring_lock);
2334}
2335
Pavel Begunkov7668b922020-07-07 16:36:21 +03002336static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002337{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002338 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002339 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002340
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002341 /*
2342 * We disallow the app entering submit/complete with polling, but we
2343 * still need to lock the ring to prevent racing with polled issue
2344 * that got punted to a workqueue.
2345 */
2346 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002347 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002348 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002349 * Don't enter poll loop if we already have events pending.
2350 * If we do, we can potentially be spinning for commands that
2351 * already triggered a CQE (eg in error).
2352 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002353 if (test_bit(0, &ctx->cq_check_overflow))
2354 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2355 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002356 break;
2357
2358 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002359 * If a submit got punted to a workqueue, we can have the
2360 * application entering polling for a command before it gets
2361 * issued. That app will hold the uring_lock for the duration
2362 * of the poll right here, so we need to take a breather every
2363 * now and then to ensure that the issue has a chance to add
2364 * the poll to the issued list. Otherwise we can spin here
2365 * forever, while the workqueue is stuck trying to acquire the
2366 * very same mutex.
2367 */
2368 if (!(++iters & 7)) {
2369 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002370 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002371 mutex_lock(&ctx->uring_lock);
2372 }
2373
Pavel Begunkov7668b922020-07-07 16:36:21 +03002374 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002375 if (ret <= 0)
2376 break;
2377 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002378 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002379
Jens Axboe500f9fb2019-08-19 12:15:59 -06002380 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002381 return ret;
2382}
2383
Jens Axboe491381ce2019-10-17 09:20:46 -06002384static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002385{
Jens Axboe491381ce2019-10-17 09:20:46 -06002386 /*
2387 * Tell lockdep we inherited freeze protection from submission
2388 * thread.
2389 */
2390 if (req->flags & REQ_F_ISREG) {
2391 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392
Jens Axboe491381ce2019-10-17 09:20:46 -06002393 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002395 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002396}
2397
Jens Axboeb63534c2020-06-04 11:28:00 -06002398#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002399static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002400{
2401 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002402 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002403 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002404
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002405 /* already prepared */
2406 if (req->async_data)
2407 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002408
2409 switch (req->opcode) {
2410 case IORING_OP_READV:
2411 case IORING_OP_READ_FIXED:
2412 case IORING_OP_READ:
2413 rw = READ;
2414 break;
2415 case IORING_OP_WRITEV:
2416 case IORING_OP_WRITE_FIXED:
2417 case IORING_OP_WRITE:
2418 rw = WRITE;
2419 break;
2420 default:
2421 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2422 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002423 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002424 }
2425
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002426 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2427 if (ret < 0)
2428 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002429 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002430}
Jens Axboeb63534c2020-06-04 11:28:00 -06002431#endif
2432
Pavel Begunkov23faba32021-02-11 18:28:22 +00002433static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002434{
2435#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002436 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002437
Jens Axboe355afae2020-09-02 09:30:31 -06002438 if (!S_ISBLK(mode) && !S_ISREG(mode))
2439 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002440 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002441 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002442 /*
2443 * If ref is dying, we might be running poll reap from the exit work.
2444 * Don't attempt to reissue from that path, just let it fail with
2445 * -EAGAIN.
2446 */
2447 if (percpu_ref_is_dying(&req->ctx->refs))
2448 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 Axboe491381ce2019-10-17 09:20:46 -06002490 if (kiocb->ki_flags & IOCB_WRITE)
2491 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002492
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002493 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002494 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002495
2496 WRITE_ONCE(req->result, res);
2497 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002498 smp_wmb();
2499 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002500}
2501
2502/*
2503 * After the iocb has been issued, it's safe to be found on the poll list.
2504 * Adding the kiocb to the list AFTER submission ensures that we don't
2505 * find it from a io_iopoll_getevents() thread before the issuer is done
2506 * accessing the kiocb cookie.
2507 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002508static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002509{
2510 struct io_ring_ctx *ctx = req->ctx;
2511
2512 /*
2513 * Track whether we have multiple files in our lists. This will impact
2514 * how we do polling eventually, not spinning if we're on potentially
2515 * different devices.
2516 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002517 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002518 ctx->poll_multi_file = false;
2519 } else if (!ctx->poll_multi_file) {
2520 struct io_kiocb *list_req;
2521
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002522 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002523 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002524 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002525 ctx->poll_multi_file = true;
2526 }
2527
2528 /*
2529 * For fast devices, IO may have already completed. If it has, add
2530 * it to the front so we find it first.
2531 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002532 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002533 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002534 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002535 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002536
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002537 /*
2538 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2539 * task context or in io worker task context. If current task context is
2540 * sq thread, we don't need to check whether should wake up sq thread.
2541 */
2542 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002543 wq_has_sleeper(&ctx->sq_data->wait))
2544 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002545}
2546
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002547static inline void io_state_file_put(struct io_submit_state *state)
2548{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002549 if (state->file_refs) {
2550 fput_many(state->file, state->file_refs);
2551 state->file_refs = 0;
2552 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002553}
2554
2555/*
2556 * Get as many references to a file as we have IOs left in this submission,
2557 * assuming most submissions are for one file, or at least that each file
2558 * has more than one submission.
2559 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002560static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002561{
2562 if (!state)
2563 return fget(fd);
2564
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002565 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002566 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002567 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002568 return state->file;
2569 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002570 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002571 }
2572 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002573 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002574 return NULL;
2575
2576 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002577 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002578 return state->file;
2579}
2580
Jens Axboe4503b762020-06-01 10:00:27 -06002581static bool io_bdev_nowait(struct block_device *bdev)
2582{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002583 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002584}
2585
Jens Axboe2b188cc2019-01-07 10:46:33 -07002586/*
2587 * If we tracked the file through the SCM inflight mechanism, we could support
2588 * any file. For now, just ensure that anything potentially problematic is done
2589 * inline.
2590 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002591static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002592{
2593 umode_t mode = file_inode(file)->i_mode;
2594
Jens Axboe4503b762020-06-01 10:00:27 -06002595 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002596 if (IS_ENABLED(CONFIG_BLOCK) &&
2597 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002598 return true;
2599 return false;
2600 }
2601 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002602 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002603 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002604 if (IS_ENABLED(CONFIG_BLOCK) &&
2605 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002606 file->f_op != &io_uring_fops)
2607 return true;
2608 return false;
2609 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002610
Jens Axboec5b85622020-06-09 19:23:05 -06002611 /* any ->read/write should understand O_NONBLOCK */
2612 if (file->f_flags & O_NONBLOCK)
2613 return true;
2614
Jens Axboeaf197f52020-04-28 13:15:06 -06002615 if (!(file->f_mode & FMODE_NOWAIT))
2616 return false;
2617
2618 if (rw == READ)
2619 return file->f_op->read_iter != NULL;
2620
2621 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002622}
2623
Pavel Begunkova88fc402020-09-30 22:57:53 +03002624static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625{
Jens Axboedef596e2019-01-09 08:59:42 -07002626 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002627 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002628 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002629 unsigned ioprio;
2630 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002632 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002633 req->flags |= REQ_F_ISREG;
2634
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002636 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002637 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002638 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002639 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002640 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002641 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2642 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2643 if (unlikely(ret))
2644 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002646 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2647 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2648 req->flags |= REQ_F_NOWAIT;
2649
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650 ioprio = READ_ONCE(sqe->ioprio);
2651 if (ioprio) {
2652 ret = ioprio_check_cap(ioprio);
2653 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002654 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002655
2656 kiocb->ki_ioprio = ioprio;
2657 } else
2658 kiocb->ki_ioprio = get_current_ioprio();
2659
Jens Axboedef596e2019-01-09 08:59:42 -07002660 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002661 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2662 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002663 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664
Jens Axboedef596e2019-01-09 08:59:42 -07002665 kiocb->ki_flags |= IOCB_HIPRI;
2666 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002667 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002668 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002669 if (kiocb->ki_flags & IOCB_HIPRI)
2670 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002671 kiocb->ki_complete = io_complete_rw;
2672 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002673
Jens Axboe3529d8c2019-12-19 18:24:38 -07002674 req->rw.addr = READ_ONCE(sqe->addr);
2675 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002676 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002678}
2679
2680static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2681{
2682 switch (ret) {
2683 case -EIOCBQUEUED:
2684 break;
2685 case -ERESTARTSYS:
2686 case -ERESTARTNOINTR:
2687 case -ERESTARTNOHAND:
2688 case -ERESTART_RESTARTBLOCK:
2689 /*
2690 * We can't just restart the syscall, since previously
2691 * submitted sqes may already be in progress. Just fail this
2692 * IO with EINTR.
2693 */
2694 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002695 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002696 default:
2697 kiocb->ki_complete(kiocb, ret, 0);
2698 }
2699}
2700
Jens Axboea1d7c392020-06-22 11:09:46 -06002701static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002702 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002703{
Jens Axboeba042912019-12-25 16:33:42 -07002704 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002705 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002706
Jens Axboe227c0c92020-08-13 11:51:40 -06002707 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002708 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002709 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002710 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002711 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002712 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002713 }
2714
Jens Axboeba042912019-12-25 16:33:42 -07002715 if (req->flags & REQ_F_CUR_POS)
2716 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002717 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002718 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002719 else
2720 io_rw_done(kiocb, ret);
2721}
2722
Pavel Begunkov847595d2021-02-04 13:52:06 +00002723static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002724{
Jens Axboe9adbd452019-12-20 08:45:55 -07002725 struct io_ring_ctx *ctx = req->ctx;
2726 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002727 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002728 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002729 size_t offset;
2730 u64 buf_addr;
2731
Jens Axboeedafcce2019-01-09 09:16:05 -07002732 if (unlikely(buf_index >= ctx->nr_user_bufs))
2733 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002734 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2735 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002736 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002737
2738 /* overflow */
2739 if (buf_addr + len < buf_addr)
2740 return -EFAULT;
2741 /* not inside the mapped region */
2742 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2743 return -EFAULT;
2744
2745 /*
2746 * May not be a start of buffer, set size appropriately
2747 * and advance us to the beginning.
2748 */
2749 offset = buf_addr - imu->ubuf;
2750 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002751
2752 if (offset) {
2753 /*
2754 * Don't use iov_iter_advance() here, as it's really slow for
2755 * using the latter parts of a big fixed buffer - it iterates
2756 * over each segment manually. We can cheat a bit here, because
2757 * we know that:
2758 *
2759 * 1) it's a BVEC iter, we set it up
2760 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2761 * first and last bvec
2762 *
2763 * So just find our index, and adjust the iterator afterwards.
2764 * If the offset is within the first bvec (or the whole first
2765 * bvec, just use iov_iter_advance(). This makes it easier
2766 * since we can just skip the first segment, which may not
2767 * be PAGE_SIZE aligned.
2768 */
2769 const struct bio_vec *bvec = imu->bvec;
2770
2771 if (offset <= bvec->bv_len) {
2772 iov_iter_advance(iter, offset);
2773 } else {
2774 unsigned long seg_skip;
2775
2776 /* skip first vec */
2777 offset -= bvec->bv_len;
2778 seg_skip = 1 + (offset >> PAGE_SHIFT);
2779
2780 iter->bvec = bvec + seg_skip;
2781 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002782 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002783 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002784 }
2785 }
2786
Pavel Begunkov847595d2021-02-04 13:52:06 +00002787 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002788}
2789
Jens Axboebcda7ba2020-02-23 16:42:51 -07002790static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2791{
2792 if (needs_lock)
2793 mutex_unlock(&ctx->uring_lock);
2794}
2795
2796static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2797{
2798 /*
2799 * "Normal" inline submissions always hold the uring_lock, since we
2800 * grab it from the system call. Same is true for the SQPOLL offload.
2801 * The only exception is when we've detached the request and issue it
2802 * from an async worker thread, grab the lock for that case.
2803 */
2804 if (needs_lock)
2805 mutex_lock(&ctx->uring_lock);
2806}
2807
2808static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2809 int bgid, struct io_buffer *kbuf,
2810 bool needs_lock)
2811{
2812 struct io_buffer *head;
2813
2814 if (req->flags & REQ_F_BUFFER_SELECTED)
2815 return kbuf;
2816
2817 io_ring_submit_lock(req->ctx, needs_lock);
2818
2819 lockdep_assert_held(&req->ctx->uring_lock);
2820
2821 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2822 if (head) {
2823 if (!list_empty(&head->list)) {
2824 kbuf = list_last_entry(&head->list, struct io_buffer,
2825 list);
2826 list_del(&kbuf->list);
2827 } else {
2828 kbuf = head;
2829 idr_remove(&req->ctx->io_buffer_idr, bgid);
2830 }
2831 if (*len > kbuf->len)
2832 *len = kbuf->len;
2833 } else {
2834 kbuf = ERR_PTR(-ENOBUFS);
2835 }
2836
2837 io_ring_submit_unlock(req->ctx, needs_lock);
2838
2839 return kbuf;
2840}
2841
Jens Axboe4d954c22020-02-27 07:31:19 -07002842static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2843 bool needs_lock)
2844{
2845 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002846 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002847
2848 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002849 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002850 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2851 if (IS_ERR(kbuf))
2852 return kbuf;
2853 req->rw.addr = (u64) (unsigned long) kbuf;
2854 req->flags |= REQ_F_BUFFER_SELECTED;
2855 return u64_to_user_ptr(kbuf->addr);
2856}
2857
2858#ifdef CONFIG_COMPAT
2859static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2860 bool needs_lock)
2861{
2862 struct compat_iovec __user *uiov;
2863 compat_ssize_t clen;
2864 void __user *buf;
2865 ssize_t len;
2866
2867 uiov = u64_to_user_ptr(req->rw.addr);
2868 if (!access_ok(uiov, sizeof(*uiov)))
2869 return -EFAULT;
2870 if (__get_user(clen, &uiov->iov_len))
2871 return -EFAULT;
2872 if (clen < 0)
2873 return -EINVAL;
2874
2875 len = clen;
2876 buf = io_rw_buffer_select(req, &len, needs_lock);
2877 if (IS_ERR(buf))
2878 return PTR_ERR(buf);
2879 iov[0].iov_base = buf;
2880 iov[0].iov_len = (compat_size_t) len;
2881 return 0;
2882}
2883#endif
2884
2885static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2886 bool needs_lock)
2887{
2888 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2889 void __user *buf;
2890 ssize_t len;
2891
2892 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2893 return -EFAULT;
2894
2895 len = iov[0].iov_len;
2896 if (len < 0)
2897 return -EINVAL;
2898 buf = io_rw_buffer_select(req, &len, needs_lock);
2899 if (IS_ERR(buf))
2900 return PTR_ERR(buf);
2901 iov[0].iov_base = buf;
2902 iov[0].iov_len = len;
2903 return 0;
2904}
2905
2906static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2907 bool needs_lock)
2908{
Jens Axboedddb3e22020-06-04 11:27:01 -06002909 if (req->flags & REQ_F_BUFFER_SELECTED) {
2910 struct io_buffer *kbuf;
2911
2912 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2913 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2914 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002915 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002916 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00002917 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07002918 return -EINVAL;
2919
2920#ifdef CONFIG_COMPAT
2921 if (req->ctx->compat)
2922 return io_compat_import(req, iov, needs_lock);
2923#endif
2924
2925 return __io_iov_buffer_select(req, iov, needs_lock);
2926}
2927
Pavel Begunkov847595d2021-02-04 13:52:06 +00002928static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
2929 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002930{
Jens Axboe9adbd452019-12-20 08:45:55 -07002931 void __user *buf = u64_to_user_ptr(req->rw.addr);
2932 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002933 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07002934 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002935
Pavel Begunkov7d009162019-11-25 23:14:40 +03002936 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002937 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002938 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002939 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002940
Jens Axboebcda7ba2020-02-23 16:42:51 -07002941 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002942 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002943 return -EINVAL;
2944
Jens Axboe3a6820f2019-12-22 15:19:35 -07002945 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002946 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002947 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002948 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002949 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002950 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002951 }
2952
Jens Axboe3a6820f2019-12-22 15:19:35 -07002953 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2954 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00002955 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002956 }
2957
Jens Axboe4d954c22020-02-27 07:31:19 -07002958 if (req->flags & REQ_F_BUFFER_SELECT) {
2959 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00002960 if (!ret)
2961 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07002962 *iovec = NULL;
2963 return ret;
2964 }
2965
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002966 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
2967 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968}
2969
Jens Axboe0fef9482020-08-26 10:36:20 -06002970static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2971{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03002972 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06002973}
2974
Jens Axboe32960612019-09-23 11:05:34 -06002975/*
2976 * For files that don't have ->read_iter() and ->write_iter(), handle them
2977 * by looping over ->read() or ->write() manually.
2978 */
Jens Axboe4017eb92020-10-22 14:14:12 -06002979static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06002980{
Jens Axboe4017eb92020-10-22 14:14:12 -06002981 struct kiocb *kiocb = &req->rw.kiocb;
2982 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06002983 ssize_t ret = 0;
2984
2985 /*
2986 * Don't support polled IO through this interface, and we can't
2987 * support non-blocking either. For the latter, this just causes
2988 * the kiocb to be handled from an async context.
2989 */
2990 if (kiocb->ki_flags & IOCB_HIPRI)
2991 return -EOPNOTSUPP;
2992 if (kiocb->ki_flags & IOCB_NOWAIT)
2993 return -EAGAIN;
2994
2995 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002996 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002997 ssize_t nr;
2998
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002999 if (!iov_iter_is_bvec(iter)) {
3000 iovec = iov_iter_iovec(iter);
3001 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003002 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3003 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003004 }
3005
Jens Axboe32960612019-09-23 11:05:34 -06003006 if (rw == READ) {
3007 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003008 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003009 } else {
3010 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003011 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003012 }
3013
3014 if (nr < 0) {
3015 if (!ret)
3016 ret = nr;
3017 break;
3018 }
3019 ret += nr;
3020 if (nr != iovec.iov_len)
3021 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003022 req->rw.len -= nr;
3023 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003024 iov_iter_advance(iter, nr);
3025 }
3026
3027 return ret;
3028}
3029
Jens Axboeff6165b2020-08-13 09:47:43 -06003030static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3031 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003032{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003033 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003034
Jens Axboeff6165b2020-08-13 09:47:43 -06003035 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003036 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003037 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003038 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003039 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003040 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003041 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003042 unsigned iov_off = 0;
3043
3044 rw->iter.iov = rw->fast_iov;
3045 if (iter->iov != fast_iov) {
3046 iov_off = iter->iov - fast_iov;
3047 rw->iter.iov += iov_off;
3048 }
3049 if (rw->fast_iov != fast_iov)
3050 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003051 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003052 } else {
3053 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003054 }
3055}
3056
Jens Axboee8c2bc12020-08-15 18:44:09 -07003057static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003058{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003059 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3060 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3061 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003062}
3063
Jens Axboee8c2bc12020-08-15 18:44:09 -07003064static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003065{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003066 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003067 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003068
Jens Axboee8c2bc12020-08-15 18:44:09 -07003069 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003070}
3071
Jens Axboeff6165b2020-08-13 09:47:43 -06003072static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3073 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003074 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003075{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003076 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003077 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003078 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003079 if (__io_alloc_async_data(req)) {
3080 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003081 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003082 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003083
Jens Axboeff6165b2020-08-13 09:47:43 -06003084 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003085 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003086 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003087}
3088
Pavel Begunkov73debe62020-09-30 22:57:54 +03003089static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003090{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003091 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003092 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003093 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003094
Pavel Begunkov2846c482020-11-07 13:16:27 +00003095 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003096 if (unlikely(ret < 0))
3097 return ret;
3098
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003099 iorw->bytes_done = 0;
3100 iorw->free_iovec = iov;
3101 if (iov)
3102 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003103 return 0;
3104}
3105
Pavel Begunkov73debe62020-09-30 22:57:54 +03003106static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003107{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003108 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3109 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003110 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003111}
3112
Jens Axboec1dd91d2020-08-03 16:43:59 -06003113/*
3114 * This is our waitqueue callback handler, registered through lock_page_async()
3115 * when we initially tried to do the IO with the iocb armed our waitqueue.
3116 * This gets called when the page is unlocked, and we generally expect that to
3117 * happen when the page IO is completed and the page is now uptodate. This will
3118 * queue a task_work based retry of the operation, attempting to copy the data
3119 * again. If the latter fails because the page was NOT uptodate, then we will
3120 * do a thread based blocking retry of the operation. That's the unexpected
3121 * slow path.
3122 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003123static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3124 int sync, void *arg)
3125{
3126 struct wait_page_queue *wpq;
3127 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003128 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003129
3130 wpq = container_of(wait, struct wait_page_queue, wait);
3131
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003132 if (!wake_page_match(wpq, key))
3133 return 0;
3134
Hao Xuc8d317a2020-09-29 20:00:45 +08003135 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003136 list_del_init(&wait->entry);
3137
Jens Axboebcf5a062020-05-22 09:24:42 -06003138 /* submit ref gets dropped, acquire a new one */
3139 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003140 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003141 return 1;
3142}
3143
Jens Axboec1dd91d2020-08-03 16:43:59 -06003144/*
3145 * This controls whether a given IO request should be armed for async page
3146 * based retry. If we return false here, the request is handed to the async
3147 * worker threads for retry. If we're doing buffered reads on a regular file,
3148 * we prepare a private wait_page_queue entry and retry the operation. This
3149 * will either succeed because the page is now uptodate and unlocked, or it
3150 * will register a callback when the page is unlocked at IO completion. Through
3151 * that callback, io_uring uses task_work to setup a retry of the operation.
3152 * That retry will attempt the buffered read again. The retry will generally
3153 * succeed, or in rare cases where it fails, we then fall back to using the
3154 * async worker threads for a blocking retry.
3155 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003156static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003157{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003158 struct io_async_rw *rw = req->async_data;
3159 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003160 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003161
3162 /* never retry for NOWAIT, we just complete with -EAGAIN */
3163 if (req->flags & REQ_F_NOWAIT)
3164 return false;
3165
Jens Axboe227c0c92020-08-13 11:51:40 -06003166 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003167 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003168 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003169
Jens Axboebcf5a062020-05-22 09:24:42 -06003170 /*
3171 * just use poll if we can, and don't attempt if the fs doesn't
3172 * support callback based unlocks
3173 */
3174 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3175 return false;
3176
Jens Axboe3b2a4432020-08-16 10:58:43 -07003177 wait->wait.func = io_async_buf_func;
3178 wait->wait.private = req;
3179 wait->wait.flags = 0;
3180 INIT_LIST_HEAD(&wait->wait.entry);
3181 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003182 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003183 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003184 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003185}
3186
3187static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3188{
3189 if (req->file->f_op->read_iter)
3190 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003191 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003192 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003193 else
3194 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003195}
3196
Pavel Begunkov889fca72021-02-10 00:03:09 +00003197static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003198{
3199 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003200 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003201 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003202 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003203 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003204 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003205
Pavel Begunkov2846c482020-11-07 13:16:27 +00003206 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003207 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003208 iovec = NULL;
3209 } else {
3210 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3211 if (ret < 0)
3212 return ret;
3213 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003214 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003215 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003216
Jens Axboefd6c2e42019-12-18 12:19:41 -07003217 /* Ensure we clear previously set non-block flag */
3218 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003219 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003220 else
3221 kiocb->ki_flags |= IOCB_NOWAIT;
3222
Pavel Begunkov24c74672020-06-21 13:09:51 +03003223 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003224 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3225 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003226 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003227 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003228
Pavel Begunkov632546c2020-11-07 13:16:26 +00003229 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003230 if (unlikely(ret)) {
3231 kfree(iovec);
3232 return ret;
3233 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003234
Jens Axboe227c0c92020-08-13 11:51:40 -06003235 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003236
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003237 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003238 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003239 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003240 /* IOPOLL retry should happen for io-wq threads */
3241 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003242 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003243 /* no retry on NONBLOCK nor RWF_NOWAIT */
3244 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003245 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003246 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003247 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003248 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003249 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003250 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003251 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003252 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003253 }
3254
Jens Axboe227c0c92020-08-13 11:51:40 -06003255 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003256 if (ret2)
3257 return ret2;
3258
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003259 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003260 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003261 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003262 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003263
Pavel Begunkovb23df912021-02-04 13:52:04 +00003264 do {
3265 io_size -= ret;
3266 rw->bytes_done += ret;
3267 /* if we can retry, do so with the callbacks armed */
3268 if (!io_rw_should_retry(req)) {
3269 kiocb->ki_flags &= ~IOCB_WAITQ;
3270 return -EAGAIN;
3271 }
3272
3273 /*
3274 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3275 * we get -EIOCBQUEUED, then we'll get a notification when the
3276 * desired page gets unlocked. We can also get a partial read
3277 * here, and if we do, then just retry at the new offset.
3278 */
3279 ret = io_iter_do_read(req, iter);
3280 if (ret == -EIOCBQUEUED)
3281 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003282 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003283 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003284done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003285 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003286out_free:
3287 /* it's faster to check here then delegate to kfree */
3288 if (iovec)
3289 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003290 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003291}
3292
Pavel Begunkov73debe62020-09-30 22:57:54 +03003293static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003294{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003295 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3296 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003297 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003298}
3299
Pavel Begunkov889fca72021-02-10 00:03:09 +00003300static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003301{
3302 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003303 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003304 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003305 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003306 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003307 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003308
Pavel Begunkov2846c482020-11-07 13:16:27 +00003309 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003310 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003311 iovec = NULL;
3312 } else {
3313 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3314 if (ret < 0)
3315 return ret;
3316 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003317 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003318 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003319
Jens Axboefd6c2e42019-12-18 12:19:41 -07003320 /* Ensure we clear previously set non-block flag */
3321 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003322 kiocb->ki_flags &= ~IOCB_NOWAIT;
3323 else
3324 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003325
Pavel Begunkov24c74672020-06-21 13:09:51 +03003326 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003327 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003328 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003329
Jens Axboe10d59342019-12-09 20:16:22 -07003330 /* file path doesn't support NOWAIT for non-direct_IO */
3331 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3332 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003333 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003334
Pavel Begunkov632546c2020-11-07 13:16:26 +00003335 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003336 if (unlikely(ret))
3337 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003338
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003339 /*
3340 * Open-code file_start_write here to grab freeze protection,
3341 * which will be released by another thread in
3342 * io_complete_rw(). Fool lockdep by telling it the lock got
3343 * released so that it doesn't complain about the held lock when
3344 * we return to userspace.
3345 */
3346 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003347 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003348 __sb_writers_release(file_inode(req->file)->i_sb,
3349 SB_FREEZE_WRITE);
3350 }
3351 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003352
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003353 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003354 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003355 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003356 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003357 else
3358 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003359
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003360 /*
3361 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3362 * retry them without IOCB_NOWAIT.
3363 */
3364 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3365 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003366 /* no retry on NONBLOCK nor RWF_NOWAIT */
3367 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003368 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003369 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003370 /* IOPOLL retry should happen for io-wq threads */
3371 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3372 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003373done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003374 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003375 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003376copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003377 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003378 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003379 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003380 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003381 }
Jens Axboe31b51512019-01-18 22:56:34 -07003382out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003383 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003384 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003385 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003386 return ret;
3387}
3388
Jens Axboe80a261f2020-09-28 14:23:58 -06003389static int io_renameat_prep(struct io_kiocb *req,
3390 const struct io_uring_sqe *sqe)
3391{
3392 struct io_rename *ren = &req->rename;
3393 const char __user *oldf, *newf;
3394
3395 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3396 return -EBADF;
3397
3398 ren->old_dfd = READ_ONCE(sqe->fd);
3399 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3400 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3401 ren->new_dfd = READ_ONCE(sqe->len);
3402 ren->flags = READ_ONCE(sqe->rename_flags);
3403
3404 ren->oldpath = getname(oldf);
3405 if (IS_ERR(ren->oldpath))
3406 return PTR_ERR(ren->oldpath);
3407
3408 ren->newpath = getname(newf);
3409 if (IS_ERR(ren->newpath)) {
3410 putname(ren->oldpath);
3411 return PTR_ERR(ren->newpath);
3412 }
3413
3414 req->flags |= REQ_F_NEED_CLEANUP;
3415 return 0;
3416}
3417
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003418static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003419{
3420 struct io_rename *ren = &req->rename;
3421 int ret;
3422
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003423 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003424 return -EAGAIN;
3425
3426 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3427 ren->newpath, ren->flags);
3428
3429 req->flags &= ~REQ_F_NEED_CLEANUP;
3430 if (ret < 0)
3431 req_set_fail_links(req);
3432 io_req_complete(req, ret);
3433 return 0;
3434}
3435
Jens Axboe14a11432020-09-28 14:27:37 -06003436static int io_unlinkat_prep(struct io_kiocb *req,
3437 const struct io_uring_sqe *sqe)
3438{
3439 struct io_unlink *un = &req->unlink;
3440 const char __user *fname;
3441
3442 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3443 return -EBADF;
3444
3445 un->dfd = READ_ONCE(sqe->fd);
3446
3447 un->flags = READ_ONCE(sqe->unlink_flags);
3448 if (un->flags & ~AT_REMOVEDIR)
3449 return -EINVAL;
3450
3451 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3452 un->filename = getname(fname);
3453 if (IS_ERR(un->filename))
3454 return PTR_ERR(un->filename);
3455
3456 req->flags |= REQ_F_NEED_CLEANUP;
3457 return 0;
3458}
3459
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003460static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003461{
3462 struct io_unlink *un = &req->unlink;
3463 int ret;
3464
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003465 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003466 return -EAGAIN;
3467
3468 if (un->flags & AT_REMOVEDIR)
3469 ret = do_rmdir(un->dfd, un->filename);
3470 else
3471 ret = do_unlinkat(un->dfd, un->filename);
3472
3473 req->flags &= ~REQ_F_NEED_CLEANUP;
3474 if (ret < 0)
3475 req_set_fail_links(req);
3476 io_req_complete(req, ret);
3477 return 0;
3478}
3479
Jens Axboe36f4fa62020-09-05 11:14:22 -06003480static int io_shutdown_prep(struct io_kiocb *req,
3481 const struct io_uring_sqe *sqe)
3482{
3483#if defined(CONFIG_NET)
3484 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3485 return -EINVAL;
3486 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3487 sqe->buf_index)
3488 return -EINVAL;
3489
3490 req->shutdown.how = READ_ONCE(sqe->len);
3491 return 0;
3492#else
3493 return -EOPNOTSUPP;
3494#endif
3495}
3496
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003497static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003498{
3499#if defined(CONFIG_NET)
3500 struct socket *sock;
3501 int ret;
3502
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003503 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003504 return -EAGAIN;
3505
Linus Torvalds48aba792020-12-16 12:44:05 -08003506 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003507 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003508 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003509
3510 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003511 if (ret < 0)
3512 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003513 io_req_complete(req, ret);
3514 return 0;
3515#else
3516 return -EOPNOTSUPP;
3517#endif
3518}
3519
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003520static int __io_splice_prep(struct io_kiocb *req,
3521 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003522{
3523 struct io_splice* sp = &req->splice;
3524 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003525
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003526 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3527 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003528
3529 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003530 sp->len = READ_ONCE(sqe->len);
3531 sp->flags = READ_ONCE(sqe->splice_flags);
3532
3533 if (unlikely(sp->flags & ~valid_flags))
3534 return -EINVAL;
3535
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003536 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3537 (sp->flags & SPLICE_F_FD_IN_FIXED));
3538 if (!sp->file_in)
3539 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003540 req->flags |= REQ_F_NEED_CLEANUP;
3541
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003542 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3543 /*
3544 * Splice operation will be punted aync, and here need to
3545 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3546 */
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003547 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003548 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003549
3550 return 0;
3551}
3552
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003553static int io_tee_prep(struct io_kiocb *req,
3554 const struct io_uring_sqe *sqe)
3555{
3556 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3557 return -EINVAL;
3558 return __io_splice_prep(req, sqe);
3559}
3560
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003561static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003562{
3563 struct io_splice *sp = &req->splice;
3564 struct file *in = sp->file_in;
3565 struct file *out = sp->file_out;
3566 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3567 long ret = 0;
3568
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003569 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003570 return -EAGAIN;
3571 if (sp->len)
3572 ret = do_tee(in, out, sp->len, flags);
3573
3574 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3575 req->flags &= ~REQ_F_NEED_CLEANUP;
3576
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003577 if (ret != sp->len)
3578 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003579 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003580 return 0;
3581}
3582
3583static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3584{
3585 struct io_splice* sp = &req->splice;
3586
3587 sp->off_in = READ_ONCE(sqe->splice_off_in);
3588 sp->off_out = READ_ONCE(sqe->off);
3589 return __io_splice_prep(req, sqe);
3590}
3591
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003592static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003593{
3594 struct io_splice *sp = &req->splice;
3595 struct file *in = sp->file_in;
3596 struct file *out = sp->file_out;
3597 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3598 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003599 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003600
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003601 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003602 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003603
3604 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3605 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003606
Jens Axboe948a7742020-05-17 14:21:38 -06003607 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003608 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003609
3610 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3611 req->flags &= ~REQ_F_NEED_CLEANUP;
3612
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003613 if (ret != sp->len)
3614 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003615 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003616 return 0;
3617}
3618
Jens Axboe2b188cc2019-01-07 10:46:33 -07003619/*
3620 * IORING_OP_NOP just posts a completion event, nothing else.
3621 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003622static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003623{
3624 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625
Jens Axboedef596e2019-01-09 08:59:42 -07003626 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3627 return -EINVAL;
3628
Pavel Begunkov889fca72021-02-10 00:03:09 +00003629 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003630 return 0;
3631}
3632
Pavel Begunkov1155c762021-02-18 18:29:38 +00003633static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003634{
Jens Axboe6b063142019-01-10 22:13:58 -07003635 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003636
Jens Axboe09bb8392019-03-13 12:39:28 -06003637 if (!req->file)
3638 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003639
Jens Axboe6b063142019-01-10 22:13:58 -07003640 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003641 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003642 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003643 return -EINVAL;
3644
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003645 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3646 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3647 return -EINVAL;
3648
3649 req->sync.off = READ_ONCE(sqe->off);
3650 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003651 return 0;
3652}
3653
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003654static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003655{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003656 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003657 int ret;
3658
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003659 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003660 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003661 return -EAGAIN;
3662
Jens Axboe9adbd452019-12-20 08:45:55 -07003663 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003664 end > 0 ? end : LLONG_MAX,
3665 req->sync.flags & IORING_FSYNC_DATASYNC);
3666 if (ret < 0)
3667 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003668 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003669 return 0;
3670}
3671
Jens Axboed63d1b52019-12-10 10:38:56 -07003672static int io_fallocate_prep(struct io_kiocb *req,
3673 const struct io_uring_sqe *sqe)
3674{
3675 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3676 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003677 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3678 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003679
3680 req->sync.off = READ_ONCE(sqe->off);
3681 req->sync.len = READ_ONCE(sqe->addr);
3682 req->sync.mode = READ_ONCE(sqe->len);
3683 return 0;
3684}
3685
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003686static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003687{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003688 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003689
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003690 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003691 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003692 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003693 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3694 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003695 if (ret < 0)
3696 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003697 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003698 return 0;
3699}
3700
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003701static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003702{
Jens Axboef8748882020-01-08 17:47:02 -07003703 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003704 int ret;
3705
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003706 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003707 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003708 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003709 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003710
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003711 /* open.how should be already initialised */
3712 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003713 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003714
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003715 req->open.dfd = READ_ONCE(sqe->fd);
3716 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003717 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003718 if (IS_ERR(req->open.filename)) {
3719 ret = PTR_ERR(req->open.filename);
3720 req->open.filename = NULL;
3721 return ret;
3722 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003723 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003724 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003725 return 0;
3726}
3727
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003728static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3729{
3730 u64 flags, mode;
3731
Jens Axboe14587a462020-09-05 11:36:08 -06003732 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003733 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003734 mode = READ_ONCE(sqe->len);
3735 flags = READ_ONCE(sqe->open_flags);
3736 req->open.how = build_open_how(flags, mode);
3737 return __io_openat_prep(req, sqe);
3738}
3739
Jens Axboecebdb982020-01-08 17:59:24 -07003740static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3741{
3742 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003743 size_t len;
3744 int ret;
3745
Jens Axboe14587a462020-09-05 11:36:08 -06003746 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003747 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003748 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3749 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003750 if (len < OPEN_HOW_SIZE_VER0)
3751 return -EINVAL;
3752
3753 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3754 len);
3755 if (ret)
3756 return ret;
3757
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003758 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003759}
3760
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003761static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003762{
3763 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003764 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003765 bool nonblock_set;
3766 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003767 int ret;
3768
Jens Axboecebdb982020-01-08 17:59:24 -07003769 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003770 if (ret)
3771 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003772 nonblock_set = op.open_flag & O_NONBLOCK;
3773 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003774 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003775 /*
3776 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3777 * it'll always -EAGAIN
3778 */
3779 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3780 return -EAGAIN;
3781 op.lookup_flags |= LOOKUP_CACHED;
3782 op.open_flag |= O_NONBLOCK;
3783 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003784
Jens Axboe4022e7a2020-03-19 19:23:18 -06003785 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003786 if (ret < 0)
3787 goto err;
3788
3789 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003790 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003791 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3792 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003793 /*
3794 * We could hang on to this 'fd', but seems like marginal
3795 * gain for something that is now known to be a slower path.
3796 * So just put it, and we'll get a new one when we retry.
3797 */
3798 put_unused_fd(ret);
3799 return -EAGAIN;
3800 }
3801
Jens Axboe15b71ab2019-12-11 11:20:36 -07003802 if (IS_ERR(file)) {
3803 put_unused_fd(ret);
3804 ret = PTR_ERR(file);
3805 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003806 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003807 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003808 fsnotify_open(file);
3809 fd_install(ret, file);
3810 }
3811err:
3812 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003813 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003814 if (ret < 0)
3815 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003816 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003817 return 0;
3818}
3819
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003820static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003821{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003822 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07003823}
3824
Jens Axboe067524e2020-03-02 16:32:28 -07003825static int io_remove_buffers_prep(struct io_kiocb *req,
3826 const struct io_uring_sqe *sqe)
3827{
3828 struct io_provide_buf *p = &req->pbuf;
3829 u64 tmp;
3830
3831 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3832 return -EINVAL;
3833
3834 tmp = READ_ONCE(sqe->fd);
3835 if (!tmp || tmp > USHRT_MAX)
3836 return -EINVAL;
3837
3838 memset(p, 0, sizeof(*p));
3839 p->nbufs = tmp;
3840 p->bgid = READ_ONCE(sqe->buf_group);
3841 return 0;
3842}
3843
3844static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3845 int bgid, unsigned nbufs)
3846{
3847 unsigned i = 0;
3848
3849 /* shouldn't happen */
3850 if (!nbufs)
3851 return 0;
3852
3853 /* the head kbuf is the list itself */
3854 while (!list_empty(&buf->list)) {
3855 struct io_buffer *nxt;
3856
3857 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3858 list_del(&nxt->list);
3859 kfree(nxt);
3860 if (++i == nbufs)
3861 return i;
3862 }
3863 i++;
3864 kfree(buf);
3865 idr_remove(&ctx->io_buffer_idr, bgid);
3866
3867 return i;
3868}
3869
Pavel Begunkov889fca72021-02-10 00:03:09 +00003870static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07003871{
3872 struct io_provide_buf *p = &req->pbuf;
3873 struct io_ring_ctx *ctx = req->ctx;
3874 struct io_buffer *head;
3875 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003876 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07003877
3878 io_ring_submit_lock(ctx, !force_nonblock);
3879
3880 lockdep_assert_held(&ctx->uring_lock);
3881
3882 ret = -ENOENT;
3883 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3884 if (head)
3885 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003886 if (ret < 0)
3887 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003888
3889 /* need to hold the lock to complete IOPOLL requests */
3890 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003891 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003892 io_ring_submit_unlock(ctx, !force_nonblock);
3893 } else {
3894 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003895 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003896 }
Jens Axboe067524e2020-03-02 16:32:28 -07003897 return 0;
3898}
3899
Jens Axboeddf0322d2020-02-23 16:41:33 -07003900static int io_provide_buffers_prep(struct io_kiocb *req,
3901 const struct io_uring_sqe *sqe)
3902{
3903 struct io_provide_buf *p = &req->pbuf;
3904 u64 tmp;
3905
3906 if (sqe->ioprio || sqe->rw_flags)
3907 return -EINVAL;
3908
3909 tmp = READ_ONCE(sqe->fd);
3910 if (!tmp || tmp > USHRT_MAX)
3911 return -E2BIG;
3912 p->nbufs = tmp;
3913 p->addr = READ_ONCE(sqe->addr);
3914 p->len = READ_ONCE(sqe->len);
3915
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003916 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003917 return -EFAULT;
3918
3919 p->bgid = READ_ONCE(sqe->buf_group);
3920 tmp = READ_ONCE(sqe->off);
3921 if (tmp > USHRT_MAX)
3922 return -E2BIG;
3923 p->bid = tmp;
3924 return 0;
3925}
3926
3927static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3928{
3929 struct io_buffer *buf;
3930 u64 addr = pbuf->addr;
3931 int i, bid = pbuf->bid;
3932
3933 for (i = 0; i < pbuf->nbufs; i++) {
3934 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3935 if (!buf)
3936 break;
3937
3938 buf->addr = addr;
3939 buf->len = pbuf->len;
3940 buf->bid = bid;
3941 addr += pbuf->len;
3942 bid++;
3943 if (!*head) {
3944 INIT_LIST_HEAD(&buf->list);
3945 *head = buf;
3946 } else {
3947 list_add_tail(&buf->list, &(*head)->list);
3948 }
3949 }
3950
3951 return i ? i : -ENOMEM;
3952}
3953
Pavel Begunkov889fca72021-02-10 00:03:09 +00003954static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003955{
3956 struct io_provide_buf *p = &req->pbuf;
3957 struct io_ring_ctx *ctx = req->ctx;
3958 struct io_buffer *head, *list;
3959 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003960 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07003961
3962 io_ring_submit_lock(ctx, !force_nonblock);
3963
3964 lockdep_assert_held(&ctx->uring_lock);
3965
3966 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3967
3968 ret = io_add_buffers(p, &head);
3969 if (ret < 0)
3970 goto out;
3971
3972 if (!list) {
3973 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3974 GFP_KERNEL);
3975 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003976 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003977 goto out;
3978 }
3979 }
3980out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07003981 if (ret < 0)
3982 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003983
3984 /* need to hold the lock to complete IOPOLL requests */
3985 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003986 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003987 io_ring_submit_unlock(ctx, !force_nonblock);
3988 } else {
3989 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003990 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003991 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07003992 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003993}
3994
Jens Axboe3e4827b2020-01-08 15:18:09 -07003995static int io_epoll_ctl_prep(struct io_kiocb *req,
3996 const struct io_uring_sqe *sqe)
3997{
3998#if defined(CONFIG_EPOLL)
3999 if (sqe->ioprio || sqe->buf_index)
4000 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004001 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004002 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004003
4004 req->epoll.epfd = READ_ONCE(sqe->fd);
4005 req->epoll.op = READ_ONCE(sqe->len);
4006 req->epoll.fd = READ_ONCE(sqe->off);
4007
4008 if (ep_op_has_event(req->epoll.op)) {
4009 struct epoll_event __user *ev;
4010
4011 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4012 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4013 return -EFAULT;
4014 }
4015
4016 return 0;
4017#else
4018 return -EOPNOTSUPP;
4019#endif
4020}
4021
Pavel Begunkov889fca72021-02-10 00:03:09 +00004022static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004023{
4024#if defined(CONFIG_EPOLL)
4025 struct io_epoll *ie = &req->epoll;
4026 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004027 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004028
4029 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4030 if (force_nonblock && ret == -EAGAIN)
4031 return -EAGAIN;
4032
4033 if (ret < 0)
4034 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004035 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004036 return 0;
4037#else
4038 return -EOPNOTSUPP;
4039#endif
4040}
4041
Jens Axboec1ca7572019-12-25 22:18:28 -07004042static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4043{
4044#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4045 if (sqe->ioprio || sqe->buf_index || sqe->off)
4046 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004047 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4048 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004049
4050 req->madvise.addr = READ_ONCE(sqe->addr);
4051 req->madvise.len = READ_ONCE(sqe->len);
4052 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4053 return 0;
4054#else
4055 return -EOPNOTSUPP;
4056#endif
4057}
4058
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004059static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004060{
4061#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4062 struct io_madvise *ma = &req->madvise;
4063 int ret;
4064
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004065 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004066 return -EAGAIN;
4067
Minchan Kim0726b012020-10-17 16:14:50 -07004068 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004069 if (ret < 0)
4070 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004071 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004072 return 0;
4073#else
4074 return -EOPNOTSUPP;
4075#endif
4076}
4077
Jens Axboe4840e412019-12-25 22:03:45 -07004078static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4079{
4080 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4081 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004082 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4083 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004084
4085 req->fadvise.offset = READ_ONCE(sqe->off);
4086 req->fadvise.len = READ_ONCE(sqe->len);
4087 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4088 return 0;
4089}
4090
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004091static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004092{
4093 struct io_fadvise *fa = &req->fadvise;
4094 int ret;
4095
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004096 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004097 switch (fa->advice) {
4098 case POSIX_FADV_NORMAL:
4099 case POSIX_FADV_RANDOM:
4100 case POSIX_FADV_SEQUENTIAL:
4101 break;
4102 default:
4103 return -EAGAIN;
4104 }
4105 }
Jens Axboe4840e412019-12-25 22:03:45 -07004106
4107 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4108 if (ret < 0)
4109 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004110 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004111 return 0;
4112}
4113
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004114static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4115{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004116 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004117 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004118 if (sqe->ioprio || sqe->buf_index)
4119 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004120 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004121 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004122
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004123 req->statx.dfd = READ_ONCE(sqe->fd);
4124 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004125 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004126 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4127 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004128
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004129 return 0;
4130}
4131
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004132static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004133{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004134 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004135 int ret;
4136
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004137 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004138 /* only need file table for an actual valid fd */
4139 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4140 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004141 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004142 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004143
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004144 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4145 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004146
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004147 if (ret < 0)
4148 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004149 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004150 return 0;
4151}
4152
Jens Axboeb5dba592019-12-11 14:02:38 -07004153static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4154{
Jens Axboe14587a462020-09-05 11:36:08 -06004155 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004156 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004157 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4158 sqe->rw_flags || sqe->buf_index)
4159 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004160 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004161 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004162
4163 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004164 return 0;
4165}
4166
Pavel Begunkov889fca72021-02-10 00:03:09 +00004167static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004168{
Jens Axboe9eac1902021-01-19 15:50:37 -07004169 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004170 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004171 struct fdtable *fdt;
4172 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004173 int ret;
4174
Jens Axboe9eac1902021-01-19 15:50:37 -07004175 file = NULL;
4176 ret = -EBADF;
4177 spin_lock(&files->file_lock);
4178 fdt = files_fdtable(files);
4179 if (close->fd >= fdt->max_fds) {
4180 spin_unlock(&files->file_lock);
4181 goto err;
4182 }
4183 file = fdt->fd[close->fd];
4184 if (!file) {
4185 spin_unlock(&files->file_lock);
4186 goto err;
4187 }
4188
4189 if (file->f_op == &io_uring_fops) {
4190 spin_unlock(&files->file_lock);
4191 file = NULL;
4192 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004193 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004194
4195 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004196 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004197 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004198 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004199 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004200
Jens Axboe9eac1902021-01-19 15:50:37 -07004201 ret = __close_fd_get_file(close->fd, &file);
4202 spin_unlock(&files->file_lock);
4203 if (ret < 0) {
4204 if (ret == -ENOENT)
4205 ret = -EBADF;
4206 goto err;
4207 }
4208
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004209 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004210 ret = filp_close(file, current->files);
4211err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004212 if (ret < 0)
4213 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004214 if (file)
4215 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004216 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004217 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004218}
4219
Pavel Begunkov1155c762021-02-18 18:29:38 +00004220static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004221{
4222 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004223
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004224 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4225 return -EINVAL;
4226 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4227 return -EINVAL;
4228
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004229 req->sync.off = READ_ONCE(sqe->off);
4230 req->sync.len = READ_ONCE(sqe->len);
4231 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004232 return 0;
4233}
4234
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004235static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004236{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004237 int ret;
4238
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004239 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004240 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004241 return -EAGAIN;
4242
Jens Axboe9adbd452019-12-20 08:45:55 -07004243 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004244 req->sync.flags);
4245 if (ret < 0)
4246 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004247 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004248 return 0;
4249}
4250
YueHaibing469956e2020-03-04 15:53:52 +08004251#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004252static int io_setup_async_msg(struct io_kiocb *req,
4253 struct io_async_msghdr *kmsg)
4254{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004255 struct io_async_msghdr *async_msg = req->async_data;
4256
4257 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004258 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004259 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004260 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004261 return -ENOMEM;
4262 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004263 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004264 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004265 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004266 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004267 /* if were using fast_iov, set it to the new one */
4268 if (!async_msg->free_iov)
4269 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4270
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004271 return -EAGAIN;
4272}
4273
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004274static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4275 struct io_async_msghdr *iomsg)
4276{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004277 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004278 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004279 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004280 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004281}
4282
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004283static int io_sendmsg_prep_async(struct io_kiocb *req)
4284{
4285 int ret;
4286
4287 if (!io_op_defs[req->opcode].needs_async_data)
4288 return 0;
4289 ret = io_sendmsg_copy_hdr(req, req->async_data);
4290 if (!ret)
4291 req->flags |= REQ_F_NEED_CLEANUP;
4292 return ret;
4293}
4294
Jens Axboe3529d8c2019-12-19 18:24:38 -07004295static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004296{
Jens Axboee47293f2019-12-20 08:58:21 -07004297 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004298
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004299 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4300 return -EINVAL;
4301
Jens Axboee47293f2019-12-20 08:58:21 -07004302 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004303 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004304 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004305
Jens Axboed8768362020-02-27 14:17:49 -07004306#ifdef CONFIG_COMPAT
4307 if (req->ctx->compat)
4308 sr->msg_flags |= MSG_CMSG_COMPAT;
4309#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004310 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004311}
4312
Pavel Begunkov889fca72021-02-10 00:03:09 +00004313static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004314{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004315 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004316 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004317 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004318 int ret;
4319
Florent Revestdba4a922020-12-04 12:36:04 +01004320 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004321 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004322 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004323
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004324 kmsg = req->async_data;
4325 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004326 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004327 if (ret)
4328 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004329 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004330 }
4331
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004332 flags = req->sr_msg.msg_flags;
4333 if (flags & MSG_DONTWAIT)
4334 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004335 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004336 flags |= MSG_DONTWAIT;
4337
4338 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004339 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004340 return io_setup_async_msg(req, kmsg);
4341 if (ret == -ERESTARTSYS)
4342 ret = -EINTR;
4343
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004344 /* fast path, check for non-NULL to avoid function call */
4345 if (kmsg->free_iov)
4346 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004347 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004348 if (ret < 0)
4349 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004350 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004351 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004352}
4353
Pavel Begunkov889fca72021-02-10 00:03:09 +00004354static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004355{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004356 struct io_sr_msg *sr = &req->sr_msg;
4357 struct msghdr msg;
4358 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004359 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004360 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004361 int ret;
4362
Florent Revestdba4a922020-12-04 12:36:04 +01004363 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004364 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004365 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004366
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004367 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4368 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004369 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004370
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004371 msg.msg_name = NULL;
4372 msg.msg_control = NULL;
4373 msg.msg_controllen = 0;
4374 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004375
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004376 flags = req->sr_msg.msg_flags;
4377 if (flags & MSG_DONTWAIT)
4378 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004379 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004380 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004381
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004382 msg.msg_flags = flags;
4383 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004384 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004385 return -EAGAIN;
4386 if (ret == -ERESTARTSYS)
4387 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004388
Jens Axboe03b12302019-12-02 18:50:25 -07004389 if (ret < 0)
4390 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004391 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004392 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004393}
4394
Pavel Begunkov1400e692020-07-12 20:41:05 +03004395static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4396 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004397{
4398 struct io_sr_msg *sr = &req->sr_msg;
4399 struct iovec __user *uiov;
4400 size_t iov_len;
4401 int ret;
4402
Pavel Begunkov1400e692020-07-12 20:41:05 +03004403 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4404 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004405 if (ret)
4406 return ret;
4407
4408 if (req->flags & REQ_F_BUFFER_SELECT) {
4409 if (iov_len > 1)
4410 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004411 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004412 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004413 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004414 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004415 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004416 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004417 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004418 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004419 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004420 if (ret > 0)
4421 ret = 0;
4422 }
4423
4424 return ret;
4425}
4426
4427#ifdef CONFIG_COMPAT
4428static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004429 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004430{
4431 struct compat_msghdr __user *msg_compat;
4432 struct io_sr_msg *sr = &req->sr_msg;
4433 struct compat_iovec __user *uiov;
4434 compat_uptr_t ptr;
4435 compat_size_t len;
4436 int ret;
4437
Pavel Begunkov270a5942020-07-12 20:41:04 +03004438 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004439 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004440 &ptr, &len);
4441 if (ret)
4442 return ret;
4443
4444 uiov = compat_ptr(ptr);
4445 if (req->flags & REQ_F_BUFFER_SELECT) {
4446 compat_ssize_t clen;
4447
4448 if (len > 1)
4449 return -EINVAL;
4450 if (!access_ok(uiov, sizeof(*uiov)))
4451 return -EFAULT;
4452 if (__get_user(clen, &uiov->iov_len))
4453 return -EFAULT;
4454 if (clen < 0)
4455 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004456 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004457 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004458 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004459 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004460 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004461 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004462 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004463 if (ret < 0)
4464 return ret;
4465 }
4466
4467 return 0;
4468}
Jens Axboe03b12302019-12-02 18:50:25 -07004469#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004470
Pavel Begunkov1400e692020-07-12 20:41:05 +03004471static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4472 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004473{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004474 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004475
4476#ifdef CONFIG_COMPAT
4477 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004478 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004479#endif
4480
Pavel Begunkov1400e692020-07-12 20:41:05 +03004481 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004482}
4483
Jens Axboebcda7ba2020-02-23 16:42:51 -07004484static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004485 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004486{
4487 struct io_sr_msg *sr = &req->sr_msg;
4488 struct io_buffer *kbuf;
4489
Jens Axboebcda7ba2020-02-23 16:42:51 -07004490 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4491 if (IS_ERR(kbuf))
4492 return kbuf;
4493
4494 sr->kbuf = kbuf;
4495 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004496 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004497}
4498
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004499static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4500{
4501 return io_put_kbuf(req, req->sr_msg.kbuf);
4502}
4503
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004504static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004505{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004506 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004507
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004508 if (!io_op_defs[req->opcode].needs_async_data)
4509 return 0;
4510 ret = io_recvmsg_copy_hdr(req, req->async_data);
4511 if (!ret)
4512 req->flags |= REQ_F_NEED_CLEANUP;
4513 return ret;
4514}
4515
4516static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4517{
4518 struct io_sr_msg *sr = &req->sr_msg;
4519
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004520 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4521 return -EINVAL;
4522
Jens Axboe3529d8c2019-12-19 18:24:38 -07004523 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004524 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004525 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004526 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004527
Jens Axboed8768362020-02-27 14:17:49 -07004528#ifdef CONFIG_COMPAT
4529 if (req->ctx->compat)
4530 sr->msg_flags |= MSG_CMSG_COMPAT;
4531#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004532 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004533}
4534
Pavel Begunkov889fca72021-02-10 00:03:09 +00004535static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004536{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004537 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004538 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004539 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004540 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004541 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004542 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004543
Florent Revestdba4a922020-12-04 12:36:04 +01004544 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004545 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004546 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004547
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004548 kmsg = req->async_data;
4549 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004550 ret = io_recvmsg_copy_hdr(req, &iomsg);
4551 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004552 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004553 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004554 }
4555
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004556 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004557 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004558 if (IS_ERR(kbuf))
4559 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004560 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004561 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4562 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004563 1, req->sr_msg.len);
4564 }
4565
4566 flags = req->sr_msg.msg_flags;
4567 if (flags & MSG_DONTWAIT)
4568 req->flags |= REQ_F_NOWAIT;
4569 else if (force_nonblock)
4570 flags |= MSG_DONTWAIT;
4571
4572 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4573 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004574 if (force_nonblock && ret == -EAGAIN)
4575 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004576 if (ret == -ERESTARTSYS)
4577 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004578
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004579 if (req->flags & REQ_F_BUFFER_SELECTED)
4580 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004581 /* fast path, check for non-NULL to avoid function call */
4582 if (kmsg->free_iov)
4583 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004584 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004585 if (ret < 0)
4586 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004587 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004588 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004589}
4590
Pavel Begunkov889fca72021-02-10 00:03:09 +00004591static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004592{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004593 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004594 struct io_sr_msg *sr = &req->sr_msg;
4595 struct msghdr msg;
4596 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004597 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004598 struct iovec iov;
4599 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004600 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004601 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004602
Florent Revestdba4a922020-12-04 12:36:04 +01004603 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004604 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004605 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004606
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004607 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004608 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004609 if (IS_ERR(kbuf))
4610 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004611 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004612 }
4613
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004614 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004615 if (unlikely(ret))
4616 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004617
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004618 msg.msg_name = NULL;
4619 msg.msg_control = NULL;
4620 msg.msg_controllen = 0;
4621 msg.msg_namelen = 0;
4622 msg.msg_iocb = NULL;
4623 msg.msg_flags = 0;
4624
4625 flags = req->sr_msg.msg_flags;
4626 if (flags & MSG_DONTWAIT)
4627 req->flags |= REQ_F_NOWAIT;
4628 else if (force_nonblock)
4629 flags |= MSG_DONTWAIT;
4630
4631 ret = sock_recvmsg(sock, &msg, flags);
4632 if (force_nonblock && ret == -EAGAIN)
4633 return -EAGAIN;
4634 if (ret == -ERESTARTSYS)
4635 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004636out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004637 if (req->flags & REQ_F_BUFFER_SELECTED)
4638 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004639 if (ret < 0)
4640 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004641 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004642 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004643}
4644
Jens Axboe3529d8c2019-12-19 18:24:38 -07004645static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004646{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004647 struct io_accept *accept = &req->accept;
4648
Jens Axboe14587a462020-09-05 11:36:08 -06004649 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004650 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004651 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004652 return -EINVAL;
4653
Jens Axboed55e5f52019-12-11 16:12:15 -07004654 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4655 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004656 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004657 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004658 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004659}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004660
Pavel Begunkov889fca72021-02-10 00:03:09 +00004661static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004662{
4663 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004664 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004665 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004666 int ret;
4667
Jiufei Xuee697dee2020-06-10 13:41:59 +08004668 if (req->file->f_flags & O_NONBLOCK)
4669 req->flags |= REQ_F_NOWAIT;
4670
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004671 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004672 accept->addr_len, accept->flags,
4673 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004674 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004675 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004676 if (ret < 0) {
4677 if (ret == -ERESTARTSYS)
4678 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004679 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004680 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004681 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004682 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004683}
4684
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004685static int io_connect_prep_async(struct io_kiocb *req)
4686{
4687 struct io_async_connect *io = req->async_data;
4688 struct io_connect *conn = &req->connect;
4689
4690 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4691}
4692
Jens Axboe3529d8c2019-12-19 18:24:38 -07004693static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004694{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004695 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004696
Jens Axboe14587a462020-09-05 11:36:08 -06004697 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004698 return -EINVAL;
4699 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4700 return -EINVAL;
4701
Jens Axboe3529d8c2019-12-19 18:24:38 -07004702 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4703 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004704 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004705}
4706
Pavel Begunkov889fca72021-02-10 00:03:09 +00004707static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004708{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004709 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004710 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004711 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004712 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004713
Jens Axboee8c2bc12020-08-15 18:44:09 -07004714 if (req->async_data) {
4715 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004716 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004717 ret = move_addr_to_kernel(req->connect.addr,
4718 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004719 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004720 if (ret)
4721 goto out;
4722 io = &__io;
4723 }
4724
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004725 file_flags = force_nonblock ? O_NONBLOCK : 0;
4726
Jens Axboee8c2bc12020-08-15 18:44:09 -07004727 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004728 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004729 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004730 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004731 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004732 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004733 ret = -ENOMEM;
4734 goto out;
4735 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004736 io = req->async_data;
4737 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004738 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004739 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004740 if (ret == -ERESTARTSYS)
4741 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004742out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004743 if (ret < 0)
4744 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004745 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004746 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004747}
YueHaibing469956e2020-03-04 15:53:52 +08004748#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004749#define IO_NETOP_FN(op) \
4750static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4751{ \
4752 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004753}
4754
Jens Axboe99a10082021-02-19 09:35:19 -07004755#define IO_NETOP_PREP(op) \
4756IO_NETOP_FN(op) \
4757static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4758{ \
4759 return -EOPNOTSUPP; \
4760} \
4761
4762#define IO_NETOP_PREP_ASYNC(op) \
4763IO_NETOP_PREP(op) \
4764static int io_##op##_prep_async(struct io_kiocb *req) \
4765{ \
4766 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004767}
4768
Jens Axboe99a10082021-02-19 09:35:19 -07004769IO_NETOP_PREP_ASYNC(sendmsg);
4770IO_NETOP_PREP_ASYNC(recvmsg);
4771IO_NETOP_PREP_ASYNC(connect);
4772IO_NETOP_PREP(accept);
4773IO_NETOP_FN(send);
4774IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004775#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004776
Jens Axboed7718a92020-02-14 22:23:12 -07004777struct io_poll_table {
4778 struct poll_table_struct pt;
4779 struct io_kiocb *req;
4780 int error;
4781};
4782
Jens Axboed7718a92020-02-14 22:23:12 -07004783static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4784 __poll_t mask, task_work_func_t func)
4785{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004786 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004787
4788 /* for instances that support it check for an event match first: */
4789 if (mask && !(mask & poll->events))
4790 return 0;
4791
4792 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4793
4794 list_del_init(&poll->wait.entry);
4795
Jens Axboed7718a92020-02-14 22:23:12 -07004796 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004797 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004798 percpu_ref_get(&req->ctx->refs);
4799
Jens Axboed7718a92020-02-14 22:23:12 -07004800 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004801 * If this fails, then the task is exiting. When a task exits, the
4802 * work gets canceled, so just cancel this request as well instead
4803 * of executing it. We can't safely execute it anyway, as we may not
4804 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004805 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004806 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004807 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004808 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004809 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004810 }
Jens Axboed7718a92020-02-14 22:23:12 -07004811 return 1;
4812}
4813
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004814static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4815 __acquires(&req->ctx->completion_lock)
4816{
4817 struct io_ring_ctx *ctx = req->ctx;
4818
4819 if (!req->result && !READ_ONCE(poll->canceled)) {
4820 struct poll_table_struct pt = { ._key = poll->events };
4821
4822 req->result = vfs_poll(req->file, &pt) & poll->events;
4823 }
4824
4825 spin_lock_irq(&ctx->completion_lock);
4826 if (!req->result && !READ_ONCE(poll->canceled)) {
4827 add_wait_queue(poll->head, &poll->wait);
4828 return true;
4829 }
4830
4831 return false;
4832}
4833
Jens Axboed4e7cd32020-08-15 11:44:50 -07004834static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004835{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004836 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004837 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004838 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004839 return req->apoll->double_poll;
4840}
4841
4842static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4843{
4844 if (req->opcode == IORING_OP_POLL_ADD)
4845 return &req->poll;
4846 return &req->apoll->poll;
4847}
4848
4849static void io_poll_remove_double(struct io_kiocb *req)
4850{
4851 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004852
4853 lockdep_assert_held(&req->ctx->completion_lock);
4854
4855 if (poll && poll->head) {
4856 struct wait_queue_head *head = poll->head;
4857
4858 spin_lock(&head->lock);
4859 list_del_init(&poll->wait.entry);
4860 if (poll->wait.private)
4861 refcount_dec(&req->refs);
4862 poll->head = NULL;
4863 spin_unlock(&head->lock);
4864 }
4865}
4866
4867static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4868{
4869 struct io_ring_ctx *ctx = req->ctx;
4870
Jens Axboed4e7cd32020-08-15 11:44:50 -07004871 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004872 req->poll.done = true;
4873 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4874 io_commit_cqring(ctx);
4875}
4876
Jens Axboe18bceab2020-05-15 11:56:54 -06004877static void io_poll_task_func(struct callback_head *cb)
4878{
4879 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004880 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004881 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004882
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004883 if (io_poll_rewait(req, &req->poll)) {
4884 spin_unlock_irq(&ctx->completion_lock);
4885 } else {
4886 hash_del(&req->hash_node);
4887 io_poll_complete(req, req->result, 0);
4888 spin_unlock_irq(&ctx->completion_lock);
4889
4890 nxt = io_put_req_find_next(req);
4891 io_cqring_ev_posted(ctx);
4892 if (nxt)
4893 __io_req_task_submit(nxt);
4894 }
4895
Jens Axboe6d816e02020-08-11 08:04:14 -06004896 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004897}
4898
4899static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4900 int sync, void *key)
4901{
4902 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004903 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004904 __poll_t mask = key_to_poll(key);
4905
4906 /* for instances that support it check for an event match first: */
4907 if (mask && !(mask & poll->events))
4908 return 0;
4909
Jens Axboe8706e042020-09-28 08:38:54 -06004910 list_del_init(&wait->entry);
4911
Jens Axboe807abcb2020-07-17 17:09:27 -06004912 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004913 bool done;
4914
Jens Axboe807abcb2020-07-17 17:09:27 -06004915 spin_lock(&poll->head->lock);
4916 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004917 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004918 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004919 /* make sure double remove sees this as being gone */
4920 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004921 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06004922 if (!done) {
4923 /* use wait func handler, so it matches the rq type */
4924 poll->wait.func(&poll->wait, mode, sync, key);
4925 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004926 }
4927 refcount_dec(&req->refs);
4928 return 1;
4929}
4930
4931static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4932 wait_queue_func_t wake_func)
4933{
4934 poll->head = NULL;
4935 poll->done = false;
4936 poll->canceled = false;
4937 poll->events = events;
4938 INIT_LIST_HEAD(&poll->wait.entry);
4939 init_waitqueue_func_entry(&poll->wait, wake_func);
4940}
4941
4942static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004943 struct wait_queue_head *head,
4944 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004945{
4946 struct io_kiocb *req = pt->req;
4947
4948 /*
4949 * If poll->head is already set, it's because the file being polled
4950 * uses multiple waitqueues for poll handling (eg one for read, one
4951 * for write). Setup a separate io_poll_iocb if this happens.
4952 */
4953 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01004954 struct io_poll_iocb *poll_one = poll;
4955
Jens Axboe18bceab2020-05-15 11:56:54 -06004956 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004957 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004958 pt->error = -EINVAL;
4959 return;
4960 }
Jens Axboe1c3b3e62021-02-28 16:07:30 -07004961 /* double add on the same waitqueue head, ignore */
4962 if (poll->head == head)
4963 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06004964 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4965 if (!poll) {
4966 pt->error = -ENOMEM;
4967 return;
4968 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01004969 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06004970 refcount_inc(&req->refs);
4971 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004972 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004973 }
4974
4975 pt->error = 0;
4976 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004977
4978 if (poll->events & EPOLLEXCLUSIVE)
4979 add_wait_queue_exclusive(head, &poll->wait);
4980 else
4981 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004982}
4983
4984static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4985 struct poll_table_struct *p)
4986{
4987 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004988 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004989
Jens Axboe807abcb2020-07-17 17:09:27 -06004990 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004991}
4992
Jens Axboed7718a92020-02-14 22:23:12 -07004993static void io_async_task_func(struct callback_head *cb)
4994{
4995 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4996 struct async_poll *apoll = req->apoll;
4997 struct io_ring_ctx *ctx = req->ctx;
4998
4999 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5000
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005001 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005002 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005003 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005004 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005005 }
5006
Jens Axboe31067252020-05-17 17:43:31 -06005007 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005008 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005009 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005010
Jens Axboed4e7cd32020-08-15 11:44:50 -07005011 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005012 spin_unlock_irq(&ctx->completion_lock);
5013
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005014 if (!READ_ONCE(apoll->poll.canceled))
5015 __io_req_task_submit(req);
5016 else
5017 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005018
Jens Axboe6d816e02020-08-11 08:04:14 -06005019 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005020 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005021 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005022}
5023
5024static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5025 void *key)
5026{
5027 struct io_kiocb *req = wait->private;
5028 struct io_poll_iocb *poll = &req->apoll->poll;
5029
5030 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5031 key_to_poll(key));
5032
5033 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5034}
5035
5036static void io_poll_req_insert(struct io_kiocb *req)
5037{
5038 struct io_ring_ctx *ctx = req->ctx;
5039 struct hlist_head *list;
5040
5041 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5042 hlist_add_head(&req->hash_node, list);
5043}
5044
5045static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5046 struct io_poll_iocb *poll,
5047 struct io_poll_table *ipt, __poll_t mask,
5048 wait_queue_func_t wake_func)
5049 __acquires(&ctx->completion_lock)
5050{
5051 struct io_ring_ctx *ctx = req->ctx;
5052 bool cancel = false;
5053
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005054 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005055 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005056 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005057 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005058
5059 ipt->pt._key = mask;
5060 ipt->req = req;
5061 ipt->error = -EINVAL;
5062
Jens Axboed7718a92020-02-14 22:23:12 -07005063 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5064
5065 spin_lock_irq(&ctx->completion_lock);
5066 if (likely(poll->head)) {
5067 spin_lock(&poll->head->lock);
5068 if (unlikely(list_empty(&poll->wait.entry))) {
5069 if (ipt->error)
5070 cancel = true;
5071 ipt->error = 0;
5072 mask = 0;
5073 }
5074 if (mask || ipt->error)
5075 list_del_init(&poll->wait.entry);
5076 else if (cancel)
5077 WRITE_ONCE(poll->canceled, true);
5078 else if (!poll->done) /* actually waiting for an event */
5079 io_poll_req_insert(req);
5080 spin_unlock(&poll->head->lock);
5081 }
5082
5083 return mask;
5084}
5085
5086static bool io_arm_poll_handler(struct io_kiocb *req)
5087{
5088 const struct io_op_def *def = &io_op_defs[req->opcode];
5089 struct io_ring_ctx *ctx = req->ctx;
5090 struct async_poll *apoll;
5091 struct io_poll_table ipt;
5092 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005093 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005094
5095 if (!req->file || !file_can_poll(req->file))
5096 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005097 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005098 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005099 if (def->pollin)
5100 rw = READ;
5101 else if (def->pollout)
5102 rw = WRITE;
5103 else
5104 return false;
5105 /* if we can't nonblock try, then no point in arming a poll handler */
5106 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005107 return false;
5108
5109 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5110 if (unlikely(!apoll))
5111 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005112 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005113
5114 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005115 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005116
Nathan Chancellor8755d972020-03-02 16:01:19 -07005117 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005118 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005119 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005120 if (def->pollout)
5121 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005122
5123 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5124 if ((req->opcode == IORING_OP_RECVMSG) &&
5125 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5126 mask &= ~POLLIN;
5127
Jens Axboed7718a92020-02-14 22:23:12 -07005128 mask |= POLLERR | POLLPRI;
5129
5130 ipt.pt._qproc = io_async_queue_proc;
5131
5132 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5133 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005134 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005135 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005136 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005137 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005138 kfree(apoll);
5139 return false;
5140 }
5141 spin_unlock_irq(&ctx->completion_lock);
5142 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5143 apoll->poll.events);
5144 return true;
5145}
5146
5147static bool __io_poll_remove_one(struct io_kiocb *req,
5148 struct io_poll_iocb *poll)
5149{
Jens Axboeb41e9852020-02-17 09:52:41 -07005150 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005151
5152 spin_lock(&poll->head->lock);
5153 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005154 if (!list_empty(&poll->wait.entry)) {
5155 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005156 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005157 }
5158 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005159 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005160 return do_complete;
5161}
5162
5163static bool io_poll_remove_one(struct io_kiocb *req)
5164{
5165 bool do_complete;
5166
Jens Axboed4e7cd32020-08-15 11:44:50 -07005167 io_poll_remove_double(req);
5168
Jens Axboed7718a92020-02-14 22:23:12 -07005169 if (req->opcode == IORING_OP_POLL_ADD) {
5170 do_complete = __io_poll_remove_one(req, &req->poll);
5171 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005172 struct async_poll *apoll = req->apoll;
5173
Jens Axboed7718a92020-02-14 22:23:12 -07005174 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005175 do_complete = __io_poll_remove_one(req, &apoll->poll);
5176 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005177 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005178 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005179 kfree(apoll);
5180 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005181 }
5182
Jens Axboeb41e9852020-02-17 09:52:41 -07005183 if (do_complete) {
5184 io_cqring_fill_event(req, -ECANCELED);
5185 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005186 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005187 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005188 }
5189
5190 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005191}
5192
Jens Axboe76e1b642020-09-26 15:05:03 -06005193/*
5194 * Returns true if we found and killed one or more poll requests
5195 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005196static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5197 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005198{
Jens Axboe78076bb2019-12-04 19:56:40 -07005199 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005200 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005201 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005202
5203 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005204 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5205 struct hlist_head *list;
5206
5207 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005208 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005209 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005210 posted += io_poll_remove_one(req);
5211 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005212 }
5213 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005214
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005215 if (posted)
5216 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005217
5218 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005219}
5220
Jens Axboe47f46762019-11-09 17:43:02 -07005221static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5222{
Jens Axboe78076bb2019-12-04 19:56:40 -07005223 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005224 struct io_kiocb *req;
5225
Jens Axboe78076bb2019-12-04 19:56:40 -07005226 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5227 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005228 if (sqe_addr != req->user_data)
5229 continue;
5230 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005231 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005232 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005233 }
5234
5235 return -ENOENT;
5236}
5237
Jens Axboe3529d8c2019-12-19 18:24:38 -07005238static int io_poll_remove_prep(struct io_kiocb *req,
5239 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005240{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005241 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5242 return -EINVAL;
5243 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5244 sqe->poll_events)
5245 return -EINVAL;
5246
Pavel Begunkov018043b2020-10-27 23:17:18 +00005247 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005248 return 0;
5249}
5250
5251/*
5252 * Find a running poll command that matches one specified in sqe->addr,
5253 * and remove it if found.
5254 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005255static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005256{
5257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005258 int ret;
5259
Jens Axboe221c5eb2019-01-17 09:41:58 -07005260 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005261 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005262 spin_unlock_irq(&ctx->completion_lock);
5263
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005264 if (ret < 0)
5265 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005266 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005267 return 0;
5268}
5269
Jens Axboe221c5eb2019-01-17 09:41:58 -07005270static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5271 void *key)
5272{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005273 struct io_kiocb *req = wait->private;
5274 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005275
Jens Axboed7718a92020-02-14 22:23:12 -07005276 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005277}
5278
Jens Axboe221c5eb2019-01-17 09:41:58 -07005279static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5280 struct poll_table_struct *p)
5281{
5282 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5283
Jens Axboee8c2bc12020-08-15 18:44:09 -07005284 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005285}
5286
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005288{
5289 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005290 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005291
5292 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5293 return -EINVAL;
5294 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5295 return -EINVAL;
5296
Jiufei Xue5769a352020-06-17 17:53:55 +08005297 events = READ_ONCE(sqe->poll32_events);
5298#ifdef __BIG_ENDIAN
5299 events = swahw32(events);
5300#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005301 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5302 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005303 return 0;
5304}
5305
Pavel Begunkov61e98202021-02-10 00:03:08 +00005306static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005307{
5308 struct io_poll_iocb *poll = &req->poll;
5309 struct io_ring_ctx *ctx = req->ctx;
5310 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005311 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005312
Jens Axboed7718a92020-02-14 22:23:12 -07005313 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005314
Jens Axboed7718a92020-02-14 22:23:12 -07005315 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5316 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005317
Jens Axboe8c838782019-03-12 15:48:16 -06005318 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005319 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005320 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005321 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005322 spin_unlock_irq(&ctx->completion_lock);
5323
Jens Axboe8c838782019-03-12 15:48:16 -06005324 if (mask) {
5325 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005326 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005327 }
Jens Axboe8c838782019-03-12 15:48:16 -06005328 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005329}
5330
Jens Axboe5262f562019-09-17 12:26:57 -06005331static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5332{
Jens Axboead8a48a2019-11-15 08:49:11 -07005333 struct io_timeout_data *data = container_of(timer,
5334 struct io_timeout_data, timer);
5335 struct io_kiocb *req = data->req;
5336 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005337 unsigned long flags;
5338
Jens Axboe5262f562019-09-17 12:26:57 -06005339 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005340 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005341 atomic_set(&req->ctx->cq_timeouts,
5342 atomic_read(&req->ctx->cq_timeouts) + 1);
5343
Jens Axboe78e19bb2019-11-06 15:21:34 -07005344 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005345 io_commit_cqring(ctx);
5346 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5347
5348 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005349 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005350 io_put_req(req);
5351 return HRTIMER_NORESTART;
5352}
5353
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005354static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5355 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005356{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005357 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005358 struct io_kiocb *req;
5359 int ret = -ENOENT;
5360
5361 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5362 if (user_data == req->user_data) {
5363 ret = 0;
5364 break;
5365 }
5366 }
5367
5368 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005369 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005370
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005371 io = req->async_data;
5372 ret = hrtimer_try_to_cancel(&io->timer);
5373 if (ret == -1)
5374 return ERR_PTR(-EALREADY);
5375 list_del_init(&req->timeout.list);
5376 return req;
5377}
5378
5379static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5380{
5381 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5382
5383 if (IS_ERR(req))
5384 return PTR_ERR(req);
5385
5386 req_set_fail_links(req);
5387 io_cqring_fill_event(req, -ECANCELED);
5388 io_put_req_deferred(req, 1);
5389 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005390}
5391
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005392static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5393 struct timespec64 *ts, enum hrtimer_mode mode)
5394{
5395 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5396 struct io_timeout_data *data;
5397
5398 if (IS_ERR(req))
5399 return PTR_ERR(req);
5400
5401 req->timeout.off = 0; /* noseq */
5402 data = req->async_data;
5403 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5404 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5405 data->timer.function = io_timeout_fn;
5406 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5407 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005408}
5409
Jens Axboe3529d8c2019-12-19 18:24:38 -07005410static int io_timeout_remove_prep(struct io_kiocb *req,
5411 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005412{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005413 struct io_timeout_rem *tr = &req->timeout_rem;
5414
Jens Axboeb29472e2019-12-17 18:50:29 -07005415 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5416 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005417 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5418 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005419 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005420 return -EINVAL;
5421
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005422 tr->addr = READ_ONCE(sqe->addr);
5423 tr->flags = READ_ONCE(sqe->timeout_flags);
5424 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5425 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5426 return -EINVAL;
5427 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5428 return -EFAULT;
5429 } else if (tr->flags) {
5430 /* timeout removal doesn't support flags */
5431 return -EINVAL;
5432 }
5433
Jens Axboeb29472e2019-12-17 18:50:29 -07005434 return 0;
5435}
5436
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005437static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5438{
5439 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5440 : HRTIMER_MODE_REL;
5441}
5442
Jens Axboe11365042019-10-16 09:08:32 -06005443/*
5444 * Remove or update an existing timeout command
5445 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005446static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005447{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005448 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005449 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005450 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005451
Jens Axboe11365042019-10-16 09:08:32 -06005452 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005453 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005454 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005455 else
5456 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5457 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005458
Jens Axboe47f46762019-11-09 17:43:02 -07005459 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005460 io_commit_cqring(ctx);
5461 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005462 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005463 if (ret < 0)
5464 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005465 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005466 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005467}
5468
Jens Axboe3529d8c2019-12-19 18:24:38 -07005469static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005470 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005471{
Jens Axboead8a48a2019-11-15 08:49:11 -07005472 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005473 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005474 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005475
Jens Axboead8a48a2019-11-15 08:49:11 -07005476 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005477 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005478 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005479 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005480 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005481 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005482 flags = READ_ONCE(sqe->timeout_flags);
5483 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005484 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005485
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005486 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005487
Jens Axboee8c2bc12020-08-15 18:44:09 -07005488 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005489 return -ENOMEM;
5490
Jens Axboee8c2bc12020-08-15 18:44:09 -07005491 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005492 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005493
5494 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005495 return -EFAULT;
5496
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005497 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005498 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5499 return 0;
5500}
5501
Pavel Begunkov61e98202021-02-10 00:03:08 +00005502static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005503{
Jens Axboead8a48a2019-11-15 08:49:11 -07005504 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005505 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005506 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005507 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005508
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005509 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005510
Jens Axboe5262f562019-09-17 12:26:57 -06005511 /*
5512 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005513 * timeout event to be satisfied. If it isn't set, then this is
5514 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005515 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005516 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005517 entry = ctx->timeout_list.prev;
5518 goto add;
5519 }
Jens Axboe5262f562019-09-17 12:26:57 -06005520
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005521 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5522 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005523
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005524 /* Update the last seq here in case io_flush_timeouts() hasn't.
5525 * This is safe because ->completion_lock is held, and submissions
5526 * and completions are never mixed in the same ->completion_lock section.
5527 */
5528 ctx->cq_last_tm_flush = tail;
5529
Jens Axboe5262f562019-09-17 12:26:57 -06005530 /*
5531 * Insertion sort, ensuring the first entry in the list is always
5532 * the one we need first.
5533 */
Jens Axboe5262f562019-09-17 12:26:57 -06005534 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005535 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5536 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005537
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005538 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005539 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005540 /* nxt.seq is behind @tail, otherwise would've been completed */
5541 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005542 break;
5543 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005544add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005545 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005546 data->timer.function = io_timeout_fn;
5547 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005548 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005549 return 0;
5550}
5551
Jens Axboe62755e32019-10-28 21:49:21 -06005552static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005553{
Jens Axboe62755e32019-10-28 21:49:21 -06005554 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005555
Jens Axboe62755e32019-10-28 21:49:21 -06005556 return req->user_data == (unsigned long) data;
5557}
5558
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005559static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005560{
Jens Axboe62755e32019-10-28 21:49:21 -06005561 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005562 int ret = 0;
5563
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005564 if (!tctx->io_wq)
5565 return -ENOENT;
5566
5567 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005568 switch (cancel_ret) {
5569 case IO_WQ_CANCEL_OK:
5570 ret = 0;
5571 break;
5572 case IO_WQ_CANCEL_RUNNING:
5573 ret = -EALREADY;
5574 break;
5575 case IO_WQ_CANCEL_NOTFOUND:
5576 ret = -ENOENT;
5577 break;
5578 }
5579
Jens Axboee977d6d2019-11-05 12:39:45 -07005580 return ret;
5581}
5582
Jens Axboe47f46762019-11-09 17:43:02 -07005583static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5584 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005585 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005586{
5587 unsigned long flags;
5588 int ret;
5589
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005590 ret = io_async_cancel_one(req->task->io_uring,
5591 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005592 if (ret != -ENOENT) {
5593 spin_lock_irqsave(&ctx->completion_lock, flags);
5594 goto done;
5595 }
5596
5597 spin_lock_irqsave(&ctx->completion_lock, flags);
5598 ret = io_timeout_cancel(ctx, sqe_addr);
5599 if (ret != -ENOENT)
5600 goto done;
5601 ret = io_poll_cancel(ctx, sqe_addr);
5602done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005603 if (!ret)
5604 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005605 io_cqring_fill_event(req, ret);
5606 io_commit_cqring(ctx);
5607 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5608 io_cqring_ev_posted(ctx);
5609
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005610 if (ret < 0)
5611 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005612 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005613}
5614
Jens Axboe3529d8c2019-12-19 18:24:38 -07005615static int io_async_cancel_prep(struct io_kiocb *req,
5616 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005617{
Jens Axboefbf23842019-12-17 18:45:56 -07005618 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005619 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005620 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5621 return -EINVAL;
5622 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005623 return -EINVAL;
5624
Jens Axboefbf23842019-12-17 18:45:56 -07005625 req->cancel.addr = READ_ONCE(sqe->addr);
5626 return 0;
5627}
5628
Pavel Begunkov61e98202021-02-10 00:03:08 +00005629static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005630{
5631 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005632
Pavel Begunkov014db002020-03-03 21:33:12 +03005633 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005634 return 0;
5635}
5636
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005637static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005638 const struct io_uring_sqe *sqe)
5639{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005640 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5641 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005642 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5643 return -EINVAL;
5644 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005645 return -EINVAL;
5646
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005647 req->rsrc_update.offset = READ_ONCE(sqe->off);
5648 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5649 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005650 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005651 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005652 return 0;
5653}
5654
Pavel Begunkov889fca72021-02-10 00:03:09 +00005655static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005656{
5657 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005658 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005659 int ret;
5660
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005661 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005662 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005663
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005664 up.offset = req->rsrc_update.offset;
5665 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005666
5667 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005668 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005669 mutex_unlock(&ctx->uring_lock);
5670
5671 if (ret < 0)
5672 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005673 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005674 return 0;
5675}
5676
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005677static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005678{
Jens Axboed625c6e2019-12-17 19:53:05 -07005679 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005680 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005681 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005682 case IORING_OP_READV:
5683 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005684 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005685 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005686 case IORING_OP_WRITEV:
5687 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005688 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005689 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005690 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005691 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005692 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005693 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005694 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005695 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005696 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005697 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005698 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005699 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005700 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005701 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005702 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005703 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005704 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005705 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005706 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005707 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005708 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005709 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005710 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005711 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005712 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005713 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005714 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005715 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005716 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005717 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005718 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005719 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005720 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005721 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005722 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005723 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005724 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005725 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005726 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005727 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005728 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005729 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005730 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005731 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005732 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005733 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005734 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005735 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005736 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005737 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005738 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005739 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005740 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005741 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005742 case IORING_OP_SHUTDOWN:
5743 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005744 case IORING_OP_RENAMEAT:
5745 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005746 case IORING_OP_UNLINKAT:
5747 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005748 }
5749
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005750 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5751 req->opcode);
5752 return-EINVAL;
5753}
5754
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005755static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005756{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005757 switch (req->opcode) {
5758 case IORING_OP_READV:
5759 case IORING_OP_READ_FIXED:
5760 case IORING_OP_READ:
5761 return io_rw_prep_async(req, READ);
5762 case IORING_OP_WRITEV:
5763 case IORING_OP_WRITE_FIXED:
5764 case IORING_OP_WRITE:
5765 return io_rw_prep_async(req, WRITE);
5766 case IORING_OP_SENDMSG:
5767 case IORING_OP_SEND:
5768 return io_sendmsg_prep_async(req);
5769 case IORING_OP_RECVMSG:
5770 case IORING_OP_RECV:
5771 return io_recvmsg_prep_async(req);
5772 case IORING_OP_CONNECT:
5773 return io_connect_prep_async(req);
5774 }
5775 return 0;
5776}
5777
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005778static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005779{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005780 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005781 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005782 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005783 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005784 return 0;
5785 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005786 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005787 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005788}
5789
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005790static u32 io_get_sequence(struct io_kiocb *req)
5791{
5792 struct io_kiocb *pos;
5793 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005794 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005795
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005796 io_for_each_link(pos, req)
5797 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005798
5799 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5800 return total_submitted - nr_reqs;
5801}
5802
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005803static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005804{
5805 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005806 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005807 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005808 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809
5810 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005811 if (likely(list_empty_careful(&ctx->defer_list) &&
5812 !(req->flags & REQ_F_IO_DRAIN)))
5813 return 0;
5814
5815 seq = io_get_sequence(req);
5816 /* Still a chance to pass the sequence check */
5817 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818 return 0;
5819
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005820 ret = io_req_defer_prep(req);
5821 if (ret)
5822 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005823 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005824 de = kmalloc(sizeof(*de), GFP_KERNEL);
5825 if (!de)
5826 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005827
5828 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005829 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005830 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005831 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005832 io_queue_async_work(req);
5833 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005834 }
5835
5836 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005837 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005838 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005839 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005840 spin_unlock_irq(&ctx->completion_lock);
5841 return -EIOCBQUEUED;
5842}
Jens Axboeedafcce2019-01-09 09:16:05 -07005843
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005844static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005845{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005846 if (req->flags & REQ_F_BUFFER_SELECTED) {
5847 switch (req->opcode) {
5848 case IORING_OP_READV:
5849 case IORING_OP_READ_FIXED:
5850 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005851 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005852 break;
5853 case IORING_OP_RECVMSG:
5854 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005855 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005856 break;
5857 }
5858 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005859 }
5860
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005861 if (req->flags & REQ_F_NEED_CLEANUP) {
5862 switch (req->opcode) {
5863 case IORING_OP_READV:
5864 case IORING_OP_READ_FIXED:
5865 case IORING_OP_READ:
5866 case IORING_OP_WRITEV:
5867 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005868 case IORING_OP_WRITE: {
5869 struct io_async_rw *io = req->async_data;
5870 if (io->free_iovec)
5871 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005872 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005873 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005874 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005875 case IORING_OP_SENDMSG: {
5876 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005877
5878 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005879 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005880 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005881 case IORING_OP_SPLICE:
5882 case IORING_OP_TEE:
5883 io_put_file(req, req->splice.file_in,
5884 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5885 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005886 case IORING_OP_OPENAT:
5887 case IORING_OP_OPENAT2:
5888 if (req->open.filename)
5889 putname(req->open.filename);
5890 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06005891 case IORING_OP_RENAMEAT:
5892 putname(req->rename.oldpath);
5893 putname(req->rename.newpath);
5894 break;
Jens Axboe14a11432020-09-28 14:27:37 -06005895 case IORING_OP_UNLINKAT:
5896 putname(req->unlink.filename);
5897 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005898 }
5899 req->flags &= ~REQ_F_NEED_CLEANUP;
5900 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005901}
5902
Pavel Begunkov889fca72021-02-10 00:03:09 +00005903static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07005904{
Jens Axboeedafcce2019-01-09 09:16:05 -07005905 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07005906 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07005907 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005908
Jens Axboe5730b272021-02-27 15:57:30 -07005909 if (req->work.personality) {
5910 const struct cred *new_creds;
5911
5912 if (!(issue_flags & IO_URING_F_NONBLOCK))
5913 mutex_lock(&ctx->uring_lock);
5914 new_creds = idr_find(&ctx->personality_idr, req->work.personality);
5915 if (!(issue_flags & IO_URING_F_NONBLOCK))
5916 mutex_unlock(&ctx->uring_lock);
5917 if (!new_creds)
5918 return -EINVAL;
5919 creds = override_creds(new_creds);
5920 }
5921
Jens Axboed625c6e2019-12-17 19:53:05 -07005922 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005923 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005924 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07005925 break;
5926 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005927 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005928 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005929 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005930 break;
5931 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005932 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005933 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005934 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005935 break;
5936 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005937 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005938 break;
5939 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005940 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005941 break;
5942 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005943 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005944 break;
5945 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005946 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005947 break;
5948 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005949 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005950 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005951 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005952 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005953 break;
5954 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005955 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005956 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005957 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005958 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005959 break;
5960 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005961 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005962 break;
5963 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005964 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005965 break;
5966 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005967 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005968 break;
5969 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005970 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005971 break;
5972 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005973 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005974 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005975 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005976 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07005977 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005978 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005979 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005980 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005981 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005982 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07005983 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005984 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005985 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005986 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005987 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005988 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005989 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005990 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005991 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07005992 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005993 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005994 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07005995 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005996 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005997 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07005998 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005999 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006000 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006001 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006002 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006003 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006004 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006005 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006006 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006007 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006008 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006009 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006010 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006011 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006012 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006013 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006014 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006015 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006016 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006017 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006018 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006019 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006020 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006021 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006022 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006023 default:
6024 ret = -EINVAL;
6025 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006026 }
6027
Jens Axboe5730b272021-02-27 15:57:30 -07006028 if (creds)
6029 revert_creds(creds);
6030
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031 if (ret)
6032 return ret;
6033
Jens Axboeb5325762020-05-19 21:20:27 -06006034 /* If the op doesn't have a file, we're not polling for it */
6035 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006036 const bool in_async = io_wq_current_is_worker();
6037
Jens Axboe11ba8202020-01-15 21:51:17 -07006038 /* workqueue context doesn't hold uring_lock, grab it now */
6039 if (in_async)
6040 mutex_lock(&ctx->uring_lock);
6041
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006042 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006043
6044 if (in_async)
6045 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006046 }
6047
6048 return 0;
6049}
6050
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006051static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006052{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006053 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006054 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006055 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006056
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006057 timeout = io_prep_linked_timeout(req);
6058 if (timeout)
6059 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006060
Jens Axboe4014d942021-01-19 15:53:54 -07006061 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006062 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006063
Jens Axboe561fb042019-10-24 07:25:42 -06006064 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006065 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006066 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006067 /*
6068 * We can get EAGAIN for polled IO even though we're
6069 * forcing a sync submission from here, since we can't
6070 * wait for request slots on the block side.
6071 */
6072 if (ret != -EAGAIN)
6073 break;
6074 cond_resched();
6075 } while (1);
6076 }
Jens Axboe31b51512019-01-18 22:56:34 -07006077
Pavel Begunkova3df76982021-02-18 22:32:52 +00006078 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006079 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006080 /* io-wq is going to take one down */
6081 refcount_inc(&req->refs);
6082 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006083 }
Jens Axboe31b51512019-01-18 22:56:34 -07006084}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085
Jens Axboe65e19f52019-10-26 07:20:21 -06006086static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6087 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006088{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006089 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006090
Jens Axboe05f3fb32019-12-09 11:22:50 -07006091 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006092 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006093}
6094
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006095static struct file *io_file_get(struct io_submit_state *state,
6096 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006097{
6098 struct io_ring_ctx *ctx = req->ctx;
6099 struct file *file;
6100
6101 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006102 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006103 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006104 fd = array_index_nospec(fd, ctx->nr_user_files);
6105 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006106 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006107 } else {
6108 trace_io_uring_file_get(ctx, fd);
6109 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006110 }
6111
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006112 if (file && unlikely(file->f_op == &io_uring_fops))
6113 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006114 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006115}
6116
Jens Axboe2665abf2019-11-05 12:40:47 -07006117static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6118{
Jens Axboead8a48a2019-11-15 08:49:11 -07006119 struct io_timeout_data *data = container_of(timer,
6120 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006121 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006122 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006123 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006124
6125 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006126 prev = req->timeout.head;
6127 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006128
6129 /*
6130 * We don't expect the list to be empty, that will only happen if we
6131 * race with the completion of the linked work.
6132 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006133 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006134 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006135 else
6136 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006137 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6138
6139 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006140 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006141 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006142 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006143 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006144 io_req_complete_post(req, -ETIME, 0);
6145 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006146 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006147 return HRTIMER_NORESTART;
6148}
6149
Jens Axboe7271ef32020-08-10 09:55:22 -06006150static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006151{
Jens Axboe76a46e02019-11-10 23:34:16 -07006152 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006153 * If the back reference is NULL, then our linked request finished
6154 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006155 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006156 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006157 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006158
Jens Axboead8a48a2019-11-15 08:49:11 -07006159 data->timer.function = io_link_timeout_fn;
6160 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6161 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006162 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006163}
6164
6165static void io_queue_linked_timeout(struct io_kiocb *req)
6166{
6167 struct io_ring_ctx *ctx = req->ctx;
6168
6169 spin_lock_irq(&ctx->completion_lock);
6170 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006171 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006172
Jens Axboe2665abf2019-11-05 12:40:47 -07006173 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006174 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006175}
6176
Jens Axboead8a48a2019-11-15 08:49:11 -07006177static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006178{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006179 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006181 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6182 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006183 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006184
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006185 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006186 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006187 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006188 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006189}
6190
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006191static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006192{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006193 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194 int ret;
6195
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006196 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006197
6198 /*
6199 * We async punt it if the file wasn't marked NOWAIT, or if the file
6200 * doesn't support non-blocking read/write attempts
6201 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006202 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006203 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006204 /*
6205 * Queued up for async execution, worker will release
6206 * submit reference when the iocb is actually submitted.
6207 */
6208 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006210 } else if (likely(!ret)) {
6211 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006212 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006213 struct io_ring_ctx *ctx = req->ctx;
6214 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006215
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006216 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006217 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006218 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006219 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006220 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006221 }
6222 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006223 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006224 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006225 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006226 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006227 if (linked_timeout)
6228 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006229}
6230
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006231static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006232{
6233 int ret;
6234
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006235 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006236 if (ret) {
6237 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006238fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006239 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006240 io_put_req(req);
6241 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006242 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006243 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006244 ret = io_req_defer_prep(req);
6245 if (unlikely(ret))
6246 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006247 io_queue_async_work(req);
6248 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006249 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006250 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006251}
6252
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006253/*
6254 * Check SQE restrictions (opcode and flags).
6255 *
6256 * Returns 'true' if SQE is allowed, 'false' otherwise.
6257 */
6258static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6259 struct io_kiocb *req,
6260 unsigned int sqe_flags)
6261{
6262 if (!ctx->restricted)
6263 return true;
6264
6265 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6266 return false;
6267
6268 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6269 ctx->restrictions.sqe_flags_required)
6270 return false;
6271
6272 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6273 ctx->restrictions.sqe_flags_required))
6274 return false;
6275
6276 return true;
6277}
6278
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006279static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006280 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006281{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006282 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006283 unsigned int sqe_flags;
Jens Axboe5730b272021-02-27 15:57:30 -07006284 int ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006285
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006286 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006287 /* same numerical values with corresponding REQ_F_*, safe to copy */
6288 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006289 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006290 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006291 req->file = NULL;
6292 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006293 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006294 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006295 /* one is dropped after submission, the other at completion */
6296 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006297 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006298 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006299
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006300 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006301 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6302 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006303 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006304 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006305
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006306 if (unlikely(req->opcode >= IORING_OP_LAST))
6307 return -EINVAL;
6308
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006309 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6310 return -EACCES;
6311
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006312 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6313 !io_op_defs[req->opcode].buffer_select)
6314 return -EOPNOTSUPP;
6315
Jens Axboe5730b272021-02-27 15:57:30 -07006316 req->work.list.next = NULL;
6317 req->work.flags = 0;
6318 req->work.personality = READ_ONCE(sqe->personality);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006319 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006320
Jens Axboe27926b62020-10-28 09:33:23 -06006321 /*
6322 * Plug now if we have more than 1 IO left after this, and the target
6323 * is potentially a read/write to block based storage.
6324 */
6325 if (!state->plug_started && state->ios_left > 1 &&
6326 io_op_defs[req->opcode].plug) {
6327 blk_start_plug(&state->plug);
6328 state->plug_started = true;
6329 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006330
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006331 if (io_op_defs[req->opcode].needs_file) {
6332 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006333
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006334 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006335 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006336 ret = -EBADF;
6337 }
6338
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006339 state->ios_left--;
6340 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006341}
6342
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006343static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006344 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006345{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006346 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006347 int ret;
6348
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006349 ret = io_init_req(ctx, req, sqe);
6350 if (unlikely(ret)) {
6351fail_req:
6352 io_put_req(req);
6353 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006354 if (link->head) {
6355 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006356 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006357 io_put_req(link->head);
6358 io_req_complete(link->head, -ECANCELED);
6359 link->head = NULL;
6360 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006361 return ret;
6362 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006363 ret = io_req_prep(req, sqe);
6364 if (unlikely(ret))
6365 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006366
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006367 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006368 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6369 true, ctx->flags & IORING_SETUP_SQPOLL);
6370
Jens Axboe6c271ce2019-01-10 11:22:30 -07006371 /*
6372 * If we already have a head request, queue this one for async
6373 * submittal once the head completes. If we don't have a head but
6374 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6375 * submitted sync once the chain is complete. If none of those
6376 * conditions are true (normal request), then just queue it.
6377 */
6378 if (link->head) {
6379 struct io_kiocb *head = link->head;
6380
6381 /*
6382 * Taking sequential execution of a link, draining both sides
6383 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6384 * requests in the link. So, it drains the head and the
6385 * next after the link request. The last one is done via
6386 * drain_next flag to persist the effect across calls.
6387 */
6388 if (req->flags & REQ_F_IO_DRAIN) {
6389 head->flags |= REQ_F_IO_DRAIN;
6390 ctx->drain_next = 1;
6391 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006392 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006393 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006394 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006395 trace_io_uring_link(ctx, req, head);
6396 link->last->link = req;
6397 link->last = req;
6398
6399 /* last request of a link, enqueue the link */
6400 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006401 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006402 link->head = NULL;
6403 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006404 } else {
6405 if (unlikely(ctx->drain_next)) {
6406 req->flags |= REQ_F_IO_DRAIN;
6407 ctx->drain_next = 0;
6408 }
6409 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006410 link->head = req;
6411 link->last = req;
6412 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006413 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006414 }
6415 }
6416
6417 return 0;
6418}
6419
6420/*
6421 * Batched submission is done, ensure local IO is flushed out.
6422 */
6423static void io_submit_state_end(struct io_submit_state *state,
6424 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006425{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006426 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006427 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006428 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006429 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006430 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006431 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006432 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006433}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006434
Jens Axboe9e645e112019-05-10 16:07:28 -06006435/*
6436 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006437 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006438static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006439 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006440{
6441 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006442 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006443 /* set only head, no need to init link_last in advance */
6444 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006445}
6446
Jens Axboe193155c2020-02-22 23:22:19 -07006447static void io_commit_sqring(struct io_ring_ctx *ctx)
6448{
Jens Axboe75c6a032020-01-28 10:15:23 -07006449 struct io_rings *rings = ctx->rings;
6450
6451 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006452 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006453 * since once we write the new head, the application could
6454 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006455 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006456 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006457}
6458
Jens Axboe9e645e112019-05-10 16:07:28 -06006459/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006460 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006461 * that is mapped by userspace. This means that care needs to be taken to
6462 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006463 * being a good citizen. If members of the sqe are validated and then later
6464 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006465 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006466 */
6467static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006468{
6469 u32 *sq_array = ctx->sq_array;
6470 unsigned head;
6471
6472 /*
6473 * The cached sq head (or cq tail) serves two purposes:
6474 *
6475 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006476 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006477 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006478 * though the application is the one updating it.
6479 */
6480 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6481 if (likely(head < ctx->sq_entries))
6482 return &ctx->sq_sqes[head];
6483
6484 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006485 ctx->cached_sq_dropped++;
6486 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6487 return NULL;
6488}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006489
Jens Axboe0f212202020-09-13 13:09:39 -06006490static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006491{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006492 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006493
Jens Axboec4a2ed72019-11-21 21:01:26 -07006494 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006495 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006496 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006497 return -EBUSY;
6498 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006499
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006500 /* make sure SQ entry isn't read before tail */
6501 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006502
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006503 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6504 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006505
Jens Axboed8a6df12020-10-15 16:24:45 -06006506 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006507 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006508 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006509
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006510 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006511 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006512 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006513
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006514 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006515 if (unlikely(!req)) {
6516 if (!submitted)
6517 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006518 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006519 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006520 sqe = io_get_sqe(ctx);
6521 if (unlikely(!sqe)) {
6522 kmem_cache_free(req_cachep, req);
6523 break;
6524 }
Jens Axboed3656342019-12-18 09:50:26 -07006525 /* will complete beyond this point, count as submitted */
6526 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006527 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006528 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006529 }
6530
Pavel Begunkov9466f432020-01-25 22:34:01 +03006531 if (unlikely(submitted != nr)) {
6532 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006533 struct io_uring_task *tctx = current->io_uring;
6534 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006535
Jens Axboed8a6df12020-10-15 16:24:45 -06006536 percpu_ref_put_many(&ctx->refs, unused);
6537 percpu_counter_sub(&tctx->inflight, unused);
6538 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006539 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006540
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006541 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006542 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6543 io_commit_sqring(ctx);
6544
Jens Axboe6c271ce2019-01-10 11:22:30 -07006545 return submitted;
6546}
6547
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006548static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6549{
6550 /* Tell userspace we may need a wakeup call */
6551 spin_lock_irq(&ctx->completion_lock);
6552 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6553 spin_unlock_irq(&ctx->completion_lock);
6554}
6555
6556static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6557{
6558 spin_lock_irq(&ctx->completion_lock);
6559 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6560 spin_unlock_irq(&ctx->completion_lock);
6561}
6562
Xiaoguang Wang08369242020-11-03 14:15:59 +08006563static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006564{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006565 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006566 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006567
Jens Axboec8d1ba52020-09-14 11:07:26 -06006568 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006569 /* if we're handling multiple rings, cap submit size for fairness */
6570 if (cap_entries && to_submit > 8)
6571 to_submit = 8;
6572
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006573 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6574 unsigned nr_events = 0;
6575
Xiaoguang Wang08369242020-11-03 14:15:59 +08006576 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006577 if (!list_empty(&ctx->iopoll_list))
6578 io_do_iopoll(ctx, &nr_events, 0);
6579
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00006580 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006581 ret = io_submit_sqes(ctx, to_submit);
6582 mutex_unlock(&ctx->uring_lock);
6583 }
Jens Axboe90554202020-09-03 12:12:41 -06006584
6585 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6586 wake_up(&ctx->sqo_sq_wait);
6587
Xiaoguang Wang08369242020-11-03 14:15:59 +08006588 return ret;
6589}
6590
6591static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6592{
6593 struct io_ring_ctx *ctx;
6594 unsigned sq_thread_idle = 0;
6595
6596 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6597 if (sq_thread_idle < ctx->sq_thread_idle)
6598 sq_thread_idle = ctx->sq_thread_idle;
6599 }
6600
6601 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006602}
6603
Jens Axboe69fb2132020-09-14 11:16:23 -06006604static void io_sqd_init_new(struct io_sq_data *sqd)
6605{
6606 struct io_ring_ctx *ctx;
6607
6608 while (!list_empty(&sqd->ctx_new_list)) {
6609 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006610 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6611 complete(&ctx->sq_thread_comp);
6612 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006613
6614 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006615}
6616
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006617static bool io_sq_thread_should_stop(struct io_sq_data *sqd)
6618{
6619 return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6620}
6621
6622static bool io_sq_thread_should_park(struct io_sq_data *sqd)
6623{
6624 return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
6625}
6626
6627static void io_sq_thread_parkme(struct io_sq_data *sqd)
6628{
6629 for (;;) {
6630 /*
6631 * TASK_PARKED is a special state; we must serialize against
6632 * possible pending wakeups to avoid store-store collisions on
6633 * task->state.
6634 *
6635 * Such a collision might possibly result in the task state
6636 * changin from TASK_PARKED and us failing the
6637 * wait_task_inactive() in kthread_park().
6638 */
6639 set_special_state(TASK_PARKED);
6640 if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state))
6641 break;
6642
6643 /*
6644 * Thread is going to call schedule(), do not preempt it,
6645 * or the caller of kthread_park() may spend more time in
6646 * wait_task_inactive().
6647 */
6648 preempt_disable();
6649 complete(&sqd->completion);
6650 schedule_preempt_disabled();
6651 preempt_enable();
6652 }
6653 __set_current_state(TASK_RUNNING);
6654}
6655
Jens Axboe6c271ce2019-01-10 11:22:30 -07006656static int io_sq_thread(void *data)
6657{
Jens Axboe69fb2132020-09-14 11:16:23 -06006658 struct io_sq_data *sqd = data;
6659 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006660 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006661 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006662 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006663
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006664 sprintf(buf, "iou-sqp-%d", sqd->task_pid);
6665 set_task_comm(current, buf);
6666 sqd->thread = current;
6667 current->pf_io_worker = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006668
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006669 if (sqd->sq_cpu != -1)
6670 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6671 else
6672 set_cpus_allowed_ptr(current, cpu_online_mask);
6673 current->flags |= PF_NO_SETAFFINITY;
6674
6675 complete(&sqd->completion);
6676
6677 wait_for_completion(&sqd->startup);
6678
6679 while (!io_sq_thread_should_stop(sqd)) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006680 int ret;
6681 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006682
6683 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006684 * Any changes to the sqd lists are synchronized through the
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006685 * thread parking. This synchronizes the thread vs users,
Jens Axboe69fb2132020-09-14 11:16:23 -06006686 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006687 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006688 if (io_sq_thread_should_park(sqd)) {
6689 io_sq_thread_parkme(sqd);
6690 continue;
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006691 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006692 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006693 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006694 timeout = jiffies + sqd->sq_thread_idle;
6695 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006696 if (fatal_signal_pending(current))
6697 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006698 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006699 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006700 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006701 ret = __io_sq_thread(ctx, cap_entries);
6702 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6703 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006704 }
6705
Xiaoguang Wang08369242020-11-03 14:15:59 +08006706 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006707 io_run_task_work();
6708 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006709 if (sqt_spin)
6710 timeout = jiffies + sqd->sq_thread_idle;
6711 continue;
6712 }
6713
Xiaoguang Wang08369242020-11-03 14:15:59 +08006714 needs_sched = true;
6715 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6716 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6717 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6718 !list_empty_careful(&ctx->iopoll_list)) {
6719 needs_sched = false;
6720 break;
6721 }
6722 if (io_sqring_entries(ctx)) {
6723 needs_sched = false;
6724 break;
6725 }
6726 }
6727
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006728 if (needs_sched && !io_sq_thread_should_park(sqd)) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006729 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6730 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006731
Jens Axboe69fb2132020-09-14 11:16:23 -06006732 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06006733 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6734 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006735 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006736
6737 finish_wait(&sqd->wait, &wait);
6738 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006739 }
6740
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006741 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6742 io_uring_cancel_sqpoll(ctx);
6743
Jens Axboe4c6e2772020-07-01 11:29:10 -06006744 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006745
Jens Axboe86293972021-02-26 13:46:49 -07006746 if (io_sq_thread_should_park(sqd))
6747 io_sq_thread_parkme(sqd);
6748
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006749 /*
6750 * Clear thread under lock so that concurrent parks work correctly
6751 */
Jens Axboe86293972021-02-26 13:46:49 -07006752 complete(&sqd->completion);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006753 mutex_lock(&sqd->lock);
6754 sqd->thread = NULL;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07006755 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6756 ctx->sqo_exec = 1;
6757 io_ring_set_wakeup_flag(ctx);
6758 }
Jens Axboe06058632019-04-13 09:26:03 -06006759
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006760 complete(&sqd->exited);
Jens Axboee54945a2021-02-26 11:27:15 -07006761 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006762 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006763}
6764
Jens Axboebda52162019-09-24 13:47:15 -06006765struct io_wait_queue {
6766 struct wait_queue_entry wq;
6767 struct io_ring_ctx *ctx;
6768 unsigned to_wait;
6769 unsigned nr_timeouts;
6770};
6771
Pavel Begunkov6c503152021-01-04 20:36:36 +00006772static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006773{
6774 struct io_ring_ctx *ctx = iowq->ctx;
6775
6776 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006777 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006778 * started waiting. For timeouts, we always want to return to userspace,
6779 * regardless of event count.
6780 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006781 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006782 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6783}
6784
6785static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6786 int wake_flags, void *key)
6787{
6788 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6789 wq);
6790
Pavel Begunkov6c503152021-01-04 20:36:36 +00006791 /*
6792 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6793 * the task, and the next invocation will do it.
6794 */
6795 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6796 return autoremove_wake_function(curr, mode, wake_flags, key);
6797 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006798}
6799
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006800static int io_run_task_work_sig(void)
6801{
6802 if (io_run_task_work())
6803 return 1;
6804 if (!signal_pending(current))
6805 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06006806 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
6807 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006808 return -EINTR;
6809}
6810
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006811/* when returns >0, the caller should retry */
6812static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6813 struct io_wait_queue *iowq,
6814 signed long *timeout)
6815{
6816 int ret;
6817
6818 /* make sure we run task_work before checking for signals */
6819 ret = io_run_task_work_sig();
6820 if (ret || io_should_wake(iowq))
6821 return ret;
6822 /* let the caller flush overflows, retry */
6823 if (test_bit(0, &ctx->cq_check_overflow))
6824 return 1;
6825
6826 *timeout = schedule_timeout(*timeout);
6827 return !*timeout ? -ETIME : 1;
6828}
6829
Jens Axboe2b188cc2019-01-07 10:46:33 -07006830/*
6831 * Wait until events become available, if we don't already have some. The
6832 * application must reap them itself, as they reside on the shared cq ring.
6833 */
6834static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006835 const sigset_t __user *sig, size_t sigsz,
6836 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006837{
Jens Axboebda52162019-09-24 13:47:15 -06006838 struct io_wait_queue iowq = {
6839 .wq = {
6840 .private = current,
6841 .func = io_wake_function,
6842 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6843 },
6844 .ctx = ctx,
6845 .to_wait = min_events,
6846 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006847 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006848 signed long timeout = MAX_SCHEDULE_TIMEOUT;
6849 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006850
Jens Axboeb41e9852020-02-17 09:52:41 -07006851 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006852 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6853 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006854 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006855 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006856 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006857 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006858
6859 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006860#ifdef CONFIG_COMPAT
6861 if (in_compat_syscall())
6862 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006863 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006864 else
6865#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006866 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006867
Jens Axboe2b188cc2019-01-07 10:46:33 -07006868 if (ret)
6869 return ret;
6870 }
6871
Hao Xuc73ebb62020-11-03 10:54:37 +08006872 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006873 struct timespec64 ts;
6874
Hao Xuc73ebb62020-11-03 10:54:37 +08006875 if (get_timespec64(&ts, uts))
6876 return -EFAULT;
6877 timeout = timespec64_to_jiffies(&ts);
6878 }
6879
Jens Axboebda52162019-09-24 13:47:15 -06006880 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006881 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006882 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006883 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06006884 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6885 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006886 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
6887 finish_wait(&ctx->wait, &iowq.wq);
6888 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06006889
Jens Axboeb7db41c2020-07-04 08:55:50 -06006890 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006891
Hristo Venev75b28af2019-08-26 17:23:46 +00006892 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006893}
6894
Jens Axboe6b063142019-01-10 22:13:58 -07006895static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6896{
6897#if defined(CONFIG_UNIX)
6898 if (ctx->ring_sock) {
6899 struct sock *sock = ctx->ring_sock->sk;
6900 struct sk_buff *skb;
6901
6902 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6903 kfree_skb(skb);
6904 }
6905#else
6906 int i;
6907
Jens Axboe65e19f52019-10-26 07:20:21 -06006908 for (i = 0; i < ctx->nr_user_files; i++) {
6909 struct file *file;
6910
6911 file = io_file_from_index(ctx, i);
6912 if (file)
6913 fput(file);
6914 }
Jens Axboe6b063142019-01-10 22:13:58 -07006915#endif
6916}
6917
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00006918static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006919{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006920 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006921
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006922 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006923 complete(&data->done);
6924}
6925
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006926static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00006927{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006928 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00006929}
6930
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006931static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07006932{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006933 spin_unlock_bh(&ctx->rsrc_ref_lock);
6934}
6935
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006936static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
6937 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006938 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07006939{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006940 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006941 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006942 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006943 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006944 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07006945}
6946
Hao Xu8bad28d2021-02-19 17:19:36 +08006947static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07006948{
Hao Xu8bad28d2021-02-19 17:19:36 +08006949 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006950
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006951 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00006952 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00006953 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006954 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006955 if (ref_node)
6956 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006957}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006958
Hao Xu8bad28d2021-02-19 17:19:36 +08006959static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
6960 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006961 void (*rsrc_put)(struct io_ring_ctx *ctx,
6962 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08006963{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006964 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08006965 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006966
Hao Xu8bad28d2021-02-19 17:19:36 +08006967 if (data->quiesce)
6968 return -ENXIO;
6969
6970 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00006971 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006972 ret = -ENOMEM;
6973 backup_node = alloc_fixed_rsrc_ref_node(ctx);
6974 if (!backup_node)
6975 break;
6976 backup_node->rsrc_data = data;
6977 backup_node->rsrc_put = rsrc_put;
6978
Hao Xu8bad28d2021-02-19 17:19:36 +08006979 io_sqe_rsrc_kill_node(ctx, data);
6980 percpu_ref_kill(&data->refs);
6981 flush_delayed_work(&ctx->rsrc_put_work);
6982
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00006983 ret = wait_for_completion_interruptible(&data->done);
6984 if (!ret)
6985 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006986
Jens Axboecb5e1b82021-02-25 07:37:35 -07006987 percpu_ref_resurrect(&data->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006988 io_sqe_rsrc_set_node(ctx, data, backup_node);
6989 backup_node = NULL;
Jens Axboecb5e1b82021-02-25 07:37:35 -07006990 reinit_completion(&data->done);
Hao Xu8bad28d2021-02-19 17:19:36 +08006991 mutex_unlock(&ctx->uring_lock);
6992 ret = io_run_task_work_sig();
6993 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006994 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08006995 data->quiesce = false;
6996
6997 if (backup_node)
6998 destroy_fixed_rsrc_ref_node(backup_node);
6999 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007000}
7001
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007002static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7003{
7004 struct fixed_rsrc_data *data;
7005
7006 data = kzalloc(sizeof(*data), GFP_KERNEL);
7007 if (!data)
7008 return NULL;
7009
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007010 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007011 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7012 kfree(data);
7013 return NULL;
7014 }
7015 data->ctx = ctx;
7016 init_completion(&data->done);
7017 return data;
7018}
7019
7020static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7021{
7022 percpu_ref_exit(&data->refs);
7023 kfree(data->table);
7024 kfree(data);
7025}
7026
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007027static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7028{
7029 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007030 unsigned nr_tables, i;
7031 int ret;
7032
Hao Xu8bad28d2021-02-19 17:19:36 +08007033 /*
7034 * percpu_ref_is_dying() is to stop parallel files unregister
7035 * Since we possibly drop uring lock later in this function to
7036 * run task work.
7037 */
7038 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007039 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007040 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007041 if (ret)
7042 return ret;
7043
Jens Axboe6b063142019-01-10 22:13:58 -07007044 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007045 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7046 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007047 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007048 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007049 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007050 ctx->nr_user_files = 0;
7051 return 0;
7052}
7053
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007054static void io_sq_thread_unpark(struct io_sq_data *sqd)
7055 __releases(&sqd->lock)
7056{
7057 if (!sqd->thread)
7058 return;
7059 if (sqd->thread == current)
7060 return;
7061 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7062 wake_up_state(sqd->thread, TASK_PARKED);
7063 mutex_unlock(&sqd->lock);
7064}
7065
7066static bool io_sq_thread_park(struct io_sq_data *sqd)
7067 __acquires(&sqd->lock)
7068{
7069 if (sqd->thread == current)
7070 return true;
7071 mutex_lock(&sqd->lock);
7072 if (!sqd->thread) {
7073 mutex_unlock(&sqd->lock);
7074 return false;
7075 }
7076 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7077 wake_up_process(sqd->thread);
7078 wait_for_completion(&sqd->completion);
7079 return true;
7080}
7081
7082static void io_sq_thread_stop(struct io_sq_data *sqd)
7083{
Jens Axboee54945a2021-02-26 11:27:15 -07007084 if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state))
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007085 return;
Jens Axboee54945a2021-02-26 11:27:15 -07007086 mutex_lock(&sqd->lock);
7087 if (sqd->thread) {
7088 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7089 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state));
7090 wake_up_process(sqd->thread);
7091 mutex_unlock(&sqd->lock);
7092 wait_for_completion(&sqd->exited);
7093 WARN_ON_ONCE(sqd->thread);
7094 } else {
7095 mutex_unlock(&sqd->lock);
7096 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007097}
7098
Jens Axboe534ca6d2020-09-02 13:52:19 -06007099static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007100{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007101 if (refcount_dec_and_test(&sqd->refs)) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007102 io_sq_thread_stop(sqd);
7103 kfree(sqd);
7104 }
7105}
7106
7107static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7108{
7109 struct io_sq_data *sqd = ctx->sq_data;
7110
7111 if (sqd) {
Jens Axboeeb858902021-02-25 10:13:29 -07007112 complete(&sqd->startup);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007113 if (sqd->thread) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007114 wait_for_completion(&ctx->sq_thread_comp);
7115 io_sq_thread_park(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007116 }
7117
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007118 mutex_lock(&sqd->ctx_lock);
7119 list_del(&ctx->sqd_list);
7120 io_sqd_update_thread_idle(sqd);
7121 mutex_unlock(&sqd->ctx_lock);
7122
7123 if (sqd->thread)
7124 io_sq_thread_unpark(sqd);
7125
7126 io_put_sq_data(sqd);
7127 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007128 }
7129}
7130
Jens Axboeaa061652020-09-02 14:50:27 -06007131static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7132{
7133 struct io_ring_ctx *ctx_attach;
7134 struct io_sq_data *sqd;
7135 struct fd f;
7136
7137 f = fdget(p->wq_fd);
7138 if (!f.file)
7139 return ERR_PTR(-ENXIO);
7140 if (f.file->f_op != &io_uring_fops) {
7141 fdput(f);
7142 return ERR_PTR(-EINVAL);
7143 }
7144
7145 ctx_attach = f.file->private_data;
7146 sqd = ctx_attach->sq_data;
7147 if (!sqd) {
7148 fdput(f);
7149 return ERR_PTR(-EINVAL);
7150 }
7151
7152 refcount_inc(&sqd->refs);
7153 fdput(f);
7154 return sqd;
7155}
7156
Jens Axboe534ca6d2020-09-02 13:52:19 -06007157static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7158{
7159 struct io_sq_data *sqd;
7160
Jens Axboeaa061652020-09-02 14:50:27 -06007161 if (p->flags & IORING_SETUP_ATTACH_WQ)
7162 return io_attach_sq_data(p);
7163
Jens Axboe534ca6d2020-09-02 13:52:19 -06007164 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7165 if (!sqd)
7166 return ERR_PTR(-ENOMEM);
7167
7168 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007169 INIT_LIST_HEAD(&sqd->ctx_list);
7170 INIT_LIST_HEAD(&sqd->ctx_new_list);
7171 mutex_init(&sqd->ctx_lock);
7172 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007173 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007174 init_completion(&sqd->startup);
7175 init_completion(&sqd->completion);
7176 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007177 return sqd;
7178}
7179
Jens Axboe6b063142019-01-10 22:13:58 -07007180#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007181/*
7182 * Ensure the UNIX gc is aware of our file set, so we are certain that
7183 * the io_uring can be safely unregistered on process exit, even if we have
7184 * loops in the file referencing.
7185 */
7186static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7187{
7188 struct sock *sk = ctx->ring_sock->sk;
7189 struct scm_fp_list *fpl;
7190 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007191 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007192
Jens Axboe6b063142019-01-10 22:13:58 -07007193 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7194 if (!fpl)
7195 return -ENOMEM;
7196
7197 skb = alloc_skb(0, GFP_KERNEL);
7198 if (!skb) {
7199 kfree(fpl);
7200 return -ENOMEM;
7201 }
7202
7203 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007204
Jens Axboe08a45172019-10-03 08:11:03 -06007205 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007206 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007207 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007208 struct file *file = io_file_from_index(ctx, i + offset);
7209
7210 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007211 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007212 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007213 unix_inflight(fpl->user, fpl->fp[nr_files]);
7214 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007215 }
7216
Jens Axboe08a45172019-10-03 08:11:03 -06007217 if (nr_files) {
7218 fpl->max = SCM_MAX_FD;
7219 fpl->count = nr_files;
7220 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007221 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007222 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7223 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007224
Jens Axboe08a45172019-10-03 08:11:03 -06007225 for (i = 0; i < nr_files; i++)
7226 fput(fpl->fp[i]);
7227 } else {
7228 kfree_skb(skb);
7229 kfree(fpl);
7230 }
Jens Axboe6b063142019-01-10 22:13:58 -07007231
7232 return 0;
7233}
7234
7235/*
7236 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7237 * causes regular reference counting to break down. We rely on the UNIX
7238 * garbage collection to take care of this problem for us.
7239 */
7240static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7241{
7242 unsigned left, total;
7243 int ret = 0;
7244
7245 total = 0;
7246 left = ctx->nr_user_files;
7247 while (left) {
7248 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007249
7250 ret = __io_sqe_files_scm(ctx, this_files, total);
7251 if (ret)
7252 break;
7253 left -= this_files;
7254 total += this_files;
7255 }
7256
7257 if (!ret)
7258 return 0;
7259
7260 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007261 struct file *file = io_file_from_index(ctx, total);
7262
7263 if (file)
7264 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007265 total++;
7266 }
7267
7268 return ret;
7269}
7270#else
7271static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7272{
7273 return 0;
7274}
7275#endif
7276
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007277static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007278 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007279{
7280 int i;
7281
7282 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007283 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007284 unsigned this_files;
7285
7286 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7287 table->files = kcalloc(this_files, sizeof(struct file *),
7288 GFP_KERNEL);
7289 if (!table->files)
7290 break;
7291 nr_files -= this_files;
7292 }
7293
7294 if (i == nr_tables)
7295 return 0;
7296
7297 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007298 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007299 kfree(table->files);
7300 }
7301 return 1;
7302}
7303
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007304static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007305{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007306 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007307#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007308 struct sock *sock = ctx->ring_sock->sk;
7309 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7310 struct sk_buff *skb;
7311 int i;
7312
7313 __skb_queue_head_init(&list);
7314
7315 /*
7316 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7317 * remove this entry and rearrange the file array.
7318 */
7319 skb = skb_dequeue(head);
7320 while (skb) {
7321 struct scm_fp_list *fp;
7322
7323 fp = UNIXCB(skb).fp;
7324 for (i = 0; i < fp->count; i++) {
7325 int left;
7326
7327 if (fp->fp[i] != file)
7328 continue;
7329
7330 unix_notinflight(fp->user, fp->fp[i]);
7331 left = fp->count - 1 - i;
7332 if (left) {
7333 memmove(&fp->fp[i], &fp->fp[i + 1],
7334 left * sizeof(struct file *));
7335 }
7336 fp->count--;
7337 if (!fp->count) {
7338 kfree_skb(skb);
7339 skb = NULL;
7340 } else {
7341 __skb_queue_tail(&list, skb);
7342 }
7343 fput(file);
7344 file = NULL;
7345 break;
7346 }
7347
7348 if (!file)
7349 break;
7350
7351 __skb_queue_tail(&list, skb);
7352
7353 skb = skb_dequeue(head);
7354 }
7355
7356 if (skb_peek(&list)) {
7357 spin_lock_irq(&head->lock);
7358 while ((skb = __skb_dequeue(&list)) != NULL)
7359 __skb_queue_tail(head, skb);
7360 spin_unlock_irq(&head->lock);
7361 }
7362#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007363 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007364#endif
7365}
7366
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007367static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007368{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007369 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7370 struct io_ring_ctx *ctx = rsrc_data->ctx;
7371 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007372
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007373 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7374 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007375 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007376 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007377 }
7378
Xiaoguang Wang05589552020-03-31 14:05:18 +08007379 percpu_ref_exit(&ref_node->refs);
7380 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007381 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007382}
7383
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007384static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007385{
7386 struct io_ring_ctx *ctx;
7387 struct llist_node *node;
7388
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007389 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7390 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007391
7392 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007393 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007394 struct llist_node *next = node->next;
7395
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007396 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7397 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007398 node = next;
7399 }
7400}
7401
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007402static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7403 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007404{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007405 struct fixed_rsrc_table *table;
7406
7407 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7408 return &table->files[i & IORING_FILE_TABLE_MASK];
7409}
7410
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007411static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007412{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007413 struct fixed_rsrc_ref_node *ref_node;
7414 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007415 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007416 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007417 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007418
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007419 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7420 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007421 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007422
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007423 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007424 ref_node->done = true;
7425
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007426 while (!list_empty(&ctx->rsrc_ref_list)) {
7427 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007428 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007429 /* recycle ref nodes in order */
7430 if (!ref_node->done)
7431 break;
7432 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007433 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007434 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007435 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007436
7437 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007438 delay = 0;
7439
Jens Axboe4a38aed22020-05-14 17:21:15 -06007440 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007441 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007442 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007443 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007444}
7445
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007446static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007447 struct io_ring_ctx *ctx)
7448{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007449 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007450
7451 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7452 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007453 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007454
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007455 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007456 0, GFP_KERNEL)) {
7457 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007458 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007459 }
7460 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007461 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007462 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007463 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007464}
7465
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007466static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7467 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007468{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007469 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007470 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007471}
7472
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007473static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007474{
7475 percpu_ref_exit(&ref_node->refs);
7476 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007477}
7478
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007479
Jens Axboe05f3fb32019-12-09 11:22:50 -07007480static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7481 unsigned nr_args)
7482{
7483 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007484 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007485 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007486 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007487 struct fixed_rsrc_ref_node *ref_node;
7488 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007489
7490 if (ctx->file_data)
7491 return -EBUSY;
7492 if (!nr_args)
7493 return -EINVAL;
7494 if (nr_args > IORING_MAX_FIXED_FILES)
7495 return -EMFILE;
7496
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007497 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007498 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007499 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007500 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007501
7502 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007503 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007504 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007505 if (!file_data->table)
7506 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007507
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007508 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007509 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007510
Jens Axboe05f3fb32019-12-09 11:22:50 -07007511 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007512 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7513 ret = -EFAULT;
7514 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007515 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007516 /* allow sparse sets */
7517 if (fd == -1)
7518 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007519
Jens Axboe05f3fb32019-12-09 11:22:50 -07007520 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007521 ret = -EBADF;
7522 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007523 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007524
7525 /*
7526 * Don't allow io_uring instances to be registered. If UNIX
7527 * isn't enabled, then this causes a reference cycle and this
7528 * instance can never get freed. If UNIX is enabled we'll
7529 * handle it just fine, but there's still no point in allowing
7530 * a ring fd as it doesn't support regular read/write anyway.
7531 */
7532 if (file->f_op == &io_uring_fops) {
7533 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007534 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007535 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007536 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007537 }
7538
Jens Axboe05f3fb32019-12-09 11:22:50 -07007539 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007540 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007541 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007542 return ret;
7543 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007544
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007545 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007546 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007547 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007548 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007549 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007550 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007551
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007552 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007553 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007554out_fput:
7555 for (i = 0; i < ctx->nr_user_files; i++) {
7556 file = io_file_from_index(ctx, i);
7557 if (file)
7558 fput(file);
7559 }
7560 for (i = 0; i < nr_tables; i++)
7561 kfree(file_data->table[i].files);
7562 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007563out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007564 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007565 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007566 return ret;
7567}
7568
Jens Axboec3a31e62019-10-03 13:59:56 -06007569static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7570 int index)
7571{
7572#if defined(CONFIG_UNIX)
7573 struct sock *sock = ctx->ring_sock->sk;
7574 struct sk_buff_head *head = &sock->sk_receive_queue;
7575 struct sk_buff *skb;
7576
7577 /*
7578 * See if we can merge this file into an existing skb SCM_RIGHTS
7579 * file set. If there's no room, fall back to allocating a new skb
7580 * and filling it in.
7581 */
7582 spin_lock_irq(&head->lock);
7583 skb = skb_peek(head);
7584 if (skb) {
7585 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7586
7587 if (fpl->count < SCM_MAX_FD) {
7588 __skb_unlink(skb, head);
7589 spin_unlock_irq(&head->lock);
7590 fpl->fp[fpl->count] = get_file(file);
7591 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7592 fpl->count++;
7593 spin_lock_irq(&head->lock);
7594 __skb_queue_head(head, skb);
7595 } else {
7596 skb = NULL;
7597 }
7598 }
7599 spin_unlock_irq(&head->lock);
7600
7601 if (skb) {
7602 fput(file);
7603 return 0;
7604 }
7605
7606 return __io_sqe_files_scm(ctx, 1, index);
7607#else
7608 return 0;
7609#endif
7610}
7611
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007612static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007613{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007614 struct io_rsrc_put *prsrc;
7615 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007616
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007617 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7618 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007619 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007620
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007621 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007622 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007623
Hillf Dantona5318d32020-03-23 17:47:15 +08007624 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007625}
7626
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007627static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7628 struct file *file)
7629{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007630 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007631}
7632
Jens Axboe05f3fb32019-12-09 11:22:50 -07007633static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007634 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007635 unsigned nr_args)
7636{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007637 struct fixed_rsrc_data *data = ctx->file_data;
7638 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007639 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007640 __s32 __user *fds;
7641 int fd, i, err;
7642 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007643 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007644
Jens Axboe05f3fb32019-12-09 11:22:50 -07007645 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007646 return -EOVERFLOW;
7647 if (done > ctx->nr_user_files)
7648 return -EINVAL;
7649
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007650 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007651 if (!ref_node)
7652 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007653 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007654
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007655 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007656 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007657 err = 0;
7658 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7659 err = -EFAULT;
7660 break;
7661 }
noah4e0377a2021-01-26 15:23:28 -05007662 if (fd == IORING_REGISTER_FILES_SKIP)
7663 continue;
7664
Pavel Begunkov67973b92021-01-26 13:51:09 +00007665 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007666 file_slot = io_fixed_file_slot(ctx->file_data, i);
7667
7668 if (*file_slot) {
7669 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007670 if (err)
7671 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007672 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007673 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007674 }
7675 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007676 file = fget(fd);
7677 if (!file) {
7678 err = -EBADF;
7679 break;
7680 }
7681 /*
7682 * Don't allow io_uring instances to be registered. If
7683 * UNIX isn't enabled, then this causes a reference
7684 * cycle and this instance can never get freed. If UNIX
7685 * is enabled we'll handle it just fine, but there's
7686 * still no point in allowing a ring fd as it doesn't
7687 * support regular read/write anyway.
7688 */
7689 if (file->f_op == &io_uring_fops) {
7690 fput(file);
7691 err = -EBADF;
7692 break;
7693 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07007694 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007695 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007696 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07007697 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007698 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007699 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007700 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007701 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007702 }
7703
Xiaoguang Wang05589552020-03-31 14:05:18 +08007704 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007705 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007706 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007707 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007708 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007709
7710 return done ? done : err;
7711}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007712
Jens Axboe05f3fb32019-12-09 11:22:50 -07007713static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7714 unsigned nr_args)
7715{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007716 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007717
7718 if (!ctx->file_data)
7719 return -ENXIO;
7720 if (!nr_args)
7721 return -EINVAL;
7722 if (copy_from_user(&up, arg, sizeof(up)))
7723 return -EFAULT;
7724 if (up.resv)
7725 return -EINVAL;
7726
7727 return __io_sqe_files_update(ctx, &up, nr_args);
7728}
Jens Axboec3a31e62019-10-03 13:59:56 -06007729
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007730static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007731{
7732 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7733
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007734 req = io_put_req_find_next(req);
7735 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007736}
7737
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007738static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007739{
Jens Axboee9418942021-02-19 12:33:30 -07007740 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007741 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007742 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007743
Jens Axboee9418942021-02-19 12:33:30 -07007744 hash = ctx->hash_map;
7745 if (!hash) {
7746 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
7747 if (!hash)
7748 return ERR_PTR(-ENOMEM);
7749 refcount_set(&hash->refs, 1);
7750 init_waitqueue_head(&hash->wait);
7751 ctx->hash_map = hash;
7752 }
7753
7754 data.hash = hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007755 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007756 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007757
Jens Axboed25e3a32021-02-16 11:41:41 -07007758 /* Do QD, or 4 * CPUS, whatever is smallest */
7759 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007760
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007761 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007762}
7763
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007764static int io_uring_alloc_task_context(struct task_struct *task,
7765 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007766{
7767 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007768 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007769
7770 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7771 if (unlikely(!tctx))
7772 return -ENOMEM;
7773
Jens Axboed8a6df12020-10-15 16:24:45 -06007774 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7775 if (unlikely(ret)) {
7776 kfree(tctx);
7777 return ret;
7778 }
7779
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007780 tctx->io_wq = io_init_wq_offload(ctx);
7781 if (IS_ERR(tctx->io_wq)) {
7782 ret = PTR_ERR(tctx->io_wq);
7783 percpu_counter_destroy(&tctx->inflight);
7784 kfree(tctx);
7785 return ret;
7786 }
7787
Jens Axboe0f212202020-09-13 13:09:39 -06007788 xa_init(&tctx->xa);
7789 init_waitqueue_head(&tctx->wait);
7790 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007791 atomic_set(&tctx->in_idle, 0);
7792 tctx->sqpoll = false;
Jens Axboe0f212202020-09-13 13:09:39 -06007793 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007794 spin_lock_init(&tctx->task_lock);
7795 INIT_WQ_LIST(&tctx->task_list);
7796 tctx->task_state = 0;
7797 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007798 return 0;
7799}
7800
7801void __io_uring_free(struct task_struct *tsk)
7802{
7803 struct io_uring_task *tctx = tsk->io_uring;
7804
7805 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00007806 WARN_ON_ONCE(tctx->io_wq);
7807
Jens Axboed8a6df12020-10-15 16:24:45 -06007808 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007809 kfree(tctx);
7810 tsk->io_uring = NULL;
7811}
7812
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007813static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx)
7814{
7815 int ret;
7816
7817 clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7818 reinit_completion(&sqd->completion);
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00007819 ctx->sqo_exec = 0;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007820 sqd->task_pid = current->pid;
7821 current->flags |= PF_IO_WORKER;
7822 ret = io_wq_fork_thread(io_sq_thread, sqd);
7823 current->flags &= ~PF_IO_WORKER;
7824 if (ret < 0) {
7825 sqd->thread = NULL;
7826 return ret;
7827 }
7828 wait_for_completion(&sqd->completion);
7829 return io_uring_alloc_task_context(sqd->thread, ctx);
7830}
7831
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007832static int io_sq_offload_create(struct io_ring_ctx *ctx,
7833 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007834{
7835 int ret;
7836
Jens Axboed25e3a32021-02-16 11:41:41 -07007837 /* Retain compatibility with failing for an invalid attach attempt */
7838 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7839 IORING_SETUP_ATTACH_WQ) {
7840 struct fd f;
7841
7842 f = fdget(p->wq_fd);
7843 if (!f.file)
7844 return -ENXIO;
7845 if (f.file->f_op != &io_uring_fops) {
7846 fdput(f);
7847 return -EINVAL;
7848 }
7849 fdput(f);
7850 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007851 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007852 struct io_sq_data *sqd;
7853
Jens Axboe3ec482d2019-04-08 10:51:01 -06007854 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007855 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007856 goto err;
7857
Jens Axboe534ca6d2020-09-02 13:52:19 -06007858 sqd = io_get_sq_data(p);
7859 if (IS_ERR(sqd)) {
7860 ret = PTR_ERR(sqd);
7861 goto err;
7862 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007863
Jens Axboe534ca6d2020-09-02 13:52:19 -06007864 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007865 io_sq_thread_park(sqd);
7866 mutex_lock(&sqd->ctx_lock);
7867 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7868 mutex_unlock(&sqd->ctx_lock);
7869 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007870
Jens Axboe917257d2019-04-13 09:28:55 -06007871 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7872 if (!ctx->sq_thread_idle)
7873 ctx->sq_thread_idle = HZ;
7874
Jens Axboeaa061652020-09-02 14:50:27 -06007875 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007876 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007877
Jens Axboe6c271ce2019-01-10 11:22:30 -07007878 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007879 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007880
Jens Axboe917257d2019-04-13 09:28:55 -06007881 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007882 if (cpu >= nr_cpu_ids)
7883 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007884 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007885 goto err;
7886
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007887 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007888 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007889 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007890 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007891
7892 sqd->task_pid = current->pid;
7893 current->flags |= PF_IO_WORKER;
7894 ret = io_wq_fork_thread(io_sq_thread, sqd);
7895 current->flags &= ~PF_IO_WORKER;
7896 if (ret < 0) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007897 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007898 goto err;
7899 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007900 wait_for_completion(&sqd->completion);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007901 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06007902 if (ret)
7903 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007904 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7905 /* Can't have SQ_AFF without SQPOLL */
7906 ret = -EINVAL;
7907 goto err;
7908 }
7909
Jens Axboe2b188cc2019-01-07 10:46:33 -07007910 return 0;
7911err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007912 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007913 return ret;
7914}
7915
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007916static void io_sq_offload_start(struct io_ring_ctx *ctx)
7917{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007918 struct io_sq_data *sqd = ctx->sq_data;
7919
Jens Axboe3ebba792021-02-28 15:32:18 -07007920 ctx->flags &= ~IORING_SETUP_R_DISABLED;
Jens Axboeeb858902021-02-25 10:13:29 -07007921 if (ctx->flags & IORING_SETUP_SQPOLL)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007922 complete(&sqd->startup);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007923}
7924
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007925static inline void __io_unaccount_mem(struct user_struct *user,
7926 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007927{
7928 atomic_long_sub(nr_pages, &user->locked_vm);
7929}
7930
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007931static inline int __io_account_mem(struct user_struct *user,
7932 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007933{
7934 unsigned long page_limit, cur_pages, new_pages;
7935
7936 /* Don't allow more pages than we can safely lock */
7937 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7938
7939 do {
7940 cur_pages = atomic_long_read(&user->locked_vm);
7941 new_pages = cur_pages + nr_pages;
7942 if (new_pages > page_limit)
7943 return -ENOMEM;
7944 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7945 new_pages) != cur_pages);
7946
7947 return 0;
7948}
7949
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007950static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007951{
Jens Axboe62e398b2021-02-21 16:19:37 -07007952 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007953 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007954
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007955 if (ctx->mm_account)
7956 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007957}
7958
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007959static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007960{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007961 int ret;
7962
Jens Axboe62e398b2021-02-21 16:19:37 -07007963 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007964 ret = __io_account_mem(ctx->user, nr_pages);
7965 if (ret)
7966 return ret;
7967 }
7968
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007969 if (ctx->mm_account)
7970 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007971
7972 return 0;
7973}
7974
Jens Axboe2b188cc2019-01-07 10:46:33 -07007975static void io_mem_free(void *ptr)
7976{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007977 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007978
Mark Rutland52e04ef2019-04-30 17:30:21 +01007979 if (!ptr)
7980 return;
7981
7982 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007983 if (put_page_testzero(page))
7984 free_compound_page(page);
7985}
7986
7987static void *io_mem_alloc(size_t size)
7988{
7989 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007990 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007991
7992 return (void *) __get_free_pages(gfp_flags, get_order(size));
7993}
7994
Hristo Venev75b28af2019-08-26 17:23:46 +00007995static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7996 size_t *sq_offset)
7997{
7998 struct io_rings *rings;
7999 size_t off, sq_array_size;
8000
8001 off = struct_size(rings, cqes, cq_entries);
8002 if (off == SIZE_MAX)
8003 return SIZE_MAX;
8004
8005#ifdef CONFIG_SMP
8006 off = ALIGN(off, SMP_CACHE_BYTES);
8007 if (off == 0)
8008 return SIZE_MAX;
8009#endif
8010
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008011 if (sq_offset)
8012 *sq_offset = off;
8013
Hristo Venev75b28af2019-08-26 17:23:46 +00008014 sq_array_size = array_size(sizeof(u32), sq_entries);
8015 if (sq_array_size == SIZE_MAX)
8016 return SIZE_MAX;
8017
8018 if (check_add_overflow(off, sq_array_size, &off))
8019 return SIZE_MAX;
8020
Hristo Venev75b28af2019-08-26 17:23:46 +00008021 return off;
8022}
8023
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008024static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008025{
8026 int i, j;
8027
8028 if (!ctx->user_bufs)
8029 return -ENXIO;
8030
8031 for (i = 0; i < ctx->nr_user_bufs; i++) {
8032 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8033
8034 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008035 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008036
Jens Axboede293932020-09-17 16:19:16 -06008037 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008038 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008039 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008040 imu->nr_bvecs = 0;
8041 }
8042
8043 kfree(ctx->user_bufs);
8044 ctx->user_bufs = NULL;
8045 ctx->nr_user_bufs = 0;
8046 return 0;
8047}
8048
8049static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8050 void __user *arg, unsigned index)
8051{
8052 struct iovec __user *src;
8053
8054#ifdef CONFIG_COMPAT
8055 if (ctx->compat) {
8056 struct compat_iovec __user *ciovs;
8057 struct compat_iovec ciov;
8058
8059 ciovs = (struct compat_iovec __user *) arg;
8060 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8061 return -EFAULT;
8062
Jens Axboed55e5f52019-12-11 16:12:15 -07008063 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008064 dst->iov_len = ciov.iov_len;
8065 return 0;
8066 }
8067#endif
8068 src = (struct iovec __user *) arg;
8069 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8070 return -EFAULT;
8071 return 0;
8072}
8073
Jens Axboede293932020-09-17 16:19:16 -06008074/*
8075 * Not super efficient, but this is just a registration time. And we do cache
8076 * the last compound head, so generally we'll only do a full search if we don't
8077 * match that one.
8078 *
8079 * We check if the given compound head page has already been accounted, to
8080 * avoid double accounting it. This allows us to account the full size of the
8081 * page, not just the constituent pages of a huge page.
8082 */
8083static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8084 int nr_pages, struct page *hpage)
8085{
8086 int i, j;
8087
8088 /* check current page array */
8089 for (i = 0; i < nr_pages; i++) {
8090 if (!PageCompound(pages[i]))
8091 continue;
8092 if (compound_head(pages[i]) == hpage)
8093 return true;
8094 }
8095
8096 /* check previously registered pages */
8097 for (i = 0; i < ctx->nr_user_bufs; i++) {
8098 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8099
8100 for (j = 0; j < imu->nr_bvecs; j++) {
8101 if (!PageCompound(imu->bvec[j].bv_page))
8102 continue;
8103 if (compound_head(imu->bvec[j].bv_page) == hpage)
8104 return true;
8105 }
8106 }
8107
8108 return false;
8109}
8110
8111static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8112 int nr_pages, struct io_mapped_ubuf *imu,
8113 struct page **last_hpage)
8114{
8115 int i, ret;
8116
8117 for (i = 0; i < nr_pages; i++) {
8118 if (!PageCompound(pages[i])) {
8119 imu->acct_pages++;
8120 } else {
8121 struct page *hpage;
8122
8123 hpage = compound_head(pages[i]);
8124 if (hpage == *last_hpage)
8125 continue;
8126 *last_hpage = hpage;
8127 if (headpage_already_acct(ctx, pages, i, hpage))
8128 continue;
8129 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8130 }
8131 }
8132
8133 if (!imu->acct_pages)
8134 return 0;
8135
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008136 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008137 if (ret)
8138 imu->acct_pages = 0;
8139 return ret;
8140}
8141
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008142static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8143 struct io_mapped_ubuf *imu,
8144 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008145{
8146 struct vm_area_struct **vmas = NULL;
8147 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008148 unsigned long off, start, end, ubuf;
8149 size_t size;
8150 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008151
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008152 ubuf = (unsigned long) iov->iov_base;
8153 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8154 start = ubuf >> PAGE_SHIFT;
8155 nr_pages = end - start;
8156
8157 ret = -ENOMEM;
8158
8159 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8160 if (!pages)
8161 goto done;
8162
8163 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8164 GFP_KERNEL);
8165 if (!vmas)
8166 goto done;
8167
8168 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8169 GFP_KERNEL);
8170 if (!imu->bvec)
8171 goto done;
8172
8173 ret = 0;
8174 mmap_read_lock(current->mm);
8175 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8176 pages, vmas);
8177 if (pret == nr_pages) {
8178 /* don't support file backed memory */
8179 for (i = 0; i < nr_pages; i++) {
8180 struct vm_area_struct *vma = vmas[i];
8181
8182 if (vma->vm_file &&
8183 !is_file_hugepages(vma->vm_file)) {
8184 ret = -EOPNOTSUPP;
8185 break;
8186 }
8187 }
8188 } else {
8189 ret = pret < 0 ? pret : -EFAULT;
8190 }
8191 mmap_read_unlock(current->mm);
8192 if (ret) {
8193 /*
8194 * if we did partial map, or found file backed vmas,
8195 * release any pages we did get
8196 */
8197 if (pret > 0)
8198 unpin_user_pages(pages, pret);
8199 kvfree(imu->bvec);
8200 goto done;
8201 }
8202
8203 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8204 if (ret) {
8205 unpin_user_pages(pages, pret);
8206 kvfree(imu->bvec);
8207 goto done;
8208 }
8209
8210 off = ubuf & ~PAGE_MASK;
8211 size = iov->iov_len;
8212 for (i = 0; i < nr_pages; i++) {
8213 size_t vec_len;
8214
8215 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8216 imu->bvec[i].bv_page = pages[i];
8217 imu->bvec[i].bv_len = vec_len;
8218 imu->bvec[i].bv_offset = off;
8219 off = 0;
8220 size -= vec_len;
8221 }
8222 /* store original address for later verification */
8223 imu->ubuf = ubuf;
8224 imu->len = iov->iov_len;
8225 imu->nr_bvecs = nr_pages;
8226 ret = 0;
8227done:
8228 kvfree(pages);
8229 kvfree(vmas);
8230 return ret;
8231}
8232
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008233static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008234{
Jens Axboeedafcce2019-01-09 09:16:05 -07008235 if (ctx->user_bufs)
8236 return -EBUSY;
8237 if (!nr_args || nr_args > UIO_MAXIOV)
8238 return -EINVAL;
8239
8240 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8241 GFP_KERNEL);
8242 if (!ctx->user_bufs)
8243 return -ENOMEM;
8244
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008245 return 0;
8246}
8247
8248static int io_buffer_validate(struct iovec *iov)
8249{
8250 /*
8251 * Don't impose further limits on the size and buffer
8252 * constraints here, we'll -EINVAL later when IO is
8253 * submitted if they are wrong.
8254 */
8255 if (!iov->iov_base || !iov->iov_len)
8256 return -EFAULT;
8257
8258 /* arbitrary limit, but we need something */
8259 if (iov->iov_len > SZ_1G)
8260 return -EFAULT;
8261
8262 return 0;
8263}
8264
8265static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8266 unsigned int nr_args)
8267{
8268 int i, ret;
8269 struct iovec iov;
8270 struct page *last_hpage = NULL;
8271
8272 ret = io_buffers_map_alloc(ctx, nr_args);
8273 if (ret)
8274 return ret;
8275
Jens Axboeedafcce2019-01-09 09:16:05 -07008276 for (i = 0; i < nr_args; i++) {
8277 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008278
8279 ret = io_copy_iov(ctx, &iov, arg, i);
8280 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008281 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008282
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008283 ret = io_buffer_validate(&iov);
8284 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008285 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008286
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008287 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8288 if (ret)
8289 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008290
8291 ctx->nr_user_bufs++;
8292 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008293
8294 if (ret)
8295 io_sqe_buffers_unregister(ctx);
8296
Jens Axboeedafcce2019-01-09 09:16:05 -07008297 return ret;
8298}
8299
Jens Axboe9b402842019-04-11 11:45:41 -06008300static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8301{
8302 __s32 __user *fds = arg;
8303 int fd;
8304
8305 if (ctx->cq_ev_fd)
8306 return -EBUSY;
8307
8308 if (copy_from_user(&fd, fds, sizeof(*fds)))
8309 return -EFAULT;
8310
8311 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8312 if (IS_ERR(ctx->cq_ev_fd)) {
8313 int ret = PTR_ERR(ctx->cq_ev_fd);
8314 ctx->cq_ev_fd = NULL;
8315 return ret;
8316 }
8317
8318 return 0;
8319}
8320
8321static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8322{
8323 if (ctx->cq_ev_fd) {
8324 eventfd_ctx_put(ctx->cq_ev_fd);
8325 ctx->cq_ev_fd = NULL;
8326 return 0;
8327 }
8328
8329 return -ENXIO;
8330}
8331
Jens Axboe5a2e7452020-02-23 16:23:11 -07008332static int __io_destroy_buffers(int id, void *p, void *data)
8333{
8334 struct io_ring_ctx *ctx = data;
8335 struct io_buffer *buf = p;
8336
Jens Axboe067524e2020-03-02 16:32:28 -07008337 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008338 return 0;
8339}
8340
8341static void io_destroy_buffers(struct io_ring_ctx *ctx)
8342{
8343 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8344 idr_destroy(&ctx->io_buffer_idr);
8345}
8346
Jens Axboe68e68ee2021-02-13 09:00:02 -07008347static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008348{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008349 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008350
Jens Axboe68e68ee2021-02-13 09:00:02 -07008351 list_for_each_entry_safe(req, nxt, list, compl.list) {
8352 if (tsk && req->task != tsk)
8353 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008354 list_del(&req->compl.list);
8355 kmem_cache_free(req_cachep, req);
8356 }
8357}
8358
Jens Axboe4010fec2021-02-27 15:04:18 -07008359static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008360{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008361 struct io_submit_state *submit_state = &ctx->submit_state;
Pavel Begunkove5547d22021-02-23 22:17:20 +00008362 struct io_comp_state *cs = &ctx->submit_state.comp;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008363
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008364 mutex_lock(&ctx->uring_lock);
8365
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008366 if (submit_state->free_reqs) {
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008367 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8368 submit_state->reqs);
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008369 submit_state->free_reqs = 0;
8370 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008371
8372 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkove5547d22021-02-23 22:17:20 +00008373 list_splice_init(&cs->locked_free_list, &cs->free_list);
8374 cs->locked_free_nr = 0;
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008375 spin_unlock_irq(&ctx->completion_lock);
8376
Pavel Begunkove5547d22021-02-23 22:17:20 +00008377 io_req_cache_free(&cs->free_list, NULL);
8378
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008379 mutex_unlock(&ctx->uring_lock);
8380}
8381
Jens Axboe2b188cc2019-01-07 10:46:33 -07008382static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8383{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008384 /*
8385 * Some may use context even when all refs and requests have been put,
8386 * and they are free to do so while still holding uring_lock, see
8387 * __io_req_task_submit(). Wait for them to finish.
8388 */
8389 mutex_lock(&ctx->uring_lock);
8390 mutex_unlock(&ctx->uring_lock);
8391
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008392 io_sq_thread_finish(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008393 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008394
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008395 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008396 mmdrop(ctx->mm_account);
8397 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008398 }
Jens Axboedef596e2019-01-09 08:59:42 -07008399
Hao Xu8bad28d2021-02-19 17:19:36 +08008400 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008401 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008402 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008403 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008404 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008405 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008406
Jens Axboe2b188cc2019-01-07 10:46:33 -07008407#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008408 if (ctx->ring_sock) {
8409 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008410 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008411 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008412#endif
8413
Hristo Venev75b28af2019-08-26 17:23:46 +00008414 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008415 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008416
8417 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008418 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008419 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008420 if (ctx->hash_map)
8421 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008422 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008423 kfree(ctx);
8424}
8425
8426static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8427{
8428 struct io_ring_ctx *ctx = file->private_data;
8429 __poll_t mask = 0;
8430
8431 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008432 /*
8433 * synchronizes with barrier from wq_has_sleeper call in
8434 * io_commit_cqring
8435 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008436 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008437 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008438 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008439
8440 /*
8441 * Don't flush cqring overflow list here, just do a simple check.
8442 * Otherwise there could possible be ABBA deadlock:
8443 * CPU0 CPU1
8444 * ---- ----
8445 * lock(&ctx->uring_lock);
8446 * lock(&ep->mtx);
8447 * lock(&ctx->uring_lock);
8448 * lock(&ep->mtx);
8449 *
8450 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8451 * pushs them to do the flush.
8452 */
8453 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008454 mask |= EPOLLIN | EPOLLRDNORM;
8455
8456 return mask;
8457}
8458
8459static int io_uring_fasync(int fd, struct file *file, int on)
8460{
8461 struct io_ring_ctx *ctx = file->private_data;
8462
8463 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8464}
8465
Yejune Deng0bead8c2020-12-24 11:02:20 +08008466static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008467{
Jens Axboe4379bf82021-02-15 13:40:22 -07008468 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008469
Jens Axboe4379bf82021-02-15 13:40:22 -07008470 creds = idr_remove(&ctx->personality_idr, id);
8471 if (creds) {
8472 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008473 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008474 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008475
8476 return -EINVAL;
8477}
8478
8479static int io_remove_personalities(int id, void *p, void *data)
8480{
8481 struct io_ring_ctx *ctx = data;
8482
8483 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008484 return 0;
8485}
8486
Pavel Begunkovba50a032021-02-26 15:47:56 +00008487static bool io_run_ctx_fallback(struct io_ring_ctx *ctx)
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008488{
Pavel Begunkov28c47212021-02-28 22:04:54 +00008489 struct callback_head *work, *next;
Pavel Begunkovba50a032021-02-26 15:47:56 +00008490 bool executed = false;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008491
8492 do {
Pavel Begunkov28c47212021-02-28 22:04:54 +00008493 work = xchg(&ctx->exit_task_work, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008494 if (!work)
8495 break;
8496
8497 do {
8498 next = work->next;
8499 work->func(work);
8500 work = next;
8501 cond_resched();
8502 } while (work);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008503 executed = true;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008504 } while (1);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008505
8506 return executed;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008507}
8508
Jens Axboe85faa7b2020-04-09 18:14:00 -06008509static void io_ring_exit_work(struct work_struct *work)
8510{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008511 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8512 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008513
Jens Axboe56952e92020-06-17 15:00:04 -06008514 /*
8515 * If we're doing polled IO and end up having requests being
8516 * submitted async (out-of-line), then completions can come in while
8517 * we're waiting for refs to drop. We need to reap these manually,
8518 * as nobody else will be looking for them.
8519 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008520 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008521 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008522 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008523 io_ring_ctx_free(ctx);
8524}
8525
Jens Axboe2b188cc2019-01-07 10:46:33 -07008526static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8527{
8528 mutex_lock(&ctx->uring_lock);
8529 percpu_ref_kill(&ctx->refs);
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008530 /* if force is set, the ring is going away. always drop after that */
8531 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008532 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008533 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008534 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008535 mutex_unlock(&ctx->uring_lock);
8536
Pavel Begunkov6b819282020-11-06 13:00:25 +00008537 io_kill_timeouts(ctx, NULL, NULL);
8538 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008539
Jens Axboe15dff282019-11-13 09:09:23 -07008540 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008541 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008542
Jens Axboe85faa7b2020-04-09 18:14:00 -06008543 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008544 /*
8545 * Use system_unbound_wq to avoid spawning tons of event kworkers
8546 * if we're exiting a ton of rings at the same time. It just adds
8547 * noise and overhead, there's no discernable change in runtime
8548 * over using system_wq.
8549 */
8550 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008551}
8552
8553static int io_uring_release(struct inode *inode, struct file *file)
8554{
8555 struct io_ring_ctx *ctx = file->private_data;
8556
8557 file->private_data = NULL;
8558 io_ring_ctx_wait_and_kill(ctx);
8559 return 0;
8560}
8561
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008562struct io_task_cancel {
8563 struct task_struct *task;
8564 struct files_struct *files;
8565};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008566
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008567static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008568{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008569 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008570 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008571 bool ret;
8572
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008573 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008574 unsigned long flags;
8575 struct io_ring_ctx *ctx = req->ctx;
8576
8577 /* protect against races with linked timeouts */
8578 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008579 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008580 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8581 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008582 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008583 }
8584 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008585}
8586
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008587static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008588 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008589 struct files_struct *files)
8590{
8591 struct io_defer_entry *de = NULL;
8592 LIST_HEAD(list);
8593
8594 spin_lock_irq(&ctx->completion_lock);
8595 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008596 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008597 list_cut_position(&list, &ctx->defer_list, &de->list);
8598 break;
8599 }
8600 }
8601 spin_unlock_irq(&ctx->completion_lock);
8602
8603 while (!list_empty(&list)) {
8604 de = list_first_entry(&list, struct io_defer_entry, list);
8605 list_del_init(&de->list);
8606 req_set_fail_links(de->req);
8607 io_put_req(de->req);
8608 io_req_complete(de->req, -ECANCELED);
8609 kfree(de);
8610 }
8611}
8612
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008613static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8614 struct task_struct *task,
8615 struct files_struct *files)
8616{
8617 struct io_task_cancel cancel = { .task = task, .files = files, };
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008618 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008619
8620 while (1) {
8621 enum io_wq_cancel cret;
8622 bool ret = false;
8623
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008624 if (tctx && tctx->io_wq) {
8625 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008626 &cancel, true);
8627 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8628 }
8629
8630 /* SQPOLL thread does its own polling */
8631 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8632 while (!list_empty_careful(&ctx->iopoll_list)) {
8633 io_iopoll_try_reap_events(ctx);
8634 ret = true;
8635 }
8636 }
8637
8638 ret |= io_poll_remove_all(ctx, task, files);
8639 ret |= io_kill_timeouts(ctx, task, files);
8640 ret |= io_run_task_work();
Pavel Begunkovba50a032021-02-26 15:47:56 +00008641 ret |= io_run_ctx_fallback(ctx);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008642 io_cqring_overflow_flush(ctx, true, task, files);
8643 if (!ret)
8644 break;
8645 cond_resched();
8646 }
8647}
8648
Pavel Begunkovca70f002021-01-26 15:28:27 +00008649static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8650 struct task_struct *task,
8651 struct files_struct *files)
8652{
8653 struct io_kiocb *req;
8654 int cnt = 0;
8655
8656 spin_lock_irq(&ctx->inflight_lock);
8657 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8658 cnt += io_match_task(req, task, files);
8659 spin_unlock_irq(&ctx->inflight_lock);
8660 return cnt;
8661}
8662
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008663static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008664 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008665 struct files_struct *files)
8666{
Jens Axboefcb323c2019-10-24 12:39:47 -06008667 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008668 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008669 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008670
Pavel Begunkovca70f002021-01-26 15:28:27 +00008671 inflight = io_uring_count_inflight(ctx, task, files);
8672 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008673 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008674
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008675 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008676
Pavel Begunkov34343782021-02-10 11:45:42 +00008677 if (ctx->sq_data)
8678 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008679 prepare_to_wait(&task->io_uring->wait, &wait,
8680 TASK_UNINTERRUPTIBLE);
8681 if (inflight == io_uring_count_inflight(ctx, task, files))
8682 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008683 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00008684 if (ctx->sq_data)
8685 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06008686 }
Jens Axboe0f212202020-09-13 13:09:39 -06008687}
8688
8689/*
8690 * We need to iteratively cancel requests, in case a request has dependent
8691 * hard links. These persist even for failure of cancelations, hence keep
8692 * looping until none are found.
8693 */
8694static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8695 struct files_struct *files)
8696{
8697 struct task_struct *task = current;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008698 bool did_park = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008699
Jens Axboefdaf0832020-10-30 09:37:30 -06008700 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00008701 /* never started, nothing to cancel */
8702 if (ctx->flags & IORING_SETUP_R_DISABLED) {
8703 io_sq_offload_start(ctx);
8704 return;
8705 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008706 did_park = io_sq_thread_park(ctx->sq_data);
8707 if (did_park) {
8708 task = ctx->sq_data->thread;
8709 atomic_inc(&task->io_uring->in_idle);
8710 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008711 }
Jens Axboe0f212202020-09-13 13:09:39 -06008712
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008713 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008714
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008715 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008716 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008717 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008718
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008719 if (did_park) {
Jens Axboefdaf0832020-10-30 09:37:30 -06008720 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008721 io_sq_thread_unpark(ctx->sq_data);
8722 }
Jens Axboe0f212202020-09-13 13:09:39 -06008723}
8724
8725/*
8726 * Note that this task has used io_uring. We use it for cancelation purposes.
8727 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008728static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008729{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008730 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008731 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008732
8733 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008734 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008735 if (unlikely(ret))
8736 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008737 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008738 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008739 if (tctx->last != file) {
8740 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008741
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008742 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008743 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008744 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8745 file, GFP_KERNEL));
8746 if (ret) {
8747 fput(file);
8748 return ret;
8749 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008750
8751 /* one and only SQPOLL file note, held by sqo_task */
8752 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8753 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008754 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008755 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008756 }
8757
Jens Axboefdaf0832020-10-30 09:37:30 -06008758 /*
8759 * This is race safe in that the task itself is doing this, hence it
8760 * cannot be going through the exit/cancel paths at the same time.
8761 * This cannot be modified while exit/cancel is running.
8762 */
8763 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8764 tctx->sqpoll = true;
8765
Jens Axboe0f212202020-09-13 13:09:39 -06008766 return 0;
8767}
8768
8769/*
8770 * Remove this io_uring_file -> task mapping.
8771 */
8772static void io_uring_del_task_file(struct file *file)
8773{
8774 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008775
8776 if (tctx->last == file)
8777 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008778 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008779 if (file)
8780 fput(file);
8781}
8782
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008783static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008784{
8785 struct file *file;
8786 unsigned long index;
8787
8788 xa_for_each(&tctx->xa, index, file)
8789 io_uring_del_task_file(file);
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008790 if (tctx->io_wq) {
8791 io_wq_put_and_exit(tctx->io_wq);
8792 tctx->io_wq = NULL;
8793 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008794}
8795
Jens Axboe0f212202020-09-13 13:09:39 -06008796void __io_uring_files_cancel(struct files_struct *files)
8797{
8798 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008799 struct file *file;
8800 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008801
8802 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008803 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008804 xa_for_each(&tctx->xa, index, file)
8805 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008806 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008807
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008808 if (files)
8809 io_uring_clean_tctx(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008810}
8811
8812static s64 tctx_inflight(struct io_uring_task *tctx)
8813{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008814 return percpu_counter_sum(&tctx->inflight);
8815}
8816
8817static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
8818{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008819 struct io_sq_data *sqd = ctx->sq_data;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008820 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06008821 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008822 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008823
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008824 if (!sqd)
8825 return;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008826 if (!io_sq_thread_park(sqd))
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008827 return;
8828 tctx = ctx->sq_data->thread->io_uring;
Jens Axboee54945a2021-02-26 11:27:15 -07008829 /* can happen on fork/alloc failure, just ignore that state */
8830 if (!tctx) {
8831 io_sq_thread_unpark(sqd);
8832 return;
8833 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008834
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008835 atomic_inc(&tctx->in_idle);
8836 do {
8837 /* read completions before cancelations */
8838 inflight = tctx_inflight(tctx);
8839 if (!inflight)
8840 break;
8841 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008842
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008843 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8844 /*
8845 * If we've seen completions, retry without waiting. This
8846 * avoids a race where a completion comes in before we did
8847 * prepare_to_wait().
8848 */
8849 if (inflight == tctx_inflight(tctx))
8850 schedule();
8851 finish_wait(&tctx->wait, &wait);
8852 } while (1);
8853 atomic_dec(&tctx->in_idle);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008854 io_sq_thread_unpark(sqd);
Jens Axboe0f212202020-09-13 13:09:39 -06008855}
8856
Jens Axboe0f212202020-09-13 13:09:39 -06008857/*
8858 * Find any io_uring fd that this task has registered or done IO on, and cancel
8859 * requests.
8860 */
8861void __io_uring_task_cancel(void)
8862{
8863 struct io_uring_task *tctx = current->io_uring;
8864 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008865 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008866
8867 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008868 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008869
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008870 if (tctx->sqpoll) {
8871 struct file *file;
8872 unsigned long index;
8873
8874 xa_for_each(&tctx->xa, index, file)
8875 io_uring_cancel_sqpoll(file->private_data);
8876 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008877
Jens Axboed8a6df12020-10-15 16:24:45 -06008878 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008879 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008880 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008881 if (!inflight)
8882 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008883 __io_uring_files_cancel(NULL);
8884
8885 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8886
8887 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008888 * If we've seen completions, retry without waiting. This
8889 * avoids a race where a completion comes in before we did
8890 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008891 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008892 if (inflight == tctx_inflight(tctx))
8893 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00008894 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008895 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008896
Jens Axboefdaf0832020-10-30 09:37:30 -06008897 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008898
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008899 io_uring_clean_tctx(tctx);
8900 /* all current's requests should be gone, we can kill tctx */
8901 __io_uring_free(current);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008902}
8903
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008904static void *io_uring_validate_mmap_request(struct file *file,
8905 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008906{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008907 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008908 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008909 struct page *page;
8910 void *ptr;
8911
8912 switch (offset) {
8913 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008914 case IORING_OFF_CQ_RING:
8915 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008916 break;
8917 case IORING_OFF_SQES:
8918 ptr = ctx->sq_sqes;
8919 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008920 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008921 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008922 }
8923
8924 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008925 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008926 return ERR_PTR(-EINVAL);
8927
8928 return ptr;
8929}
8930
8931#ifdef CONFIG_MMU
8932
8933static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8934{
8935 size_t sz = vma->vm_end - vma->vm_start;
8936 unsigned long pfn;
8937 void *ptr;
8938
8939 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8940 if (IS_ERR(ptr))
8941 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008942
8943 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8944 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8945}
8946
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008947#else /* !CONFIG_MMU */
8948
8949static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8950{
8951 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8952}
8953
8954static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8955{
8956 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8957}
8958
8959static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8960 unsigned long addr, unsigned long len,
8961 unsigned long pgoff, unsigned long flags)
8962{
8963 void *ptr;
8964
8965 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8966 if (IS_ERR(ptr))
8967 return PTR_ERR(ptr);
8968
8969 return (unsigned long) ptr;
8970}
8971
8972#endif /* !CONFIG_MMU */
8973
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008974static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06008975{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008976 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06008977 DEFINE_WAIT(wait);
8978
8979 do {
8980 if (!io_sqring_full(ctx))
8981 break;
Jens Axboe90554202020-09-03 12:12:41 -06008982 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8983
8984 if (!io_sqring_full(ctx))
8985 break;
Jens Axboe90554202020-09-03 12:12:41 -06008986 schedule();
8987 } while (!signal_pending(current));
8988
8989 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008990 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06008991}
8992
Hao Xuc73ebb62020-11-03 10:54:37 +08008993static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
8994 struct __kernel_timespec __user **ts,
8995 const sigset_t __user **sig)
8996{
8997 struct io_uring_getevents_arg arg;
8998
8999 /*
9000 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9001 * is just a pointer to the sigset_t.
9002 */
9003 if (!(flags & IORING_ENTER_EXT_ARG)) {
9004 *sig = (const sigset_t __user *) argp;
9005 *ts = NULL;
9006 return 0;
9007 }
9008
9009 /*
9010 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9011 * timespec and sigset_t pointers if good.
9012 */
9013 if (*argsz != sizeof(arg))
9014 return -EINVAL;
9015 if (copy_from_user(&arg, argp, sizeof(arg)))
9016 return -EFAULT;
9017 *sig = u64_to_user_ptr(arg.sigmask);
9018 *argsz = arg.sigmask_sz;
9019 *ts = u64_to_user_ptr(arg.ts);
9020 return 0;
9021}
9022
Jens Axboe2b188cc2019-01-07 10:46:33 -07009023SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009024 u32, min_complete, u32, flags, const void __user *, argp,
9025 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009026{
9027 struct io_ring_ctx *ctx;
9028 long ret = -EBADF;
9029 int submitted = 0;
9030 struct fd f;
9031
Jens Axboe4c6e2772020-07-01 11:29:10 -06009032 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009033
Jens Axboe90554202020-09-03 12:12:41 -06009034 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009035 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009036 return -EINVAL;
9037
9038 f = fdget(fd);
9039 if (!f.file)
9040 return -EBADF;
9041
9042 ret = -EOPNOTSUPP;
9043 if (f.file->f_op != &io_uring_fops)
9044 goto out_fput;
9045
9046 ret = -ENXIO;
9047 ctx = f.file->private_data;
9048 if (!percpu_ref_tryget(&ctx->refs))
9049 goto out_fput;
9050
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009051 ret = -EBADFD;
9052 if (ctx->flags & IORING_SETUP_R_DISABLED)
9053 goto out;
9054
Jens Axboe6c271ce2019-01-10 11:22:30 -07009055 /*
9056 * For SQ polling, the thread will do all submissions and completions.
9057 * Just return the requested submit count, and wake the thread if
9058 * we were asked to.
9059 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009060 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009061 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009062 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009063
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009064 if (unlikely(ctx->sqo_exec)) {
9065 ret = io_sq_thread_fork(ctx->sq_data, ctx);
9066 if (ret)
9067 goto out;
9068 ctx->sqo_exec = 0;
9069 }
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009070 ret = -EOWNERDEAD;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009071 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009072 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009073 if (flags & IORING_ENTER_SQ_WAIT) {
9074 ret = io_sqpoll_wait_sq(ctx);
9075 if (ret)
9076 goto out;
9077 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009078 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009079 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009080 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009081 if (unlikely(ret))
9082 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009083 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009084 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009085 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009086
9087 if (submitted != to_submit)
9088 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009089 }
9090 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009091 const sigset_t __user *sig;
9092 struct __kernel_timespec __user *ts;
9093
9094 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9095 if (unlikely(ret))
9096 goto out;
9097
Jens Axboe2b188cc2019-01-07 10:46:33 -07009098 min_complete = min(min_complete, ctx->cq_entries);
9099
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009100 /*
9101 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9102 * space applications don't need to do io completion events
9103 * polling again, they can rely on io_sq_thread to do polling
9104 * work, which can reduce cpu usage and uring_lock contention.
9105 */
9106 if (ctx->flags & IORING_SETUP_IOPOLL &&
9107 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009108 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009109 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009110 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009111 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009112 }
9113
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009114out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009115 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009116out_fput:
9117 fdput(f);
9118 return submitted ? submitted : ret;
9119}
9120
Tobias Klauserbebdb652020-02-26 18:38:32 +01009121#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009122static int io_uring_show_cred(int id, void *p, void *data)
9123{
Jens Axboe4379bf82021-02-15 13:40:22 -07009124 const struct cred *cred = p;
Jens Axboe87ce9552020-01-30 08:25:34 -07009125 struct seq_file *m = data;
9126 struct user_namespace *uns = seq_user_ns(m);
9127 struct group_info *gi;
9128 kernel_cap_t cap;
9129 unsigned __capi;
9130 int g;
9131
9132 seq_printf(m, "%5d\n", id);
9133 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9134 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9135 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9136 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9137 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9138 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9139 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9140 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9141 seq_puts(m, "\n\tGroups:\t");
9142 gi = cred->group_info;
9143 for (g = 0; g < gi->ngroups; g++) {
9144 seq_put_decimal_ull(m, g ? " " : "",
9145 from_kgid_munged(uns, gi->gid[g]));
9146 }
9147 seq_puts(m, "\n\tCapEff:\t");
9148 cap = cred->cap_effective;
9149 CAP_FOR_EACH_U32(__capi)
9150 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9151 seq_putc(m, '\n');
9152 return 0;
9153}
9154
9155static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9156{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009157 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009158 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009159 int i;
9160
Jens Axboefad8e0d2020-09-28 08:57:48 -06009161 /*
9162 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9163 * since fdinfo case grabs it in the opposite direction of normal use
9164 * cases. If we fail to get the lock, we just don't iterate any
9165 * structures that could be going away outside the io_uring mutex.
9166 */
9167 has_lock = mutex_trylock(&ctx->uring_lock);
9168
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009169 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009170 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009171 if (!sq->thread)
9172 sq = NULL;
9173 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009174
9175 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9176 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009177 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009178 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009179 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009180
Jens Axboe87ce9552020-01-30 08:25:34 -07009181 if (f)
9182 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9183 else
9184 seq_printf(m, "%5u: <none>\n", i);
9185 }
9186 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009187 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009188 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9189
9190 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9191 (unsigned int) buf->len);
9192 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009193 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009194 seq_printf(m, "Personalities:\n");
9195 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9196 }
Jens Axboed7718a92020-02-14 22:23:12 -07009197 seq_printf(m, "PollList:\n");
9198 spin_lock_irq(&ctx->completion_lock);
9199 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9200 struct hlist_head *list = &ctx->cancel_hash[i];
9201 struct io_kiocb *req;
9202
9203 hlist_for_each_entry(req, list, hash_node)
9204 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9205 req->task->task_works != NULL);
9206 }
9207 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009208 if (has_lock)
9209 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009210}
9211
9212static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9213{
9214 struct io_ring_ctx *ctx = f->private_data;
9215
9216 if (percpu_ref_tryget(&ctx->refs)) {
9217 __io_uring_show_fdinfo(ctx, m);
9218 percpu_ref_put(&ctx->refs);
9219 }
9220}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009221#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009222
Jens Axboe2b188cc2019-01-07 10:46:33 -07009223static const struct file_operations io_uring_fops = {
9224 .release = io_uring_release,
9225 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009226#ifndef CONFIG_MMU
9227 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9228 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9229#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009230 .poll = io_uring_poll,
9231 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009232#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009233 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009234#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009235};
9236
9237static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9238 struct io_uring_params *p)
9239{
Hristo Venev75b28af2019-08-26 17:23:46 +00009240 struct io_rings *rings;
9241 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009242
Jens Axboebd740482020-08-05 12:58:23 -06009243 /* make sure these are sane, as we already accounted them */
9244 ctx->sq_entries = p->sq_entries;
9245 ctx->cq_entries = p->cq_entries;
9246
Hristo Venev75b28af2019-08-26 17:23:46 +00009247 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9248 if (size == SIZE_MAX)
9249 return -EOVERFLOW;
9250
9251 rings = io_mem_alloc(size);
9252 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009253 return -ENOMEM;
9254
Hristo Venev75b28af2019-08-26 17:23:46 +00009255 ctx->rings = rings;
9256 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9257 rings->sq_ring_mask = p->sq_entries - 1;
9258 rings->cq_ring_mask = p->cq_entries - 1;
9259 rings->sq_ring_entries = p->sq_entries;
9260 rings->cq_ring_entries = p->cq_entries;
9261 ctx->sq_mask = rings->sq_ring_mask;
9262 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009263
9264 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009265 if (size == SIZE_MAX) {
9266 io_mem_free(ctx->rings);
9267 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009268 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009269 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009270
9271 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009272 if (!ctx->sq_sqes) {
9273 io_mem_free(ctx->rings);
9274 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009275 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009276 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009277
Jens Axboe2b188cc2019-01-07 10:46:33 -07009278 return 0;
9279}
9280
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009281static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9282{
9283 int ret, fd;
9284
9285 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9286 if (fd < 0)
9287 return fd;
9288
9289 ret = io_uring_add_task_file(ctx, file);
9290 if (ret) {
9291 put_unused_fd(fd);
9292 return ret;
9293 }
9294 fd_install(fd, file);
9295 return fd;
9296}
9297
Jens Axboe2b188cc2019-01-07 10:46:33 -07009298/*
9299 * Allocate an anonymous fd, this is what constitutes the application
9300 * visible backing of an io_uring instance. The application mmaps this
9301 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9302 * we have to tie this fd to a socket for file garbage collection purposes.
9303 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009304static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009305{
9306 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009307#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009308 int ret;
9309
Jens Axboe2b188cc2019-01-07 10:46:33 -07009310 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9311 &ctx->ring_sock);
9312 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009313 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009314#endif
9315
Jens Axboe2b188cc2019-01-07 10:46:33 -07009316 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9317 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009318#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009319 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009320 sock_release(ctx->ring_sock);
9321 ctx->ring_sock = NULL;
9322 } else {
9323 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009324 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009325#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009326 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009327}
9328
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009329static int io_uring_create(unsigned entries, struct io_uring_params *p,
9330 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009332 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009333 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009334 int ret;
9335
Jens Axboe8110c1a2019-12-28 15:39:54 -07009336 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009337 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009338 if (entries > IORING_MAX_ENTRIES) {
9339 if (!(p->flags & IORING_SETUP_CLAMP))
9340 return -EINVAL;
9341 entries = IORING_MAX_ENTRIES;
9342 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009343
9344 /*
9345 * Use twice as many entries for the CQ ring. It's possible for the
9346 * application to drive a higher depth than the size of the SQ ring,
9347 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009348 * some flexibility in overcommitting a bit. If the application has
9349 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9350 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009351 */
9352 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009353 if (p->flags & IORING_SETUP_CQSIZE) {
9354 /*
9355 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9356 * to a power-of-two, if it isn't already. We do NOT impose
9357 * any cq vs sq ring sizing.
9358 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009359 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009360 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009361 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9362 if (!(p->flags & IORING_SETUP_CLAMP))
9363 return -EINVAL;
9364 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9365 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009366 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9367 if (p->cq_entries < p->sq_entries)
9368 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009369 } else {
9370 p->cq_entries = 2 * p->sq_entries;
9371 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009372
Jens Axboe2b188cc2019-01-07 10:46:33 -07009373 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009374 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009376 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009377 if (!capable(CAP_IPC_LOCK))
9378 ctx->user = get_uid(current_user());
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009379 ctx->sqo_task = current;
Jens Axboe2aede0e2020-09-14 10:45:53 -06009380
9381 /*
9382 * This is just grabbed for accounting purposes. When a process exits,
9383 * the mm is exited and dropped before the files, hence we need to hang
9384 * on to this mm purely for the purposes of being able to unaccount
9385 * memory (locked/pinned vm). It's not used for anything else.
9386 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009387 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009388 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009389
Jens Axboe2b188cc2019-01-07 10:46:33 -07009390 ret = io_allocate_scq_urings(ctx, p);
9391 if (ret)
9392 goto err;
9393
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009394 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009395 if (ret)
9396 goto err;
9397
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009398 if (!(p->flags & IORING_SETUP_R_DISABLED))
9399 io_sq_offload_start(ctx);
9400
Jens Axboe2b188cc2019-01-07 10:46:33 -07009401 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009402 p->sq_off.head = offsetof(struct io_rings, sq.head);
9403 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9404 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9405 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9406 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9407 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9408 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009409
9410 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009411 p->cq_off.head = offsetof(struct io_rings, cq.head);
9412 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9413 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9414 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9415 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9416 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009417 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009418
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009419 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9420 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009421 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009422 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Jens Axboe1c0aa1f2021-02-20 11:55:28 -07009423 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009424
9425 if (copy_to_user(params, p, sizeof(*p))) {
9426 ret = -EFAULT;
9427 goto err;
9428 }
Jens Axboed1719f72020-07-30 13:43:53 -06009429
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009430 file = io_uring_get_file(ctx);
9431 if (IS_ERR(file)) {
9432 ret = PTR_ERR(file);
9433 goto err;
9434 }
9435
Jens Axboed1719f72020-07-30 13:43:53 -06009436 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009437 * Install ring fd as the very last thing, so we don't risk someone
9438 * having closed it before we finish setup
9439 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009440 ret = io_uring_install_fd(ctx, file);
9441 if (ret < 0) {
9442 /* fput will clean it up */
9443 fput(file);
9444 return ret;
9445 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009446
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009447 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009448 return ret;
9449err:
9450 io_ring_ctx_wait_and_kill(ctx);
9451 return ret;
9452}
9453
9454/*
9455 * Sets up an aio uring context, and returns the fd. Applications asks for a
9456 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9457 * params structure passed in.
9458 */
9459static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9460{
9461 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009462 int i;
9463
9464 if (copy_from_user(&p, params, sizeof(p)))
9465 return -EFAULT;
9466 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9467 if (p.resv[i])
9468 return -EINVAL;
9469 }
9470
Jens Axboe6c271ce2019-01-10 11:22:30 -07009471 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009472 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009473 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9474 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009475 return -EINVAL;
9476
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009477 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009478}
9479
9480SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9481 struct io_uring_params __user *, params)
9482{
9483 return io_uring_setup(entries, params);
9484}
9485
Jens Axboe66f4af92020-01-16 15:36:52 -07009486static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9487{
9488 struct io_uring_probe *p;
9489 size_t size;
9490 int i, ret;
9491
9492 size = struct_size(p, ops, nr_args);
9493 if (size == SIZE_MAX)
9494 return -EOVERFLOW;
9495 p = kzalloc(size, GFP_KERNEL);
9496 if (!p)
9497 return -ENOMEM;
9498
9499 ret = -EFAULT;
9500 if (copy_from_user(p, arg, size))
9501 goto out;
9502 ret = -EINVAL;
9503 if (memchr_inv(p, 0, size))
9504 goto out;
9505
9506 p->last_op = IORING_OP_LAST - 1;
9507 if (nr_args > IORING_OP_LAST)
9508 nr_args = IORING_OP_LAST;
9509
9510 for (i = 0; i < nr_args; i++) {
9511 p->ops[i].op = i;
9512 if (!io_op_defs[i].not_supported)
9513 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9514 }
9515 p->ops_len = i;
9516
9517 ret = 0;
9518 if (copy_to_user(arg, p, size))
9519 ret = -EFAULT;
9520out:
9521 kfree(p);
9522 return ret;
9523}
9524
Jens Axboe071698e2020-01-28 10:04:42 -07009525static int io_register_personality(struct io_ring_ctx *ctx)
9526{
Jens Axboe4379bf82021-02-15 13:40:22 -07009527 const struct cred *creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009528 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009529
Jens Axboe4379bf82021-02-15 13:40:22 -07009530 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009531
Jens Axboe4379bf82021-02-15 13:40:22 -07009532 ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9533 USHRT_MAX, GFP_KERNEL);
9534 if (ret < 0)
9535 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009536 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009537}
9538
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009539static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9540 unsigned int nr_args)
9541{
9542 struct io_uring_restriction *res;
9543 size_t size;
9544 int i, ret;
9545
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009546 /* Restrictions allowed only if rings started disabled */
9547 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9548 return -EBADFD;
9549
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009550 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009551 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009552 return -EBUSY;
9553
9554 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9555 return -EINVAL;
9556
9557 size = array_size(nr_args, sizeof(*res));
9558 if (size == SIZE_MAX)
9559 return -EOVERFLOW;
9560
9561 res = memdup_user(arg, size);
9562 if (IS_ERR(res))
9563 return PTR_ERR(res);
9564
9565 ret = 0;
9566
9567 for (i = 0; i < nr_args; i++) {
9568 switch (res[i].opcode) {
9569 case IORING_RESTRICTION_REGISTER_OP:
9570 if (res[i].register_op >= IORING_REGISTER_LAST) {
9571 ret = -EINVAL;
9572 goto out;
9573 }
9574
9575 __set_bit(res[i].register_op,
9576 ctx->restrictions.register_op);
9577 break;
9578 case IORING_RESTRICTION_SQE_OP:
9579 if (res[i].sqe_op >= IORING_OP_LAST) {
9580 ret = -EINVAL;
9581 goto out;
9582 }
9583
9584 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9585 break;
9586 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9587 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9588 break;
9589 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9590 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9591 break;
9592 default:
9593 ret = -EINVAL;
9594 goto out;
9595 }
9596 }
9597
9598out:
9599 /* Reset all restrictions if an error happened */
9600 if (ret != 0)
9601 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9602 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009603 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009604
9605 kfree(res);
9606 return ret;
9607}
9608
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009609static int io_register_enable_rings(struct io_ring_ctx *ctx)
9610{
9611 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9612 return -EBADFD;
9613
9614 if (ctx->restrictions.registered)
9615 ctx->restricted = 1;
9616
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009617 io_sq_offload_start(ctx);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009618 return 0;
9619}
9620
Jens Axboe071698e2020-01-28 10:04:42 -07009621static bool io_register_op_must_quiesce(int op)
9622{
9623 switch (op) {
9624 case IORING_UNREGISTER_FILES:
9625 case IORING_REGISTER_FILES_UPDATE:
9626 case IORING_REGISTER_PROBE:
9627 case IORING_REGISTER_PERSONALITY:
9628 case IORING_UNREGISTER_PERSONALITY:
9629 return false;
9630 default:
9631 return true;
9632 }
9633}
9634
Jens Axboeedafcce2019-01-09 09:16:05 -07009635static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9636 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009637 __releases(ctx->uring_lock)
9638 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009639{
9640 int ret;
9641
Jens Axboe35fa71a2019-04-22 10:23:23 -06009642 /*
9643 * We're inside the ring mutex, if the ref is already dying, then
9644 * someone else killed the ctx or is already going through
9645 * io_uring_register().
9646 */
9647 if (percpu_ref_is_dying(&ctx->refs))
9648 return -ENXIO;
9649
Jens Axboe071698e2020-01-28 10:04:42 -07009650 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009651 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009652
Jens Axboe05f3fb32019-12-09 11:22:50 -07009653 /*
9654 * Drop uring mutex before waiting for references to exit. If
9655 * another thread is currently inside io_uring_enter() it might
9656 * need to grab the uring_lock to make progress. If we hold it
9657 * here across the drain wait, then we can deadlock. It's safe
9658 * to drop the mutex here, since no new references will come in
9659 * after we've killed the percpu ref.
9660 */
9661 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009662 do {
9663 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9664 if (!ret)
9665 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009666 ret = io_run_task_work_sig();
9667 if (ret < 0)
9668 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009669 } while (1);
9670
Jens Axboe05f3fb32019-12-09 11:22:50 -07009671 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009672
Jens Axboec1503682020-01-08 08:26:07 -07009673 if (ret) {
9674 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009675 goto out_quiesce;
9676 }
9677 }
9678
9679 if (ctx->restricted) {
9680 if (opcode >= IORING_REGISTER_LAST) {
9681 ret = -EINVAL;
9682 goto out;
9683 }
9684
9685 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9686 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009687 goto out;
9688 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009689 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009690
9691 switch (opcode) {
9692 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009693 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -07009694 break;
9695 case IORING_UNREGISTER_BUFFERS:
9696 ret = -EINVAL;
9697 if (arg || nr_args)
9698 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009699 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07009700 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009701 case IORING_REGISTER_FILES:
9702 ret = io_sqe_files_register(ctx, arg, nr_args);
9703 break;
9704 case IORING_UNREGISTER_FILES:
9705 ret = -EINVAL;
9706 if (arg || nr_args)
9707 break;
9708 ret = io_sqe_files_unregister(ctx);
9709 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009710 case IORING_REGISTER_FILES_UPDATE:
9711 ret = io_sqe_files_update(ctx, arg, nr_args);
9712 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009713 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009714 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009715 ret = -EINVAL;
9716 if (nr_args != 1)
9717 break;
9718 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009719 if (ret)
9720 break;
9721 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9722 ctx->eventfd_async = 1;
9723 else
9724 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009725 break;
9726 case IORING_UNREGISTER_EVENTFD:
9727 ret = -EINVAL;
9728 if (arg || nr_args)
9729 break;
9730 ret = io_eventfd_unregister(ctx);
9731 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009732 case IORING_REGISTER_PROBE:
9733 ret = -EINVAL;
9734 if (!arg || nr_args > 256)
9735 break;
9736 ret = io_probe(ctx, arg, nr_args);
9737 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009738 case IORING_REGISTER_PERSONALITY:
9739 ret = -EINVAL;
9740 if (arg || nr_args)
9741 break;
9742 ret = io_register_personality(ctx);
9743 break;
9744 case IORING_UNREGISTER_PERSONALITY:
9745 ret = -EINVAL;
9746 if (arg)
9747 break;
9748 ret = io_unregister_personality(ctx, nr_args);
9749 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009750 case IORING_REGISTER_ENABLE_RINGS:
9751 ret = -EINVAL;
9752 if (arg || nr_args)
9753 break;
9754 ret = io_register_enable_rings(ctx);
9755 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009756 case IORING_REGISTER_RESTRICTIONS:
9757 ret = io_register_restrictions(ctx, arg, nr_args);
9758 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009759 default:
9760 ret = -EINVAL;
9761 break;
9762 }
9763
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009764out:
Jens Axboe071698e2020-01-28 10:04:42 -07009765 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009766 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009767 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009768out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009769 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009770 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009771 return ret;
9772}
9773
9774SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9775 void __user *, arg, unsigned int, nr_args)
9776{
9777 struct io_ring_ctx *ctx;
9778 long ret = -EBADF;
9779 struct fd f;
9780
9781 f = fdget(fd);
9782 if (!f.file)
9783 return -EBADF;
9784
9785 ret = -EOPNOTSUPP;
9786 if (f.file->f_op != &io_uring_fops)
9787 goto out_fput;
9788
9789 ctx = f.file->private_data;
9790
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +00009791 io_run_task_work();
9792
Jens Axboeedafcce2019-01-09 09:16:05 -07009793 mutex_lock(&ctx->uring_lock);
9794 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9795 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009796 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9797 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009798out_fput:
9799 fdput(f);
9800 return ret;
9801}
9802
Jens Axboe2b188cc2019-01-07 10:46:33 -07009803static int __init io_uring_init(void)
9804{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009805#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9806 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9807 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9808} while (0)
9809
9810#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9811 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9812 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9813 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9814 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9815 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9816 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9817 BUILD_BUG_SQE_ELEM(8, __u64, off);
9818 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9819 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009820 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009821 BUILD_BUG_SQE_ELEM(24, __u32, len);
9822 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9823 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9824 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9825 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009826 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9827 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009828 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9829 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9830 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9831 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9832 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9833 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9834 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9835 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009836 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009837 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9838 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9839 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009840 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009841
Jens Axboed3656342019-12-18 09:50:26 -07009842 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009843 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -07009844 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
9845 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009846 return 0;
9847};
9848__initcall(io_uring_init);