blob: 9ed79509f3898fed9607afb9970b5a63dd298590 [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 Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
107struct io_uring {
108 u32 head ____cacheline_aligned_in_smp;
109 u32 tail ____cacheline_aligned_in_smp;
110};
111
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * This data is shared with the application through the mmap at offsets
114 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 *
116 * The offsets to the member fields are published through struct
117 * io_sqring_offsets when calling io_uring_setup.
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
121 * Head and tail offsets into the ring; the offsets need to be
122 * masked to get valid indices.
123 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * The kernel controls head of the sq ring and the tail of the cq ring,
125 * and the application controls tail of the sq ring and the head of the
126 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000130 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 * ring_entries - 1)
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_ring_mask, cq_ring_mask;
134 /* Ring sizes (constant, power of 2) */
135 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
137 * Number of invalid entries dropped by the kernel due to
138 * invalid index stored in array
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
142 * cached value).
143 *
144 * After a new SQ head value was read by the application this
145 * counter includes all submissions that were dropped reaching
146 * the new SQ head (and possibly more).
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200150 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 *
152 * Written by the kernel, shouldn't be modified by the
153 * application.
154 *
155 * The application needs a full memory barrier before checking
156 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200160 * Runtime CQ flags
161 *
162 * Written by the application, shouldn't be modified by the
163 * kernel.
164 */
165 u32 cq_flags;
166 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 * Number of completion events lost because the queue was full;
168 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800169 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * the completion queue.
171 *
172 * Written by the kernel, shouldn't be modified by the
173 * application (i.e. get number of "new events" by comparing to
174 * cached value).
175 *
176 * As completion events come in out of order this counter is not
177 * ordered with any other data.
178 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000179 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 /*
181 * Ring buffer of completion events.
182 *
183 * The kernel writes completion events fresh every time they are
184 * produced, so the application is allowed to modify pending
185 * entries.
186 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000187 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700188};
189
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000190enum io_uring_cmd_flags {
191 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000192 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000193};
194
Jens Axboeedafcce2019-01-09 09:16:05 -0700195struct io_mapped_ubuf {
196 u64 ubuf;
197 size_t len;
198 struct bio_vec *bvec;
199 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600200 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700201};
202
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000203struct io_ring_ctx;
204
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000205struct io_rsrc_put {
206 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000207 union {
208 void *rsrc;
209 struct file *file;
210 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000211};
212
213struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600214 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700215};
216
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000217struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct percpu_ref refs;
219 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000220 struct list_head rsrc_list;
221 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000222 void (*rsrc_put)(struct io_ring_ctx *ctx,
223 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600224 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000225 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800226};
227
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000228struct fixed_rsrc_data {
229 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700230 struct io_ring_ctx *ctx;
231
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700233 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700234 struct completion done;
235};
236
Jens Axboe5a2e7452020-02-23 16:23:11 -0700237struct io_buffer {
238 struct list_head list;
239 __u64 addr;
240 __s32 len;
241 __u16 bid;
242};
243
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200244struct io_restriction {
245 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
246 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
247 u8 sqe_flags_allowed;
248 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200249 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200250};
251
Jens Axboe534ca6d2020-09-02 13:52:19 -0600252struct io_sq_data {
253 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600254 struct mutex lock;
255
256 /* ctx's that are using this sqd */
257 struct list_head ctx_list;
258 struct list_head ctx_new_list;
259 struct mutex ctx_lock;
260
Jens Axboe534ca6d2020-09-02 13:52:19 -0600261 struct task_struct *thread;
262 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800263
264 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600265};
266
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000267#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000268#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000269#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000270#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000271
272struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000273 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000274 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700275 unsigned int locked_free_nr;
276 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000277 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700278 /* IRQ completion list, under ->completion_lock */
279 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000280};
281
282struct io_submit_state {
283 struct blk_plug plug;
284
285 /*
286 * io_kiocb alloc cache
287 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000288 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000289 unsigned int free_reqs;
290
291 bool plug_started;
292
293 /*
294 * Batch completion logic
295 */
296 struct io_comp_state comp;
297
298 /*
299 * File reference cache
300 */
301 struct file *file;
302 unsigned int fd;
303 unsigned int file_refs;
304 unsigned int ios_left;
305};
306
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307struct io_ring_ctx {
308 struct {
309 struct percpu_ref refs;
310 } ____cacheline_aligned_in_smp;
311
312 struct {
313 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800314 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700315 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800316 unsigned int cq_overflow_flushed: 1;
317 unsigned int drain_next: 1;
318 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200319 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000320 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700321
Hristo Venev75b28af2019-08-26 17:23:46 +0000322 /*
323 * Ring buffer of indices into array of io_uring_sqe, which is
324 * mmapped by the application using the IORING_OFF_SQES offset.
325 *
326 * This indirection could e.g. be used to assign fixed
327 * io_uring_sqe entries to operations and only submit them to
328 * the queue when needed.
329 *
330 * The kernel modifies neither the indices array nor the entries
331 * array.
332 */
333 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334 unsigned cached_sq_head;
335 unsigned sq_entries;
336 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700337 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600338 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100339 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700340 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600341
342 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600343 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700344 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345
Jens Axboead3eb2c2019-12-18 17:12:20 -0700346 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700347 } ____cacheline_aligned_in_smp;
348
Hristo Venev75b28af2019-08-26 17:23:46 +0000349 struct io_rings *rings;
350
Jens Axboe2b188cc2019-01-07 10:46:33 -0700351 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600352 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600353
354 /*
355 * For SQPOLL usage - we hold a reference to the parent task, so we
356 * have access to the ->files
357 */
358 struct task_struct *sqo_task;
359
360 /* Only used for accounting purposes */
361 struct mm_struct *mm_account;
362
Dennis Zhou91d8f512020-09-16 13:41:05 -0700363#ifdef CONFIG_BLK_CGROUP
364 struct cgroup_subsys_state *sqo_blkcg_css;
365#endif
366
Jens Axboe534ca6d2020-09-02 13:52:19 -0600367 struct io_sq_data *sq_data; /* if using sq thread polling */
368
Jens Axboe90554202020-09-03 12:12:41 -0600369 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600370 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700371
Jens Axboe6b063142019-01-10 22:13:58 -0700372 /*
373 * If used, fixed file set. Writers must ensure that ->refs is dead,
374 * readers must ensure that ->refs is alive as long as the file* is
375 * used. Only updated through io_uring_register(2).
376 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000377 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700378 unsigned nr_user_files;
379
Jens Axboeedafcce2019-01-09 09:16:05 -0700380 /* if used, fixed mapped user buffers */
381 unsigned nr_user_bufs;
382 struct io_mapped_ubuf *user_bufs;
383
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384 struct user_struct *user;
385
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700386 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700387
Jens Axboe4ea33a92020-10-15 13:46:44 -0600388#ifdef CONFIG_AUDIT
389 kuid_t loginuid;
390 unsigned int sessionid;
391#endif
392
Jens Axboe0f158b42020-05-14 17:18:39 -0600393 struct completion ref_comp;
394 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700395
396#if defined(CONFIG_UNIX)
397 struct socket *ring_sock;
398#endif
399
Jens Axboe5a2e7452020-02-23 16:23:11 -0700400 struct idr io_buffer_idr;
401
Jens Axboe071698e2020-01-28 10:04:42 -0700402 struct idr personality_idr;
403
Jens Axboe206aefd2019-11-07 18:27:42 -0700404 struct {
405 unsigned cached_cq_tail;
406 unsigned cq_entries;
407 unsigned cq_mask;
408 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500409 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700410 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700411 struct wait_queue_head cq_wait;
412 struct fasync_struct *cq_fasync;
413 struct eventfd_ctx *cq_ev_fd;
414 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700415
416 struct {
417 struct mutex uring_lock;
418 wait_queue_head_t wait;
419 } ____cacheline_aligned_in_smp;
420
421 struct {
422 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700423
Jens Axboedef596e2019-01-09 08:59:42 -0700424 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300425 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700426 * io_uring instances that don't use IORING_SETUP_SQPOLL.
427 * For SQPOLL, only the single threaded io_sq_thread() will
428 * manipulate the list, hence no extra locking is needed there.
429 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300430 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700431 struct hlist_head *cancel_hash;
432 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700433 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600434
435 spinlock_t inflight_lock;
436 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700437 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600438
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000439 struct delayed_work rsrc_put_work;
440 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000441 struct list_head rsrc_ref_list;
442 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600443
Jens Axboe85faa7b2020-04-09 18:14:00 -0600444 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200445 struct io_restriction restrictions;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000446 struct io_submit_state submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447};
448
Jens Axboe09bb8392019-03-13 12:39:28 -0600449/*
450 * First field must be the file pointer in all the
451 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
452 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700453struct io_poll_iocb {
454 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000455 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700456 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600457 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700458 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700459 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700460};
461
Pavel Begunkov018043b2020-10-27 23:17:18 +0000462struct io_poll_remove {
463 struct file *file;
464 u64 addr;
465};
466
Jens Axboeb5dba592019-12-11 14:02:38 -0700467struct io_close {
468 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700469 int fd;
470};
471
Jens Axboead8a48a2019-11-15 08:49:11 -0700472struct io_timeout_data {
473 struct io_kiocb *req;
474 struct hrtimer timer;
475 struct timespec64 ts;
476 enum hrtimer_mode mode;
477};
478
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700479struct io_accept {
480 struct file *file;
481 struct sockaddr __user *addr;
482 int __user *addr_len;
483 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600484 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700485};
486
487struct io_sync {
488 struct file *file;
489 loff_t len;
490 loff_t off;
491 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700492 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700493};
494
Jens Axboefbf23842019-12-17 18:45:56 -0700495struct io_cancel {
496 struct file *file;
497 u64 addr;
498};
499
Jens Axboeb29472e2019-12-17 18:50:29 -0700500struct io_timeout {
501 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300502 u32 off;
503 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300504 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000505 /* head of the link, used by linked timeouts only */
506 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700507};
508
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100509struct io_timeout_rem {
510 struct file *file;
511 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000512
513 /* timeout update */
514 struct timespec64 ts;
515 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100516};
517
Jens Axboe9adbd452019-12-20 08:45:55 -0700518struct io_rw {
519 /* NOTE: kiocb has the file as the first member, so don't do it here */
520 struct kiocb kiocb;
521 u64 addr;
522 u64 len;
523};
524
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700525struct io_connect {
526 struct file *file;
527 struct sockaddr __user *addr;
528 int addr_len;
529};
530
Jens Axboee47293f2019-12-20 08:58:21 -0700531struct io_sr_msg {
532 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700533 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300534 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700535 void __user *buf;
536 };
Jens Axboee47293f2019-12-20 08:58:21 -0700537 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700538 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700539 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700540 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700541};
542
Jens Axboe15b71ab2019-12-11 11:20:36 -0700543struct io_open {
544 struct file *file;
545 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700546 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700547 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600548 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700549};
550
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000551struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700552 struct file *file;
553 u64 arg;
554 u32 nr_args;
555 u32 offset;
556};
557
Jens Axboe4840e412019-12-25 22:03:45 -0700558struct io_fadvise {
559 struct file *file;
560 u64 offset;
561 u32 len;
562 u32 advice;
563};
564
Jens Axboec1ca7572019-12-25 22:18:28 -0700565struct io_madvise {
566 struct file *file;
567 u64 addr;
568 u32 len;
569 u32 advice;
570};
571
Jens Axboe3e4827b2020-01-08 15:18:09 -0700572struct io_epoll {
573 struct file *file;
574 int epfd;
575 int op;
576 int fd;
577 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700578};
579
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300580struct io_splice {
581 struct file *file_out;
582 struct file *file_in;
583 loff_t off_out;
584 loff_t off_in;
585 u64 len;
586 unsigned int flags;
587};
588
Jens Axboeddf0322d2020-02-23 16:41:33 -0700589struct io_provide_buf {
590 struct file *file;
591 __u64 addr;
592 __s32 len;
593 __u32 bgid;
594 __u16 nbufs;
595 __u16 bid;
596};
597
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700598struct io_statx {
599 struct file *file;
600 int dfd;
601 unsigned int mask;
602 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700603 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700604 struct statx __user *buffer;
605};
606
Jens Axboe36f4fa62020-09-05 11:14:22 -0600607struct io_shutdown {
608 struct file *file;
609 int how;
610};
611
Jens Axboe80a261f2020-09-28 14:23:58 -0600612struct io_rename {
613 struct file *file;
614 int old_dfd;
615 int new_dfd;
616 struct filename *oldpath;
617 struct filename *newpath;
618 int flags;
619};
620
Jens Axboe14a11432020-09-28 14:27:37 -0600621struct io_unlink {
622 struct file *file;
623 int dfd;
624 int flags;
625 struct filename *filename;
626};
627
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300628struct io_completion {
629 struct file *file;
630 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300631 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300632};
633
Jens Axboef499a022019-12-02 16:28:46 -0700634struct io_async_connect {
635 struct sockaddr_storage address;
636};
637
Jens Axboe03b12302019-12-02 18:50:25 -0700638struct io_async_msghdr {
639 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000640 /* points to an allocated iov, if NULL we use fast_iov instead */
641 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700642 struct sockaddr __user *uaddr;
643 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700644 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700645};
646
Jens Axboef67676d2019-12-02 11:03:47 -0700647struct io_async_rw {
648 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600649 const struct iovec *free_iovec;
650 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600651 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600652 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700653};
654
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300655enum {
656 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
657 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
658 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
659 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
660 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700661 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300662
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300663 REQ_F_FAIL_LINK_BIT,
664 REQ_F_INFLIGHT_BIT,
665 REQ_F_CUR_POS_BIT,
666 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300667 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300668 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300669 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700670 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700671 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600672 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800673 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100674 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000675 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700676
677 /* not a real bit, just to check we're not overflowing the space */
678 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300679};
680
681enum {
682 /* ctx owns file */
683 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
684 /* drain existing IO first */
685 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
686 /* linked sqes */
687 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
688 /* doesn't sever on completion < 0 */
689 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
690 /* IOSQE_ASYNC */
691 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700692 /* IOSQE_BUFFER_SELECT */
693 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300694
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300695 /* fail rest of links */
696 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
697 /* on inflight list */
698 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
699 /* read/write uses file position */
700 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
701 /* must not punt to workers */
702 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100703 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300704 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300705 /* regular file */
706 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300707 /* needs cleanup */
708 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700709 /* already went through poll handler */
710 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700711 /* buffer already selected */
712 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600713 /* doesn't need file table for this request */
714 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800715 /* io_wq_work is initialized */
716 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100717 /* linked timeout is active, i.e. prepared by link's head */
718 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000719 /* completion is deferred through io_comp_state */
720 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700721};
722
723struct async_poll {
724 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600725 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300726};
727
Jens Axboe7cbf1722021-02-10 00:03:20 +0000728struct io_task_work {
729 struct io_wq_work_node node;
730 task_work_func_t func;
731};
732
Jens Axboe09bb8392019-03-13 12:39:28 -0600733/*
734 * NOTE! Each of the iocb union members has the file pointer
735 * as the first entry in their struct definition. So you can
736 * access the file pointer through any of the sub-structs,
737 * or directly as just 'ki_filp' in this struct.
738 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700739struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700740 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600741 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700742 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700743 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000744 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700745 struct io_accept accept;
746 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700747 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700748 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100749 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700750 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700751 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700752 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700753 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000754 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700755 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700756 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700757 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300758 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700759 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700760 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600761 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600762 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600763 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300764 /* use only after cleaning per-op data, see io_clean_op() */
765 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700766 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700767
Jens Axboee8c2bc12020-08-15 18:44:09 -0700768 /* opcode allocated if it needs to store data for async defer */
769 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700770 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800771 /* polled IO has completed */
772 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700773
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700774 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300775 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700776
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300777 struct io_ring_ctx *ctx;
778 unsigned int flags;
779 refcount_t refs;
780 struct task_struct *task;
781 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000783 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000784 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700785
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300786 /*
787 * 1. used with ctx->iopoll_list with reads/writes
788 * 2. to track reqs with ->files (see io_op_def::file_table)
789 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300790 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000791 union {
792 struct io_task_work io_task_work;
793 struct callback_head task_work;
794 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300795 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
796 struct hlist_node hash_node;
797 struct async_poll *apoll;
798 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700799};
800
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300801struct io_defer_entry {
802 struct list_head list;
803 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300804 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300805};
806
Jens Axboed3656342019-12-18 09:50:26 -0700807struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700808 /* needs req->file assigned */
809 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700810 /* hash wq insertion if file is a regular file */
811 unsigned hash_reg_file : 1;
812 /* unbound wq insertion if file is a non-regular file */
813 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700814 /* opcode is not supported by this kernel */
815 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700816 /* set if opcode supports polled "wait" */
817 unsigned pollin : 1;
818 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700819 /* op supports buffer selection */
820 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700821 /* must always have async data allocated */
822 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600823 /* should block plug */
824 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700825 /* size of async data needed, if any */
826 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600827 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700828};
829
Jens Axboe09186822020-10-13 15:01:40 -0600830static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_NOP] = {},
832 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700833 .needs_file = 1,
834 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700835 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700836 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700837 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600838 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600840 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_file = 1,
844 .hash_reg_file = 1,
845 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700847 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600848 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700849 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600850 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
851 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700852 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300853 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700854 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600855 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700858 .needs_file = 1,
859 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700860 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600861 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700862 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600863 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700866 .needs_file = 1,
867 .hash_reg_file = 1,
868 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700869 .pollout = 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 Axboe4017eb92020-10-22 14:14:12 -0600872 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
873 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700874 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300875 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700876 .needs_file = 1,
877 .unbound_nonreg_file = 1,
878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_POLL_REMOVE] = {},
880 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700881 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600882 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700885 .needs_file = 1,
886 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700887 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700888 .needs_async_data = 1,
889 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000890 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700891 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300892 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700893 .needs_file = 1,
894 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700895 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700896 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700897 .needs_async_data = 1,
898 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000899 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700902 .needs_async_data = 1,
903 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600904 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700905 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000906 [IORING_OP_TIMEOUT_REMOVE] = {
907 /* used by timeout updates' prep() */
908 .work_flags = IO_WQ_WORK_MM,
909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700911 .needs_file = 1,
912 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700913 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600914 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
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 Axboe0f203762020-10-14 09:23:55 -0600920 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700921 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300922 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700923 .needs_file = 1,
924 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700925 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700926 .needs_async_data = 1,
927 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600928 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700929 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300930 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700931 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600932 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700933 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300934 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600935 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600936 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700937 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300938 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600939 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700940 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300941 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600942 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600945 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
946 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700947 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300948 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700949 .needs_file = 1,
950 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700951 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700952 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600953 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700954 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600955 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700958 .needs_file = 1,
959 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700960 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600961 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700962 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600963 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
964 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700967 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600968 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700969 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300970 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600971 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700974 .needs_file = 1,
975 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700976 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600977 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700978 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300979 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700980 .needs_file = 1,
981 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700982 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700983 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600984 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700985 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300986 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600987 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600988 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700989 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700990 [IORING_OP_EPOLL_CTL] = {
991 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600992 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700993 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300994 [IORING_OP_SPLICE] = {
995 .needs_file = 1,
996 .hash_reg_file = 1,
997 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600998 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700999 },
1000 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001001 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001002 [IORING_OP_TEE] = {
1003 .needs_file = 1,
1004 .hash_reg_file = 1,
1005 .unbound_nonreg_file = 1,
1006 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001007 [IORING_OP_SHUTDOWN] = {
1008 .needs_file = 1,
1009 },
Jens Axboe80a261f2020-09-28 14:23:58 -06001010 [IORING_OP_RENAMEAT] = {
1011 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1012 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1013 },
Jens Axboe14a11432020-09-28 14:27:37 -06001014 [IORING_OP_UNLINKAT] = {
1015 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1016 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1017 },
Jens Axboed3656342019-12-18 09:50:26 -07001018};
1019
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001020static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1021 struct task_struct *task,
1022 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001023static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001024static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001025 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001026static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1027 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001028
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001029static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001030 unsigned int issue_flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001031static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001032static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001033static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001034static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001035static void io_dismantle_req(struct io_kiocb *req);
1036static void io_put_task(struct task_struct *task, int nr);
1037static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001038static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001039static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001040static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001041static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001042 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001043 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001044static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001045static struct file *io_file_get(struct io_submit_state *state,
1046 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001047static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001048static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001049
Pavel Begunkov847595d2021-02-04 13:52:06 +00001050static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1051 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001052static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1053 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001054 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001055static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001056static void io_submit_flush_completions(struct io_comp_state *cs,
1057 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001058
Jens Axboe2b188cc2019-01-07 10:46:33 -07001059static struct kmem_cache *req_cachep;
1060
Jens Axboe09186822020-10-13 15:01:40 -06001061static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001062
1063struct sock *io_uring_get_socket(struct file *file)
1064{
1065#if defined(CONFIG_UNIX)
1066 if (file->f_op == &io_uring_fops) {
1067 struct io_ring_ctx *ctx = file->private_data;
1068
1069 return ctx->ring_sock->sk;
1070 }
1071#endif
1072 return NULL;
1073}
1074EXPORT_SYMBOL(io_uring_get_socket);
1075
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001076#define io_for_each_link(pos, head) \
1077 for (pos = (head); pos; pos = pos->link)
1078
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001079static inline void io_clean_op(struct io_kiocb *req)
1080{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001081 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001082 __io_clean_op(req);
1083}
1084
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001085static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001086{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001087 struct io_ring_ctx *ctx = req->ctx;
1088
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001089 if (!req->fixed_rsrc_refs) {
1090 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1091 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001092 }
1093}
1094
Pavel Begunkov08d23632020-11-06 13:00:22 +00001095static bool io_match_task(struct io_kiocb *head,
1096 struct task_struct *task,
1097 struct files_struct *files)
1098{
1099 struct io_kiocb *req;
1100
Jens Axboe84965ff2021-01-23 15:51:11 -07001101 if (task && head->task != task) {
1102 /* in terms of cancelation, always match if req task is dead */
1103 if (head->task->flags & PF_EXITING)
1104 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001105 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001106 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001107 if (!files)
1108 return true;
1109
1110 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001111 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1112 continue;
1113 if (req->file && req->file->f_op == &io_uring_fops)
1114 return true;
1115 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001116 req->work.identity->files == files)
1117 return true;
1118 }
1119 return false;
1120}
1121
Jens Axboe28cea78a2020-09-14 10:51:17 -06001122static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001123{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001124 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001125 struct mm_struct *mm = current->mm;
1126
1127 if (mm) {
1128 kthread_unuse_mm(mm);
1129 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001130 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001131 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001132 if (files) {
1133 struct nsproxy *nsproxy = current->nsproxy;
1134
1135 task_lock(current);
1136 current->files = NULL;
1137 current->nsproxy = NULL;
1138 task_unlock(current);
1139 put_files_struct(files);
1140 put_nsproxy(nsproxy);
1141 }
1142}
1143
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001144static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001145{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001146 if (current->flags & PF_EXITING)
1147 return -EFAULT;
1148
Jens Axboe28cea78a2020-09-14 10:51:17 -06001149 if (!current->files) {
1150 struct files_struct *files;
1151 struct nsproxy *nsproxy;
1152
1153 task_lock(ctx->sqo_task);
1154 files = ctx->sqo_task->files;
1155 if (!files) {
1156 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001157 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001158 }
1159 atomic_inc(&files->count);
1160 get_nsproxy(ctx->sqo_task->nsproxy);
1161 nsproxy = ctx->sqo_task->nsproxy;
1162 task_unlock(ctx->sqo_task);
1163
1164 task_lock(current);
1165 current->files = files;
1166 current->nsproxy = nsproxy;
1167 task_unlock(current);
1168 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001169 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001170}
1171
1172static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1173{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001174 struct mm_struct *mm;
1175
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001176 if (current->flags & PF_EXITING)
1177 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001178 if (current->mm)
1179 return 0;
1180
1181 /* Should never happen */
1182 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1183 return -EFAULT;
1184
1185 task_lock(ctx->sqo_task);
1186 mm = ctx->sqo_task->mm;
1187 if (unlikely(!mm || !mmget_not_zero(mm)))
1188 mm = NULL;
1189 task_unlock(ctx->sqo_task);
1190
1191 if (mm) {
1192 kthread_use_mm(mm);
1193 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001194 }
1195
Jens Axboe4b70cf92020-11-02 10:39:05 -07001196 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001197}
1198
Jens Axboe28cea78a2020-09-14 10:51:17 -06001199static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1200 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001201{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001202 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001203 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001204
1205 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001206 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001207 if (unlikely(ret))
1208 return ret;
1209 }
1210
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001211 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1212 ret = __io_sq_thread_acquire_files(ctx);
1213 if (unlikely(ret))
1214 return ret;
1215 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001216
1217 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001218}
1219
Dennis Zhou91d8f512020-09-16 13:41:05 -07001220static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1221 struct cgroup_subsys_state **cur_css)
1222
1223{
1224#ifdef CONFIG_BLK_CGROUP
1225 /* puts the old one when swapping */
1226 if (*cur_css != ctx->sqo_blkcg_css) {
1227 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1228 *cur_css = ctx->sqo_blkcg_css;
1229 }
1230#endif
1231}
1232
1233static void io_sq_thread_unassociate_blkcg(void)
1234{
1235#ifdef CONFIG_BLK_CGROUP
1236 kthread_associate_blkcg(NULL);
1237#endif
1238}
1239
Jens Axboec40f6372020-06-25 15:39:59 -06001240static inline void req_set_fail_links(struct io_kiocb *req)
1241{
1242 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1243 req->flags |= REQ_F_FAIL_LINK;
1244}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001245
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001246/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001247 * None of these are dereferenced, they are simply used to check if any of
1248 * them have changed. If we're under current and check they are still the
1249 * same, we're fine to grab references to them for actual out-of-line use.
1250 */
1251static void io_init_identity(struct io_identity *id)
1252{
1253 id->files = current->files;
1254 id->mm = current->mm;
1255#ifdef CONFIG_BLK_CGROUP
1256 rcu_read_lock();
1257 id->blkcg_css = blkcg_css();
1258 rcu_read_unlock();
1259#endif
1260 id->creds = current_cred();
1261 id->nsproxy = current->nsproxy;
1262 id->fs = current->fs;
1263 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001264#ifdef CONFIG_AUDIT
1265 id->loginuid = current->loginuid;
1266 id->sessionid = current->sessionid;
1267#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001268 refcount_set(&id->count, 1);
1269}
1270
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001271static inline void __io_req_init_async(struct io_kiocb *req)
1272{
1273 memset(&req->work, 0, sizeof(req->work));
1274 req->flags |= REQ_F_WORK_INITIALIZED;
1275}
1276
Jens Axboe1e6fa522020-10-15 08:46:24 -06001277/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001278 * Note: must call io_req_init_async() for the first time you
1279 * touch any members of io_wq_work.
1280 */
1281static inline void io_req_init_async(struct io_kiocb *req)
1282{
Jens Axboe500a3732020-10-15 17:38:03 -06001283 struct io_uring_task *tctx = current->io_uring;
1284
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001285 if (req->flags & REQ_F_WORK_INITIALIZED)
1286 return;
1287
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001288 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001289
1290 /* Grab a ref if this isn't our static identity */
1291 req->work.identity = tctx->identity;
1292 if (tctx->identity != &tctx->__identity)
1293 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001294}
1295
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1297{
1298 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1299
Jens Axboe0f158b42020-05-14 17:18:39 -06001300 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001301}
1302
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001303static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1304{
1305 return !req->timeout.off;
1306}
1307
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1309{
1310 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001311 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001312
1313 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1314 if (!ctx)
1315 return NULL;
1316
Jens Axboe78076bb2019-12-04 19:56:40 -07001317 /*
1318 * Use 5 bits less than the max cq entries, that should give us around
1319 * 32 entries per hash list if totally full and uniformly spread.
1320 */
1321 hash_bits = ilog2(p->cq_entries);
1322 hash_bits -= 5;
1323 if (hash_bits <= 0)
1324 hash_bits = 1;
1325 ctx->cancel_hash_bits = hash_bits;
1326 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1327 GFP_KERNEL);
1328 if (!ctx->cancel_hash)
1329 goto err;
1330 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1331
Roman Gushchin21482892019-05-07 10:01:48 -07001332 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001333 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1334 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001335
1336 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001337 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001338 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001340 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001341 init_completion(&ctx->ref_comp);
1342 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001343 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001344 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345 mutex_init(&ctx->uring_lock);
1346 init_waitqueue_head(&ctx->wait);
1347 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001348 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001349 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001350 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001351 spin_lock_init(&ctx->inflight_lock);
1352 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001353 spin_lock_init(&ctx->rsrc_ref_lock);
1354 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001355 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1356 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001357 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001358 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001360err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001361 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001362 kfree(ctx);
1363 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364}
1365
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001366static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001367{
Jens Axboe2bc99302020-07-09 09:43:27 -06001368 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1369 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001370
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001371 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001372 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001373 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001374
Bob Liu9d858b22019-11-13 18:06:25 +08001375 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001376}
1377
Jens Axboe5c3462c2020-10-15 09:02:33 -06001378static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001379{
Jens Axboe500a3732020-10-15 17:38:03 -06001380 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001381 return;
1382 if (refcount_dec_and_test(&req->work.identity->count))
1383 kfree(req->work.identity);
1384}
1385
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001386static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001387{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001388 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001389 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001390
Pavel Begunkove86d0042021-02-01 18:59:54 +00001391 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001392 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001393#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001394 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001395 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001396#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001397 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001398 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001399 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001400 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001401
Jens Axboe98447d62020-10-14 10:48:51 -06001402 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001403 if (--fs->users)
1404 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001405 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001406 if (fs)
1407 free_fs_struct(fs);
1408 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001409 if (req->work.flags & IO_WQ_WORK_FILES) {
1410 put_files_struct(req->work.identity->files);
1411 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001412 }
1413 if (req->flags & REQ_F_INFLIGHT) {
1414 struct io_ring_ctx *ctx = req->ctx;
1415 struct io_uring_task *tctx = req->task->io_uring;
1416 unsigned long flags;
1417
1418 spin_lock_irqsave(&ctx->inflight_lock, flags);
1419 list_del(&req->inflight_entry);
1420 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1421 req->flags &= ~REQ_F_INFLIGHT;
1422 if (atomic_read(&tctx->in_idle))
1423 wake_up(&tctx->wait);
1424 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001425
Pavel Begunkove86d0042021-02-01 18:59:54 +00001426 req->flags &= ~REQ_F_WORK_INITIALIZED;
1427 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1428 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001429 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001430}
1431
1432/*
1433 * Create a private copy of io_identity, since some fields don't match
1434 * the current context.
1435 */
1436static bool io_identity_cow(struct io_kiocb *req)
1437{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001438 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001439 const struct cred *creds = NULL;
1440 struct io_identity *id;
1441
1442 if (req->work.flags & IO_WQ_WORK_CREDS)
1443 creds = req->work.identity->creds;
1444
1445 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1446 if (unlikely(!id)) {
1447 req->work.flags |= IO_WQ_WORK_CANCEL;
1448 return false;
1449 }
1450
1451 /*
1452 * We can safely just re-init the creds we copied Either the field
1453 * matches the current one, or we haven't grabbed it yet. The only
1454 * exception is ->creds, through registered personalities, so handle
1455 * that one separately.
1456 */
1457 io_init_identity(id);
1458 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001459 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001460
1461 /* add one for this request */
1462 refcount_inc(&id->count);
1463
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001464 /* drop tctx and req identity references, if needed */
1465 if (tctx->identity != &tctx->__identity &&
1466 refcount_dec_and_test(&tctx->identity->count))
1467 kfree(tctx->identity);
1468 if (req->work.identity != &tctx->__identity &&
1469 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001470 kfree(req->work.identity);
1471
1472 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001473 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001474 return true;
1475}
1476
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001477static void io_req_track_inflight(struct io_kiocb *req)
1478{
1479 struct io_ring_ctx *ctx = req->ctx;
1480
1481 if (!(req->flags & REQ_F_INFLIGHT)) {
1482 io_req_init_async(req);
1483 req->flags |= REQ_F_INFLIGHT;
1484
1485 spin_lock_irq(&ctx->inflight_lock);
1486 list_add(&req->inflight_entry, &ctx->inflight_list);
1487 spin_unlock_irq(&ctx->inflight_lock);
1488 }
1489}
1490
Jens Axboe1e6fa522020-10-15 08:46:24 -06001491static bool io_grab_identity(struct io_kiocb *req)
1492{
1493 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001494 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001495
Jens Axboe69228332020-10-20 14:28:41 -06001496 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1497 if (id->fsize != rlimit(RLIMIT_FSIZE))
1498 return false;
1499 req->work.flags |= IO_WQ_WORK_FSIZE;
1500 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001501#ifdef CONFIG_BLK_CGROUP
1502 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1503 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1504 rcu_read_lock();
1505 if (id->blkcg_css != blkcg_css()) {
1506 rcu_read_unlock();
1507 return false;
1508 }
1509 /*
1510 * This should be rare, either the cgroup is dying or the task
1511 * is moving cgroups. Just punt to root for the handful of ios.
1512 */
1513 if (css_tryget_online(id->blkcg_css))
1514 req->work.flags |= IO_WQ_WORK_BLKCG;
1515 rcu_read_unlock();
1516 }
1517#endif
1518 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1519 if (id->creds != current_cred())
1520 return false;
1521 get_cred(id->creds);
1522 req->work.flags |= IO_WQ_WORK_CREDS;
1523 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001524#ifdef CONFIG_AUDIT
1525 if (!uid_eq(current->loginuid, id->loginuid) ||
1526 current->sessionid != id->sessionid)
1527 return false;
1528#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001529 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1530 (def->work_flags & IO_WQ_WORK_FS)) {
1531 if (current->fs != id->fs)
1532 return false;
1533 spin_lock(&id->fs->lock);
1534 if (!id->fs->in_exec) {
1535 id->fs->users++;
1536 req->work.flags |= IO_WQ_WORK_FS;
1537 } else {
1538 req->work.flags |= IO_WQ_WORK_CANCEL;
1539 }
1540 spin_unlock(&current->fs->lock);
1541 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001542 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1543 (def->work_flags & IO_WQ_WORK_FILES) &&
1544 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1545 if (id->files != current->files ||
1546 id->nsproxy != current->nsproxy)
1547 return false;
1548 atomic_inc(&id->files->count);
1549 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001550 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001551 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001552 }
Jens Axboe77788772020-12-29 10:50:46 -07001553 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1554 (def->work_flags & IO_WQ_WORK_MM)) {
1555 if (id->mm != current->mm)
1556 return false;
1557 mmgrab(id->mm);
1558 req->work.flags |= IO_WQ_WORK_MM;
1559 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001560
1561 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001562}
1563
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001564static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001565{
Jens Axboed3656342019-12-18 09:50:26 -07001566 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001567 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001568
Pavel Begunkov16d59802020-07-12 16:16:47 +03001569 io_req_init_async(req);
1570
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001571 if (req->flags & REQ_F_FORCE_ASYNC)
1572 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1573
Jens Axboed3656342019-12-18 09:50:26 -07001574 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001575 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001576 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001577 } else {
1578 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001579 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001580 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001581
Jens Axboe1e6fa522020-10-15 08:46:24 -06001582 /* if we fail grabbing identity, we must COW, regrab, and retry */
1583 if (io_grab_identity(req))
1584 return;
1585
1586 if (!io_identity_cow(req))
1587 return;
1588
1589 /* can't fail at this point */
1590 if (!io_grab_identity(req))
1591 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001592}
1593
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001594static void io_prep_async_link(struct io_kiocb *req)
1595{
1596 struct io_kiocb *cur;
1597
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001598 io_for_each_link(cur, req)
1599 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001600}
1601
Jens Axboe7271ef32020-08-10 09:55:22 -06001602static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001603{
Jackie Liua197f662019-11-08 08:09:12 -07001604 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001605 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001606
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001607 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1608 &req->work, req->flags);
1609 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001610 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001611}
1612
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001613static void io_queue_async_work(struct io_kiocb *req)
1614{
Jens Axboe7271ef32020-08-10 09:55:22 -06001615 struct io_kiocb *link;
1616
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001617 /* init ->work of the whole link before punting */
1618 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001619 link = __io_queue_async_work(req);
1620
1621 if (link)
1622 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001623}
1624
Jens Axboe5262f562019-09-17 12:26:57 -06001625static void io_kill_timeout(struct io_kiocb *req)
1626{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001627 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001628 int ret;
1629
Jens Axboee8c2bc12020-08-15 18:44:09 -07001630 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001631 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001632 atomic_set(&req->ctx->cq_timeouts,
1633 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001634 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001635 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001636 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001637 }
1638}
1639
Jens Axboe76e1b642020-09-26 15:05:03 -06001640/*
1641 * Returns true if we found and killed one or more timeouts
1642 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001643static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1644 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001645{
1646 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001647 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001648
1649 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001650 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001651 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001652 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001653 canceled++;
1654 }
Jens Axboef3606e32020-09-22 08:18:24 -06001655 }
Jens Axboe5262f562019-09-17 12:26:57 -06001656 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001657 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001658}
1659
Pavel Begunkov04518942020-05-26 20:34:05 +03001660static void __io_queue_deferred(struct io_ring_ctx *ctx)
1661{
1662 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001663 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1664 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001665
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001666 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001667 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001668 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001669 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001670 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001671 } while (!list_empty(&ctx->defer_list));
1672}
1673
Pavel Begunkov360428f2020-05-30 14:54:17 +03001674static void io_flush_timeouts(struct io_ring_ctx *ctx)
1675{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001676 u32 seq;
1677
1678 if (list_empty(&ctx->timeout_list))
1679 return;
1680
1681 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1682
1683 do {
1684 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001685 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001686 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001687
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001688 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001689 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001690
1691 /*
1692 * Since seq can easily wrap around over time, subtract
1693 * the last seq at which timeouts were flushed before comparing.
1694 * Assuming not more than 2^31-1 events have happened since,
1695 * these subtractions won't have wrapped, so we can check if
1696 * target is in [last_seq, current_seq] by comparing the two.
1697 */
1698 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1699 events_got = seq - ctx->cq_last_tm_flush;
1700 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001701 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001702
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001703 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001704 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001705 } while (!list_empty(&ctx->timeout_list));
1706
1707 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001708}
1709
Jens Axboede0617e2019-04-06 21:51:27 -06001710static void io_commit_cqring(struct io_ring_ctx *ctx)
1711{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001712 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001713
1714 /* order cqe stores with ring update */
1715 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001716
Pavel Begunkov04518942020-05-26 20:34:05 +03001717 if (unlikely(!list_empty(&ctx->defer_list)))
1718 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001719}
1720
Jens Axboe90554202020-09-03 12:12:41 -06001721static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1722{
1723 struct io_rings *r = ctx->rings;
1724
1725 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1726}
1727
Pavel Begunkov888aae22021-01-19 13:32:39 +00001728static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1729{
1730 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1731}
1732
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1734{
Hristo Venev75b28af2019-08-26 17:23:46 +00001735 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736 unsigned tail;
1737
Stefan Bühler115e12e2019-04-24 23:54:18 +02001738 /*
1739 * writes to the cq entry need to come after reading head; the
1740 * control dependency is enough as we're using WRITE_ONCE to
1741 * fill the cq entry
1742 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001743 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744 return NULL;
1745
Pavel Begunkov888aae22021-01-19 13:32:39 +00001746 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001747 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001748}
1749
Jens Axboef2842ab2020-01-08 11:04:00 -07001750static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1751{
Jens Axboef0b493e2020-02-01 21:30:11 -07001752 if (!ctx->cq_ev_fd)
1753 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001754 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1755 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001756 if (!ctx->eventfd_async)
1757 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001758 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001759}
1760
Jens Axboeb41e9852020-02-17 09:52:41 -07001761static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001762{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001763 /* see waitqueue_active() comment */
1764 smp_mb();
1765
Jens Axboe8c838782019-03-12 15:48:16 -06001766 if (waitqueue_active(&ctx->wait))
1767 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001768 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1769 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001770 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001771 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001772 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001773 wake_up_interruptible(&ctx->cq_wait);
1774 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1775 }
Jens Axboe8c838782019-03-12 15:48:16 -06001776}
1777
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001778static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1779{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001780 /* see waitqueue_active() comment */
1781 smp_mb();
1782
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001783 if (ctx->flags & IORING_SETUP_SQPOLL) {
1784 if (waitqueue_active(&ctx->wait))
1785 wake_up(&ctx->wait);
1786 }
1787 if (io_should_trigger_evfd(ctx))
1788 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001789 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001790 wake_up_interruptible(&ctx->cq_wait);
1791 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1792 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001793}
1794
Jens Axboec4a2ed72019-11-21 21:01:26 -07001795/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001796static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1797 struct task_struct *tsk,
1798 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001800 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001801 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001802 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001803 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001804 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001805 LIST_HEAD(list);
1806
Pavel Begunkove23de152020-12-17 00:24:37 +00001807 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1808 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001809
Jens Axboeb18032b2021-01-24 16:58:56 -07001810 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001811 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001812 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001813 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001814 continue;
1815
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001816 cqe = io_get_cqring(ctx);
1817 if (!cqe && !force)
1818 break;
1819
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001820 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001821 if (cqe) {
1822 WRITE_ONCE(cqe->user_data, req->user_data);
1823 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001824 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001825 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001826 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001827 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001828 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001829 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001830 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001831 }
1832
Pavel Begunkov09e88402020-12-17 00:24:38 +00001833 all_flushed = list_empty(&ctx->cq_overflow_list);
1834 if (all_flushed) {
1835 clear_bit(0, &ctx->sq_check_overflow);
1836 clear_bit(0, &ctx->cq_check_overflow);
1837 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1838 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001839
Jens Axboeb18032b2021-01-24 16:58:56 -07001840 if (posted)
1841 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001842 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001843 if (posted)
1844 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001845
1846 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001847 req = list_first_entry(&list, struct io_kiocb, compl.list);
1848 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001849 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001850 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001851
Pavel Begunkov09e88402020-12-17 00:24:38 +00001852 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001853}
1854
Pavel Begunkov6c503152021-01-04 20:36:36 +00001855static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1856 struct task_struct *tsk,
1857 struct files_struct *files)
1858{
1859 if (test_bit(0, &ctx->cq_check_overflow)) {
1860 /* iopoll syncs against uring_lock, not completion_lock */
1861 if (ctx->flags & IORING_SETUP_IOPOLL)
1862 mutex_lock(&ctx->uring_lock);
1863 __io_cqring_overflow_flush(ctx, force, tsk, files);
1864 if (ctx->flags & IORING_SETUP_IOPOLL)
1865 mutex_unlock(&ctx->uring_lock);
1866 }
1867}
1868
Jens Axboebcda7ba2020-02-23 16:42:51 -07001869static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001871 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001872 struct io_uring_cqe *cqe;
1873
Jens Axboe78e19bb2019-11-06 15:21:34 -07001874 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001875
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876 /*
1877 * If we can't get a cq entry, userspace overflowed the
1878 * submission (by quite a lot). Increment the overflow count in
1879 * the ring.
1880 */
1881 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001882 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001883 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001885 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001886 } else if (ctx->cq_overflow_flushed ||
1887 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001888 /*
1889 * If we're in ring overflow flush mode, or in task cancel mode,
1890 * then we cannot store the request for later flushing, we need
1891 * to drop it on the floor.
1892 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001893 ctx->cached_cq_overflow++;
1894 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001895 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001896 if (list_empty(&ctx->cq_overflow_list)) {
1897 set_bit(0, &ctx->sq_check_overflow);
1898 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001899 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001900 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001901 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001902 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001903 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001904 refcount_inc(&req->refs);
1905 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001906 }
1907}
1908
Jens Axboebcda7ba2020-02-23 16:42:51 -07001909static void io_cqring_fill_event(struct io_kiocb *req, long res)
1910{
1911 __io_cqring_fill_event(req, res, 0);
1912}
1913
Jens Axboec7dae4b2021-02-09 19:53:37 -07001914static inline void io_req_complete_post(struct io_kiocb *req, long res,
1915 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001917 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001918 unsigned long flags;
1919
1920 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001921 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001923 /*
1924 * If we're the last reference to this request, add to our locked
1925 * free_list cache.
1926 */
1927 if (refcount_dec_and_test(&req->refs)) {
1928 struct io_comp_state *cs = &ctx->submit_state.comp;
1929
1930 io_dismantle_req(req);
1931 io_put_task(req->task, 1);
1932 list_add(&req->compl.list, &cs->locked_free_list);
1933 cs->locked_free_nr++;
1934 } else
1935 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1937
Jens Axboe8c838782019-03-12 15:48:16 -06001938 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001939 if (req) {
1940 io_queue_next(req);
1941 percpu_ref_put(&ctx->refs);
1942 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943}
1944
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001945static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001946 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001947{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001948 io_clean_op(req);
1949 req->result = res;
1950 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001951 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001952}
1953
Pavel Begunkov889fca72021-02-10 00:03:09 +00001954static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1955 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001956{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001957 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1958 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001959 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001960 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001961}
1962
1963static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001964{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001965 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001966}
1967
Jens Axboec7dae4b2021-02-09 19:53:37 -07001968static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001969{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001970 struct io_submit_state *state = &ctx->submit_state;
1971 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001972 struct io_kiocb *req = NULL;
1973
Jens Axboec7dae4b2021-02-09 19:53:37 -07001974 /*
1975 * If we have more than a batch's worth of requests in our IRQ side
1976 * locked cache, grab the lock and move them over to our submission
1977 * side cache.
1978 */
1979 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1980 spin_lock_irq(&ctx->completion_lock);
1981 list_splice_init(&cs->locked_free_list, &cs->free_list);
1982 cs->locked_free_nr = 0;
1983 spin_unlock_irq(&ctx->completion_lock);
1984 }
1985
1986 while (!list_empty(&cs->free_list)) {
1987 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001988 compl.list);
1989 list_del(&req->compl.list);
1990 state->reqs[state->free_reqs++] = req;
1991 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1992 break;
1993 }
1994
1995 return req != NULL;
1996}
1997
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001998static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001999{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002000 struct io_submit_state *state = &ctx->submit_state;
2001
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002002 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
2003
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002004 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002005 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002006 int ret;
2007
Jens Axboec7dae4b2021-02-09 19:53:37 -07002008 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002009 goto got_req;
2010
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002011 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
2012 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06002013
2014 /*
2015 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2016 * retry single alloc to be on the safe side.
2017 */
2018 if (unlikely(ret <= 0)) {
2019 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2020 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00002021 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06002022 ret = 1;
2023 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002024 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002026got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03002027 state->free_reqs--;
2028 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002029}
2030
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002031static inline void io_put_file(struct io_kiocb *req, struct file *file,
2032 bool fixed)
2033{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002034 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002035 fput(file);
2036}
2037
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002038static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002039{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002040 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002041
Jens Axboee8c2bc12020-08-15 18:44:09 -07002042 if (req->async_data)
2043 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002044 if (req->file)
2045 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002046 if (req->fixed_rsrc_refs)
2047 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002048 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002049}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002050
Pavel Begunkov7c660732021-01-25 11:42:21 +00002051static inline void io_put_task(struct task_struct *task, int nr)
2052{
2053 struct io_uring_task *tctx = task->io_uring;
2054
2055 percpu_counter_sub(&tctx->inflight, nr);
2056 if (unlikely(atomic_read(&tctx->in_idle)))
2057 wake_up(&tctx->wait);
2058 put_task_struct_many(task, nr);
2059}
2060
Pavel Begunkov216578e2020-10-13 09:44:00 +01002061static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002062{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002063 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002064
Pavel Begunkov216578e2020-10-13 09:44:00 +01002065 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002066 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002067
Pavel Begunkov3893f392021-02-10 00:03:15 +00002068 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002069 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002070}
2071
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002072static inline void io_remove_next_linked(struct io_kiocb *req)
2073{
2074 struct io_kiocb *nxt = req->link;
2075
2076 req->link = nxt->link;
2077 nxt->link = NULL;
2078}
2079
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002080static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002081{
Jackie Liua197f662019-11-08 08:09:12 -07002082 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002083 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002084 bool cancelled = false;
2085 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002086
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002087 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002088 link = req->link;
2089
Pavel Begunkov900fad42020-10-19 16:39:16 +01002090 /*
2091 * Can happen if a linked timeout fired and link had been like
2092 * req -> link t-out -> link t-out [-> ...]
2093 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002094 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2095 struct io_timeout_data *io = link->async_data;
2096 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002097
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002098 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002099 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002100 ret = hrtimer_try_to_cancel(&io->timer);
2101 if (ret != -1) {
2102 io_cqring_fill_event(link, -ECANCELED);
2103 io_commit_cqring(ctx);
2104 cancelled = true;
2105 }
2106 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002107 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002108 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002109
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002110 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002111 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002112 io_put_req(link);
2113 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002114}
2115
Jens Axboe4d7dd462019-11-20 13:03:52 -07002116
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002117static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002118{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002119 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002120 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002121 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002122
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002123 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002124 link = req->link;
2125 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002126
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002127 while (link) {
2128 nxt = link->link;
2129 link->link = NULL;
2130
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002131 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002132 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002133
2134 /*
2135 * It's ok to free under spinlock as they're not linked anymore,
2136 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2137 * work.fs->lock.
2138 */
2139 if (link->flags & REQ_F_WORK_INITIALIZED)
2140 io_put_req_deferred(link, 2);
2141 else
2142 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002143 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002144 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002145 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002146 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002147
Jens Axboe2665abf2019-11-05 12:40:47 -07002148 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002149}
2150
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002151static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002152{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002153 if (req->flags & REQ_F_LINK_TIMEOUT)
2154 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002155
Jens Axboe9e645e112019-05-10 16:07:28 -06002156 /*
2157 * If LINK is set, we have dependent requests in this chain. If we
2158 * didn't fail this request, queue the first one up, moving any other
2159 * dependencies to the next request. In case of failure, fail the rest
2160 * of the chain.
2161 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002162 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2163 struct io_kiocb *nxt = req->link;
2164
2165 req->link = NULL;
2166 return nxt;
2167 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002168 io_fail_links(req);
2169 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002170}
Jens Axboe2665abf2019-11-05 12:40:47 -07002171
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002172static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002173{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002174 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002175 return NULL;
2176 return __io_req_find_next(req);
2177}
2178
Jens Axboe7cbf1722021-02-10 00:03:20 +00002179static bool __tctx_task_work(struct io_uring_task *tctx)
2180{
Jens Axboe65453d12021-02-10 00:03:21 +00002181 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002182 struct io_wq_work_list list;
2183 struct io_wq_work_node *node;
2184
2185 if (wq_list_empty(&tctx->task_list))
2186 return false;
2187
2188 spin_lock(&tctx->task_lock);
2189 list = tctx->task_list;
2190 INIT_WQ_LIST(&tctx->task_list);
2191 spin_unlock(&tctx->task_lock);
2192
2193 node = list.first;
2194 while (node) {
2195 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00002196 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002197 struct io_kiocb *req;
2198
2199 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00002200 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002201 req->task_work.func(&req->task_work);
2202 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00002203
2204 if (!ctx) {
2205 ctx = this_ctx;
2206 } else if (ctx != this_ctx) {
2207 mutex_lock(&ctx->uring_lock);
2208 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2209 mutex_unlock(&ctx->uring_lock);
2210 ctx = this_ctx;
2211 }
2212 }
2213
2214 if (ctx && ctx->submit_state.comp.nr) {
2215 mutex_lock(&ctx->uring_lock);
2216 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2217 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002218 }
2219
2220 return list.first != NULL;
2221}
2222
2223static void tctx_task_work(struct callback_head *cb)
2224{
2225 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2226
2227 while (__tctx_task_work(tctx))
2228 cond_resched();
2229
2230 clear_bit(0, &tctx->task_state);
2231}
2232
2233static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2234 enum task_work_notify_mode notify)
2235{
2236 struct io_uring_task *tctx = tsk->io_uring;
2237 struct io_wq_work_node *node, *prev;
2238 int ret;
2239
2240 WARN_ON_ONCE(!tctx);
2241
2242 spin_lock(&tctx->task_lock);
2243 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
2244 spin_unlock(&tctx->task_lock);
2245
2246 /* task_work already pending, we're done */
2247 if (test_bit(0, &tctx->task_state) ||
2248 test_and_set_bit(0, &tctx->task_state))
2249 return 0;
2250
2251 if (!task_work_add(tsk, &tctx->task_work, notify))
2252 return 0;
2253
2254 /*
2255 * Slow path - we failed, find and delete work. if the work is not
2256 * in the list, it got run and we're fine.
2257 */
2258 ret = 0;
2259 spin_lock(&tctx->task_lock);
2260 wq_list_for_each(node, prev, &tctx->task_list) {
2261 if (&req->io_task_work.node == node) {
2262 wq_list_del(&tctx->task_list, node, prev);
2263 ret = 1;
2264 break;
2265 }
2266 }
2267 spin_unlock(&tctx->task_lock);
2268 clear_bit(0, &tctx->task_state);
2269 return ret;
2270}
2271
Jens Axboe355fb9e2020-10-22 20:19:35 -06002272static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002273{
2274 struct task_struct *tsk = req->task;
2275 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002276 enum task_work_notify_mode notify;
2277 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002278
Jens Axboe6200b0a2020-09-13 14:38:30 -06002279 if (tsk->flags & PF_EXITING)
2280 return -ESRCH;
2281
Jens Axboec2c4c832020-07-01 15:37:11 -06002282 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002283 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2284 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2285 * processing task_work. There's no reliable way to tell if TWA_RESUME
2286 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002287 */
Jens Axboe91989c72020-10-16 09:02:26 -06002288 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002289 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002290 notify = TWA_SIGNAL;
2291
Jens Axboe7cbf1722021-02-10 00:03:20 +00002292 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002293 if (!ret)
2294 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002295
Jens Axboec2c4c832020-07-01 15:37:11 -06002296 return ret;
2297}
2298
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002299static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002300 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002301{
2302 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2303
2304 init_task_work(&req->task_work, cb);
2305 task_work_add(tsk, &req->task_work, TWA_NONE);
2306 wake_up_process(tsk);
2307}
2308
Jens Axboec40f6372020-06-25 15:39:59 -06002309static void __io_req_task_cancel(struct io_kiocb *req, int error)
2310{
2311 struct io_ring_ctx *ctx = req->ctx;
2312
2313 spin_lock_irq(&ctx->completion_lock);
2314 io_cqring_fill_event(req, error);
2315 io_commit_cqring(ctx);
2316 spin_unlock_irq(&ctx->completion_lock);
2317
2318 io_cqring_ev_posted(ctx);
2319 req_set_fail_links(req);
2320 io_double_put_req(req);
2321}
2322
2323static void io_req_task_cancel(struct callback_head *cb)
2324{
2325 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002326 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002327
2328 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002329 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002330}
2331
2332static void __io_req_task_submit(struct io_kiocb *req)
2333{
2334 struct io_ring_ctx *ctx = req->ctx;
2335
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002336 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002337 if (!ctx->sqo_dead &&
2338 !__io_sq_thread_acquire_mm(ctx) &&
2339 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002340 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002341 else
Jens Axboec40f6372020-06-25 15:39:59 -06002342 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002343 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002344}
2345
Jens Axboec40f6372020-06-25 15:39:59 -06002346static void io_req_task_submit(struct callback_head *cb)
2347{
2348 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002349 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002350
2351 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002352 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002353}
2354
2355static void io_req_task_queue(struct io_kiocb *req)
2356{
Jens Axboec40f6372020-06-25 15:39:59 -06002357 int ret;
2358
Jens Axboe7cbf1722021-02-10 00:03:20 +00002359 req->task_work.func = io_req_task_submit;
Jens Axboe6d816e02020-08-11 08:04:14 -06002360 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002361
Jens Axboe355fb9e2020-10-22 20:19:35 -06002362 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002363 if (unlikely(ret))
2364 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002365}
2366
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002367static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002368{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002369 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002370
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002371 if (nxt)
2372 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002373}
2374
Jens Axboe9e645e112019-05-10 16:07:28 -06002375static void io_free_req(struct io_kiocb *req)
2376{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002377 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002378 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002379}
2380
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002381struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002382 struct task_struct *task;
2383 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002384 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002385};
2386
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002387static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002388{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002389 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002390 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002391 rb->task = NULL;
2392}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002393
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002394static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2395 struct req_batch *rb)
2396{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002397 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002398 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002399 rb->task = NULL;
2400 }
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002401 if (rb->ctx_refs)
2402 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002403}
2404
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002405static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2406 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002407{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002408 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002409
Jens Axboee3bc8e92020-09-24 08:45:57 -06002410 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002411 if (rb->task)
2412 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002413 rb->task = req->task;
2414 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002415 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002416 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002417 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002418
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002419 io_dismantle_req(req);
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002420 if (state->free_reqs != ARRAY_SIZE(state->reqs)) {
2421 state->reqs[state->free_reqs++] = req;
2422 } else {
Jens Axboe1b4c3512021-02-10 00:03:19 +00002423 struct io_comp_state *cs = &req->ctx->submit_state.comp;
2424
2425 list_add(&req->compl.list, &cs->free_list);
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002426 }
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002427}
2428
Pavel Begunkov905c1722021-02-10 00:03:14 +00002429static void io_submit_flush_completions(struct io_comp_state *cs,
2430 struct io_ring_ctx *ctx)
2431{
2432 int i, nr = cs->nr;
2433 struct io_kiocb *req;
2434 struct req_batch rb;
2435
2436 io_init_req_batch(&rb);
2437 spin_lock_irq(&ctx->completion_lock);
2438 for (i = 0; i < nr; i++) {
2439 req = cs->reqs[i];
2440 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2441 }
2442 io_commit_cqring(ctx);
2443 spin_unlock_irq(&ctx->completion_lock);
2444
2445 io_cqring_ev_posted(ctx);
2446 for (i = 0; i < nr; i++) {
2447 req = cs->reqs[i];
2448
2449 /* submission and completion refs */
2450 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002451 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002452 }
2453
2454 io_req_free_batch_finish(ctx, &rb);
2455 cs->nr = 0;
2456}
2457
Jens Axboeba816ad2019-09-28 11:36:45 -06002458/*
2459 * Drop reference to request, return next in chain (if there is one) if this
2460 * was the last reference to this request.
2461 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002462static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002463{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002464 struct io_kiocb *nxt = NULL;
2465
Jens Axboe2a44f462020-02-25 13:25:41 -07002466 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002467 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002468 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002469 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002470 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002471}
2472
Jens Axboe2b188cc2019-01-07 10:46:33 -07002473static void io_put_req(struct io_kiocb *req)
2474{
Jens Axboedef596e2019-01-09 08:59:42 -07002475 if (refcount_dec_and_test(&req->refs))
2476 io_free_req(req);
2477}
2478
Pavel Begunkov216578e2020-10-13 09:44:00 +01002479static void io_put_req_deferred_cb(struct callback_head *cb)
2480{
2481 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2482
2483 io_free_req(req);
2484}
2485
2486static void io_free_req_deferred(struct io_kiocb *req)
2487{
2488 int ret;
2489
Jens Axboe7cbf1722021-02-10 00:03:20 +00002490 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002491 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002492 if (unlikely(ret))
2493 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002494}
2495
2496static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2497{
2498 if (refcount_sub_and_test(refs, &req->refs))
2499 io_free_req_deferred(req);
2500}
2501
Jens Axboe978db572019-11-14 22:39:04 -07002502static void io_double_put_req(struct io_kiocb *req)
2503{
2504 /* drop both submit and complete references */
2505 if (refcount_sub_and_test(2, &req->refs))
2506 io_free_req(req);
2507}
2508
Pavel Begunkov6c503152021-01-04 20:36:36 +00002509static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002510{
2511 /* See comment at the top of this file */
2512 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002513 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002514}
2515
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002516static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2517{
2518 struct io_rings *rings = ctx->rings;
2519
2520 /* make sure SQ entry isn't read before tail */
2521 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2522}
2523
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002524static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002525{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002526 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002527
Jens Axboebcda7ba2020-02-23 16:42:51 -07002528 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2529 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002530 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002531 kfree(kbuf);
2532 return cflags;
2533}
2534
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002535static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2536{
2537 struct io_buffer *kbuf;
2538
2539 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2540 return io_put_kbuf(req, kbuf);
2541}
2542
Jens Axboe4c6e2772020-07-01 11:29:10 -06002543static inline bool io_run_task_work(void)
2544{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002545 /*
2546 * Not safe to run on exiting task, and the task_work handling will
2547 * not add work to such a task.
2548 */
2549 if (unlikely(current->flags & PF_EXITING))
2550 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002551 if (current->task_works) {
2552 __set_current_state(TASK_RUNNING);
2553 task_work_run();
2554 return true;
2555 }
2556
2557 return false;
2558}
2559
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002560static void io_iopoll_queue(struct list_head *again)
2561{
2562 struct io_kiocb *req;
2563
2564 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002565 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2566 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002567 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002568 } while (!list_empty(again));
2569}
2570
Jens Axboedef596e2019-01-09 08:59:42 -07002571/*
2572 * Find and free completed poll iocbs
2573 */
2574static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2575 struct list_head *done)
2576{
Jens Axboe8237e042019-12-28 10:48:22 -07002577 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002578 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002579 LIST_HEAD(again);
2580
2581 /* order with ->result store in io_complete_rw_iopoll() */
2582 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002583
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002584 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002585 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002586 int cflags = 0;
2587
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002588 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002589 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002590 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002591 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002592 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002593 continue;
2594 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002595 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002596
Jens Axboebcda7ba2020-02-23 16:42:51 -07002597 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002598 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002599
2600 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002601 (*nr_events)++;
2602
Pavel Begunkovc3524382020-06-28 12:52:32 +03002603 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002604 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002605 }
Jens Axboedef596e2019-01-09 08:59:42 -07002606
Jens Axboe09bb8392019-03-13 12:39:28 -06002607 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002608 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002609 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002610
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002611 if (!list_empty(&again))
2612 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002613}
2614
Jens Axboedef596e2019-01-09 08:59:42 -07002615static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2616 long min)
2617{
2618 struct io_kiocb *req, *tmp;
2619 LIST_HEAD(done);
2620 bool spin;
2621 int ret;
2622
2623 /*
2624 * Only spin for completions if we don't have multiple devices hanging
2625 * off our complete list, and we're under the requested amount.
2626 */
2627 spin = !ctx->poll_multi_file && *nr_events < min;
2628
2629 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002630 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002631 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002632
2633 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002634 * Move completed and retryable entries to our local lists.
2635 * If we find a request that requires polling, break out
2636 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002637 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002638 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002639 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002640 continue;
2641 }
2642 if (!list_empty(&done))
2643 break;
2644
2645 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2646 if (ret < 0)
2647 break;
2648
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002649 /* iopoll may have completed current req */
2650 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002651 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002652
Jens Axboedef596e2019-01-09 08:59:42 -07002653 if (ret && spin)
2654 spin = false;
2655 ret = 0;
2656 }
2657
2658 if (!list_empty(&done))
2659 io_iopoll_complete(ctx, nr_events, &done);
2660
2661 return ret;
2662}
2663
2664/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002665 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002666 * non-spinning poll check - we'll still enter the driver poll loop, but only
2667 * as a non-spinning completion check.
2668 */
2669static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2670 long min)
2671{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002672 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002673 int ret;
2674
2675 ret = io_do_iopoll(ctx, nr_events, min);
2676 if (ret < 0)
2677 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002678 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002679 return 0;
2680 }
2681
2682 return 1;
2683}
2684
2685/*
2686 * We can't just wait for polled events to come to us, we have to actively
2687 * find and complete them.
2688 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002689static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002690{
2691 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2692 return;
2693
2694 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002695 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002696 unsigned int nr_events = 0;
2697
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002698 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002699
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002700 /* let it sleep and repeat later if can't complete a request */
2701 if (nr_events == 0)
2702 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002703 /*
2704 * Ensure we allow local-to-the-cpu processing to take place,
2705 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002706 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002707 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002708 if (need_resched()) {
2709 mutex_unlock(&ctx->uring_lock);
2710 cond_resched();
2711 mutex_lock(&ctx->uring_lock);
2712 }
Jens Axboedef596e2019-01-09 08:59:42 -07002713 }
2714 mutex_unlock(&ctx->uring_lock);
2715}
2716
Pavel Begunkov7668b922020-07-07 16:36:21 +03002717static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002718{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002719 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002720 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002721
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002722 /*
2723 * We disallow the app entering submit/complete with polling, but we
2724 * still need to lock the ring to prevent racing with polled issue
2725 * that got punted to a workqueue.
2726 */
2727 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002728 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002729 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002730 * Don't enter poll loop if we already have events pending.
2731 * If we do, we can potentially be spinning for commands that
2732 * already triggered a CQE (eg in error).
2733 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002734 if (test_bit(0, &ctx->cq_check_overflow))
2735 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2736 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002737 break;
2738
2739 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002740 * If a submit got punted to a workqueue, we can have the
2741 * application entering polling for a command before it gets
2742 * issued. That app will hold the uring_lock for the duration
2743 * of the poll right here, so we need to take a breather every
2744 * now and then to ensure that the issue has a chance to add
2745 * the poll to the issued list. Otherwise we can spin here
2746 * forever, while the workqueue is stuck trying to acquire the
2747 * very same mutex.
2748 */
2749 if (!(++iters & 7)) {
2750 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002751 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002752 mutex_lock(&ctx->uring_lock);
2753 }
2754
Pavel Begunkov7668b922020-07-07 16:36:21 +03002755 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002756 if (ret <= 0)
2757 break;
2758 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002759 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002760
Jens Axboe500f9fb2019-08-19 12:15:59 -06002761 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002762 return ret;
2763}
2764
Jens Axboe491381ce2019-10-17 09:20:46 -06002765static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002766{
Jens Axboe491381ce2019-10-17 09:20:46 -06002767 /*
2768 * Tell lockdep we inherited freeze protection from submission
2769 * thread.
2770 */
2771 if (req->flags & REQ_F_ISREG) {
2772 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002773
Jens Axboe491381ce2019-10-17 09:20:46 -06002774 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002775 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002776 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002777}
2778
Jens Axboea1d7c392020-06-22 11:09:46 -06002779static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002780 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002781{
Jens Axboe9adbd452019-12-20 08:45:55 -07002782 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002783 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784
Jens Axboe491381ce2019-10-17 09:20:46 -06002785 if (kiocb->ki_flags & IOCB_WRITE)
2786 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002787
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002788 if (res != req->result)
2789 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002790 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002791 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002792 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002793}
2794
Jens Axboeb63534c2020-06-04 11:28:00 -06002795#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002796static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002797{
2798 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002799 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002800 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002801
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002802 /* already prepared */
2803 if (req->async_data)
2804 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002805
2806 switch (req->opcode) {
2807 case IORING_OP_READV:
2808 case IORING_OP_READ_FIXED:
2809 case IORING_OP_READ:
2810 rw = READ;
2811 break;
2812 case IORING_OP_WRITEV:
2813 case IORING_OP_WRITE_FIXED:
2814 case IORING_OP_WRITE:
2815 rw = WRITE;
2816 break;
2817 default:
2818 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2819 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002820 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002821 }
2822
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002823 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2824 if (ret < 0)
2825 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002826 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002827}
Jens Axboeb63534c2020-06-04 11:28:00 -06002828#endif
2829
2830static bool io_rw_reissue(struct io_kiocb *req, long res)
2831{
2832#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002833 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002834 int ret;
2835
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002836 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002837 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002838 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002839 if (!S_ISBLK(mode) && !S_ISREG(mode))
2840 return false;
2841 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002842 return false;
2843
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002844 lockdep_assert_held(&req->ctx->uring_lock);
2845
Jens Axboe28cea78a2020-09-14 10:51:17 -06002846 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002847
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002848 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002849 refcount_inc(&req->refs);
2850 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002851 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002852 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002853 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002854#endif
2855 return false;
2856}
2857
Jens Axboea1d7c392020-06-22 11:09:46 -06002858static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002859 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002860{
2861 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002862 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002863}
2864
2865static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2866{
Jens Axboe9adbd452019-12-20 08:45:55 -07002867 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002868
Pavel Begunkov889fca72021-02-10 00:03:09 +00002869 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870}
2871
Jens Axboedef596e2019-01-09 08:59:42 -07002872static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2873{
Jens Axboe9adbd452019-12-20 08:45:55 -07002874 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002875
Jens Axboe491381ce2019-10-17 09:20:46 -06002876 if (kiocb->ki_flags & IOCB_WRITE)
2877 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002878
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002879 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002880 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002881
2882 WRITE_ONCE(req->result, res);
2883 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002884 smp_wmb();
2885 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002886}
2887
2888/*
2889 * After the iocb has been issued, it's safe to be found on the poll list.
2890 * Adding the kiocb to the list AFTER submission ensures that we don't
2891 * find it from a io_iopoll_getevents() thread before the issuer is done
2892 * accessing the kiocb cookie.
2893 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002894static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002895{
2896 struct io_ring_ctx *ctx = req->ctx;
2897
2898 /*
2899 * Track whether we have multiple files in our lists. This will impact
2900 * how we do polling eventually, not spinning if we're on potentially
2901 * different devices.
2902 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002903 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002904 ctx->poll_multi_file = false;
2905 } else if (!ctx->poll_multi_file) {
2906 struct io_kiocb *list_req;
2907
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002908 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002909 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002910 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002911 ctx->poll_multi_file = true;
2912 }
2913
2914 /*
2915 * For fast devices, IO may have already completed. If it has, add
2916 * it to the front so we find it first.
2917 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002918 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002919 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002920 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002921 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002922
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002923 /*
2924 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2925 * task context or in io worker task context. If current task context is
2926 * sq thread, we don't need to check whether should wake up sq thread.
2927 */
2928 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002929 wq_has_sleeper(&ctx->sq_data->wait))
2930 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002931}
2932
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002933static inline void io_state_file_put(struct io_submit_state *state)
2934{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002935 if (state->file_refs) {
2936 fput_many(state->file, state->file_refs);
2937 state->file_refs = 0;
2938 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002939}
2940
2941/*
2942 * Get as many references to a file as we have IOs left in this submission,
2943 * assuming most submissions are for one file, or at least that each file
2944 * has more than one submission.
2945 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002946static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002947{
2948 if (!state)
2949 return fget(fd);
2950
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002951 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002952 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002953 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002954 return state->file;
2955 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002956 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002957 }
2958 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002959 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002960 return NULL;
2961
2962 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002963 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002964 return state->file;
2965}
2966
Jens Axboe4503b762020-06-01 10:00:27 -06002967static bool io_bdev_nowait(struct block_device *bdev)
2968{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002969 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002970}
2971
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972/*
2973 * If we tracked the file through the SCM inflight mechanism, we could support
2974 * any file. For now, just ensure that anything potentially problematic is done
2975 * inline.
2976 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002977static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978{
2979 umode_t mode = file_inode(file)->i_mode;
2980
Jens Axboe4503b762020-06-01 10:00:27 -06002981 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002982 if (IS_ENABLED(CONFIG_BLOCK) &&
2983 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002984 return true;
2985 return false;
2986 }
2987 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002988 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002989 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002990 if (IS_ENABLED(CONFIG_BLOCK) &&
2991 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002992 file->f_op != &io_uring_fops)
2993 return true;
2994 return false;
2995 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002996
Jens Axboec5b85622020-06-09 19:23:05 -06002997 /* any ->read/write should understand O_NONBLOCK */
2998 if (file->f_flags & O_NONBLOCK)
2999 return true;
3000
Jens Axboeaf197f52020-04-28 13:15:06 -06003001 if (!(file->f_mode & FMODE_NOWAIT))
3002 return false;
3003
3004 if (rw == READ)
3005 return file->f_op->read_iter != NULL;
3006
3007 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003008}
3009
Pavel Begunkova88fc402020-09-30 22:57:53 +03003010static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003011{
Jens Axboedef596e2019-01-09 08:59:42 -07003012 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07003013 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003014 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06003015 unsigned ioprio;
3016 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003017
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003018 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06003019 req->flags |= REQ_F_ISREG;
3020
Jens Axboe2b188cc2019-01-07 10:46:33 -07003021 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003022 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07003023 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003024 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07003025 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003027 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
3028 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3029 if (unlikely(ret))
3030 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003031
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003032 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
3033 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
3034 req->flags |= REQ_F_NOWAIT;
3035
Jens Axboe2b188cc2019-01-07 10:46:33 -07003036 ioprio = READ_ONCE(sqe->ioprio);
3037 if (ioprio) {
3038 ret = ioprio_check_cap(ioprio);
3039 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06003040 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003041
3042 kiocb->ki_ioprio = ioprio;
3043 } else
3044 kiocb->ki_ioprio = get_current_ioprio();
3045
Jens Axboedef596e2019-01-09 08:59:42 -07003046 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003047 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
3048 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003049 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003050
Jens Axboedef596e2019-01-09 08:59:42 -07003051 kiocb->ki_flags |= IOCB_HIPRI;
3052 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003053 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003054 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003055 if (kiocb->ki_flags & IOCB_HIPRI)
3056 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003057 kiocb->ki_complete = io_complete_rw;
3058 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003059
Jens Axboe3529d8c2019-12-19 18:24:38 -07003060 req->rw.addr = READ_ONCE(sqe->addr);
3061 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003062 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003063 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003064}
3065
3066static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3067{
3068 switch (ret) {
3069 case -EIOCBQUEUED:
3070 break;
3071 case -ERESTARTSYS:
3072 case -ERESTARTNOINTR:
3073 case -ERESTARTNOHAND:
3074 case -ERESTART_RESTARTBLOCK:
3075 /*
3076 * We can't just restart the syscall, since previously
3077 * submitted sqes may already be in progress. Just fail this
3078 * IO with EINTR.
3079 */
3080 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003081 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003082 default:
3083 kiocb->ki_complete(kiocb, ret, 0);
3084 }
3085}
3086
Jens Axboea1d7c392020-06-22 11:09:46 -06003087static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003088 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003089{
Jens Axboeba042912019-12-25 16:33:42 -07003090 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07003091 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003092
Jens Axboe227c0c92020-08-13 11:51:40 -06003093 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003094 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003095 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003096 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003097 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003098 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003099 }
3100
Jens Axboeba042912019-12-25 16:33:42 -07003101 if (req->flags & REQ_F_CUR_POS)
3102 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003103 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00003104 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003105 else
3106 io_rw_done(kiocb, ret);
3107}
3108
Pavel Begunkov847595d2021-02-04 13:52:06 +00003109static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003110{
Jens Axboe9adbd452019-12-20 08:45:55 -07003111 struct io_ring_ctx *ctx = req->ctx;
3112 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003113 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003114 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003115 size_t offset;
3116 u64 buf_addr;
3117
Jens Axboeedafcce2019-01-09 09:16:05 -07003118 if (unlikely(buf_index >= ctx->nr_user_bufs))
3119 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003120 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3121 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003122 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003123
3124 /* overflow */
3125 if (buf_addr + len < buf_addr)
3126 return -EFAULT;
3127 /* not inside the mapped region */
3128 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3129 return -EFAULT;
3130
3131 /*
3132 * May not be a start of buffer, set size appropriately
3133 * and advance us to the beginning.
3134 */
3135 offset = buf_addr - imu->ubuf;
3136 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003137
3138 if (offset) {
3139 /*
3140 * Don't use iov_iter_advance() here, as it's really slow for
3141 * using the latter parts of a big fixed buffer - it iterates
3142 * over each segment manually. We can cheat a bit here, because
3143 * we know that:
3144 *
3145 * 1) it's a BVEC iter, we set it up
3146 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3147 * first and last bvec
3148 *
3149 * So just find our index, and adjust the iterator afterwards.
3150 * If the offset is within the first bvec (or the whole first
3151 * bvec, just use iov_iter_advance(). This makes it easier
3152 * since we can just skip the first segment, which may not
3153 * be PAGE_SIZE aligned.
3154 */
3155 const struct bio_vec *bvec = imu->bvec;
3156
3157 if (offset <= bvec->bv_len) {
3158 iov_iter_advance(iter, offset);
3159 } else {
3160 unsigned long seg_skip;
3161
3162 /* skip first vec */
3163 offset -= bvec->bv_len;
3164 seg_skip = 1 + (offset >> PAGE_SHIFT);
3165
3166 iter->bvec = bvec + seg_skip;
3167 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003168 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003169 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003170 }
3171 }
3172
Pavel Begunkov847595d2021-02-04 13:52:06 +00003173 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003174}
3175
Jens Axboebcda7ba2020-02-23 16:42:51 -07003176static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3177{
3178 if (needs_lock)
3179 mutex_unlock(&ctx->uring_lock);
3180}
3181
3182static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3183{
3184 /*
3185 * "Normal" inline submissions always hold the uring_lock, since we
3186 * grab it from the system call. Same is true for the SQPOLL offload.
3187 * The only exception is when we've detached the request and issue it
3188 * from an async worker thread, grab the lock for that case.
3189 */
3190 if (needs_lock)
3191 mutex_lock(&ctx->uring_lock);
3192}
3193
3194static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3195 int bgid, struct io_buffer *kbuf,
3196 bool needs_lock)
3197{
3198 struct io_buffer *head;
3199
3200 if (req->flags & REQ_F_BUFFER_SELECTED)
3201 return kbuf;
3202
3203 io_ring_submit_lock(req->ctx, needs_lock);
3204
3205 lockdep_assert_held(&req->ctx->uring_lock);
3206
3207 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3208 if (head) {
3209 if (!list_empty(&head->list)) {
3210 kbuf = list_last_entry(&head->list, struct io_buffer,
3211 list);
3212 list_del(&kbuf->list);
3213 } else {
3214 kbuf = head;
3215 idr_remove(&req->ctx->io_buffer_idr, bgid);
3216 }
3217 if (*len > kbuf->len)
3218 *len = kbuf->len;
3219 } else {
3220 kbuf = ERR_PTR(-ENOBUFS);
3221 }
3222
3223 io_ring_submit_unlock(req->ctx, needs_lock);
3224
3225 return kbuf;
3226}
3227
Jens Axboe4d954c22020-02-27 07:31:19 -07003228static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3229 bool needs_lock)
3230{
3231 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003232 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003233
3234 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003235 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003236 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3237 if (IS_ERR(kbuf))
3238 return kbuf;
3239 req->rw.addr = (u64) (unsigned long) kbuf;
3240 req->flags |= REQ_F_BUFFER_SELECTED;
3241 return u64_to_user_ptr(kbuf->addr);
3242}
3243
3244#ifdef CONFIG_COMPAT
3245static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3246 bool needs_lock)
3247{
3248 struct compat_iovec __user *uiov;
3249 compat_ssize_t clen;
3250 void __user *buf;
3251 ssize_t len;
3252
3253 uiov = u64_to_user_ptr(req->rw.addr);
3254 if (!access_ok(uiov, sizeof(*uiov)))
3255 return -EFAULT;
3256 if (__get_user(clen, &uiov->iov_len))
3257 return -EFAULT;
3258 if (clen < 0)
3259 return -EINVAL;
3260
3261 len = clen;
3262 buf = io_rw_buffer_select(req, &len, needs_lock);
3263 if (IS_ERR(buf))
3264 return PTR_ERR(buf);
3265 iov[0].iov_base = buf;
3266 iov[0].iov_len = (compat_size_t) len;
3267 return 0;
3268}
3269#endif
3270
3271static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3272 bool needs_lock)
3273{
3274 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3275 void __user *buf;
3276 ssize_t len;
3277
3278 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3279 return -EFAULT;
3280
3281 len = iov[0].iov_len;
3282 if (len < 0)
3283 return -EINVAL;
3284 buf = io_rw_buffer_select(req, &len, needs_lock);
3285 if (IS_ERR(buf))
3286 return PTR_ERR(buf);
3287 iov[0].iov_base = buf;
3288 iov[0].iov_len = len;
3289 return 0;
3290}
3291
3292static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3293 bool needs_lock)
3294{
Jens Axboedddb3e22020-06-04 11:27:01 -06003295 if (req->flags & REQ_F_BUFFER_SELECTED) {
3296 struct io_buffer *kbuf;
3297
3298 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3299 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3300 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003301 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003302 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003303 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003304 return -EINVAL;
3305
3306#ifdef CONFIG_COMPAT
3307 if (req->ctx->compat)
3308 return io_compat_import(req, iov, needs_lock);
3309#endif
3310
3311 return __io_iov_buffer_select(req, iov, needs_lock);
3312}
3313
Pavel Begunkov847595d2021-02-04 13:52:06 +00003314static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3315 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003316{
Jens Axboe9adbd452019-12-20 08:45:55 -07003317 void __user *buf = u64_to_user_ptr(req->rw.addr);
3318 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003319 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003320 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003321
Pavel Begunkov7d009162019-11-25 23:14:40 +03003322 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003323 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003324 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003325 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003326
Jens Axboebcda7ba2020-02-23 16:42:51 -07003327 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003328 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003329 return -EINVAL;
3330
Jens Axboe3a6820f2019-12-22 15:19:35 -07003331 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003332 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003333 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003334 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003335 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003336 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003337 }
3338
Jens Axboe3a6820f2019-12-22 15:19:35 -07003339 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3340 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003341 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003342 }
3343
Jens Axboe4d954c22020-02-27 07:31:19 -07003344 if (req->flags & REQ_F_BUFFER_SELECT) {
3345 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003346 if (!ret)
3347 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003348 *iovec = NULL;
3349 return ret;
3350 }
3351
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003352 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3353 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003354}
3355
Jens Axboe0fef9482020-08-26 10:36:20 -06003356static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3357{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003358 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003359}
3360
Jens Axboe32960612019-09-23 11:05:34 -06003361/*
3362 * For files that don't have ->read_iter() and ->write_iter(), handle them
3363 * by looping over ->read() or ->write() manually.
3364 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003365static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003366{
Jens Axboe4017eb92020-10-22 14:14:12 -06003367 struct kiocb *kiocb = &req->rw.kiocb;
3368 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003369 ssize_t ret = 0;
3370
3371 /*
3372 * Don't support polled IO through this interface, and we can't
3373 * support non-blocking either. For the latter, this just causes
3374 * the kiocb to be handled from an async context.
3375 */
3376 if (kiocb->ki_flags & IOCB_HIPRI)
3377 return -EOPNOTSUPP;
3378 if (kiocb->ki_flags & IOCB_NOWAIT)
3379 return -EAGAIN;
3380
3381 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003382 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003383 ssize_t nr;
3384
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003385 if (!iov_iter_is_bvec(iter)) {
3386 iovec = iov_iter_iovec(iter);
3387 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003388 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3389 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003390 }
3391
Jens Axboe32960612019-09-23 11:05:34 -06003392 if (rw == READ) {
3393 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003394 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003395 } else {
3396 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003397 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003398 }
3399
3400 if (nr < 0) {
3401 if (!ret)
3402 ret = nr;
3403 break;
3404 }
3405 ret += nr;
3406 if (nr != iovec.iov_len)
3407 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003408 req->rw.len -= nr;
3409 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003410 iov_iter_advance(iter, nr);
3411 }
3412
3413 return ret;
3414}
3415
Jens Axboeff6165b2020-08-13 09:47:43 -06003416static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3417 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003418{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003419 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003420
Jens Axboeff6165b2020-08-13 09:47:43 -06003421 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003422 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003423 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003424 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003425 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003426 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003427 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003428 unsigned iov_off = 0;
3429
3430 rw->iter.iov = rw->fast_iov;
3431 if (iter->iov != fast_iov) {
3432 iov_off = iter->iov - fast_iov;
3433 rw->iter.iov += iov_off;
3434 }
3435 if (rw->fast_iov != fast_iov)
3436 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003437 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003438 } else {
3439 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003440 }
3441}
3442
Jens Axboee8c2bc12020-08-15 18:44:09 -07003443static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003444{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003445 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3446 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3447 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003448}
3449
Jens Axboee8c2bc12020-08-15 18:44:09 -07003450static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003451{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003452 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003453 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003454
Jens Axboee8c2bc12020-08-15 18:44:09 -07003455 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003456}
3457
Jens Axboeff6165b2020-08-13 09:47:43 -06003458static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3459 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003460 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003461{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003462 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003463 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003464 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003465 if (__io_alloc_async_data(req)) {
3466 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003467 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003468 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003469
Jens Axboeff6165b2020-08-13 09:47:43 -06003470 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003471 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003472 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003473}
3474
Pavel Begunkov73debe62020-09-30 22:57:54 +03003475static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003476{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003477 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003478 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003479 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003480
Pavel Begunkov2846c482020-11-07 13:16:27 +00003481 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003482 if (unlikely(ret < 0))
3483 return ret;
3484
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003485 iorw->bytes_done = 0;
3486 iorw->free_iovec = iov;
3487 if (iov)
3488 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003489 return 0;
3490}
3491
Pavel Begunkov73debe62020-09-30 22:57:54 +03003492static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003493{
3494 ssize_t ret;
3495
Pavel Begunkova88fc402020-09-30 22:57:53 +03003496 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003497 if (ret)
3498 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003499
Jens Axboe3529d8c2019-12-19 18:24:38 -07003500 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3501 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003502
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003503 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003504 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003505 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003506 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003507}
3508
Jens Axboec1dd91d2020-08-03 16:43:59 -06003509/*
3510 * This is our waitqueue callback handler, registered through lock_page_async()
3511 * when we initially tried to do the IO with the iocb armed our waitqueue.
3512 * This gets called when the page is unlocked, and we generally expect that to
3513 * happen when the page IO is completed and the page is now uptodate. This will
3514 * queue a task_work based retry of the operation, attempting to copy the data
3515 * again. If the latter fails because the page was NOT uptodate, then we will
3516 * do a thread based blocking retry of the operation. That's the unexpected
3517 * slow path.
3518 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003519static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3520 int sync, void *arg)
3521{
3522 struct wait_page_queue *wpq;
3523 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003524 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003525 int ret;
3526
3527 wpq = container_of(wait, struct wait_page_queue, wait);
3528
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003529 if (!wake_page_match(wpq, key))
3530 return 0;
3531
Hao Xuc8d317a2020-09-29 20:00:45 +08003532 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003533 list_del_init(&wait->entry);
3534
Jens Axboe7cbf1722021-02-10 00:03:20 +00003535 req->task_work.func = io_req_task_submit;
Jens Axboe6d816e02020-08-11 08:04:14 -06003536 percpu_ref_get(&req->ctx->refs);
3537
Jens Axboebcf5a062020-05-22 09:24:42 -06003538 /* submit ref gets dropped, acquire a new one */
3539 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003540 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003541 if (unlikely(ret))
3542 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003543 return 1;
3544}
3545
Jens Axboec1dd91d2020-08-03 16:43:59 -06003546/*
3547 * This controls whether a given IO request should be armed for async page
3548 * based retry. If we return false here, the request is handed to the async
3549 * worker threads for retry. If we're doing buffered reads on a regular file,
3550 * we prepare a private wait_page_queue entry and retry the operation. This
3551 * will either succeed because the page is now uptodate and unlocked, or it
3552 * will register a callback when the page is unlocked at IO completion. Through
3553 * that callback, io_uring uses task_work to setup a retry of the operation.
3554 * That retry will attempt the buffered read again. The retry will generally
3555 * succeed, or in rare cases where it fails, we then fall back to using the
3556 * async worker threads for a blocking retry.
3557 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003558static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003559{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003560 struct io_async_rw *rw = req->async_data;
3561 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003562 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003563
3564 /* never retry for NOWAIT, we just complete with -EAGAIN */
3565 if (req->flags & REQ_F_NOWAIT)
3566 return false;
3567
Jens Axboe227c0c92020-08-13 11:51:40 -06003568 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003569 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003570 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003571
Jens Axboebcf5a062020-05-22 09:24:42 -06003572 /*
3573 * just use poll if we can, and don't attempt if the fs doesn't
3574 * support callback based unlocks
3575 */
3576 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3577 return false;
3578
Jens Axboe3b2a4432020-08-16 10:58:43 -07003579 wait->wait.func = io_async_buf_func;
3580 wait->wait.private = req;
3581 wait->wait.flags = 0;
3582 INIT_LIST_HEAD(&wait->wait.entry);
3583 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003584 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003585 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003586 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003587}
3588
3589static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3590{
3591 if (req->file->f_op->read_iter)
3592 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003593 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003594 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003595 else
3596 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003597}
3598
Pavel Begunkov889fca72021-02-10 00:03:09 +00003599static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003600{
3601 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003602 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003603 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003604 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003605 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003606 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003607
Pavel Begunkov2846c482020-11-07 13:16:27 +00003608 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003609 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003610 iovec = NULL;
3611 } else {
3612 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3613 if (ret < 0)
3614 return ret;
3615 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003616 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003617 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003618
Jens Axboefd6c2e42019-12-18 12:19:41 -07003619 /* Ensure we clear previously set non-block flag */
3620 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003621 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003622 else
3623 kiocb->ki_flags |= IOCB_NOWAIT;
3624
Pavel Begunkov24c74672020-06-21 13:09:51 +03003625 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003626 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3627 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003628 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003629 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003630
Pavel Begunkov632546c2020-11-07 13:16:26 +00003631 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003632 if (unlikely(ret)) {
3633 kfree(iovec);
3634 return ret;
3635 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003636
Jens Axboe227c0c92020-08-13 11:51:40 -06003637 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003638
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003639 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003640 /* it's faster to check here then delegate to kfree */
3641 if (iovec)
3642 kfree(iovec);
3643 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003644 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003645 /* IOPOLL retry should happen for io-wq threads */
3646 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003647 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003648 /* no retry on NONBLOCK nor RWF_NOWAIT */
3649 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003650 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003651 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003652 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003653 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003654 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003655 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003656 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003657 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003658 }
3659
Jens Axboe227c0c92020-08-13 11:51:40 -06003660 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003661 if (ret2)
3662 return ret2;
3663
Jens Axboee8c2bc12020-08-15 18:44:09 -07003664 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003665 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003666 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003667
Pavel Begunkovb23df912021-02-04 13:52:04 +00003668 do {
3669 io_size -= ret;
3670 rw->bytes_done += ret;
3671 /* if we can retry, do so with the callbacks armed */
3672 if (!io_rw_should_retry(req)) {
3673 kiocb->ki_flags &= ~IOCB_WAITQ;
3674 return -EAGAIN;
3675 }
3676
3677 /*
3678 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3679 * we get -EIOCBQUEUED, then we'll get a notification when the
3680 * desired page gets unlocked. We can also get a partial read
3681 * here, and if we do, then just retry at the new offset.
3682 */
3683 ret = io_iter_do_read(req, iter);
3684 if (ret == -EIOCBQUEUED)
3685 return 0;
3686 /* we got some bytes, but not all. retry. */
3687 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003688done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003689 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003690 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003691}
3692
Pavel Begunkov73debe62020-09-30 22:57:54 +03003693static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003694{
3695 ssize_t ret;
3696
Pavel Begunkova88fc402020-09-30 22:57:53 +03003697 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003698 if (ret)
3699 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003700
Jens Axboe3529d8c2019-12-19 18:24:38 -07003701 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3702 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003703
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003704 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003705 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003706 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003707 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003708}
3709
Pavel Begunkov889fca72021-02-10 00:03:09 +00003710static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003711{
3712 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003713 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003714 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003715 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003716 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003717 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003718
Pavel Begunkov2846c482020-11-07 13:16:27 +00003719 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003720 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003721 iovec = NULL;
3722 } else {
3723 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3724 if (ret < 0)
3725 return ret;
3726 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003727 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003728 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003729
Jens Axboefd6c2e42019-12-18 12:19:41 -07003730 /* Ensure we clear previously set non-block flag */
3731 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003732 kiocb->ki_flags &= ~IOCB_NOWAIT;
3733 else
3734 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003735
Pavel Begunkov24c74672020-06-21 13:09:51 +03003736 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003737 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003738 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003739
Jens Axboe10d59342019-12-09 20:16:22 -07003740 /* file path doesn't support NOWAIT for non-direct_IO */
3741 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3742 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003743 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003744
Pavel Begunkov632546c2020-11-07 13:16:26 +00003745 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003746 if (unlikely(ret))
3747 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003748
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003749 /*
3750 * Open-code file_start_write here to grab freeze protection,
3751 * which will be released by another thread in
3752 * io_complete_rw(). Fool lockdep by telling it the lock got
3753 * released so that it doesn't complain about the held lock when
3754 * we return to userspace.
3755 */
3756 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003757 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003758 __sb_writers_release(file_inode(req->file)->i_sb,
3759 SB_FREEZE_WRITE);
3760 }
3761 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003762
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003763 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003764 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003765 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003766 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003767 else
3768 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003769
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003770 /*
3771 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3772 * retry them without IOCB_NOWAIT.
3773 */
3774 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3775 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003776 /* no retry on NONBLOCK nor RWF_NOWAIT */
3777 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003778 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003779 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003780 /* IOPOLL retry should happen for io-wq threads */
3781 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3782 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003783done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003784 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003785 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003786copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003787 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003788 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003789 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003790 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003791 }
Jens Axboe31b51512019-01-18 22:56:34 -07003792out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003793 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003794 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003795 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003796 return ret;
3797}
3798
Jens Axboe80a261f2020-09-28 14:23:58 -06003799static int io_renameat_prep(struct io_kiocb *req,
3800 const struct io_uring_sqe *sqe)
3801{
3802 struct io_rename *ren = &req->rename;
3803 const char __user *oldf, *newf;
3804
3805 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3806 return -EBADF;
3807
3808 ren->old_dfd = READ_ONCE(sqe->fd);
3809 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3810 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3811 ren->new_dfd = READ_ONCE(sqe->len);
3812 ren->flags = READ_ONCE(sqe->rename_flags);
3813
3814 ren->oldpath = getname(oldf);
3815 if (IS_ERR(ren->oldpath))
3816 return PTR_ERR(ren->oldpath);
3817
3818 ren->newpath = getname(newf);
3819 if (IS_ERR(ren->newpath)) {
3820 putname(ren->oldpath);
3821 return PTR_ERR(ren->newpath);
3822 }
3823
3824 req->flags |= REQ_F_NEED_CLEANUP;
3825 return 0;
3826}
3827
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003828static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003829{
3830 struct io_rename *ren = &req->rename;
3831 int ret;
3832
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003833 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003834 return -EAGAIN;
3835
3836 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3837 ren->newpath, ren->flags);
3838
3839 req->flags &= ~REQ_F_NEED_CLEANUP;
3840 if (ret < 0)
3841 req_set_fail_links(req);
3842 io_req_complete(req, ret);
3843 return 0;
3844}
3845
Jens Axboe14a11432020-09-28 14:27:37 -06003846static int io_unlinkat_prep(struct io_kiocb *req,
3847 const struct io_uring_sqe *sqe)
3848{
3849 struct io_unlink *un = &req->unlink;
3850 const char __user *fname;
3851
3852 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3853 return -EBADF;
3854
3855 un->dfd = READ_ONCE(sqe->fd);
3856
3857 un->flags = READ_ONCE(sqe->unlink_flags);
3858 if (un->flags & ~AT_REMOVEDIR)
3859 return -EINVAL;
3860
3861 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3862 un->filename = getname(fname);
3863 if (IS_ERR(un->filename))
3864 return PTR_ERR(un->filename);
3865
3866 req->flags |= REQ_F_NEED_CLEANUP;
3867 return 0;
3868}
3869
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003870static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003871{
3872 struct io_unlink *un = &req->unlink;
3873 int ret;
3874
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003875 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003876 return -EAGAIN;
3877
3878 if (un->flags & AT_REMOVEDIR)
3879 ret = do_rmdir(un->dfd, un->filename);
3880 else
3881 ret = do_unlinkat(un->dfd, un->filename);
3882
3883 req->flags &= ~REQ_F_NEED_CLEANUP;
3884 if (ret < 0)
3885 req_set_fail_links(req);
3886 io_req_complete(req, ret);
3887 return 0;
3888}
3889
Jens Axboe36f4fa62020-09-05 11:14:22 -06003890static int io_shutdown_prep(struct io_kiocb *req,
3891 const struct io_uring_sqe *sqe)
3892{
3893#if defined(CONFIG_NET)
3894 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3895 return -EINVAL;
3896 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3897 sqe->buf_index)
3898 return -EINVAL;
3899
3900 req->shutdown.how = READ_ONCE(sqe->len);
3901 return 0;
3902#else
3903 return -EOPNOTSUPP;
3904#endif
3905}
3906
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003907static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003908{
3909#if defined(CONFIG_NET)
3910 struct socket *sock;
3911 int ret;
3912
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003913 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003914 return -EAGAIN;
3915
Linus Torvalds48aba792020-12-16 12:44:05 -08003916 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003917 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003918 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003919
3920 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003921 if (ret < 0)
3922 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003923 io_req_complete(req, ret);
3924 return 0;
3925#else
3926 return -EOPNOTSUPP;
3927#endif
3928}
3929
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003930static int __io_splice_prep(struct io_kiocb *req,
3931 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003932{
3933 struct io_splice* sp = &req->splice;
3934 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003935
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003936 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3937 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003938
3939 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003940 sp->len = READ_ONCE(sqe->len);
3941 sp->flags = READ_ONCE(sqe->splice_flags);
3942
3943 if (unlikely(sp->flags & ~valid_flags))
3944 return -EINVAL;
3945
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003946 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3947 (sp->flags & SPLICE_F_FD_IN_FIXED));
3948 if (!sp->file_in)
3949 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003950 req->flags |= REQ_F_NEED_CLEANUP;
3951
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003952 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3953 /*
3954 * Splice operation will be punted aync, and here need to
3955 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3956 */
3957 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003958 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003959 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003960
3961 return 0;
3962}
3963
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003964static int io_tee_prep(struct io_kiocb *req,
3965 const struct io_uring_sqe *sqe)
3966{
3967 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3968 return -EINVAL;
3969 return __io_splice_prep(req, sqe);
3970}
3971
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003972static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003973{
3974 struct io_splice *sp = &req->splice;
3975 struct file *in = sp->file_in;
3976 struct file *out = sp->file_out;
3977 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3978 long ret = 0;
3979
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003980 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003981 return -EAGAIN;
3982 if (sp->len)
3983 ret = do_tee(in, out, sp->len, flags);
3984
3985 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3986 req->flags &= ~REQ_F_NEED_CLEANUP;
3987
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003988 if (ret != sp->len)
3989 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003990 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003991 return 0;
3992}
3993
3994static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3995{
3996 struct io_splice* sp = &req->splice;
3997
3998 sp->off_in = READ_ONCE(sqe->splice_off_in);
3999 sp->off_out = READ_ONCE(sqe->off);
4000 return __io_splice_prep(req, sqe);
4001}
4002
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004003static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004004{
4005 struct io_splice *sp = &req->splice;
4006 struct file *in = sp->file_in;
4007 struct file *out = sp->file_out;
4008 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4009 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004010 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004011
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004012 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004013 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004014
4015 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4016 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004017
Jens Axboe948a7742020-05-17 14:21:38 -06004018 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004019 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004020
4021 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
4022 req->flags &= ~REQ_F_NEED_CLEANUP;
4023
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004024 if (ret != sp->len)
4025 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004026 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004027 return 0;
4028}
4029
Jens Axboe2b188cc2019-01-07 10:46:33 -07004030/*
4031 * IORING_OP_NOP just posts a completion event, nothing else.
4032 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004033static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004034{
4035 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004036
Jens Axboedef596e2019-01-09 08:59:42 -07004037 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4038 return -EINVAL;
4039
Pavel Begunkov889fca72021-02-10 00:03:09 +00004040 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004041 return 0;
4042}
4043
Jens Axboe3529d8c2019-12-19 18:24:38 -07004044static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004045{
Jens Axboe6b063142019-01-10 22:13:58 -07004046 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004047
Jens Axboe09bb8392019-03-13 12:39:28 -06004048 if (!req->file)
4049 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004050
Jens Axboe6b063142019-01-10 22:13:58 -07004051 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004052 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07004053 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004054 return -EINVAL;
4055
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004056 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4057 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4058 return -EINVAL;
4059
4060 req->sync.off = READ_ONCE(sqe->off);
4061 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004062 return 0;
4063}
4064
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004065static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004066{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004067 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004068 int ret;
4069
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004070 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004071 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004072 return -EAGAIN;
4073
Jens Axboe9adbd452019-12-20 08:45:55 -07004074 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004075 end > 0 ? end : LLONG_MAX,
4076 req->sync.flags & IORING_FSYNC_DATASYNC);
4077 if (ret < 0)
4078 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004079 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004080 return 0;
4081}
4082
Jens Axboed63d1b52019-12-10 10:38:56 -07004083static int io_fallocate_prep(struct io_kiocb *req,
4084 const struct io_uring_sqe *sqe)
4085{
4086 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4087 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004088 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4089 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004090
4091 req->sync.off = READ_ONCE(sqe->off);
4092 req->sync.len = READ_ONCE(sqe->addr);
4093 req->sync.mode = READ_ONCE(sqe->len);
4094 return 0;
4095}
4096
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004097static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004098{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004099 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004100
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004101 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004102 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004103 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004104 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4105 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004106 if (ret < 0)
4107 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004108 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004109 return 0;
4110}
4111
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004112static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004113{
Jens Axboef8748882020-01-08 17:47:02 -07004114 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004115 int ret;
4116
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004117 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004118 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004119 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004120 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004121
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004122 /* open.how should be already initialised */
4123 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004124 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004125
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004126 req->open.dfd = READ_ONCE(sqe->fd);
4127 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004128 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004129 if (IS_ERR(req->open.filename)) {
4130 ret = PTR_ERR(req->open.filename);
4131 req->open.filename = NULL;
4132 return ret;
4133 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004134 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004135 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004136 return 0;
4137}
4138
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004139static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4140{
4141 u64 flags, mode;
4142
Jens Axboe14587a462020-09-05 11:36:08 -06004143 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004144 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004145 mode = READ_ONCE(sqe->len);
4146 flags = READ_ONCE(sqe->open_flags);
4147 req->open.how = build_open_how(flags, mode);
4148 return __io_openat_prep(req, sqe);
4149}
4150
Jens Axboecebdb982020-01-08 17:59:24 -07004151static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4152{
4153 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004154 size_t len;
4155 int ret;
4156
Jens Axboe14587a462020-09-05 11:36:08 -06004157 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004158 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004159 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4160 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004161 if (len < OPEN_HOW_SIZE_VER0)
4162 return -EINVAL;
4163
4164 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4165 len);
4166 if (ret)
4167 return ret;
4168
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004169 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004170}
4171
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004172static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004173{
4174 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004175 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004176 bool nonblock_set;
4177 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004178 int ret;
4179
Jens Axboecebdb982020-01-08 17:59:24 -07004180 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004181 if (ret)
4182 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004183 nonblock_set = op.open_flag & O_NONBLOCK;
4184 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004185 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004186 /*
4187 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4188 * it'll always -EAGAIN
4189 */
4190 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4191 return -EAGAIN;
4192 op.lookup_flags |= LOOKUP_CACHED;
4193 op.open_flag |= O_NONBLOCK;
4194 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004195
Jens Axboe4022e7a2020-03-19 19:23:18 -06004196 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004197 if (ret < 0)
4198 goto err;
4199
4200 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004201 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004202 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4203 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004204 /*
4205 * We could hang on to this 'fd', but seems like marginal
4206 * gain for something that is now known to be a slower path.
4207 * So just put it, and we'll get a new one when we retry.
4208 */
4209 put_unused_fd(ret);
4210 return -EAGAIN;
4211 }
4212
Jens Axboe15b71ab2019-12-11 11:20:36 -07004213 if (IS_ERR(file)) {
4214 put_unused_fd(ret);
4215 ret = PTR_ERR(file);
4216 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004217 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004218 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004219 fsnotify_open(file);
4220 fd_install(ret, file);
4221 }
4222err:
4223 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004224 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004225 if (ret < 0)
4226 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004227 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004228 return 0;
4229}
4230
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004231static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004232{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004233 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004234}
4235
Jens Axboe067524e2020-03-02 16:32:28 -07004236static int io_remove_buffers_prep(struct io_kiocb *req,
4237 const struct io_uring_sqe *sqe)
4238{
4239 struct io_provide_buf *p = &req->pbuf;
4240 u64 tmp;
4241
4242 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4243 return -EINVAL;
4244
4245 tmp = READ_ONCE(sqe->fd);
4246 if (!tmp || tmp > USHRT_MAX)
4247 return -EINVAL;
4248
4249 memset(p, 0, sizeof(*p));
4250 p->nbufs = tmp;
4251 p->bgid = READ_ONCE(sqe->buf_group);
4252 return 0;
4253}
4254
4255static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4256 int bgid, unsigned nbufs)
4257{
4258 unsigned i = 0;
4259
4260 /* shouldn't happen */
4261 if (!nbufs)
4262 return 0;
4263
4264 /* the head kbuf is the list itself */
4265 while (!list_empty(&buf->list)) {
4266 struct io_buffer *nxt;
4267
4268 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4269 list_del(&nxt->list);
4270 kfree(nxt);
4271 if (++i == nbufs)
4272 return i;
4273 }
4274 i++;
4275 kfree(buf);
4276 idr_remove(&ctx->io_buffer_idr, bgid);
4277
4278 return i;
4279}
4280
Pavel Begunkov889fca72021-02-10 00:03:09 +00004281static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004282{
4283 struct io_provide_buf *p = &req->pbuf;
4284 struct io_ring_ctx *ctx = req->ctx;
4285 struct io_buffer *head;
4286 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004287 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004288
4289 io_ring_submit_lock(ctx, !force_nonblock);
4290
4291 lockdep_assert_held(&ctx->uring_lock);
4292
4293 ret = -ENOENT;
4294 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4295 if (head)
4296 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004297 if (ret < 0)
4298 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004299
4300 /* need to hold the lock to complete IOPOLL requests */
4301 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004302 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004303 io_ring_submit_unlock(ctx, !force_nonblock);
4304 } else {
4305 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004306 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004307 }
Jens Axboe067524e2020-03-02 16:32:28 -07004308 return 0;
4309}
4310
Jens Axboeddf0322d2020-02-23 16:41:33 -07004311static int io_provide_buffers_prep(struct io_kiocb *req,
4312 const struct io_uring_sqe *sqe)
4313{
4314 struct io_provide_buf *p = &req->pbuf;
4315 u64 tmp;
4316
4317 if (sqe->ioprio || sqe->rw_flags)
4318 return -EINVAL;
4319
4320 tmp = READ_ONCE(sqe->fd);
4321 if (!tmp || tmp > USHRT_MAX)
4322 return -E2BIG;
4323 p->nbufs = tmp;
4324 p->addr = READ_ONCE(sqe->addr);
4325 p->len = READ_ONCE(sqe->len);
4326
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004327 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004328 return -EFAULT;
4329
4330 p->bgid = READ_ONCE(sqe->buf_group);
4331 tmp = READ_ONCE(sqe->off);
4332 if (tmp > USHRT_MAX)
4333 return -E2BIG;
4334 p->bid = tmp;
4335 return 0;
4336}
4337
4338static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4339{
4340 struct io_buffer *buf;
4341 u64 addr = pbuf->addr;
4342 int i, bid = pbuf->bid;
4343
4344 for (i = 0; i < pbuf->nbufs; i++) {
4345 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4346 if (!buf)
4347 break;
4348
4349 buf->addr = addr;
4350 buf->len = pbuf->len;
4351 buf->bid = bid;
4352 addr += pbuf->len;
4353 bid++;
4354 if (!*head) {
4355 INIT_LIST_HEAD(&buf->list);
4356 *head = buf;
4357 } else {
4358 list_add_tail(&buf->list, &(*head)->list);
4359 }
4360 }
4361
4362 return i ? i : -ENOMEM;
4363}
4364
Pavel Begunkov889fca72021-02-10 00:03:09 +00004365static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004366{
4367 struct io_provide_buf *p = &req->pbuf;
4368 struct io_ring_ctx *ctx = req->ctx;
4369 struct io_buffer *head, *list;
4370 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004371 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004372
4373 io_ring_submit_lock(ctx, !force_nonblock);
4374
4375 lockdep_assert_held(&ctx->uring_lock);
4376
4377 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4378
4379 ret = io_add_buffers(p, &head);
4380 if (ret < 0)
4381 goto out;
4382
4383 if (!list) {
4384 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4385 GFP_KERNEL);
4386 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004387 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004388 goto out;
4389 }
4390 }
4391out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004392 if (ret < 0)
4393 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004394
4395 /* need to hold the lock to complete IOPOLL requests */
4396 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004397 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004398 io_ring_submit_unlock(ctx, !force_nonblock);
4399 } else {
4400 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004401 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004402 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004403 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004404}
4405
Jens Axboe3e4827b2020-01-08 15:18:09 -07004406static int io_epoll_ctl_prep(struct io_kiocb *req,
4407 const struct io_uring_sqe *sqe)
4408{
4409#if defined(CONFIG_EPOLL)
4410 if (sqe->ioprio || sqe->buf_index)
4411 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004412 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004413 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004414
4415 req->epoll.epfd = READ_ONCE(sqe->fd);
4416 req->epoll.op = READ_ONCE(sqe->len);
4417 req->epoll.fd = READ_ONCE(sqe->off);
4418
4419 if (ep_op_has_event(req->epoll.op)) {
4420 struct epoll_event __user *ev;
4421
4422 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4423 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4424 return -EFAULT;
4425 }
4426
4427 return 0;
4428#else
4429 return -EOPNOTSUPP;
4430#endif
4431}
4432
Pavel Begunkov889fca72021-02-10 00:03:09 +00004433static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004434{
4435#if defined(CONFIG_EPOLL)
4436 struct io_epoll *ie = &req->epoll;
4437 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004438 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004439
4440 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4441 if (force_nonblock && ret == -EAGAIN)
4442 return -EAGAIN;
4443
4444 if (ret < 0)
4445 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004446 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004447 return 0;
4448#else
4449 return -EOPNOTSUPP;
4450#endif
4451}
4452
Jens Axboec1ca7572019-12-25 22:18:28 -07004453static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4454{
4455#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4456 if (sqe->ioprio || sqe->buf_index || sqe->off)
4457 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004458 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4459 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004460
4461 req->madvise.addr = READ_ONCE(sqe->addr);
4462 req->madvise.len = READ_ONCE(sqe->len);
4463 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4464 return 0;
4465#else
4466 return -EOPNOTSUPP;
4467#endif
4468}
4469
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004470static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004471{
4472#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4473 struct io_madvise *ma = &req->madvise;
4474 int ret;
4475
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004476 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004477 return -EAGAIN;
4478
Minchan Kim0726b012020-10-17 16:14:50 -07004479 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004480 if (ret < 0)
4481 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004482 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004483 return 0;
4484#else
4485 return -EOPNOTSUPP;
4486#endif
4487}
4488
Jens Axboe4840e412019-12-25 22:03:45 -07004489static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4490{
4491 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4492 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004493 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4494 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004495
4496 req->fadvise.offset = READ_ONCE(sqe->off);
4497 req->fadvise.len = READ_ONCE(sqe->len);
4498 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4499 return 0;
4500}
4501
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004502static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004503{
4504 struct io_fadvise *fa = &req->fadvise;
4505 int ret;
4506
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004507 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004508 switch (fa->advice) {
4509 case POSIX_FADV_NORMAL:
4510 case POSIX_FADV_RANDOM:
4511 case POSIX_FADV_SEQUENTIAL:
4512 break;
4513 default:
4514 return -EAGAIN;
4515 }
4516 }
Jens Axboe4840e412019-12-25 22:03:45 -07004517
4518 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4519 if (ret < 0)
4520 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004521 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004522 return 0;
4523}
4524
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004525static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4526{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004527 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004528 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004529 if (sqe->ioprio || sqe->buf_index)
4530 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004531 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004532 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004533
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004534 req->statx.dfd = READ_ONCE(sqe->fd);
4535 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004536 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004537 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4538 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004539
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004540 return 0;
4541}
4542
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004543static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004544{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004545 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004546 int ret;
4547
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004548 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004549 /* only need file table for an actual valid fd */
4550 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4551 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004552 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004553 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004554
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004555 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4556 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004557
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004558 if (ret < 0)
4559 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004560 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004561 return 0;
4562}
4563
Jens Axboeb5dba592019-12-11 14:02:38 -07004564static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4565{
Jens Axboe14587a462020-09-05 11:36:08 -06004566 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004567 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004568 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4569 sqe->rw_flags || sqe->buf_index)
4570 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004571 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004572 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004573
4574 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004575 return 0;
4576}
4577
Pavel Begunkov889fca72021-02-10 00:03:09 +00004578static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004579{
Jens Axboe9eac1902021-01-19 15:50:37 -07004580 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004581 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004582 struct fdtable *fdt;
4583 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004584 int ret;
4585
Jens Axboe9eac1902021-01-19 15:50:37 -07004586 file = NULL;
4587 ret = -EBADF;
4588 spin_lock(&files->file_lock);
4589 fdt = files_fdtable(files);
4590 if (close->fd >= fdt->max_fds) {
4591 spin_unlock(&files->file_lock);
4592 goto err;
4593 }
4594 file = fdt->fd[close->fd];
4595 if (!file) {
4596 spin_unlock(&files->file_lock);
4597 goto err;
4598 }
4599
4600 if (file->f_op == &io_uring_fops) {
4601 spin_unlock(&files->file_lock);
4602 file = NULL;
4603 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004604 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004605
4606 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004607 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004608 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004609 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004610 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004611
Jens Axboe9eac1902021-01-19 15:50:37 -07004612 ret = __close_fd_get_file(close->fd, &file);
4613 spin_unlock(&files->file_lock);
4614 if (ret < 0) {
4615 if (ret == -ENOENT)
4616 ret = -EBADF;
4617 goto err;
4618 }
4619
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004620 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004621 ret = filp_close(file, current->files);
4622err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004623 if (ret < 0)
4624 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004625 if (file)
4626 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004627 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004628 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004629}
4630
Jens Axboe3529d8c2019-12-19 18:24:38 -07004631static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004632{
4633 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004634
4635 if (!req->file)
4636 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004637
4638 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4639 return -EINVAL;
4640 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4641 return -EINVAL;
4642
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004643 req->sync.off = READ_ONCE(sqe->off);
4644 req->sync.len = READ_ONCE(sqe->len);
4645 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004646 return 0;
4647}
4648
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004649static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004650{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004651 int ret;
4652
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004653 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004654 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004655 return -EAGAIN;
4656
Jens Axboe9adbd452019-12-20 08:45:55 -07004657 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004658 req->sync.flags);
4659 if (ret < 0)
4660 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004661 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004662 return 0;
4663}
4664
YueHaibing469956e2020-03-04 15:53:52 +08004665#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004666static int io_setup_async_msg(struct io_kiocb *req,
4667 struct io_async_msghdr *kmsg)
4668{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004669 struct io_async_msghdr *async_msg = req->async_data;
4670
4671 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004672 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004673 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004674 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004675 return -ENOMEM;
4676 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004677 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004678 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004679 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004680 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004681 /* if were using fast_iov, set it to the new one */
4682 if (!async_msg->free_iov)
4683 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4684
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004685 return -EAGAIN;
4686}
4687
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004688static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4689 struct io_async_msghdr *iomsg)
4690{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004691 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004692 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004693 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004694 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004695}
4696
Jens Axboe3529d8c2019-12-19 18:24:38 -07004697static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004698{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004699 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004700 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004701 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004702
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004703 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4704 return -EINVAL;
4705
Jens Axboee47293f2019-12-20 08:58:21 -07004706 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004707 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004708 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004709
Jens Axboed8768362020-02-27 14:17:49 -07004710#ifdef CONFIG_COMPAT
4711 if (req->ctx->compat)
4712 sr->msg_flags |= MSG_CMSG_COMPAT;
4713#endif
4714
Jens Axboee8c2bc12020-08-15 18:44:09 -07004715 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004716 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004717 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004718 if (!ret)
4719 req->flags |= REQ_F_NEED_CLEANUP;
4720 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004721}
4722
Pavel Begunkov889fca72021-02-10 00:03:09 +00004723static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004724{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004725 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004726 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004727 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004728 int ret;
4729
Florent Revestdba4a922020-12-04 12:36:04 +01004730 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004731 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004732 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004733
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004734 kmsg = req->async_data;
4735 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004736 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004737 if (ret)
4738 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004739 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004740 }
4741
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004742 flags = req->sr_msg.msg_flags;
4743 if (flags & MSG_DONTWAIT)
4744 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004745 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004746 flags |= MSG_DONTWAIT;
4747
4748 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004749 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004750 return io_setup_async_msg(req, kmsg);
4751 if (ret == -ERESTARTSYS)
4752 ret = -EINTR;
4753
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004754 /* fast path, check for non-NULL to avoid function call */
4755 if (kmsg->free_iov)
4756 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004757 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004758 if (ret < 0)
4759 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004760 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004761 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004762}
4763
Pavel Begunkov889fca72021-02-10 00:03:09 +00004764static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004765{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004766 struct io_sr_msg *sr = &req->sr_msg;
4767 struct msghdr msg;
4768 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004769 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004770 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004771 int ret;
4772
Florent Revestdba4a922020-12-04 12:36:04 +01004773 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004774 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004775 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004776
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004777 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4778 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004779 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004780
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004781 msg.msg_name = NULL;
4782 msg.msg_control = NULL;
4783 msg.msg_controllen = 0;
4784 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004785
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004786 flags = req->sr_msg.msg_flags;
4787 if (flags & MSG_DONTWAIT)
4788 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004789 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004790 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004791
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004792 msg.msg_flags = flags;
4793 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004794 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004795 return -EAGAIN;
4796 if (ret == -ERESTARTSYS)
4797 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004798
Jens Axboe03b12302019-12-02 18:50:25 -07004799 if (ret < 0)
4800 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004801 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004802 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004803}
4804
Pavel Begunkov1400e692020-07-12 20:41:05 +03004805static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4806 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004807{
4808 struct io_sr_msg *sr = &req->sr_msg;
4809 struct iovec __user *uiov;
4810 size_t iov_len;
4811 int ret;
4812
Pavel Begunkov1400e692020-07-12 20:41:05 +03004813 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4814 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004815 if (ret)
4816 return ret;
4817
4818 if (req->flags & REQ_F_BUFFER_SELECT) {
4819 if (iov_len > 1)
4820 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004821 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004822 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004823 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004824 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004825 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004826 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004827 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004828 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004829 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004830 if (ret > 0)
4831 ret = 0;
4832 }
4833
4834 return ret;
4835}
4836
4837#ifdef CONFIG_COMPAT
4838static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004839 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004840{
4841 struct compat_msghdr __user *msg_compat;
4842 struct io_sr_msg *sr = &req->sr_msg;
4843 struct compat_iovec __user *uiov;
4844 compat_uptr_t ptr;
4845 compat_size_t len;
4846 int ret;
4847
Pavel Begunkov270a5942020-07-12 20:41:04 +03004848 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004849 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004850 &ptr, &len);
4851 if (ret)
4852 return ret;
4853
4854 uiov = compat_ptr(ptr);
4855 if (req->flags & REQ_F_BUFFER_SELECT) {
4856 compat_ssize_t clen;
4857
4858 if (len > 1)
4859 return -EINVAL;
4860 if (!access_ok(uiov, sizeof(*uiov)))
4861 return -EFAULT;
4862 if (__get_user(clen, &uiov->iov_len))
4863 return -EFAULT;
4864 if (clen < 0)
4865 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004866 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004867 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004868 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004869 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004870 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004871 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004872 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004873 if (ret < 0)
4874 return ret;
4875 }
4876
4877 return 0;
4878}
Jens Axboe03b12302019-12-02 18:50:25 -07004879#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004880
Pavel Begunkov1400e692020-07-12 20:41:05 +03004881static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4882 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004883{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004884 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004885
4886#ifdef CONFIG_COMPAT
4887 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004888 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004889#endif
4890
Pavel Begunkov1400e692020-07-12 20:41:05 +03004891 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004892}
4893
Jens Axboebcda7ba2020-02-23 16:42:51 -07004894static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004895 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004896{
4897 struct io_sr_msg *sr = &req->sr_msg;
4898 struct io_buffer *kbuf;
4899
Jens Axboebcda7ba2020-02-23 16:42:51 -07004900 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4901 if (IS_ERR(kbuf))
4902 return kbuf;
4903
4904 sr->kbuf = kbuf;
4905 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004906 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004907}
4908
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004909static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4910{
4911 return io_put_kbuf(req, req->sr_msg.kbuf);
4912}
4913
Jens Axboe3529d8c2019-12-19 18:24:38 -07004914static int io_recvmsg_prep(struct io_kiocb *req,
4915 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004916{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004917 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004918 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004919 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004920
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004921 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4922 return -EINVAL;
4923
Jens Axboe3529d8c2019-12-19 18:24:38 -07004924 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004925 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004926 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004927 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004928
Jens Axboed8768362020-02-27 14:17:49 -07004929#ifdef CONFIG_COMPAT
4930 if (req->ctx->compat)
4931 sr->msg_flags |= MSG_CMSG_COMPAT;
4932#endif
4933
Jens Axboee8c2bc12020-08-15 18:44:09 -07004934 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004935 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004936 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004937 if (!ret)
4938 req->flags |= REQ_F_NEED_CLEANUP;
4939 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004940}
4941
Pavel Begunkov889fca72021-02-10 00:03:09 +00004942static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004943{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004944 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004945 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004946 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004947 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004948 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004949 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004950
Florent Revestdba4a922020-12-04 12:36:04 +01004951 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004952 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004953 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004954
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004955 kmsg = req->async_data;
4956 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004957 ret = io_recvmsg_copy_hdr(req, &iomsg);
4958 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004959 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004960 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004961 }
4962
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004963 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004964 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004965 if (IS_ERR(kbuf))
4966 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004967 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004968 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4969 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004970 1, req->sr_msg.len);
4971 }
4972
4973 flags = req->sr_msg.msg_flags;
4974 if (flags & MSG_DONTWAIT)
4975 req->flags |= REQ_F_NOWAIT;
4976 else if (force_nonblock)
4977 flags |= MSG_DONTWAIT;
4978
4979 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4980 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004981 if (force_nonblock && ret == -EAGAIN)
4982 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004983 if (ret == -ERESTARTSYS)
4984 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004985
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004986 if (req->flags & REQ_F_BUFFER_SELECTED)
4987 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004988 /* fast path, check for non-NULL to avoid function call */
4989 if (kmsg->free_iov)
4990 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004991 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004992 if (ret < 0)
4993 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004994 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004995 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004996}
4997
Pavel Begunkov889fca72021-02-10 00:03:09 +00004998static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004999{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005000 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005001 struct io_sr_msg *sr = &req->sr_msg;
5002 struct msghdr msg;
5003 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005004 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005005 struct iovec iov;
5006 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005007 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005008 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005009
Florent Revestdba4a922020-12-04 12:36:04 +01005010 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005011 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005012 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005013
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005014 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005015 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005016 if (IS_ERR(kbuf))
5017 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005018 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005019 }
5020
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005021 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005022 if (unlikely(ret))
5023 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005024
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005025 msg.msg_name = NULL;
5026 msg.msg_control = NULL;
5027 msg.msg_controllen = 0;
5028 msg.msg_namelen = 0;
5029 msg.msg_iocb = NULL;
5030 msg.msg_flags = 0;
5031
5032 flags = req->sr_msg.msg_flags;
5033 if (flags & MSG_DONTWAIT)
5034 req->flags |= REQ_F_NOWAIT;
5035 else if (force_nonblock)
5036 flags |= MSG_DONTWAIT;
5037
5038 ret = sock_recvmsg(sock, &msg, flags);
5039 if (force_nonblock && ret == -EAGAIN)
5040 return -EAGAIN;
5041 if (ret == -ERESTARTSYS)
5042 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005043out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005044 if (req->flags & REQ_F_BUFFER_SELECTED)
5045 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07005046 if (ret < 0)
5047 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005048 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005049 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005050}
5051
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005053{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005054 struct io_accept *accept = &req->accept;
5055
Jens Axboe14587a462020-09-05 11:36:08 -06005056 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005057 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05005058 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005059 return -EINVAL;
5060
Jens Axboed55e5f52019-12-11 16:12:15 -07005061 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5062 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005063 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005064 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005065 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005066}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005067
Pavel Begunkov889fca72021-02-10 00:03:09 +00005068static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005069{
5070 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005071 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005072 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005073 int ret;
5074
Jiufei Xuee697dee2020-06-10 13:41:59 +08005075 if (req->file->f_flags & O_NONBLOCK)
5076 req->flags |= REQ_F_NOWAIT;
5077
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005078 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005079 accept->addr_len, accept->flags,
5080 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005081 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005082 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005083 if (ret < 0) {
5084 if (ret == -ERESTARTSYS)
5085 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005086 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005087 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005088 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005089 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005090}
5091
Jens Axboe3529d8c2019-12-19 18:24:38 -07005092static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005093{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005094 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005095 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005096
Jens Axboe14587a462020-09-05 11:36:08 -06005097 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005098 return -EINVAL;
5099 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5100 return -EINVAL;
5101
Jens Axboe3529d8c2019-12-19 18:24:38 -07005102 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5103 conn->addr_len = READ_ONCE(sqe->addr2);
5104
5105 if (!io)
5106 return 0;
5107
5108 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005109 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005110}
5111
Pavel Begunkov889fca72021-02-10 00:03:09 +00005112static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005113{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005114 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005115 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005116 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005117 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005118
Jens Axboee8c2bc12020-08-15 18:44:09 -07005119 if (req->async_data) {
5120 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005121 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005122 ret = move_addr_to_kernel(req->connect.addr,
5123 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005124 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005125 if (ret)
5126 goto out;
5127 io = &__io;
5128 }
5129
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005130 file_flags = force_nonblock ? O_NONBLOCK : 0;
5131
Jens Axboee8c2bc12020-08-15 18:44:09 -07005132 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005133 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005134 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005135 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005136 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005137 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005138 ret = -ENOMEM;
5139 goto out;
5140 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005141 io = req->async_data;
5142 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005143 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005144 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005145 if (ret == -ERESTARTSYS)
5146 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005147out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005148 if (ret < 0)
5149 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005150 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005151 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005152}
YueHaibing469956e2020-03-04 15:53:52 +08005153#else /* !CONFIG_NET */
5154static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5155{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005156 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005157}
5158
Pavel Begunkov889fca72021-02-10 00:03:09 +00005159static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005160{
YueHaibing469956e2020-03-04 15:53:52 +08005161 return -EOPNOTSUPP;
5162}
5163
Pavel Begunkov889fca72021-02-10 00:03:09 +00005164static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005165{
5166 return -EOPNOTSUPP;
5167}
5168
5169static int io_recvmsg_prep(struct io_kiocb *req,
5170 const struct io_uring_sqe *sqe)
5171{
5172 return -EOPNOTSUPP;
5173}
5174
Pavel Begunkov889fca72021-02-10 00:03:09 +00005175static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005176{
5177 return -EOPNOTSUPP;
5178}
5179
Pavel Begunkov889fca72021-02-10 00:03:09 +00005180static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005181{
5182 return -EOPNOTSUPP;
5183}
5184
5185static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5186{
5187 return -EOPNOTSUPP;
5188}
5189
Pavel Begunkov889fca72021-02-10 00:03:09 +00005190static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005191{
5192 return -EOPNOTSUPP;
5193}
5194
5195static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5196{
5197 return -EOPNOTSUPP;
5198}
5199
Pavel Begunkov889fca72021-02-10 00:03:09 +00005200static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005201{
5202 return -EOPNOTSUPP;
5203}
5204#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005205
Jens Axboed7718a92020-02-14 22:23:12 -07005206struct io_poll_table {
5207 struct poll_table_struct pt;
5208 struct io_kiocb *req;
5209 int error;
5210};
5211
Jens Axboed7718a92020-02-14 22:23:12 -07005212static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5213 __poll_t mask, task_work_func_t func)
5214{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005215 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005216
5217 /* for instances that support it check for an event match first: */
5218 if (mask && !(mask & poll->events))
5219 return 0;
5220
5221 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5222
5223 list_del_init(&poll->wait.entry);
5224
Jens Axboed7718a92020-02-14 22:23:12 -07005225 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00005226 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005227 percpu_ref_get(&req->ctx->refs);
5228
Jens Axboed7718a92020-02-14 22:23:12 -07005229 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005230 * If this fails, then the task is exiting. When a task exits, the
5231 * work gets canceled, so just cancel this request as well instead
5232 * of executing it. We can't safely execute it anyway, as we may not
5233 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005234 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005235 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005236 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005237 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005238 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005239 }
Jens Axboed7718a92020-02-14 22:23:12 -07005240 return 1;
5241}
5242
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005243static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5244 __acquires(&req->ctx->completion_lock)
5245{
5246 struct io_ring_ctx *ctx = req->ctx;
5247
5248 if (!req->result && !READ_ONCE(poll->canceled)) {
5249 struct poll_table_struct pt = { ._key = poll->events };
5250
5251 req->result = vfs_poll(req->file, &pt) & poll->events;
5252 }
5253
5254 spin_lock_irq(&ctx->completion_lock);
5255 if (!req->result && !READ_ONCE(poll->canceled)) {
5256 add_wait_queue(poll->head, &poll->wait);
5257 return true;
5258 }
5259
5260 return false;
5261}
5262
Jens Axboed4e7cd32020-08-15 11:44:50 -07005263static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005264{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005265 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005266 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005267 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005268 return req->apoll->double_poll;
5269}
5270
5271static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5272{
5273 if (req->opcode == IORING_OP_POLL_ADD)
5274 return &req->poll;
5275 return &req->apoll->poll;
5276}
5277
5278static void io_poll_remove_double(struct io_kiocb *req)
5279{
5280 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005281
5282 lockdep_assert_held(&req->ctx->completion_lock);
5283
5284 if (poll && poll->head) {
5285 struct wait_queue_head *head = poll->head;
5286
5287 spin_lock(&head->lock);
5288 list_del_init(&poll->wait.entry);
5289 if (poll->wait.private)
5290 refcount_dec(&req->refs);
5291 poll->head = NULL;
5292 spin_unlock(&head->lock);
5293 }
5294}
5295
5296static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5297{
5298 struct io_ring_ctx *ctx = req->ctx;
5299
Jens Axboed4e7cd32020-08-15 11:44:50 -07005300 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005301 req->poll.done = true;
5302 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5303 io_commit_cqring(ctx);
5304}
5305
Jens Axboe18bceab2020-05-15 11:56:54 -06005306static void io_poll_task_func(struct callback_head *cb)
5307{
5308 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005309 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005310 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005311
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005312 if (io_poll_rewait(req, &req->poll)) {
5313 spin_unlock_irq(&ctx->completion_lock);
5314 } else {
5315 hash_del(&req->hash_node);
5316 io_poll_complete(req, req->result, 0);
5317 spin_unlock_irq(&ctx->completion_lock);
5318
5319 nxt = io_put_req_find_next(req);
5320 io_cqring_ev_posted(ctx);
5321 if (nxt)
5322 __io_req_task_submit(nxt);
5323 }
5324
Jens Axboe6d816e02020-08-11 08:04:14 -06005325 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005326}
5327
5328static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5329 int sync, void *key)
5330{
5331 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005332 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005333 __poll_t mask = key_to_poll(key);
5334
5335 /* for instances that support it check for an event match first: */
5336 if (mask && !(mask & poll->events))
5337 return 0;
5338
Jens Axboe8706e042020-09-28 08:38:54 -06005339 list_del_init(&wait->entry);
5340
Jens Axboe807abcb2020-07-17 17:09:27 -06005341 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005342 bool done;
5343
Jens Axboe807abcb2020-07-17 17:09:27 -06005344 spin_lock(&poll->head->lock);
5345 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005346 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005347 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005348 /* make sure double remove sees this as being gone */
5349 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005350 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005351 if (!done) {
5352 /* use wait func handler, so it matches the rq type */
5353 poll->wait.func(&poll->wait, mode, sync, key);
5354 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005355 }
5356 refcount_dec(&req->refs);
5357 return 1;
5358}
5359
5360static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5361 wait_queue_func_t wake_func)
5362{
5363 poll->head = NULL;
5364 poll->done = false;
5365 poll->canceled = false;
5366 poll->events = events;
5367 INIT_LIST_HEAD(&poll->wait.entry);
5368 init_waitqueue_func_entry(&poll->wait, wake_func);
5369}
5370
5371static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005372 struct wait_queue_head *head,
5373 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005374{
5375 struct io_kiocb *req = pt->req;
5376
5377 /*
5378 * If poll->head is already set, it's because the file being polled
5379 * uses multiple waitqueues for poll handling (eg one for read, one
5380 * for write). Setup a separate io_poll_iocb if this happens.
5381 */
5382 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005383 struct io_poll_iocb *poll_one = poll;
5384
Jens Axboe18bceab2020-05-15 11:56:54 -06005385 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005386 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005387 pt->error = -EINVAL;
5388 return;
5389 }
5390 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5391 if (!poll) {
5392 pt->error = -ENOMEM;
5393 return;
5394 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005395 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005396 refcount_inc(&req->refs);
5397 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005398 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005399 }
5400
5401 pt->error = 0;
5402 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005403
5404 if (poll->events & EPOLLEXCLUSIVE)
5405 add_wait_queue_exclusive(head, &poll->wait);
5406 else
5407 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005408}
5409
5410static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5411 struct poll_table_struct *p)
5412{
5413 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005414 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005415
Jens Axboe807abcb2020-07-17 17:09:27 -06005416 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005417}
5418
Jens Axboed7718a92020-02-14 22:23:12 -07005419static void io_async_task_func(struct callback_head *cb)
5420{
5421 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5422 struct async_poll *apoll = req->apoll;
5423 struct io_ring_ctx *ctx = req->ctx;
5424
5425 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5426
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005427 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005428 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005429 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005430 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005431 }
5432
Jens Axboe31067252020-05-17 17:43:31 -06005433 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005434 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005435 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005436
Jens Axboed4e7cd32020-08-15 11:44:50 -07005437 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005438 spin_unlock_irq(&ctx->completion_lock);
5439
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005440 if (!READ_ONCE(apoll->poll.canceled))
5441 __io_req_task_submit(req);
5442 else
5443 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005444
Jens Axboe6d816e02020-08-11 08:04:14 -06005445 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005446 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005447 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005448}
5449
5450static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5451 void *key)
5452{
5453 struct io_kiocb *req = wait->private;
5454 struct io_poll_iocb *poll = &req->apoll->poll;
5455
5456 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5457 key_to_poll(key));
5458
5459 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5460}
5461
5462static void io_poll_req_insert(struct io_kiocb *req)
5463{
5464 struct io_ring_ctx *ctx = req->ctx;
5465 struct hlist_head *list;
5466
5467 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5468 hlist_add_head(&req->hash_node, list);
5469}
5470
5471static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5472 struct io_poll_iocb *poll,
5473 struct io_poll_table *ipt, __poll_t mask,
5474 wait_queue_func_t wake_func)
5475 __acquires(&ctx->completion_lock)
5476{
5477 struct io_ring_ctx *ctx = req->ctx;
5478 bool cancel = false;
5479
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005480 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005481 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005482 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005483 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005484
5485 ipt->pt._key = mask;
5486 ipt->req = req;
5487 ipt->error = -EINVAL;
5488
Jens Axboed7718a92020-02-14 22:23:12 -07005489 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5490
5491 spin_lock_irq(&ctx->completion_lock);
5492 if (likely(poll->head)) {
5493 spin_lock(&poll->head->lock);
5494 if (unlikely(list_empty(&poll->wait.entry))) {
5495 if (ipt->error)
5496 cancel = true;
5497 ipt->error = 0;
5498 mask = 0;
5499 }
5500 if (mask || ipt->error)
5501 list_del_init(&poll->wait.entry);
5502 else if (cancel)
5503 WRITE_ONCE(poll->canceled, true);
5504 else if (!poll->done) /* actually waiting for an event */
5505 io_poll_req_insert(req);
5506 spin_unlock(&poll->head->lock);
5507 }
5508
5509 return mask;
5510}
5511
5512static bool io_arm_poll_handler(struct io_kiocb *req)
5513{
5514 const struct io_op_def *def = &io_op_defs[req->opcode];
5515 struct io_ring_ctx *ctx = req->ctx;
5516 struct async_poll *apoll;
5517 struct io_poll_table ipt;
5518 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005519 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005520
5521 if (!req->file || !file_can_poll(req->file))
5522 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005523 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005524 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005525 if (def->pollin)
5526 rw = READ;
5527 else if (def->pollout)
5528 rw = WRITE;
5529 else
5530 return false;
5531 /* if we can't nonblock try, then no point in arming a poll handler */
5532 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005533 return false;
5534
5535 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5536 if (unlikely(!apoll))
5537 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005538 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005539
5540 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005541 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005542
Nathan Chancellor8755d972020-03-02 16:01:19 -07005543 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005544 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005545 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005546 if (def->pollout)
5547 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005548
5549 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5550 if ((req->opcode == IORING_OP_RECVMSG) &&
5551 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5552 mask &= ~POLLIN;
5553
Jens Axboed7718a92020-02-14 22:23:12 -07005554 mask |= POLLERR | POLLPRI;
5555
5556 ipt.pt._qproc = io_async_queue_proc;
5557
5558 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5559 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005560 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005561 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005562 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005563 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005564 kfree(apoll);
5565 return false;
5566 }
5567 spin_unlock_irq(&ctx->completion_lock);
5568 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5569 apoll->poll.events);
5570 return true;
5571}
5572
5573static bool __io_poll_remove_one(struct io_kiocb *req,
5574 struct io_poll_iocb *poll)
5575{
Jens Axboeb41e9852020-02-17 09:52:41 -07005576 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005577
5578 spin_lock(&poll->head->lock);
5579 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005580 if (!list_empty(&poll->wait.entry)) {
5581 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005582 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005583 }
5584 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005585 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005586 return do_complete;
5587}
5588
5589static bool io_poll_remove_one(struct io_kiocb *req)
5590{
5591 bool do_complete;
5592
Jens Axboed4e7cd32020-08-15 11:44:50 -07005593 io_poll_remove_double(req);
5594
Jens Axboed7718a92020-02-14 22:23:12 -07005595 if (req->opcode == IORING_OP_POLL_ADD) {
5596 do_complete = __io_poll_remove_one(req, &req->poll);
5597 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005598 struct async_poll *apoll = req->apoll;
5599
Jens Axboed7718a92020-02-14 22:23:12 -07005600 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005601 do_complete = __io_poll_remove_one(req, &apoll->poll);
5602 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005603 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005604 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005605 kfree(apoll);
5606 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005607 }
5608
Jens Axboeb41e9852020-02-17 09:52:41 -07005609 if (do_complete) {
5610 io_cqring_fill_event(req, -ECANCELED);
5611 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005612 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005613 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005614 }
5615
5616 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005617}
5618
Jens Axboe76e1b642020-09-26 15:05:03 -06005619/*
5620 * Returns true if we found and killed one or more poll requests
5621 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005622static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5623 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005624{
Jens Axboe78076bb2019-12-04 19:56:40 -07005625 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005626 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005627 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005628
5629 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005630 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5631 struct hlist_head *list;
5632
5633 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005634 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005635 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005636 posted += io_poll_remove_one(req);
5637 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005638 }
5639 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005640
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005641 if (posted)
5642 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005643
5644 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005645}
5646
Jens Axboe47f46762019-11-09 17:43:02 -07005647static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5648{
Jens Axboe78076bb2019-12-04 19:56:40 -07005649 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005650 struct io_kiocb *req;
5651
Jens Axboe78076bb2019-12-04 19:56:40 -07005652 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5653 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005654 if (sqe_addr != req->user_data)
5655 continue;
5656 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005657 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005658 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005659 }
5660
5661 return -ENOENT;
5662}
5663
Jens Axboe3529d8c2019-12-19 18:24:38 -07005664static int io_poll_remove_prep(struct io_kiocb *req,
5665 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005666{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005667 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5668 return -EINVAL;
5669 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5670 sqe->poll_events)
5671 return -EINVAL;
5672
Pavel Begunkov018043b2020-10-27 23:17:18 +00005673 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005674 return 0;
5675}
5676
5677/*
5678 * Find a running poll command that matches one specified in sqe->addr,
5679 * and remove it if found.
5680 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005681static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005682{
5683 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005684 int ret;
5685
Jens Axboe221c5eb2019-01-17 09:41:58 -07005686 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005687 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005688 spin_unlock_irq(&ctx->completion_lock);
5689
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005690 if (ret < 0)
5691 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005692 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005693 return 0;
5694}
5695
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5697 void *key)
5698{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005699 struct io_kiocb *req = wait->private;
5700 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005701
Jens Axboed7718a92020-02-14 22:23:12 -07005702 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005703}
5704
Jens Axboe221c5eb2019-01-17 09:41:58 -07005705static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5706 struct poll_table_struct *p)
5707{
5708 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5709
Jens Axboee8c2bc12020-08-15 18:44:09 -07005710 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005711}
5712
Jens Axboe3529d8c2019-12-19 18:24:38 -07005713static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005714{
5715 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005716 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005717
5718 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5719 return -EINVAL;
5720 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5721 return -EINVAL;
5722
Jiufei Xue5769a352020-06-17 17:53:55 +08005723 events = READ_ONCE(sqe->poll32_events);
5724#ifdef __BIG_ENDIAN
5725 events = swahw32(events);
5726#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005727 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5728 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005729 return 0;
5730}
5731
Pavel Begunkov61e98202021-02-10 00:03:08 +00005732static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005733{
5734 struct io_poll_iocb *poll = &req->poll;
5735 struct io_ring_ctx *ctx = req->ctx;
5736 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005737 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005738
Jens Axboed7718a92020-02-14 22:23:12 -07005739 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005740
Jens Axboed7718a92020-02-14 22:23:12 -07005741 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5742 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005743
Jens Axboe8c838782019-03-12 15:48:16 -06005744 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005745 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005746 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005747 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005748 spin_unlock_irq(&ctx->completion_lock);
5749
Jens Axboe8c838782019-03-12 15:48:16 -06005750 if (mask) {
5751 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005752 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005753 }
Jens Axboe8c838782019-03-12 15:48:16 -06005754 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005755}
5756
Jens Axboe5262f562019-09-17 12:26:57 -06005757static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5758{
Jens Axboead8a48a2019-11-15 08:49:11 -07005759 struct io_timeout_data *data = container_of(timer,
5760 struct io_timeout_data, timer);
5761 struct io_kiocb *req = data->req;
5762 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005763 unsigned long flags;
5764
Jens Axboe5262f562019-09-17 12:26:57 -06005765 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005766 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005767 atomic_set(&req->ctx->cq_timeouts,
5768 atomic_read(&req->ctx->cq_timeouts) + 1);
5769
Jens Axboe78e19bb2019-11-06 15:21:34 -07005770 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005771 io_commit_cqring(ctx);
5772 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5773
5774 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005775 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005776 io_put_req(req);
5777 return HRTIMER_NORESTART;
5778}
5779
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005780static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5781 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005782{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005783 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005784 struct io_kiocb *req;
5785 int ret = -ENOENT;
5786
5787 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5788 if (user_data == req->user_data) {
5789 ret = 0;
5790 break;
5791 }
5792 }
5793
5794 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005795 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005796
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005797 io = req->async_data;
5798 ret = hrtimer_try_to_cancel(&io->timer);
5799 if (ret == -1)
5800 return ERR_PTR(-EALREADY);
5801 list_del_init(&req->timeout.list);
5802 return req;
5803}
5804
5805static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5806{
5807 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5808
5809 if (IS_ERR(req))
5810 return PTR_ERR(req);
5811
5812 req_set_fail_links(req);
5813 io_cqring_fill_event(req, -ECANCELED);
5814 io_put_req_deferred(req, 1);
5815 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005816}
5817
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005818static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5819 struct timespec64 *ts, enum hrtimer_mode mode)
5820{
5821 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5822 struct io_timeout_data *data;
5823
5824 if (IS_ERR(req))
5825 return PTR_ERR(req);
5826
5827 req->timeout.off = 0; /* noseq */
5828 data = req->async_data;
5829 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5830 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5831 data->timer.function = io_timeout_fn;
5832 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5833 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005834}
5835
Jens Axboe3529d8c2019-12-19 18:24:38 -07005836static int io_timeout_remove_prep(struct io_kiocb *req,
5837 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005838{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005839 struct io_timeout_rem *tr = &req->timeout_rem;
5840
Jens Axboeb29472e2019-12-17 18:50:29 -07005841 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5842 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005843 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5844 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005845 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005846 return -EINVAL;
5847
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005848 tr->addr = READ_ONCE(sqe->addr);
5849 tr->flags = READ_ONCE(sqe->timeout_flags);
5850 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5851 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5852 return -EINVAL;
5853 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5854 return -EFAULT;
5855 } else if (tr->flags) {
5856 /* timeout removal doesn't support flags */
5857 return -EINVAL;
5858 }
5859
Jens Axboeb29472e2019-12-17 18:50:29 -07005860 return 0;
5861}
5862
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005863static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5864{
5865 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5866 : HRTIMER_MODE_REL;
5867}
5868
Jens Axboe11365042019-10-16 09:08:32 -06005869/*
5870 * Remove or update an existing timeout command
5871 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005872static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005873{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005874 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005875 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005876 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005877
Jens Axboe11365042019-10-16 09:08:32 -06005878 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005879 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005880 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005881 else
5882 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5883 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005884
Jens Axboe47f46762019-11-09 17:43:02 -07005885 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005886 io_commit_cqring(ctx);
5887 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005888 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005889 if (ret < 0)
5890 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005891 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005892 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005893}
5894
Jens Axboe3529d8c2019-12-19 18:24:38 -07005895static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005896 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005897{
Jens Axboead8a48a2019-11-15 08:49:11 -07005898 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005899 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005900 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005901
Jens Axboead8a48a2019-11-15 08:49:11 -07005902 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005903 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005904 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005905 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005906 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005907 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005908 flags = READ_ONCE(sqe->timeout_flags);
5909 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005910 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005911
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005912 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005913
Jens Axboee8c2bc12020-08-15 18:44:09 -07005914 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005915 return -ENOMEM;
5916
Jens Axboee8c2bc12020-08-15 18:44:09 -07005917 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005918 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005919
5920 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005921 return -EFAULT;
5922
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005923 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005924 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5925 return 0;
5926}
5927
Pavel Begunkov61e98202021-02-10 00:03:08 +00005928static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005929{
Jens Axboead8a48a2019-11-15 08:49:11 -07005930 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005931 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005932 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005933 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005934
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005935 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005936
Jens Axboe5262f562019-09-17 12:26:57 -06005937 /*
5938 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005939 * timeout event to be satisfied. If it isn't set, then this is
5940 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005941 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005942 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005943 entry = ctx->timeout_list.prev;
5944 goto add;
5945 }
Jens Axboe5262f562019-09-17 12:26:57 -06005946
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005947 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5948 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005949
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005950 /* Update the last seq here in case io_flush_timeouts() hasn't.
5951 * This is safe because ->completion_lock is held, and submissions
5952 * and completions are never mixed in the same ->completion_lock section.
5953 */
5954 ctx->cq_last_tm_flush = tail;
5955
Jens Axboe5262f562019-09-17 12:26:57 -06005956 /*
5957 * Insertion sort, ensuring the first entry in the list is always
5958 * the one we need first.
5959 */
Jens Axboe5262f562019-09-17 12:26:57 -06005960 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005961 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5962 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005963
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005964 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005965 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005966 /* nxt.seq is behind @tail, otherwise would've been completed */
5967 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005968 break;
5969 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005970add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005971 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005972 data->timer.function = io_timeout_fn;
5973 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005974 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005975 return 0;
5976}
5977
Jens Axboe62755e32019-10-28 21:49:21 -06005978static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005979{
Jens Axboe62755e32019-10-28 21:49:21 -06005980 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005981
Jens Axboe62755e32019-10-28 21:49:21 -06005982 return req->user_data == (unsigned long) data;
5983}
5984
Jens Axboee977d6d2019-11-05 12:39:45 -07005985static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005986{
Jens Axboe62755e32019-10-28 21:49:21 -06005987 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005988 int ret = 0;
5989
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005990 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005991 switch (cancel_ret) {
5992 case IO_WQ_CANCEL_OK:
5993 ret = 0;
5994 break;
5995 case IO_WQ_CANCEL_RUNNING:
5996 ret = -EALREADY;
5997 break;
5998 case IO_WQ_CANCEL_NOTFOUND:
5999 ret = -ENOENT;
6000 break;
6001 }
6002
Jens Axboee977d6d2019-11-05 12:39:45 -07006003 return ret;
6004}
6005
Jens Axboe47f46762019-11-09 17:43:02 -07006006static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
6007 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03006008 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07006009{
6010 unsigned long flags;
6011 int ret;
6012
6013 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
6014 if (ret != -ENOENT) {
6015 spin_lock_irqsave(&ctx->completion_lock, flags);
6016 goto done;
6017 }
6018
6019 spin_lock_irqsave(&ctx->completion_lock, flags);
6020 ret = io_timeout_cancel(ctx, sqe_addr);
6021 if (ret != -ENOENT)
6022 goto done;
6023 ret = io_poll_cancel(ctx, sqe_addr);
6024done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07006025 if (!ret)
6026 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006027 io_cqring_fill_event(req, ret);
6028 io_commit_cqring(ctx);
6029 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6030 io_cqring_ev_posted(ctx);
6031
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006032 if (ret < 0)
6033 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03006034 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07006035}
6036
Jens Axboe3529d8c2019-12-19 18:24:38 -07006037static int io_async_cancel_prep(struct io_kiocb *req,
6038 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006039{
Jens Axboefbf23842019-12-17 18:45:56 -07006040 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006041 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006042 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6043 return -EINVAL;
6044 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07006045 return -EINVAL;
6046
Jens Axboefbf23842019-12-17 18:45:56 -07006047 req->cancel.addr = READ_ONCE(sqe->addr);
6048 return 0;
6049}
6050
Pavel Begunkov61e98202021-02-10 00:03:08 +00006051static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006052{
6053 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07006054
Pavel Begunkov014db002020-03-03 21:33:12 +03006055 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006056 return 0;
6057}
6058
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006059static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006060 const struct io_uring_sqe *sqe)
6061{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006062 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6063 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006064 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6065 return -EINVAL;
6066 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006067 return -EINVAL;
6068
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006069 req->rsrc_update.offset = READ_ONCE(sqe->off);
6070 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6071 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006072 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006073 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006074 return 0;
6075}
6076
Pavel Begunkov889fca72021-02-10 00:03:09 +00006077static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006078{
6079 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006080 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006081 int ret;
6082
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006083 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006084 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006085
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006086 up.offset = req->rsrc_update.offset;
6087 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006088
6089 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006090 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006091 mutex_unlock(&ctx->uring_lock);
6092
6093 if (ret < 0)
6094 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006095 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006096 return 0;
6097}
6098
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006099static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006100{
Jens Axboed625c6e2019-12-17 19:53:05 -07006101 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006102 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006103 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006104 case IORING_OP_READV:
6105 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006106 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006107 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006108 case IORING_OP_WRITEV:
6109 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006110 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006111 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006112 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006113 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006114 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006115 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006116 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006117 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006118 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006119 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006120 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006121 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006122 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006123 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006124 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006125 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006126 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006127 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006128 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006129 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006130 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006131 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006132 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006133 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006134 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006135 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006136 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006137 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006138 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006139 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006140 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006141 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006142 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006143 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006144 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006145 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006146 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006147 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006148 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006149 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006150 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006151 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006152 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006153 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006154 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006155 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006156 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006157 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006158 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006159 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006160 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006161 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006162 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006163 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006164 case IORING_OP_SHUTDOWN:
6165 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006166 case IORING_OP_RENAMEAT:
6167 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006168 case IORING_OP_UNLINKAT:
6169 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006170 }
6171
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006172 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6173 req->opcode);
6174 return-EINVAL;
6175}
6176
Jens Axboedef596e2019-01-09 08:59:42 -07006177static int io_req_defer_prep(struct io_kiocb *req,
6178 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006179{
Jens Axboedef596e2019-01-09 08:59:42 -07006180 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006181 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006182 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006183 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006184 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185}
6186
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006187static u32 io_get_sequence(struct io_kiocb *req)
6188{
6189 struct io_kiocb *pos;
6190 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006191 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006192
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006193 io_for_each_link(pos, req)
6194 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006195
6196 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6197 return total_submitted - nr_reqs;
6198}
6199
Jens Axboe3529d8c2019-12-19 18:24:38 -07006200static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201{
6202 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006203 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006205 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206
6207 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006208 if (likely(list_empty_careful(&ctx->defer_list) &&
6209 !(req->flags & REQ_F_IO_DRAIN)))
6210 return 0;
6211
6212 seq = io_get_sequence(req);
6213 /* Still a chance to pass the sequence check */
6214 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006215 return 0;
6216
Jens Axboee8c2bc12020-08-15 18:44:09 -07006217 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006218 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006219 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006220 return ret;
6221 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006222 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006223 de = kmalloc(sizeof(*de), GFP_KERNEL);
6224 if (!de)
6225 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006226
6227 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006228 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006229 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006230 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006231 io_queue_async_work(req);
6232 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006233 }
6234
6235 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006236 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006237 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006238 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006239 spin_unlock_irq(&ctx->completion_lock);
6240 return -EIOCBQUEUED;
6241}
Jens Axboeedafcce2019-01-09 09:16:05 -07006242
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006243static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006244{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006245 if (req->flags & REQ_F_BUFFER_SELECTED) {
6246 switch (req->opcode) {
6247 case IORING_OP_READV:
6248 case IORING_OP_READ_FIXED:
6249 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006250 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006251 break;
6252 case IORING_OP_RECVMSG:
6253 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006254 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006255 break;
6256 }
6257 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006258 }
6259
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006260 if (req->flags & REQ_F_NEED_CLEANUP) {
6261 switch (req->opcode) {
6262 case IORING_OP_READV:
6263 case IORING_OP_READ_FIXED:
6264 case IORING_OP_READ:
6265 case IORING_OP_WRITEV:
6266 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006267 case IORING_OP_WRITE: {
6268 struct io_async_rw *io = req->async_data;
6269 if (io->free_iovec)
6270 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006271 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006272 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006273 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006274 case IORING_OP_SENDMSG: {
6275 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006276
6277 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006278 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006279 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006280 case IORING_OP_SPLICE:
6281 case IORING_OP_TEE:
6282 io_put_file(req, req->splice.file_in,
6283 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6284 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006285 case IORING_OP_OPENAT:
6286 case IORING_OP_OPENAT2:
6287 if (req->open.filename)
6288 putname(req->open.filename);
6289 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006290 case IORING_OP_RENAMEAT:
6291 putname(req->rename.oldpath);
6292 putname(req->rename.newpath);
6293 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006294 case IORING_OP_UNLINKAT:
6295 putname(req->unlink.filename);
6296 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006297 }
6298 req->flags &= ~REQ_F_NEED_CLEANUP;
6299 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006300}
6301
Pavel Begunkov889fca72021-02-10 00:03:09 +00006302static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006303{
Jens Axboeedafcce2019-01-09 09:16:05 -07006304 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006305 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006306
Jens Axboed625c6e2019-12-17 19:53:05 -07006307 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006308 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006309 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006310 break;
6311 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006312 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006313 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006314 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315 break;
6316 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006317 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006318 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006319 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320 break;
6321 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006322 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 break;
6324 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006325 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006326 break;
6327 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006328 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006329 break;
6330 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006331 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006332 break;
6333 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006334 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006335 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006336 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006337 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006338 break;
6339 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006340 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006341 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006342 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006343 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344 break;
6345 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006346 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006347 break;
6348 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006349 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006350 break;
6351 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006352 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006353 break;
6354 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006355 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 break;
6357 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006358 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006359 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006360 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006361 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006362 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006363 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006364 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006365 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006366 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006367 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006368 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006369 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006370 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006371 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006372 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006373 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006374 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006375 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006376 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006377 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006378 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006379 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006380 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006381 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006382 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006383 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006384 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006385 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006386 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006387 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006388 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006389 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006390 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006391 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006392 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006393 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006394 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006395 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006396 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006397 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006398 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006399 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006400 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006401 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006402 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006403 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006404 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006405 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006406 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006407 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006408 default:
6409 ret = -EINVAL;
6410 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006411 }
6412
6413 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006414 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006415
Jens Axboeb5325762020-05-19 21:20:27 -06006416 /* If the op doesn't have a file, we're not polling for it */
6417 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006418 const bool in_async = io_wq_current_is_worker();
6419
Jens Axboe11ba8202020-01-15 21:51:17 -07006420 /* workqueue context doesn't hold uring_lock, grab it now */
6421 if (in_async)
6422 mutex_lock(&ctx->uring_lock);
6423
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006424 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006425
6426 if (in_async)
6427 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006428 }
6429
6430 return 0;
6431}
6432
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006433static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006434{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006436 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006437 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006438
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006439 timeout = io_prep_linked_timeout(req);
6440 if (timeout)
6441 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006442
Jens Axboe4014d942021-01-19 15:53:54 -07006443 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006444 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006445
Jens Axboe561fb042019-10-24 07:25:42 -06006446 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006447 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006448 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006449 /*
6450 * We can get EAGAIN for polled IO even though we're
6451 * forcing a sync submission from here, since we can't
6452 * wait for request slots on the block side.
6453 */
6454 if (ret != -EAGAIN)
6455 break;
6456 cond_resched();
6457 } while (1);
6458 }
Jens Axboe31b51512019-01-18 22:56:34 -07006459
Jens Axboe561fb042019-10-24 07:25:42 -06006460 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006461 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006462
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006463 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6464 lock_ctx = req->ctx;
6465
6466 /*
6467 * io_iopoll_complete() does not hold completion_lock to
6468 * complete polled io, so here for polled io, we can not call
6469 * io_req_complete() directly, otherwise there maybe concurrent
6470 * access to cqring, defer_list, etc, which is not safe. Given
6471 * that io_iopoll_complete() is always called under uring_lock,
6472 * so here for polled io, we also get uring_lock to complete
6473 * it.
6474 */
6475 if (lock_ctx)
6476 mutex_lock(&lock_ctx->uring_lock);
6477
6478 req_set_fail_links(req);
6479 io_req_complete(req, ret);
6480
6481 if (lock_ctx)
6482 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006483 }
Jens Axboe31b51512019-01-18 22:56:34 -07006484}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006485
Jens Axboe65e19f52019-10-26 07:20:21 -06006486static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6487 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006488{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006489 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006490
Jens Axboe05f3fb32019-12-09 11:22:50 -07006491 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006492 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006493}
6494
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006495static struct file *io_file_get(struct io_submit_state *state,
6496 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006497{
6498 struct io_ring_ctx *ctx = req->ctx;
6499 struct file *file;
6500
6501 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006502 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006503 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006504 fd = array_index_nospec(fd, ctx->nr_user_files);
6505 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006506 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006507 } else {
6508 trace_io_uring_file_get(ctx, fd);
6509 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006510 }
6511
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006512 if (file && unlikely(file->f_op == &io_uring_fops))
6513 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006514 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006515}
6516
Jens Axboe2665abf2019-11-05 12:40:47 -07006517static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6518{
Jens Axboead8a48a2019-11-15 08:49:11 -07006519 struct io_timeout_data *data = container_of(timer,
6520 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006521 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006522 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006523 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006524
6525 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006526 prev = req->timeout.head;
6527 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006528
6529 /*
6530 * We don't expect the list to be empty, that will only happen if we
6531 * race with the completion of the linked work.
6532 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006533 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006534 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006535 else
6536 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006537 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6538
6539 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006540 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006541 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006542 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006543 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006544 io_req_complete_post(req, -ETIME, 0);
6545 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006546 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006547 return HRTIMER_NORESTART;
6548}
6549
Jens Axboe7271ef32020-08-10 09:55:22 -06006550static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006551{
Jens Axboe76a46e02019-11-10 23:34:16 -07006552 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006553 * If the back reference is NULL, then our linked request finished
6554 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006555 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006556 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006557 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006558
Jens Axboead8a48a2019-11-15 08:49:11 -07006559 data->timer.function = io_link_timeout_fn;
6560 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6561 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006562 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006563}
6564
6565static void io_queue_linked_timeout(struct io_kiocb *req)
6566{
6567 struct io_ring_ctx *ctx = req->ctx;
6568
6569 spin_lock_irq(&ctx->completion_lock);
6570 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006571 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006572
Jens Axboe2665abf2019-11-05 12:40:47 -07006573 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006574 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006575}
6576
Jens Axboead8a48a2019-11-15 08:49:11 -07006577static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006578{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006579 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006580
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006581 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6582 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006583 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006584
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006585 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006586 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006587 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006588 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006589}
6590
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006591static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006592{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006593 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006594 const struct cred *old_creds = NULL;
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006595 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006596
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006597again:
6598 linked_timeout = io_prep_linked_timeout(req);
6599
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006600 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6601 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006602 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006603 if (old_creds)
6604 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006605 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006606 old_creds = NULL; /* restored original creds */
6607 else
Jens Axboe98447d62020-10-14 10:48:51 -06006608 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006609 }
6610
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006611 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006612
6613 /*
6614 * We async punt it if the file wasn't marked NOWAIT, or if the file
6615 * doesn't support non-blocking read/write attempts
6616 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006617 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006618 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006619 /*
6620 * Queued up for async execution, worker will release
6621 * submit reference when the iocb is actually submitted.
6622 */
6623 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006624 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006625
Pavel Begunkovf063c542020-07-25 14:41:59 +03006626 if (linked_timeout)
6627 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006628 } else if (likely(!ret)) {
6629 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006630 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006631 struct io_ring_ctx *ctx = req->ctx;
6632 struct io_comp_state *cs = &ctx->submit_state.comp;
6633
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006634 cs->reqs[cs->nr++] = req;
6635 if (cs->nr == IO_COMPL_BATCH)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006636 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006637 req = NULL;
6638 } else {
6639 req = io_put_req_find_next(req);
6640 }
6641
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006642 if (linked_timeout)
6643 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006644
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006645 if (req) {
6646 if (!(req->flags & REQ_F_FORCE_ASYNC))
6647 goto again;
6648 io_queue_async_work(req);
6649 }
6650 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006651 /* un-prep timeout, so it'll be killed as any other linked */
6652 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006653 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006654 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006655 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006656 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006657
Jens Axboe193155c2020-02-22 23:22:19 -07006658 if (old_creds)
6659 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006660}
6661
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006662static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006663{
6664 int ret;
6665
Jens Axboe3529d8c2019-12-19 18:24:38 -07006666 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006667 if (ret) {
6668 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006669fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006670 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006671 io_put_req(req);
6672 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006673 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006674 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006675 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006676 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006677 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006678 goto fail_req;
6679 }
Jens Axboece35a472019-12-17 08:04:44 -07006680 io_queue_async_work(req);
6681 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006682 if (sqe) {
6683 ret = io_req_prep(req, sqe);
6684 if (unlikely(ret))
6685 goto fail_req;
6686 }
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006687 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006688 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006689}
6690
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006691static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006692{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006693 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006694 io_put_req(req);
6695 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006696 } else
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006697 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006698}
6699
Pavel Begunkov863e0562020-10-27 23:25:35 +00006700struct io_submit_link {
6701 struct io_kiocb *head;
6702 struct io_kiocb *last;
6703};
6704
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006705static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006706 struct io_submit_link *link)
Jens Axboe9e645e112019-05-10 16:07:28 -06006707{
Jackie Liua197f662019-11-08 08:09:12 -07006708 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006709 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006710
Jens Axboe9e645e112019-05-10 16:07:28 -06006711 /*
6712 * If we already have a head request, queue this one for async
6713 * submittal once the head completes. If we don't have a head but
6714 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6715 * submitted sync once the chain is complete. If none of those
6716 * conditions are true (normal request), then just queue it.
6717 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006718 if (link->head) {
6719 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006720
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006721 /*
6722 * Taking sequential execution of a link, draining both sides
6723 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6724 * requests in the link. So, it drains the head and the
6725 * next after the link request. The last one is done via
6726 * drain_next flag to persist the effect across calls.
6727 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006728 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006729 head->flags |= REQ_F_IO_DRAIN;
6730 ctx->drain_next = 1;
6731 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006732 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006733 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006734 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006735 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006736 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006737 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006738 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006739 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006740 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006741
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006742 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006743 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006744 io_queue_link_head(head);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006745 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006746 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006747 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006748 if (unlikely(ctx->drain_next)) {
6749 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006750 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006751 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006752 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006753 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006754 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006755 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006756 link->head = req;
6757 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006758 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006759 io_queue_sqe(req, sqe);
Pavel Begunkov711be032020-01-17 03:57:59 +03006760 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006761 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006762
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006763 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006764}
6765
Jens Axboe9a56a232019-01-09 09:06:50 -07006766/*
6767 * Batched submission is done, ensure local IO is flushed out.
6768 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006769static void io_submit_state_end(struct io_submit_state *state,
6770 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006771{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006772 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006773 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006774 if (state->plug_started)
6775 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006776 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07006777}
6778
6779/*
6780 * Start submission side cache.
6781 */
6782static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006783 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006784{
Jens Axboe27926b62020-10-28 09:33:23 -06006785 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006786 state->ios_left = max_ios;
6787}
6788
Jens Axboe2b188cc2019-01-07 10:46:33 -07006789static void io_commit_sqring(struct io_ring_ctx *ctx)
6790{
Hristo Venev75b28af2019-08-26 17:23:46 +00006791 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006792
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006793 /*
6794 * Ensure any loads from the SQEs are done at this point,
6795 * since once we write the new head, the application could
6796 * write new data to them.
6797 */
6798 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006799}
6800
6801/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006802 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803 * that is mapped by userspace. This means that care needs to be taken to
6804 * ensure that reads are stable, as we cannot rely on userspace always
6805 * being a good citizen. If members of the sqe are validated and then later
6806 * used, it's important that those reads are done through READ_ONCE() to
6807 * prevent a re-load down the line.
6808 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006809static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006810{
Hristo Venev75b28af2019-08-26 17:23:46 +00006811 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006812 unsigned head;
6813
6814 /*
6815 * The cached sq head (or cq tail) serves two purposes:
6816 *
6817 * 1) allows us to batch the cost of updating the user visible
6818 * head updates.
6819 * 2) allows the kernel side to track the head on its own, even
6820 * though the application is the one updating it.
6821 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006822 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006823 if (likely(head < ctx->sq_entries))
6824 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006825
6826 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006827 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006828 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006829 return NULL;
6830}
6831
6832static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6833{
6834 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006835}
6836
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006837/*
6838 * Check SQE restrictions (opcode and flags).
6839 *
6840 * Returns 'true' if SQE is allowed, 'false' otherwise.
6841 */
6842static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6843 struct io_kiocb *req,
6844 unsigned int sqe_flags)
6845{
6846 if (!ctx->restricted)
6847 return true;
6848
6849 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6850 return false;
6851
6852 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6853 ctx->restrictions.sqe_flags_required)
6854 return false;
6855
6856 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6857 ctx->restrictions.sqe_flags_required))
6858 return false;
6859
6860 return true;
6861}
6862
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006863#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6864 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6865 IOSQE_BUFFER_SELECT)
6866
6867static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006868 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006869{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006870 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006871 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006872 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006873
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006874 req->opcode = READ_ONCE(sqe->opcode);
6875 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006876 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006877 req->file = NULL;
6878 req->ctx = ctx;
6879 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006880 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006881 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006882 /* one is dropped after submission, the other at completion */
6883 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006884 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006885 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006886
6887 if (unlikely(req->opcode >= IORING_OP_LAST))
6888 return -EINVAL;
6889
Jens Axboe28cea78a2020-09-14 10:51:17 -06006890 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006891 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006892
6893 sqe_flags = READ_ONCE(sqe->flags);
6894 /* enforce forwards compatibility on users */
6895 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6896 return -EINVAL;
6897
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006898 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6899 return -EACCES;
6900
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006901 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6902 !io_op_defs[req->opcode].buffer_select)
6903 return -EOPNOTSUPP;
6904
6905 id = READ_ONCE(sqe->personality);
6906 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006907 struct io_identity *iod;
6908
Jens Axboe1e6fa522020-10-15 08:46:24 -06006909 iod = idr_find(&ctx->personality_idr, id);
6910 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006911 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006912 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006913
6914 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006915 get_cred(iod->creds);
6916 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006917 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006918 }
6919
6920 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006921 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006922 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006923
Jens Axboe27926b62020-10-28 09:33:23 -06006924 /*
6925 * Plug now if we have more than 1 IO left after this, and the target
6926 * is potentially a read/write to block based storage.
6927 */
6928 if (!state->plug_started && state->ios_left > 1 &&
6929 io_op_defs[req->opcode].plug) {
6930 blk_start_plug(&state->plug);
6931 state->plug_started = true;
6932 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006933
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006934 ret = 0;
6935 if (io_op_defs[req->opcode].needs_file) {
6936 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006937
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006938 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006939 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006940 ret = -EBADF;
6941 }
6942
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006943 state->ios_left--;
6944 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006945}
6946
Jens Axboe0f212202020-09-13 13:09:39 -06006947static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006948{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006949 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006950 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006951
Jens Axboec4a2ed72019-11-21 21:01:26 -07006952 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006953 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006954 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006955 return -EBUSY;
6956 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006957
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006958 /* make sure SQ entry isn't read before tail */
6959 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006960
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006961 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6962 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006963
Jens Axboed8a6df12020-10-15 16:24:45 -06006964 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006965 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006966
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006967 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006968 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006969
Jens Axboe6c271ce2019-01-10 11:22:30 -07006970 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006971 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006972 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006973 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006974
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006975 sqe = io_get_sqe(ctx);
6976 if (unlikely(!sqe)) {
6977 io_consume_sqe(ctx);
6978 break;
6979 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006980 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006981 if (unlikely(!req)) {
6982 if (!submitted)
6983 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006984 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006985 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006986 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006987 /* will complete beyond this point, count as submitted */
6988 submitted++;
6989
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006990 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006991 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006992fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006993 io_put_req(req);
6994 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006995 break;
6996 }
6997
Jens Axboe354420f2020-01-08 18:55:15 -07006998 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006999 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007000 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03007001 if (err)
7002 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007003 }
7004
Pavel Begunkov9466f432020-01-25 22:34:01 +03007005 if (unlikely(submitted != nr)) {
7006 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007007 struct io_uring_task *tctx = current->io_uring;
7008 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007009
Jens Axboed8a6df12020-10-15 16:24:45 -06007010 percpu_ref_put_many(&ctx->refs, unused);
7011 percpu_counter_sub(&tctx->inflight, unused);
7012 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03007013 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00007014 if (link.head)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007015 io_queue_link_head(link.head);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007016 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007017
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007018 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7019 io_commit_sqring(ctx);
7020
Jens Axboe6c271ce2019-01-10 11:22:30 -07007021 return submitted;
7022}
7023
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007024static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7025{
7026 /* Tell userspace we may need a wakeup call */
7027 spin_lock_irq(&ctx->completion_lock);
7028 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
7029 spin_unlock_irq(&ctx->completion_lock);
7030}
7031
7032static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7033{
7034 spin_lock_irq(&ctx->completion_lock);
7035 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
7036 spin_unlock_irq(&ctx->completion_lock);
7037}
7038
Xiaoguang Wang08369242020-11-03 14:15:59 +08007039static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007040{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007041 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007042 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007043
Jens Axboec8d1ba52020-09-14 11:07:26 -06007044 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007045 /* if we're handling multiple rings, cap submit size for fairness */
7046 if (cap_entries && to_submit > 8)
7047 to_submit = 8;
7048
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007049 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7050 unsigned nr_events = 0;
7051
Xiaoguang Wang08369242020-11-03 14:15:59 +08007052 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007053 if (!list_empty(&ctx->iopoll_list))
7054 io_do_iopoll(ctx, &nr_events, 0);
7055
Pavel Begunkovd9d05212021-01-08 20:57:25 +00007056 if (to_submit && !ctx->sqo_dead &&
7057 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007058 ret = io_submit_sqes(ctx, to_submit);
7059 mutex_unlock(&ctx->uring_lock);
7060 }
Jens Axboe90554202020-09-03 12:12:41 -06007061
7062 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
7063 wake_up(&ctx->sqo_sq_wait);
7064
Xiaoguang Wang08369242020-11-03 14:15:59 +08007065 return ret;
7066}
7067
7068static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7069{
7070 struct io_ring_ctx *ctx;
7071 unsigned sq_thread_idle = 0;
7072
7073 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7074 if (sq_thread_idle < ctx->sq_thread_idle)
7075 sq_thread_idle = ctx->sq_thread_idle;
7076 }
7077
7078 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007079}
7080
Jens Axboe69fb2132020-09-14 11:16:23 -06007081static void io_sqd_init_new(struct io_sq_data *sqd)
7082{
7083 struct io_ring_ctx *ctx;
7084
7085 while (!list_empty(&sqd->ctx_new_list)) {
7086 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007087 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7088 complete(&ctx->sq_thread_comp);
7089 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007090
7091 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007092}
7093
Jens Axboe6c271ce2019-01-10 11:22:30 -07007094static int io_sq_thread(void *data)
7095{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007096 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007097 struct files_struct *old_files = current->files;
7098 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007099 const struct cred *old_cred = NULL;
7100 struct io_sq_data *sqd = data;
7101 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007102 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007103 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007104
Jens Axboe28cea78a2020-09-14 10:51:17 -06007105 task_lock(current);
7106 current->files = NULL;
7107 current->nsproxy = NULL;
7108 task_unlock(current);
7109
Jens Axboe69fb2132020-09-14 11:16:23 -06007110 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007111 int ret;
7112 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007113
7114 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007115 * Any changes to the sqd lists are synchronized through the
7116 * kthread parking. This synchronizes the thread vs users,
7117 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007118 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007119 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007120 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007121 /*
7122 * When sq thread is unparked, in case the previous park operation
7123 * comes from io_put_sq_data(), which means that sq thread is going
7124 * to be stopped, so here needs to have a check.
7125 */
7126 if (kthread_should_stop())
7127 break;
7128 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007129
Xiaoguang Wang08369242020-11-03 14:15:59 +08007130 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007131 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007132 timeout = jiffies + sqd->sq_thread_idle;
7133 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007134
Xiaoguang Wang08369242020-11-03 14:15:59 +08007135 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007136 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007137 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7138 if (current->cred != ctx->creds) {
7139 if (old_cred)
7140 revert_creds(old_cred);
7141 old_cred = override_creds(ctx->creds);
7142 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007143 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007144#ifdef CONFIG_AUDIT
7145 current->loginuid = ctx->loginuid;
7146 current->sessionid = ctx->sessionid;
7147#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007148
Xiaoguang Wang08369242020-11-03 14:15:59 +08007149 ret = __io_sq_thread(ctx, cap_entries);
7150 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7151 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007152
Jens Axboe28cea78a2020-09-14 10:51:17 -06007153 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007154 }
7155
Xiaoguang Wang08369242020-11-03 14:15:59 +08007156 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007157 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007158 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007159 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007160 if (sqt_spin)
7161 timeout = jiffies + sqd->sq_thread_idle;
7162 continue;
7163 }
7164
Xiaoguang Wang08369242020-11-03 14:15:59 +08007165 needs_sched = true;
7166 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7167 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7168 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7169 !list_empty_careful(&ctx->iopoll_list)) {
7170 needs_sched = false;
7171 break;
7172 }
7173 if (io_sqring_entries(ctx)) {
7174 needs_sched = false;
7175 break;
7176 }
7177 }
7178
Hao Xu8b28fdf2021-01-31 22:39:04 +08007179 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007180 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7181 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007182
Jens Axboe69fb2132020-09-14 11:16:23 -06007183 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007184 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7185 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007186 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007187
7188 finish_wait(&sqd->wait, &wait);
7189 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007190 }
7191
Jens Axboe4c6e2772020-07-01 11:29:10 -06007192 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007193 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007194
Dennis Zhou91d8f512020-09-16 13:41:05 -07007195 if (cur_css)
7196 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007197 if (old_cred)
7198 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007199
Jens Axboe28cea78a2020-09-14 10:51:17 -06007200 task_lock(current);
7201 current->files = old_files;
7202 current->nsproxy = old_nsproxy;
7203 task_unlock(current);
7204
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007205 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007206
Jens Axboe6c271ce2019-01-10 11:22:30 -07007207 return 0;
7208}
7209
Jens Axboebda52162019-09-24 13:47:15 -06007210struct io_wait_queue {
7211 struct wait_queue_entry wq;
7212 struct io_ring_ctx *ctx;
7213 unsigned to_wait;
7214 unsigned nr_timeouts;
7215};
7216
Pavel Begunkov6c503152021-01-04 20:36:36 +00007217static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007218{
7219 struct io_ring_ctx *ctx = iowq->ctx;
7220
7221 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007222 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007223 * started waiting. For timeouts, we always want to return to userspace,
7224 * regardless of event count.
7225 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007226 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007227 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7228}
7229
7230static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7231 int wake_flags, void *key)
7232{
7233 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7234 wq);
7235
Pavel Begunkov6c503152021-01-04 20:36:36 +00007236 /*
7237 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7238 * the task, and the next invocation will do it.
7239 */
7240 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7241 return autoremove_wake_function(curr, mode, wake_flags, key);
7242 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007243}
7244
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007245static int io_run_task_work_sig(void)
7246{
7247 if (io_run_task_work())
7248 return 1;
7249 if (!signal_pending(current))
7250 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007251 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7252 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007253 return -EINTR;
7254}
7255
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007256/* when returns >0, the caller should retry */
7257static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7258 struct io_wait_queue *iowq,
7259 signed long *timeout)
7260{
7261 int ret;
7262
7263 /* make sure we run task_work before checking for signals */
7264 ret = io_run_task_work_sig();
7265 if (ret || io_should_wake(iowq))
7266 return ret;
7267 /* let the caller flush overflows, retry */
7268 if (test_bit(0, &ctx->cq_check_overflow))
7269 return 1;
7270
7271 *timeout = schedule_timeout(*timeout);
7272 return !*timeout ? -ETIME : 1;
7273}
7274
Jens Axboe2b188cc2019-01-07 10:46:33 -07007275/*
7276 * Wait until events become available, if we don't already have some. The
7277 * application must reap them itself, as they reside on the shared cq ring.
7278 */
7279static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007280 const sigset_t __user *sig, size_t sigsz,
7281 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007282{
Jens Axboebda52162019-09-24 13:47:15 -06007283 struct io_wait_queue iowq = {
7284 .wq = {
7285 .private = current,
7286 .func = io_wake_function,
7287 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7288 },
7289 .ctx = ctx,
7290 .to_wait = min_events,
7291 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007292 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007293 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7294 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007295
Jens Axboeb41e9852020-02-17 09:52:41 -07007296 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007297 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7298 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007299 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007300 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007301 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007302 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007303
7304 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007305#ifdef CONFIG_COMPAT
7306 if (in_compat_syscall())
7307 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007308 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007309 else
7310#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007311 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007312
Jens Axboe2b188cc2019-01-07 10:46:33 -07007313 if (ret)
7314 return ret;
7315 }
7316
Hao Xuc73ebb62020-11-03 10:54:37 +08007317 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007318 struct timespec64 ts;
7319
Hao Xuc73ebb62020-11-03 10:54:37 +08007320 if (get_timespec64(&ts, uts))
7321 return -EFAULT;
7322 timeout = timespec64_to_jiffies(&ts);
7323 }
7324
Jens Axboebda52162019-09-24 13:47:15 -06007325 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007326 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007327 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007328 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007329 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7330 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007331 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7332 finish_wait(&ctx->wait, &iowq.wq);
7333 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007334
Jens Axboeb7db41c2020-07-04 08:55:50 -06007335 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007336
Hristo Venev75b28af2019-08-26 17:23:46 +00007337 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007338}
7339
Jens Axboe6b063142019-01-10 22:13:58 -07007340static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7341{
7342#if defined(CONFIG_UNIX)
7343 if (ctx->ring_sock) {
7344 struct sock *sock = ctx->ring_sock->sk;
7345 struct sk_buff *skb;
7346
7347 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7348 kfree_skb(skb);
7349 }
7350#else
7351 int i;
7352
Jens Axboe65e19f52019-10-26 07:20:21 -06007353 for (i = 0; i < ctx->nr_user_files; i++) {
7354 struct file *file;
7355
7356 file = io_file_from_index(ctx, i);
7357 if (file)
7358 fput(file);
7359 }
Jens Axboe6b063142019-01-10 22:13:58 -07007360#endif
7361}
7362
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007363static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007364{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007365 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007366
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007367 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007368 complete(&data->done);
7369}
7370
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007371static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7372{
7373 spin_lock_bh(&ctx->rsrc_ref_lock);
7374}
7375
7376static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7377{
7378 spin_unlock_bh(&ctx->rsrc_ref_lock);
7379}
7380
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007381static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7382 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007383 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007384{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007385 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007386 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007387 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007388 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007389 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007390}
7391
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007392static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7393 struct io_ring_ctx *ctx,
7394 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007395{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007396 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007397 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007398
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007399 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007400 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007401 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007402 if (ref_node)
7403 percpu_ref_kill(&ref_node->refs);
7404
7405 percpu_ref_kill(&data->refs);
7406
7407 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007408 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007409 do {
7410 ret = wait_for_completion_interruptible(&data->done);
7411 if (!ret)
7412 break;
7413 ret = io_run_task_work_sig();
7414 if (ret < 0) {
7415 percpu_ref_resurrect(&data->refs);
7416 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007417 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007418 return ret;
7419 }
7420 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007422 destroy_fixed_rsrc_ref_node(backup_node);
7423 return 0;
7424}
7425
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007426static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7427{
7428 struct fixed_rsrc_data *data;
7429
7430 data = kzalloc(sizeof(*data), GFP_KERNEL);
7431 if (!data)
7432 return NULL;
7433
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007434 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007435 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7436 kfree(data);
7437 return NULL;
7438 }
7439 data->ctx = ctx;
7440 init_completion(&data->done);
7441 return data;
7442}
7443
7444static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7445{
7446 percpu_ref_exit(&data->refs);
7447 kfree(data->table);
7448 kfree(data);
7449}
7450
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007451static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7452{
7453 struct fixed_rsrc_data *data = ctx->file_data;
7454 struct fixed_rsrc_ref_node *backup_node;
7455 unsigned nr_tables, i;
7456 int ret;
7457
7458 if (!data)
7459 return -ENXIO;
7460 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7461 if (!backup_node)
7462 return -ENOMEM;
7463 init_fixed_file_ref_node(ctx, backup_node);
7464
7465 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7466 if (ret)
7467 return ret;
7468
Jens Axboe6b063142019-01-10 22:13:58 -07007469 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007470 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7471 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007472 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007473 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007474 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007475 ctx->nr_user_files = 0;
7476 return 0;
7477}
7478
Jens Axboe534ca6d2020-09-02 13:52:19 -06007479static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007480{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007481 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007482 /*
7483 * The park is a bit of a work-around, without it we get
7484 * warning spews on shutdown with SQPOLL set and affinity
7485 * set to a single CPU.
7486 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007487 if (sqd->thread) {
7488 kthread_park(sqd->thread);
7489 kthread_stop(sqd->thread);
7490 }
7491
7492 kfree(sqd);
7493 }
7494}
7495
Jens Axboeaa061652020-09-02 14:50:27 -06007496static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7497{
7498 struct io_ring_ctx *ctx_attach;
7499 struct io_sq_data *sqd;
7500 struct fd f;
7501
7502 f = fdget(p->wq_fd);
7503 if (!f.file)
7504 return ERR_PTR(-ENXIO);
7505 if (f.file->f_op != &io_uring_fops) {
7506 fdput(f);
7507 return ERR_PTR(-EINVAL);
7508 }
7509
7510 ctx_attach = f.file->private_data;
7511 sqd = ctx_attach->sq_data;
7512 if (!sqd) {
7513 fdput(f);
7514 return ERR_PTR(-EINVAL);
7515 }
7516
7517 refcount_inc(&sqd->refs);
7518 fdput(f);
7519 return sqd;
7520}
7521
Jens Axboe534ca6d2020-09-02 13:52:19 -06007522static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7523{
7524 struct io_sq_data *sqd;
7525
Jens Axboeaa061652020-09-02 14:50:27 -06007526 if (p->flags & IORING_SETUP_ATTACH_WQ)
7527 return io_attach_sq_data(p);
7528
Jens Axboe534ca6d2020-09-02 13:52:19 -06007529 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7530 if (!sqd)
7531 return ERR_PTR(-ENOMEM);
7532
7533 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007534 INIT_LIST_HEAD(&sqd->ctx_list);
7535 INIT_LIST_HEAD(&sqd->ctx_new_list);
7536 mutex_init(&sqd->ctx_lock);
7537 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007538 init_waitqueue_head(&sqd->wait);
7539 return sqd;
7540}
7541
Jens Axboe69fb2132020-09-14 11:16:23 -06007542static void io_sq_thread_unpark(struct io_sq_data *sqd)
7543 __releases(&sqd->lock)
7544{
7545 if (!sqd->thread)
7546 return;
7547 kthread_unpark(sqd->thread);
7548 mutex_unlock(&sqd->lock);
7549}
7550
7551static void io_sq_thread_park(struct io_sq_data *sqd)
7552 __acquires(&sqd->lock)
7553{
7554 if (!sqd->thread)
7555 return;
7556 mutex_lock(&sqd->lock);
7557 kthread_park(sqd->thread);
7558}
7559
Jens Axboe534ca6d2020-09-02 13:52:19 -06007560static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7561{
7562 struct io_sq_data *sqd = ctx->sq_data;
7563
7564 if (sqd) {
7565 if (sqd->thread) {
7566 /*
7567 * We may arrive here from the error branch in
7568 * io_sq_offload_create() where the kthread is created
7569 * without being waked up, thus wake it up now to make
7570 * sure the wait will complete.
7571 */
7572 wake_up_process(sqd->thread);
7573 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007574
7575 io_sq_thread_park(sqd);
7576 }
7577
7578 mutex_lock(&sqd->ctx_lock);
7579 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007580 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007581 mutex_unlock(&sqd->ctx_lock);
7582
Xiaoguang Wang08369242020-11-03 14:15:59 +08007583 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007584 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007585
7586 io_put_sq_data(sqd);
7587 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007588 }
7589}
7590
Jens Axboe6b063142019-01-10 22:13:58 -07007591static void io_finish_async(struct io_ring_ctx *ctx)
7592{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007593 io_sq_thread_stop(ctx);
7594
Jens Axboe561fb042019-10-24 07:25:42 -06007595 if (ctx->io_wq) {
7596 io_wq_destroy(ctx->io_wq);
7597 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007598 }
7599}
7600
7601#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007602/*
7603 * Ensure the UNIX gc is aware of our file set, so we are certain that
7604 * the io_uring can be safely unregistered on process exit, even if we have
7605 * loops in the file referencing.
7606 */
7607static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7608{
7609 struct sock *sk = ctx->ring_sock->sk;
7610 struct scm_fp_list *fpl;
7611 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007612 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007613
Jens Axboe6b063142019-01-10 22:13:58 -07007614 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7615 if (!fpl)
7616 return -ENOMEM;
7617
7618 skb = alloc_skb(0, GFP_KERNEL);
7619 if (!skb) {
7620 kfree(fpl);
7621 return -ENOMEM;
7622 }
7623
7624 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007625
Jens Axboe08a45172019-10-03 08:11:03 -06007626 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007627 fpl->user = get_uid(ctx->user);
7628 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007629 struct file *file = io_file_from_index(ctx, i + offset);
7630
7631 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007632 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007633 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007634 unix_inflight(fpl->user, fpl->fp[nr_files]);
7635 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007636 }
7637
Jens Axboe08a45172019-10-03 08:11:03 -06007638 if (nr_files) {
7639 fpl->max = SCM_MAX_FD;
7640 fpl->count = nr_files;
7641 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007642 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007643 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7644 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007645
Jens Axboe08a45172019-10-03 08:11:03 -06007646 for (i = 0; i < nr_files; i++)
7647 fput(fpl->fp[i]);
7648 } else {
7649 kfree_skb(skb);
7650 kfree(fpl);
7651 }
Jens Axboe6b063142019-01-10 22:13:58 -07007652
7653 return 0;
7654}
7655
7656/*
7657 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7658 * causes regular reference counting to break down. We rely on the UNIX
7659 * garbage collection to take care of this problem for us.
7660 */
7661static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7662{
7663 unsigned left, total;
7664 int ret = 0;
7665
7666 total = 0;
7667 left = ctx->nr_user_files;
7668 while (left) {
7669 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007670
7671 ret = __io_sqe_files_scm(ctx, this_files, total);
7672 if (ret)
7673 break;
7674 left -= this_files;
7675 total += this_files;
7676 }
7677
7678 if (!ret)
7679 return 0;
7680
7681 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007682 struct file *file = io_file_from_index(ctx, total);
7683
7684 if (file)
7685 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007686 total++;
7687 }
7688
7689 return ret;
7690}
7691#else
7692static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7693{
7694 return 0;
7695}
7696#endif
7697
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007698static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007699 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007700{
7701 int i;
7702
7703 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007704 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007705 unsigned this_files;
7706
7707 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7708 table->files = kcalloc(this_files, sizeof(struct file *),
7709 GFP_KERNEL);
7710 if (!table->files)
7711 break;
7712 nr_files -= this_files;
7713 }
7714
7715 if (i == nr_tables)
7716 return 0;
7717
7718 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007719 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007720 kfree(table->files);
7721 }
7722 return 1;
7723}
7724
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007725static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007726{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007727 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007728#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007729 struct sock *sock = ctx->ring_sock->sk;
7730 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7731 struct sk_buff *skb;
7732 int i;
7733
7734 __skb_queue_head_init(&list);
7735
7736 /*
7737 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7738 * remove this entry and rearrange the file array.
7739 */
7740 skb = skb_dequeue(head);
7741 while (skb) {
7742 struct scm_fp_list *fp;
7743
7744 fp = UNIXCB(skb).fp;
7745 for (i = 0; i < fp->count; i++) {
7746 int left;
7747
7748 if (fp->fp[i] != file)
7749 continue;
7750
7751 unix_notinflight(fp->user, fp->fp[i]);
7752 left = fp->count - 1 - i;
7753 if (left) {
7754 memmove(&fp->fp[i], &fp->fp[i + 1],
7755 left * sizeof(struct file *));
7756 }
7757 fp->count--;
7758 if (!fp->count) {
7759 kfree_skb(skb);
7760 skb = NULL;
7761 } else {
7762 __skb_queue_tail(&list, skb);
7763 }
7764 fput(file);
7765 file = NULL;
7766 break;
7767 }
7768
7769 if (!file)
7770 break;
7771
7772 __skb_queue_tail(&list, skb);
7773
7774 skb = skb_dequeue(head);
7775 }
7776
7777 if (skb_peek(&list)) {
7778 spin_lock_irq(&head->lock);
7779 while ((skb = __skb_dequeue(&list)) != NULL)
7780 __skb_queue_tail(head, skb);
7781 spin_unlock_irq(&head->lock);
7782 }
7783#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007784 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007785#endif
7786}
7787
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007788static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007789{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007790 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7791 struct io_ring_ctx *ctx = rsrc_data->ctx;
7792 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007793
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007794 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7795 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007796 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007797 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798 }
7799
Xiaoguang Wang05589552020-03-31 14:05:18 +08007800 percpu_ref_exit(&ref_node->refs);
7801 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007802 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007803}
7804
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007805static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007806{
7807 struct io_ring_ctx *ctx;
7808 struct llist_node *node;
7809
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007810 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7811 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007812
7813 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007814 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007815 struct llist_node *next = node->next;
7816
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007817 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7818 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007819 node = next;
7820 }
7821}
7822
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007823static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7824 unsigned i)
7825{
7826 struct fixed_rsrc_table *table;
7827
7828 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7829 return &table->files[i & IORING_FILE_TABLE_MASK];
7830}
7831
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007832static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007833{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007834 struct fixed_rsrc_ref_node *ref_node;
7835 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007836 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007837 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007838 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007839
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007840 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7841 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007842 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007843
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007844 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007845 ref_node->done = true;
7846
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007847 while (!list_empty(&ctx->rsrc_ref_list)) {
7848 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007849 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007850 /* recycle ref nodes in order */
7851 if (!ref_node->done)
7852 break;
7853 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007854 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007855 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007856 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007857
7858 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007859 delay = 0;
7860
Jens Axboe4a38aed22020-05-14 17:21:15 -06007861 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007862 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007863 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007864 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007865}
7866
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007867static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007868 struct io_ring_ctx *ctx)
7869{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007870 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007871
7872 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7873 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007874 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007875
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007876 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007877 0, GFP_KERNEL)) {
7878 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007879 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007880 }
7881 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007882 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007883 ref_node->done = false;
7884 return ref_node;
7885}
7886
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007887static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7888 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007889{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007890 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007891 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007892}
7893
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007894static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007895{
7896 percpu_ref_exit(&ref_node->refs);
7897 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007898}
7899
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007900
Jens Axboe05f3fb32019-12-09 11:22:50 -07007901static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7902 unsigned nr_args)
7903{
7904 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007905 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007906 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007907 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007908 struct fixed_rsrc_ref_node *ref_node;
7909 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007910
7911 if (ctx->file_data)
7912 return -EBUSY;
7913 if (!nr_args)
7914 return -EINVAL;
7915 if (nr_args > IORING_MAX_FIXED_FILES)
7916 return -EMFILE;
7917
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007918 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007919 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007920 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007921 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007922
7923 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007924 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007925 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007926 if (!file_data->table)
7927 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007928
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007929 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007930 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007931
7932 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007933 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7934 ret = -EFAULT;
7935 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007936 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007937 /* allow sparse sets */
7938 if (fd == -1)
7939 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007940
Jens Axboe05f3fb32019-12-09 11:22:50 -07007941 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007942 ret = -EBADF;
7943 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007944 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007945
7946 /*
7947 * Don't allow io_uring instances to be registered. If UNIX
7948 * isn't enabled, then this causes a reference cycle and this
7949 * instance can never get freed. If UNIX is enabled we'll
7950 * handle it just fine, but there's still no point in allowing
7951 * a ring fd as it doesn't support regular read/write anyway.
7952 */
7953 if (file->f_op == &io_uring_fops) {
7954 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007955 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007956 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007957 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007958 }
7959
Jens Axboe05f3fb32019-12-09 11:22:50 -07007960 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007961 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007962 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007963 return ret;
7964 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007965
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007966 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007967 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007968 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007969 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007970 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007971 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007972
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007973 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007974 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007975out_fput:
7976 for (i = 0; i < ctx->nr_user_files; i++) {
7977 file = io_file_from_index(ctx, i);
7978 if (file)
7979 fput(file);
7980 }
7981 for (i = 0; i < nr_tables; i++)
7982 kfree(file_data->table[i].files);
7983 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007984out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007985 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007986 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007987 return ret;
7988}
7989
Jens Axboec3a31e62019-10-03 13:59:56 -06007990static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7991 int index)
7992{
7993#if defined(CONFIG_UNIX)
7994 struct sock *sock = ctx->ring_sock->sk;
7995 struct sk_buff_head *head = &sock->sk_receive_queue;
7996 struct sk_buff *skb;
7997
7998 /*
7999 * See if we can merge this file into an existing skb SCM_RIGHTS
8000 * file set. If there's no room, fall back to allocating a new skb
8001 * and filling it in.
8002 */
8003 spin_lock_irq(&head->lock);
8004 skb = skb_peek(head);
8005 if (skb) {
8006 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8007
8008 if (fpl->count < SCM_MAX_FD) {
8009 __skb_unlink(skb, head);
8010 spin_unlock_irq(&head->lock);
8011 fpl->fp[fpl->count] = get_file(file);
8012 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8013 fpl->count++;
8014 spin_lock_irq(&head->lock);
8015 __skb_queue_head(head, skb);
8016 } else {
8017 skb = NULL;
8018 }
8019 }
8020 spin_unlock_irq(&head->lock);
8021
8022 if (skb) {
8023 fput(file);
8024 return 0;
8025 }
8026
8027 return __io_sqe_files_scm(ctx, 1, index);
8028#else
8029 return 0;
8030#endif
8031}
8032
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008033static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008034{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008035 struct io_rsrc_put *prsrc;
8036 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008037
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008038 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8039 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08008040 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008041
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008042 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008043 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008044
Hillf Dantona5318d32020-03-23 17:47:15 +08008045 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008046}
8047
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008048static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
8049 struct file *file)
8050{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008051 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008052}
8053
Jens Axboe05f3fb32019-12-09 11:22:50 -07008054static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008055 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008056 unsigned nr_args)
8057{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008058 struct fixed_rsrc_data *data = ctx->file_data;
8059 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008060 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06008061 __s32 __user *fds;
8062 int fd, i, err;
8063 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008064 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008065
Jens Axboe05f3fb32019-12-09 11:22:50 -07008066 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008067 return -EOVERFLOW;
8068 if (done > ctx->nr_user_files)
8069 return -EINVAL;
8070
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008071 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008072 if (!ref_node)
8073 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008074 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008075
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008076 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00008077 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008078 err = 0;
8079 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8080 err = -EFAULT;
8081 break;
8082 }
noah4e0377a2021-01-26 15:23:28 -05008083 if (fd == IORING_REGISTER_FILES_SKIP)
8084 continue;
8085
Pavel Begunkov67973b92021-01-26 13:51:09 +00008086 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008087 file_slot = io_fixed_file_slot(ctx->file_data, i);
8088
8089 if (*file_slot) {
8090 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008091 if (err)
8092 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008093 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008094 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008095 }
8096 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008097 file = fget(fd);
8098 if (!file) {
8099 err = -EBADF;
8100 break;
8101 }
8102 /*
8103 * Don't allow io_uring instances to be registered. If
8104 * UNIX isn't enabled, then this causes a reference
8105 * cycle and this instance can never get freed. If UNIX
8106 * is enabled we'll handle it just fine, but there's
8107 * still no point in allowing a ring fd as it doesn't
8108 * support regular read/write anyway.
8109 */
8110 if (file->f_op == &io_uring_fops) {
8111 fput(file);
8112 err = -EBADF;
8113 break;
8114 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008115 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008116 if (err) {
8117 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008118 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008119 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008120 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008121 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008122 }
8123
Xiaoguang Wang05589552020-03-31 14:05:18 +08008124 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008125 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008126 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008127 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008128 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008129
8130 return done ? done : err;
8131}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008132
Jens Axboe05f3fb32019-12-09 11:22:50 -07008133static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8134 unsigned nr_args)
8135{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008136 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008137
8138 if (!ctx->file_data)
8139 return -ENXIO;
8140 if (!nr_args)
8141 return -EINVAL;
8142 if (copy_from_user(&up, arg, sizeof(up)))
8143 return -EFAULT;
8144 if (up.resv)
8145 return -EINVAL;
8146
8147 return __io_sqe_files_update(ctx, &up, nr_args);
8148}
Jens Axboec3a31e62019-10-03 13:59:56 -06008149
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008150static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008151{
8152 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8153
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008154 req = io_put_req_find_next(req);
8155 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008156}
8157
Pavel Begunkov24369c22020-01-28 03:15:48 +03008158static int io_init_wq_offload(struct io_ring_ctx *ctx,
8159 struct io_uring_params *p)
8160{
8161 struct io_wq_data data;
8162 struct fd f;
8163 struct io_ring_ctx *ctx_attach;
8164 unsigned int concurrency;
8165 int ret = 0;
8166
8167 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008168 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008169 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008170
8171 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8172 /* Do QD, or 4 * CPUS, whatever is smallest */
8173 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8174
8175 ctx->io_wq = io_wq_create(concurrency, &data);
8176 if (IS_ERR(ctx->io_wq)) {
8177 ret = PTR_ERR(ctx->io_wq);
8178 ctx->io_wq = NULL;
8179 }
8180 return ret;
8181 }
8182
8183 f = fdget(p->wq_fd);
8184 if (!f.file)
8185 return -EBADF;
8186
8187 if (f.file->f_op != &io_uring_fops) {
8188 ret = -EINVAL;
8189 goto out_fput;
8190 }
8191
8192 ctx_attach = f.file->private_data;
8193 /* @io_wq is protected by holding the fd */
8194 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8195 ret = -EINVAL;
8196 goto out_fput;
8197 }
8198
8199 ctx->io_wq = ctx_attach->io_wq;
8200out_fput:
8201 fdput(f);
8202 return ret;
8203}
8204
Jens Axboe0f212202020-09-13 13:09:39 -06008205static int io_uring_alloc_task_context(struct task_struct *task)
8206{
8207 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008208 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008209
8210 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8211 if (unlikely(!tctx))
8212 return -ENOMEM;
8213
Jens Axboed8a6df12020-10-15 16:24:45 -06008214 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8215 if (unlikely(ret)) {
8216 kfree(tctx);
8217 return ret;
8218 }
8219
Jens Axboe0f212202020-09-13 13:09:39 -06008220 xa_init(&tctx->xa);
8221 init_waitqueue_head(&tctx->wait);
8222 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008223 atomic_set(&tctx->in_idle, 0);
8224 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008225 io_init_identity(&tctx->__identity);
8226 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008227 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008228 spin_lock_init(&tctx->task_lock);
8229 INIT_WQ_LIST(&tctx->task_list);
8230 tctx->task_state = 0;
8231 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008232 return 0;
8233}
8234
8235void __io_uring_free(struct task_struct *tsk)
8236{
8237 struct io_uring_task *tctx = tsk->io_uring;
8238
8239 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008240 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8241 if (tctx->identity != &tctx->__identity)
8242 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008243 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008244 kfree(tctx);
8245 tsk->io_uring = NULL;
8246}
8247
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008248static int io_sq_offload_create(struct io_ring_ctx *ctx,
8249 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008250{
8251 int ret;
8252
Jens Axboe6c271ce2019-01-10 11:22:30 -07008253 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008254 struct io_sq_data *sqd;
8255
Jens Axboe3ec482d2019-04-08 10:51:01 -06008256 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008257 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008258 goto err;
8259
Jens Axboe534ca6d2020-09-02 13:52:19 -06008260 sqd = io_get_sq_data(p);
8261 if (IS_ERR(sqd)) {
8262 ret = PTR_ERR(sqd);
8263 goto err;
8264 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008265
Jens Axboe534ca6d2020-09-02 13:52:19 -06008266 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008267 io_sq_thread_park(sqd);
8268 mutex_lock(&sqd->ctx_lock);
8269 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8270 mutex_unlock(&sqd->ctx_lock);
8271 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008272
Jens Axboe917257d2019-04-13 09:28:55 -06008273 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8274 if (!ctx->sq_thread_idle)
8275 ctx->sq_thread_idle = HZ;
8276
Jens Axboeaa061652020-09-02 14:50:27 -06008277 if (sqd->thread)
8278 goto done;
8279
Jens Axboe6c271ce2019-01-10 11:22:30 -07008280 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008281 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008282
Jens Axboe917257d2019-04-13 09:28:55 -06008283 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008284 if (cpu >= nr_cpu_ids)
8285 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008286 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008287 goto err;
8288
Jens Axboe69fb2132020-09-14 11:16:23 -06008289 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008290 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008291 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008292 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008293 "io_uring-sq");
8294 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008295 if (IS_ERR(sqd->thread)) {
8296 ret = PTR_ERR(sqd->thread);
8297 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008298 goto err;
8299 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008300 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008301 if (ret)
8302 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008303 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8304 /* Can't have SQ_AFF without SQPOLL */
8305 ret = -EINVAL;
8306 goto err;
8307 }
8308
Jens Axboeaa061652020-09-02 14:50:27 -06008309done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008310 ret = io_init_wq_offload(ctx, p);
8311 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008312 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008313
8314 return 0;
8315err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008316 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317 return ret;
8318}
8319
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008320static void io_sq_offload_start(struct io_ring_ctx *ctx)
8321{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008322 struct io_sq_data *sqd = ctx->sq_data;
8323
8324 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8325 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008326}
8327
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008328static inline void __io_unaccount_mem(struct user_struct *user,
8329 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008330{
8331 atomic_long_sub(nr_pages, &user->locked_vm);
8332}
8333
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008334static inline int __io_account_mem(struct user_struct *user,
8335 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008336{
8337 unsigned long page_limit, cur_pages, new_pages;
8338
8339 /* Don't allow more pages than we can safely lock */
8340 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8341
8342 do {
8343 cur_pages = atomic_long_read(&user->locked_vm);
8344 new_pages = cur_pages + nr_pages;
8345 if (new_pages > page_limit)
8346 return -ENOMEM;
8347 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8348 new_pages) != cur_pages);
8349
8350 return 0;
8351}
8352
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008353static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008354{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008355 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008356 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008357
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008358 if (ctx->mm_account)
8359 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008360}
8361
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008362static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008363{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008364 int ret;
8365
8366 if (ctx->limit_mem) {
8367 ret = __io_account_mem(ctx->user, nr_pages);
8368 if (ret)
8369 return ret;
8370 }
8371
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008372 if (ctx->mm_account)
8373 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008374
8375 return 0;
8376}
8377
Jens Axboe2b188cc2019-01-07 10:46:33 -07008378static void io_mem_free(void *ptr)
8379{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008380 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008381
Mark Rutland52e04ef2019-04-30 17:30:21 +01008382 if (!ptr)
8383 return;
8384
8385 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008386 if (put_page_testzero(page))
8387 free_compound_page(page);
8388}
8389
8390static void *io_mem_alloc(size_t size)
8391{
8392 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008393 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008394
8395 return (void *) __get_free_pages(gfp_flags, get_order(size));
8396}
8397
Hristo Venev75b28af2019-08-26 17:23:46 +00008398static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8399 size_t *sq_offset)
8400{
8401 struct io_rings *rings;
8402 size_t off, sq_array_size;
8403
8404 off = struct_size(rings, cqes, cq_entries);
8405 if (off == SIZE_MAX)
8406 return SIZE_MAX;
8407
8408#ifdef CONFIG_SMP
8409 off = ALIGN(off, SMP_CACHE_BYTES);
8410 if (off == 0)
8411 return SIZE_MAX;
8412#endif
8413
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008414 if (sq_offset)
8415 *sq_offset = off;
8416
Hristo Venev75b28af2019-08-26 17:23:46 +00008417 sq_array_size = array_size(sizeof(u32), sq_entries);
8418 if (sq_array_size == SIZE_MAX)
8419 return SIZE_MAX;
8420
8421 if (check_add_overflow(off, sq_array_size, &off))
8422 return SIZE_MAX;
8423
Hristo Venev75b28af2019-08-26 17:23:46 +00008424 return off;
8425}
8426
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008427static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008428{
8429 int i, j;
8430
8431 if (!ctx->user_bufs)
8432 return -ENXIO;
8433
8434 for (i = 0; i < ctx->nr_user_bufs; i++) {
8435 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8436
8437 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008438 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008439
Jens Axboede293932020-09-17 16:19:16 -06008440 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008441 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008442 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008443 imu->nr_bvecs = 0;
8444 }
8445
8446 kfree(ctx->user_bufs);
8447 ctx->user_bufs = NULL;
8448 ctx->nr_user_bufs = 0;
8449 return 0;
8450}
8451
8452static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8453 void __user *arg, unsigned index)
8454{
8455 struct iovec __user *src;
8456
8457#ifdef CONFIG_COMPAT
8458 if (ctx->compat) {
8459 struct compat_iovec __user *ciovs;
8460 struct compat_iovec ciov;
8461
8462 ciovs = (struct compat_iovec __user *) arg;
8463 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8464 return -EFAULT;
8465
Jens Axboed55e5f52019-12-11 16:12:15 -07008466 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008467 dst->iov_len = ciov.iov_len;
8468 return 0;
8469 }
8470#endif
8471 src = (struct iovec __user *) arg;
8472 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8473 return -EFAULT;
8474 return 0;
8475}
8476
Jens Axboede293932020-09-17 16:19:16 -06008477/*
8478 * Not super efficient, but this is just a registration time. And we do cache
8479 * the last compound head, so generally we'll only do a full search if we don't
8480 * match that one.
8481 *
8482 * We check if the given compound head page has already been accounted, to
8483 * avoid double accounting it. This allows us to account the full size of the
8484 * page, not just the constituent pages of a huge page.
8485 */
8486static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8487 int nr_pages, struct page *hpage)
8488{
8489 int i, j;
8490
8491 /* check current page array */
8492 for (i = 0; i < nr_pages; i++) {
8493 if (!PageCompound(pages[i]))
8494 continue;
8495 if (compound_head(pages[i]) == hpage)
8496 return true;
8497 }
8498
8499 /* check previously registered pages */
8500 for (i = 0; i < ctx->nr_user_bufs; i++) {
8501 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8502
8503 for (j = 0; j < imu->nr_bvecs; j++) {
8504 if (!PageCompound(imu->bvec[j].bv_page))
8505 continue;
8506 if (compound_head(imu->bvec[j].bv_page) == hpage)
8507 return true;
8508 }
8509 }
8510
8511 return false;
8512}
8513
8514static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8515 int nr_pages, struct io_mapped_ubuf *imu,
8516 struct page **last_hpage)
8517{
8518 int i, ret;
8519
8520 for (i = 0; i < nr_pages; i++) {
8521 if (!PageCompound(pages[i])) {
8522 imu->acct_pages++;
8523 } else {
8524 struct page *hpage;
8525
8526 hpage = compound_head(pages[i]);
8527 if (hpage == *last_hpage)
8528 continue;
8529 *last_hpage = hpage;
8530 if (headpage_already_acct(ctx, pages, i, hpage))
8531 continue;
8532 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8533 }
8534 }
8535
8536 if (!imu->acct_pages)
8537 return 0;
8538
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008539 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008540 if (ret)
8541 imu->acct_pages = 0;
8542 return ret;
8543}
8544
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008545static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8546 struct io_mapped_ubuf *imu,
8547 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008548{
8549 struct vm_area_struct **vmas = NULL;
8550 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008551 unsigned long off, start, end, ubuf;
8552 size_t size;
8553 int ret, pret, nr_pages, i;
8554
8555 ubuf = (unsigned long) iov->iov_base;
8556 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8557 start = ubuf >> PAGE_SHIFT;
8558 nr_pages = end - start;
8559
8560 ret = -ENOMEM;
8561
8562 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8563 if (!pages)
8564 goto done;
8565
8566 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8567 GFP_KERNEL);
8568 if (!vmas)
8569 goto done;
8570
8571 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8572 GFP_KERNEL);
8573 if (!imu->bvec)
8574 goto done;
8575
8576 ret = 0;
8577 mmap_read_lock(current->mm);
8578 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8579 pages, vmas);
8580 if (pret == nr_pages) {
8581 /* don't support file backed memory */
8582 for (i = 0; i < nr_pages; i++) {
8583 struct vm_area_struct *vma = vmas[i];
8584
8585 if (vma->vm_file &&
8586 !is_file_hugepages(vma->vm_file)) {
8587 ret = -EOPNOTSUPP;
8588 break;
8589 }
8590 }
8591 } else {
8592 ret = pret < 0 ? pret : -EFAULT;
8593 }
8594 mmap_read_unlock(current->mm);
8595 if (ret) {
8596 /*
8597 * if we did partial map, or found file backed vmas,
8598 * release any pages we did get
8599 */
8600 if (pret > 0)
8601 unpin_user_pages(pages, pret);
8602 kvfree(imu->bvec);
8603 goto done;
8604 }
8605
8606 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8607 if (ret) {
8608 unpin_user_pages(pages, pret);
8609 kvfree(imu->bvec);
8610 goto done;
8611 }
8612
8613 off = ubuf & ~PAGE_MASK;
8614 size = iov->iov_len;
8615 for (i = 0; i < nr_pages; i++) {
8616 size_t vec_len;
8617
8618 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8619 imu->bvec[i].bv_page = pages[i];
8620 imu->bvec[i].bv_len = vec_len;
8621 imu->bvec[i].bv_offset = off;
8622 off = 0;
8623 size -= vec_len;
8624 }
8625 /* store original address for later verification */
8626 imu->ubuf = ubuf;
8627 imu->len = iov->iov_len;
8628 imu->nr_bvecs = nr_pages;
8629 ret = 0;
8630done:
8631 kvfree(pages);
8632 kvfree(vmas);
8633 return ret;
8634}
8635
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008636static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008637{
Jens Axboeedafcce2019-01-09 09:16:05 -07008638 if (ctx->user_bufs)
8639 return -EBUSY;
8640 if (!nr_args || nr_args > UIO_MAXIOV)
8641 return -EINVAL;
8642
8643 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8644 GFP_KERNEL);
8645 if (!ctx->user_bufs)
8646 return -ENOMEM;
8647
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008648 return 0;
8649}
8650
8651static int io_buffer_validate(struct iovec *iov)
8652{
8653 /*
8654 * Don't impose further limits on the size and buffer
8655 * constraints here, we'll -EINVAL later when IO is
8656 * submitted if they are wrong.
8657 */
8658 if (!iov->iov_base || !iov->iov_len)
8659 return -EFAULT;
8660
8661 /* arbitrary limit, but we need something */
8662 if (iov->iov_len > SZ_1G)
8663 return -EFAULT;
8664
8665 return 0;
8666}
8667
8668static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8669 unsigned int nr_args)
8670{
8671 int i, ret;
8672 struct iovec iov;
8673 struct page *last_hpage = NULL;
8674
8675 ret = io_buffers_map_alloc(ctx, nr_args);
8676 if (ret)
8677 return ret;
8678
Jens Axboeedafcce2019-01-09 09:16:05 -07008679 for (i = 0; i < nr_args; i++) {
8680 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008681
8682 ret = io_copy_iov(ctx, &iov, arg, i);
8683 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008684 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008685
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008686 ret = io_buffer_validate(&iov);
8687 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008688 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008689
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008690 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8691 if (ret)
8692 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008693
8694 ctx->nr_user_bufs++;
8695 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008696
8697 if (ret)
8698 io_sqe_buffers_unregister(ctx);
8699
Jens Axboeedafcce2019-01-09 09:16:05 -07008700 return ret;
8701}
8702
Jens Axboe9b402842019-04-11 11:45:41 -06008703static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8704{
8705 __s32 __user *fds = arg;
8706 int fd;
8707
8708 if (ctx->cq_ev_fd)
8709 return -EBUSY;
8710
8711 if (copy_from_user(&fd, fds, sizeof(*fds)))
8712 return -EFAULT;
8713
8714 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8715 if (IS_ERR(ctx->cq_ev_fd)) {
8716 int ret = PTR_ERR(ctx->cq_ev_fd);
8717 ctx->cq_ev_fd = NULL;
8718 return ret;
8719 }
8720
8721 return 0;
8722}
8723
8724static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8725{
8726 if (ctx->cq_ev_fd) {
8727 eventfd_ctx_put(ctx->cq_ev_fd);
8728 ctx->cq_ev_fd = NULL;
8729 return 0;
8730 }
8731
8732 return -ENXIO;
8733}
8734
Jens Axboe5a2e7452020-02-23 16:23:11 -07008735static int __io_destroy_buffers(int id, void *p, void *data)
8736{
8737 struct io_ring_ctx *ctx = data;
8738 struct io_buffer *buf = p;
8739
Jens Axboe067524e2020-03-02 16:32:28 -07008740 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008741 return 0;
8742}
8743
8744static void io_destroy_buffers(struct io_ring_ctx *ctx)
8745{
8746 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8747 idr_destroy(&ctx->io_buffer_idr);
8748}
8749
Jens Axboec7dae4b2021-02-09 19:53:37 -07008750static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008751{
Jens Axboec7dae4b2021-02-09 19:53:37 -07008752 while (!list_empty(list)) {
Jens Axboe1b4c3512021-02-10 00:03:19 +00008753 struct io_kiocb *req;
8754
Jens Axboec7dae4b2021-02-09 19:53:37 -07008755 req = list_first_entry(list, struct io_kiocb, compl.list);
Jens Axboe1b4c3512021-02-10 00:03:19 +00008756 list_del(&req->compl.list);
8757 kmem_cache_free(req_cachep, req);
8758 }
8759}
8760
Jens Axboe2b188cc2019-01-07 10:46:33 -07008761static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8762{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008763 struct io_submit_state *submit_state = &ctx->submit_state;
8764
Jens Axboe6b063142019-01-10 22:13:58 -07008765 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008766 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008767
8768 if (ctx->sqo_task) {
8769 put_task_struct(ctx->sqo_task);
8770 ctx->sqo_task = NULL;
8771 mmdrop(ctx->mm_account);
8772 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008773 }
Jens Axboedef596e2019-01-09 08:59:42 -07008774
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008775 if (submit_state->free_reqs)
8776 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8777 submit_state->reqs);
8778
Dennis Zhou91d8f512020-09-16 13:41:05 -07008779#ifdef CONFIG_BLK_CGROUP
8780 if (ctx->sqo_blkcg_css)
8781 css_put(ctx->sqo_blkcg_css);
8782#endif
8783
Jens Axboe6b063142019-01-10 22:13:58 -07008784 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008785 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008786 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008787 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008788
Jens Axboe2b188cc2019-01-07 10:46:33 -07008789#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008790 if (ctx->ring_sock) {
8791 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008792 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008793 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008794#endif
8795
Hristo Venev75b28af2019-08-26 17:23:46 +00008796 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008797 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008798
8799 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008800 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008801 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008802 kfree(ctx->cancel_hash);
Jens Axboec7dae4b2021-02-09 19:53:37 -07008803 io_req_cache_free(&ctx->submit_state.comp.free_list);
8804 io_req_cache_free(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008805 kfree(ctx);
8806}
8807
8808static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8809{
8810 struct io_ring_ctx *ctx = file->private_data;
8811 __poll_t mask = 0;
8812
8813 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008814 /*
8815 * synchronizes with barrier from wq_has_sleeper call in
8816 * io_commit_cqring
8817 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008818 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008819 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008820 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008821
8822 /*
8823 * Don't flush cqring overflow list here, just do a simple check.
8824 * Otherwise there could possible be ABBA deadlock:
8825 * CPU0 CPU1
8826 * ---- ----
8827 * lock(&ctx->uring_lock);
8828 * lock(&ep->mtx);
8829 * lock(&ctx->uring_lock);
8830 * lock(&ep->mtx);
8831 *
8832 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8833 * pushs them to do the flush.
8834 */
8835 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008836 mask |= EPOLLIN | EPOLLRDNORM;
8837
8838 return mask;
8839}
8840
8841static int io_uring_fasync(int fd, struct file *file, int on)
8842{
8843 struct io_ring_ctx *ctx = file->private_data;
8844
8845 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8846}
8847
Yejune Deng0bead8c2020-12-24 11:02:20 +08008848static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008849{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008850 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008851
Jens Axboe1e6fa522020-10-15 08:46:24 -06008852 iod = idr_remove(&ctx->personality_idr, id);
8853 if (iod) {
8854 put_cred(iod->creds);
8855 if (refcount_dec_and_test(&iod->count))
8856 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008857 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008858 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008859
8860 return -EINVAL;
8861}
8862
8863static int io_remove_personalities(int id, void *p, void *data)
8864{
8865 struct io_ring_ctx *ctx = data;
8866
8867 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008868 return 0;
8869}
8870
Jens Axboe85faa7b2020-04-09 18:14:00 -06008871static void io_ring_exit_work(struct work_struct *work)
8872{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008873 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8874 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008875
Jens Axboe56952e92020-06-17 15:00:04 -06008876 /*
8877 * If we're doing polled IO and end up having requests being
8878 * submitted async (out-of-line), then completions can come in while
8879 * we're waiting for refs to drop. We need to reap these manually,
8880 * as nobody else will be looking for them.
8881 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008882 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008883 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008884 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008885 io_ring_ctx_free(ctx);
8886}
8887
Jens Axboe00c18642020-12-20 10:45:02 -07008888static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8889{
8890 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8891
8892 return req->ctx == data;
8893}
8894
Jens Axboe2b188cc2019-01-07 10:46:33 -07008895static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8896{
8897 mutex_lock(&ctx->uring_lock);
8898 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008899
8900 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8901 ctx->sqo_dead = 1;
8902
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008903 /* if force is set, the ring is going away. always drop after that */
8904 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008905 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008906 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008907 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008908 mutex_unlock(&ctx->uring_lock);
8909
Pavel Begunkov6b819282020-11-06 13:00:25 +00008910 io_kill_timeouts(ctx, NULL, NULL);
8911 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008912
8913 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008914 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008915
Jens Axboe15dff282019-11-13 09:09:23 -07008916 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008917 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008918
Jens Axboe85faa7b2020-04-09 18:14:00 -06008919 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008920 /*
8921 * Use system_unbound_wq to avoid spawning tons of event kworkers
8922 * if we're exiting a ton of rings at the same time. It just adds
8923 * noise and overhead, there's no discernable change in runtime
8924 * over using system_wq.
8925 */
8926 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008927}
8928
8929static int io_uring_release(struct inode *inode, struct file *file)
8930{
8931 struct io_ring_ctx *ctx = file->private_data;
8932
8933 file->private_data = NULL;
8934 io_ring_ctx_wait_and_kill(ctx);
8935 return 0;
8936}
8937
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008938struct io_task_cancel {
8939 struct task_struct *task;
8940 struct files_struct *files;
8941};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008942
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008943static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008944{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008945 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008946 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008947 bool ret;
8948
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008949 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008950 unsigned long flags;
8951 struct io_ring_ctx *ctx = req->ctx;
8952
8953 /* protect against races with linked timeouts */
8954 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008955 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008956 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8957 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008958 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008959 }
8960 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008961}
8962
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008963static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008964 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008965 struct files_struct *files)
8966{
8967 struct io_defer_entry *de = NULL;
8968 LIST_HEAD(list);
8969
8970 spin_lock_irq(&ctx->completion_lock);
8971 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008972 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008973 list_cut_position(&list, &ctx->defer_list, &de->list);
8974 break;
8975 }
8976 }
8977 spin_unlock_irq(&ctx->completion_lock);
8978
8979 while (!list_empty(&list)) {
8980 de = list_first_entry(&list, struct io_defer_entry, list);
8981 list_del_init(&de->list);
8982 req_set_fail_links(de->req);
8983 io_put_req(de->req);
8984 io_req_complete(de->req, -ECANCELED);
8985 kfree(de);
8986 }
8987}
8988
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008989static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8990 struct task_struct *task,
8991 struct files_struct *files)
8992{
8993 struct io_task_cancel cancel = { .task = task, .files = files, };
8994
8995 while (1) {
8996 enum io_wq_cancel cret;
8997 bool ret = false;
8998
8999 if (ctx->io_wq) {
9000 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
9001 &cancel, true);
9002 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9003 }
9004
9005 /* SQPOLL thread does its own polling */
9006 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
9007 while (!list_empty_careful(&ctx->iopoll_list)) {
9008 io_iopoll_try_reap_events(ctx);
9009 ret = true;
9010 }
9011 }
9012
9013 ret |= io_poll_remove_all(ctx, task, files);
9014 ret |= io_kill_timeouts(ctx, task, files);
9015 ret |= io_run_task_work();
9016 io_cqring_overflow_flush(ctx, true, task, files);
9017 if (!ret)
9018 break;
9019 cond_resched();
9020 }
9021}
9022
Pavel Begunkovca70f002021-01-26 15:28:27 +00009023static int io_uring_count_inflight(struct io_ring_ctx *ctx,
9024 struct task_struct *task,
9025 struct files_struct *files)
9026{
9027 struct io_kiocb *req;
9028 int cnt = 0;
9029
9030 spin_lock_irq(&ctx->inflight_lock);
9031 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
9032 cnt += io_match_task(req, task, files);
9033 spin_unlock_irq(&ctx->inflight_lock);
9034 return cnt;
9035}
9036
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009037static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009038 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06009039 struct files_struct *files)
9040{
Jens Axboefcb323c2019-10-24 12:39:47 -06009041 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08009042 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009043 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06009044
Pavel Begunkovca70f002021-01-26 15:28:27 +00009045 inflight = io_uring_count_inflight(ctx, task, files);
9046 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06009047 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009048
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009049 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkov34343782021-02-10 11:45:42 +00009050
9051 if (ctx->sq_data)
9052 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009053 prepare_to_wait(&task->io_uring->wait, &wait,
9054 TASK_UNINTERRUPTIBLE);
9055 if (inflight == io_uring_count_inflight(ctx, task, files))
9056 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00009057 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00009058 if (ctx->sq_data)
9059 io_sq_thread_park(ctx->sq_data);
Jens Axboefcb323c2019-10-24 12:39:47 -06009060 }
9061}
9062
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009063static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9064{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009065 mutex_lock(&ctx->uring_lock);
9066 ctx->sqo_dead = 1;
9067 mutex_unlock(&ctx->uring_lock);
9068
9069 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009070 if (ctx->rings)
9071 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009072}
9073
Jens Axboe0f212202020-09-13 13:09:39 -06009074/*
9075 * We need to iteratively cancel requests, in case a request has dependent
9076 * hard links. These persist even for failure of cancelations, hence keep
9077 * looping until none are found.
9078 */
9079static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9080 struct files_struct *files)
9081{
9082 struct task_struct *task = current;
9083
Jens Axboefdaf0832020-10-30 09:37:30 -06009084 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009085 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009086 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009087 atomic_inc(&task->io_uring->in_idle);
9088 io_sq_thread_park(ctx->sq_data);
9089 }
Jens Axboe0f212202020-09-13 13:09:39 -06009090
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009091 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009092
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009093 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009094 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009095 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009096
9097 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9098 atomic_dec(&task->io_uring->in_idle);
9099 /*
9100 * If the files that are going away are the ones in the thread
9101 * identity, clear them out.
9102 */
9103 if (task->io_uring->identity->files == files)
9104 task->io_uring->identity->files = NULL;
9105 io_sq_thread_unpark(ctx->sq_data);
9106 }
Jens Axboe0f212202020-09-13 13:09:39 -06009107}
9108
9109/*
9110 * Note that this task has used io_uring. We use it for cancelation purposes.
9111 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009112static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009113{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009114 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009115 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009116
9117 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009118 ret = io_uring_alloc_task_context(current);
9119 if (unlikely(ret))
9120 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009121 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009122 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009123 if (tctx->last != file) {
9124 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009125
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009126 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009127 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009128 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9129 file, GFP_KERNEL));
9130 if (ret) {
9131 fput(file);
9132 return ret;
9133 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009134
9135 /* one and only SQPOLL file note, held by sqo_task */
9136 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9137 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009138 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009139 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009140 }
9141
Jens Axboefdaf0832020-10-30 09:37:30 -06009142 /*
9143 * This is race safe in that the task itself is doing this, hence it
9144 * cannot be going through the exit/cancel paths at the same time.
9145 * This cannot be modified while exit/cancel is running.
9146 */
9147 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9148 tctx->sqpoll = true;
9149
Jens Axboe0f212202020-09-13 13:09:39 -06009150 return 0;
9151}
9152
9153/*
9154 * Remove this io_uring_file -> task mapping.
9155 */
9156static void io_uring_del_task_file(struct file *file)
9157{
9158 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009159
9160 if (tctx->last == file)
9161 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009162 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009163 if (file)
9164 fput(file);
9165}
9166
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009167static void io_uring_remove_task_files(struct io_uring_task *tctx)
9168{
9169 struct file *file;
9170 unsigned long index;
9171
9172 xa_for_each(&tctx->xa, index, file)
9173 io_uring_del_task_file(file);
9174}
9175
Jens Axboe0f212202020-09-13 13:09:39 -06009176void __io_uring_files_cancel(struct files_struct *files)
9177{
9178 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009179 struct file *file;
9180 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009181
9182 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009183 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009184 xa_for_each(&tctx->xa, index, file)
9185 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009186 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009187
9188 if (files)
9189 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009190}
9191
9192static s64 tctx_inflight(struct io_uring_task *tctx)
9193{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009194 return percpu_counter_sum(&tctx->inflight);
9195}
9196
9197static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9198{
9199 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009200 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009201 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009202
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009203 if (!ctx->sq_data)
9204 return;
9205 tctx = ctx->sq_data->thread->io_uring;
9206 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009207
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009208 atomic_inc(&tctx->in_idle);
9209 do {
9210 /* read completions before cancelations */
9211 inflight = tctx_inflight(tctx);
9212 if (!inflight)
9213 break;
9214 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009215
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009216 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9217 /*
9218 * If we've seen completions, retry without waiting. This
9219 * avoids a race where a completion comes in before we did
9220 * prepare_to_wait().
9221 */
9222 if (inflight == tctx_inflight(tctx))
9223 schedule();
9224 finish_wait(&tctx->wait, &wait);
9225 } while (1);
9226 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009227}
9228
Jens Axboe0f212202020-09-13 13:09:39 -06009229/*
9230 * Find any io_uring fd that this task has registered or done IO on, and cancel
9231 * requests.
9232 */
9233void __io_uring_task_cancel(void)
9234{
9235 struct io_uring_task *tctx = current->io_uring;
9236 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009237 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009238
9239 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009240 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009241
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009242 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009243 if (tctx->sqpoll) {
9244 struct file *file;
9245 unsigned long index;
9246
9247 xa_for_each(&tctx->xa, index, file)
9248 io_uring_cancel_sqpoll(file->private_data);
9249 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009250
Jens Axboed8a6df12020-10-15 16:24:45 -06009251 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009252 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009253 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009254 if (!inflight)
9255 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009256 __io_uring_files_cancel(NULL);
9257
9258 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9259
9260 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009261 * If we've seen completions, retry without waiting. This
9262 * avoids a race where a completion comes in before we did
9263 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009264 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009265 if (inflight == tctx_inflight(tctx))
9266 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009267 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009268 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009269
Jens Axboefdaf0832020-10-30 09:37:30 -06009270 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009271
9272 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009273}
9274
Jens Axboefcb323c2019-10-24 12:39:47 -06009275static int io_uring_flush(struct file *file, void *data)
9276{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009277 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009278 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009279
Jens Axboe84965ff2021-01-23 15:51:11 -07009280 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9281 io_uring_cancel_task_requests(ctx, NULL);
9282
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009283 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009284 return 0;
9285
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009286 /* we should have cancelled and erased it before PF_EXITING */
9287 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9288 xa_load(&tctx->xa, (unsigned long)file));
9289
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009290 /*
9291 * fput() is pending, will be 2 if the only other ref is our potential
9292 * task file note. If the task is exiting, drop regardless of count.
9293 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009294 if (atomic_long_read(&file->f_count) != 2)
9295 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009296
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009297 if (ctx->flags & IORING_SETUP_SQPOLL) {
9298 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009299 WARN_ON_ONCE(ctx->sqo_task != current &&
9300 xa_load(&tctx->xa, (unsigned long)file));
9301 /* sqo_dead check is for when this happens after cancellation */
9302 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009303 !xa_load(&tctx->xa, (unsigned long)file));
9304
9305 io_disable_sqo_submit(ctx);
9306 }
9307
9308 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9309 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009310 return 0;
9311}
9312
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009313static void *io_uring_validate_mmap_request(struct file *file,
9314 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009315{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009316 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009317 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009318 struct page *page;
9319 void *ptr;
9320
9321 switch (offset) {
9322 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009323 case IORING_OFF_CQ_RING:
9324 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009325 break;
9326 case IORING_OFF_SQES:
9327 ptr = ctx->sq_sqes;
9328 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009329 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009330 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331 }
9332
9333 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009334 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009335 return ERR_PTR(-EINVAL);
9336
9337 return ptr;
9338}
9339
9340#ifdef CONFIG_MMU
9341
9342static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9343{
9344 size_t sz = vma->vm_end - vma->vm_start;
9345 unsigned long pfn;
9346 void *ptr;
9347
9348 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9349 if (IS_ERR(ptr))
9350 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009351
9352 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9353 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9354}
9355
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009356#else /* !CONFIG_MMU */
9357
9358static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9359{
9360 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9361}
9362
9363static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9364{
9365 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9366}
9367
9368static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9369 unsigned long addr, unsigned long len,
9370 unsigned long pgoff, unsigned long flags)
9371{
9372 void *ptr;
9373
9374 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9375 if (IS_ERR(ptr))
9376 return PTR_ERR(ptr);
9377
9378 return (unsigned long) ptr;
9379}
9380
9381#endif /* !CONFIG_MMU */
9382
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009383static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009384{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009385 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009386 DEFINE_WAIT(wait);
9387
9388 do {
9389 if (!io_sqring_full(ctx))
9390 break;
9391
9392 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9393
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009394 if (unlikely(ctx->sqo_dead)) {
9395 ret = -EOWNERDEAD;
9396 goto out;
9397 }
9398
Jens Axboe90554202020-09-03 12:12:41 -06009399 if (!io_sqring_full(ctx))
9400 break;
9401
9402 schedule();
9403 } while (!signal_pending(current));
9404
9405 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009406out:
9407 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009408}
9409
Hao Xuc73ebb62020-11-03 10:54:37 +08009410static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9411 struct __kernel_timespec __user **ts,
9412 const sigset_t __user **sig)
9413{
9414 struct io_uring_getevents_arg arg;
9415
9416 /*
9417 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9418 * is just a pointer to the sigset_t.
9419 */
9420 if (!(flags & IORING_ENTER_EXT_ARG)) {
9421 *sig = (const sigset_t __user *) argp;
9422 *ts = NULL;
9423 return 0;
9424 }
9425
9426 /*
9427 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9428 * timespec and sigset_t pointers if good.
9429 */
9430 if (*argsz != sizeof(arg))
9431 return -EINVAL;
9432 if (copy_from_user(&arg, argp, sizeof(arg)))
9433 return -EFAULT;
9434 *sig = u64_to_user_ptr(arg.sigmask);
9435 *argsz = arg.sigmask_sz;
9436 *ts = u64_to_user_ptr(arg.ts);
9437 return 0;
9438}
9439
Jens Axboe2b188cc2019-01-07 10:46:33 -07009440SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009441 u32, min_complete, u32, flags, const void __user *, argp,
9442 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009443{
9444 struct io_ring_ctx *ctx;
9445 long ret = -EBADF;
9446 int submitted = 0;
9447 struct fd f;
9448
Jens Axboe4c6e2772020-07-01 11:29:10 -06009449 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009450
Jens Axboe90554202020-09-03 12:12:41 -06009451 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009452 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009453 return -EINVAL;
9454
9455 f = fdget(fd);
9456 if (!f.file)
9457 return -EBADF;
9458
9459 ret = -EOPNOTSUPP;
9460 if (f.file->f_op != &io_uring_fops)
9461 goto out_fput;
9462
9463 ret = -ENXIO;
9464 ctx = f.file->private_data;
9465 if (!percpu_ref_tryget(&ctx->refs))
9466 goto out_fput;
9467
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009468 ret = -EBADFD;
9469 if (ctx->flags & IORING_SETUP_R_DISABLED)
9470 goto out;
9471
Jens Axboe6c271ce2019-01-10 11:22:30 -07009472 /*
9473 * For SQ polling, the thread will do all submissions and completions.
9474 * Just return the requested submit count, and wake the thread if
9475 * we were asked to.
9476 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009477 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009478 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009479 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009480
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009481 ret = -EOWNERDEAD;
9482 if (unlikely(ctx->sqo_dead))
9483 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009484 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009485 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009486 if (flags & IORING_ENTER_SQ_WAIT) {
9487 ret = io_sqpoll_wait_sq(ctx);
9488 if (ret)
9489 goto out;
9490 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009491 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009492 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009493 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009494 if (unlikely(ret))
9495 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009496 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009497 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009498 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009499
9500 if (submitted != to_submit)
9501 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009502 }
9503 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009504 const sigset_t __user *sig;
9505 struct __kernel_timespec __user *ts;
9506
9507 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9508 if (unlikely(ret))
9509 goto out;
9510
Jens Axboe2b188cc2019-01-07 10:46:33 -07009511 min_complete = min(min_complete, ctx->cq_entries);
9512
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009513 /*
9514 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9515 * space applications don't need to do io completion events
9516 * polling again, they can rely on io_sq_thread to do polling
9517 * work, which can reduce cpu usage and uring_lock contention.
9518 */
9519 if (ctx->flags & IORING_SETUP_IOPOLL &&
9520 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009521 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009522 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009523 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009524 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009525 }
9526
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009527out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009528 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009529out_fput:
9530 fdput(f);
9531 return submitted ? submitted : ret;
9532}
9533
Tobias Klauserbebdb652020-02-26 18:38:32 +01009534#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009535static int io_uring_show_cred(int id, void *p, void *data)
9536{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009537 struct io_identity *iod = p;
9538 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009539 struct seq_file *m = data;
9540 struct user_namespace *uns = seq_user_ns(m);
9541 struct group_info *gi;
9542 kernel_cap_t cap;
9543 unsigned __capi;
9544 int g;
9545
9546 seq_printf(m, "%5d\n", id);
9547 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9548 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9549 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9550 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9551 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9552 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9553 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9554 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9555 seq_puts(m, "\n\tGroups:\t");
9556 gi = cred->group_info;
9557 for (g = 0; g < gi->ngroups; g++) {
9558 seq_put_decimal_ull(m, g ? " " : "",
9559 from_kgid_munged(uns, gi->gid[g]));
9560 }
9561 seq_puts(m, "\n\tCapEff:\t");
9562 cap = cred->cap_effective;
9563 CAP_FOR_EACH_U32(__capi)
9564 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9565 seq_putc(m, '\n');
9566 return 0;
9567}
9568
9569static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9570{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009571 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009572 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009573 int i;
9574
Jens Axboefad8e0d2020-09-28 08:57:48 -06009575 /*
9576 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9577 * since fdinfo case grabs it in the opposite direction of normal use
9578 * cases. If we fail to get the lock, we just don't iterate any
9579 * structures that could be going away outside the io_uring mutex.
9580 */
9581 has_lock = mutex_trylock(&ctx->uring_lock);
9582
Joseph Qidbbe9c62020-09-29 09:01:22 -06009583 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9584 sq = ctx->sq_data;
9585
9586 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9587 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009588 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009589 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009590 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009591
Jens Axboe87ce9552020-01-30 08:25:34 -07009592 if (f)
9593 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9594 else
9595 seq_printf(m, "%5u: <none>\n", i);
9596 }
9597 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009598 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009599 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9600
9601 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9602 (unsigned int) buf->len);
9603 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009604 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009605 seq_printf(m, "Personalities:\n");
9606 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9607 }
Jens Axboed7718a92020-02-14 22:23:12 -07009608 seq_printf(m, "PollList:\n");
9609 spin_lock_irq(&ctx->completion_lock);
9610 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9611 struct hlist_head *list = &ctx->cancel_hash[i];
9612 struct io_kiocb *req;
9613
9614 hlist_for_each_entry(req, list, hash_node)
9615 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9616 req->task->task_works != NULL);
9617 }
9618 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009619 if (has_lock)
9620 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009621}
9622
9623static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9624{
9625 struct io_ring_ctx *ctx = f->private_data;
9626
9627 if (percpu_ref_tryget(&ctx->refs)) {
9628 __io_uring_show_fdinfo(ctx, m);
9629 percpu_ref_put(&ctx->refs);
9630 }
9631}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009632#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009633
Jens Axboe2b188cc2019-01-07 10:46:33 -07009634static const struct file_operations io_uring_fops = {
9635 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009636 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009637 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009638#ifndef CONFIG_MMU
9639 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9640 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9641#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009642 .poll = io_uring_poll,
9643 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009644#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009645 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009646#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009647};
9648
9649static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9650 struct io_uring_params *p)
9651{
Hristo Venev75b28af2019-08-26 17:23:46 +00009652 struct io_rings *rings;
9653 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009654
Jens Axboebd740482020-08-05 12:58:23 -06009655 /* make sure these are sane, as we already accounted them */
9656 ctx->sq_entries = p->sq_entries;
9657 ctx->cq_entries = p->cq_entries;
9658
Hristo Venev75b28af2019-08-26 17:23:46 +00009659 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9660 if (size == SIZE_MAX)
9661 return -EOVERFLOW;
9662
9663 rings = io_mem_alloc(size);
9664 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009665 return -ENOMEM;
9666
Hristo Venev75b28af2019-08-26 17:23:46 +00009667 ctx->rings = rings;
9668 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9669 rings->sq_ring_mask = p->sq_entries - 1;
9670 rings->cq_ring_mask = p->cq_entries - 1;
9671 rings->sq_ring_entries = p->sq_entries;
9672 rings->cq_ring_entries = p->cq_entries;
9673 ctx->sq_mask = rings->sq_ring_mask;
9674 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009675
9676 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009677 if (size == SIZE_MAX) {
9678 io_mem_free(ctx->rings);
9679 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009680 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009681 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009682
9683 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009684 if (!ctx->sq_sqes) {
9685 io_mem_free(ctx->rings);
9686 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009687 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009688 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009689
Jens Axboe2b188cc2019-01-07 10:46:33 -07009690 return 0;
9691}
9692
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009693static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9694{
9695 int ret, fd;
9696
9697 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9698 if (fd < 0)
9699 return fd;
9700
9701 ret = io_uring_add_task_file(ctx, file);
9702 if (ret) {
9703 put_unused_fd(fd);
9704 return ret;
9705 }
9706 fd_install(fd, file);
9707 return fd;
9708}
9709
Jens Axboe2b188cc2019-01-07 10:46:33 -07009710/*
9711 * Allocate an anonymous fd, this is what constitutes the application
9712 * visible backing of an io_uring instance. The application mmaps this
9713 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9714 * we have to tie this fd to a socket for file garbage collection purposes.
9715 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009716static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009717{
9718 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009719#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009720 int ret;
9721
Jens Axboe2b188cc2019-01-07 10:46:33 -07009722 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9723 &ctx->ring_sock);
9724 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009725 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009726#endif
9727
Jens Axboe2b188cc2019-01-07 10:46:33 -07009728 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9729 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009730#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009731 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009732 sock_release(ctx->ring_sock);
9733 ctx->ring_sock = NULL;
9734 } else {
9735 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009736 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009737#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009738 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009739}
9740
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009741static int io_uring_create(unsigned entries, struct io_uring_params *p,
9742 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009743{
9744 struct user_struct *user = NULL;
9745 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009746 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009747 int ret;
9748
Jens Axboe8110c1a2019-12-28 15:39:54 -07009749 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009750 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009751 if (entries > IORING_MAX_ENTRIES) {
9752 if (!(p->flags & IORING_SETUP_CLAMP))
9753 return -EINVAL;
9754 entries = IORING_MAX_ENTRIES;
9755 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009756
9757 /*
9758 * Use twice as many entries for the CQ ring. It's possible for the
9759 * application to drive a higher depth than the size of the SQ ring,
9760 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009761 * some flexibility in overcommitting a bit. If the application has
9762 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9763 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009764 */
9765 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009766 if (p->flags & IORING_SETUP_CQSIZE) {
9767 /*
9768 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9769 * to a power-of-two, if it isn't already. We do NOT impose
9770 * any cq vs sq ring sizing.
9771 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009772 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009773 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009774 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9775 if (!(p->flags & IORING_SETUP_CLAMP))
9776 return -EINVAL;
9777 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9778 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009779 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9780 if (p->cq_entries < p->sq_entries)
9781 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009782 } else {
9783 p->cq_entries = 2 * p->sq_entries;
9784 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009785
9786 user = get_uid(current_user());
Jens Axboe2b188cc2019-01-07 10:46:33 -07009787
9788 ctx = io_ring_ctx_alloc(p);
9789 if (!ctx) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07009790 free_uid(user);
9791 return -ENOMEM;
9792 }
9793 ctx->compat = in_compat_syscall();
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009794 ctx->limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009795 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009796 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009797#ifdef CONFIG_AUDIT
9798 ctx->loginuid = current->loginuid;
9799 ctx->sessionid = current->sessionid;
9800#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009801 ctx->sqo_task = get_task_struct(current);
9802
9803 /*
9804 * This is just grabbed for accounting purposes. When a process exits,
9805 * the mm is exited and dropped before the files, hence we need to hang
9806 * on to this mm purely for the purposes of being able to unaccount
9807 * memory (locked/pinned vm). It's not used for anything else.
9808 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009809 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009810 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009811
Dennis Zhou91d8f512020-09-16 13:41:05 -07009812#ifdef CONFIG_BLK_CGROUP
9813 /*
9814 * The sq thread will belong to the original cgroup it was inited in.
9815 * If the cgroup goes offline (e.g. disabling the io controller), then
9816 * issued bios will be associated with the closest cgroup later in the
9817 * block layer.
9818 */
9819 rcu_read_lock();
9820 ctx->sqo_blkcg_css = blkcg_css();
9821 ret = css_tryget_online(ctx->sqo_blkcg_css);
9822 rcu_read_unlock();
9823 if (!ret) {
9824 /* don't init against a dying cgroup, have the user try again */
9825 ctx->sqo_blkcg_css = NULL;
9826 ret = -ENODEV;
9827 goto err;
9828 }
9829#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009830 ret = io_allocate_scq_urings(ctx, p);
9831 if (ret)
9832 goto err;
9833
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009834 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009835 if (ret)
9836 goto err;
9837
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009838 if (!(p->flags & IORING_SETUP_R_DISABLED))
9839 io_sq_offload_start(ctx);
9840
Jens Axboe2b188cc2019-01-07 10:46:33 -07009841 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009842 p->sq_off.head = offsetof(struct io_rings, sq.head);
9843 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9844 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9845 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9846 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9847 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9848 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009849
9850 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009851 p->cq_off.head = offsetof(struct io_rings, cq.head);
9852 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9853 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9854 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9855 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9856 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009857 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009858
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009859 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9860 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009861 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009862 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9863 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009864
9865 if (copy_to_user(params, p, sizeof(*p))) {
9866 ret = -EFAULT;
9867 goto err;
9868 }
Jens Axboed1719f72020-07-30 13:43:53 -06009869
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009870 file = io_uring_get_file(ctx);
9871 if (IS_ERR(file)) {
9872 ret = PTR_ERR(file);
9873 goto err;
9874 }
9875
Jens Axboed1719f72020-07-30 13:43:53 -06009876 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009877 * Install ring fd as the very last thing, so we don't risk someone
9878 * having closed it before we finish setup
9879 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009880 ret = io_uring_install_fd(ctx, file);
9881 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009882 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009883 /* fput will clean it up */
9884 fput(file);
9885 return ret;
9886 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009887
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009888 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009889 return ret;
9890err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009891 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009892 io_ring_ctx_wait_and_kill(ctx);
9893 return ret;
9894}
9895
9896/*
9897 * Sets up an aio uring context, and returns the fd. Applications asks for a
9898 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9899 * params structure passed in.
9900 */
9901static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9902{
9903 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009904 int i;
9905
9906 if (copy_from_user(&p, params, sizeof(p)))
9907 return -EFAULT;
9908 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9909 if (p.resv[i])
9910 return -EINVAL;
9911 }
9912
Jens Axboe6c271ce2019-01-10 11:22:30 -07009913 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009914 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009915 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9916 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009917 return -EINVAL;
9918
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009919 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009920}
9921
9922SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9923 struct io_uring_params __user *, params)
9924{
9925 return io_uring_setup(entries, params);
9926}
9927
Jens Axboe66f4af92020-01-16 15:36:52 -07009928static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9929{
9930 struct io_uring_probe *p;
9931 size_t size;
9932 int i, ret;
9933
9934 size = struct_size(p, ops, nr_args);
9935 if (size == SIZE_MAX)
9936 return -EOVERFLOW;
9937 p = kzalloc(size, GFP_KERNEL);
9938 if (!p)
9939 return -ENOMEM;
9940
9941 ret = -EFAULT;
9942 if (copy_from_user(p, arg, size))
9943 goto out;
9944 ret = -EINVAL;
9945 if (memchr_inv(p, 0, size))
9946 goto out;
9947
9948 p->last_op = IORING_OP_LAST - 1;
9949 if (nr_args > IORING_OP_LAST)
9950 nr_args = IORING_OP_LAST;
9951
9952 for (i = 0; i < nr_args; i++) {
9953 p->ops[i].op = i;
9954 if (!io_op_defs[i].not_supported)
9955 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9956 }
9957 p->ops_len = i;
9958
9959 ret = 0;
9960 if (copy_to_user(arg, p, size))
9961 ret = -EFAULT;
9962out:
9963 kfree(p);
9964 return ret;
9965}
9966
Jens Axboe071698e2020-01-28 10:04:42 -07009967static int io_register_personality(struct io_ring_ctx *ctx)
9968{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009969 struct io_identity *id;
9970 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009971
Jens Axboe1e6fa522020-10-15 08:46:24 -06009972 id = kmalloc(sizeof(*id), GFP_KERNEL);
9973 if (unlikely(!id))
9974 return -ENOMEM;
9975
9976 io_init_identity(id);
9977 id->creds = get_current_cred();
9978
9979 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9980 if (ret < 0) {
9981 put_cred(id->creds);
9982 kfree(id);
9983 }
9984 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009985}
9986
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009987static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9988 unsigned int nr_args)
9989{
9990 struct io_uring_restriction *res;
9991 size_t size;
9992 int i, ret;
9993
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009994 /* Restrictions allowed only if rings started disabled */
9995 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9996 return -EBADFD;
9997
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009998 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009999 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010000 return -EBUSY;
10001
10002 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10003 return -EINVAL;
10004
10005 size = array_size(nr_args, sizeof(*res));
10006 if (size == SIZE_MAX)
10007 return -EOVERFLOW;
10008
10009 res = memdup_user(arg, size);
10010 if (IS_ERR(res))
10011 return PTR_ERR(res);
10012
10013 ret = 0;
10014
10015 for (i = 0; i < nr_args; i++) {
10016 switch (res[i].opcode) {
10017 case IORING_RESTRICTION_REGISTER_OP:
10018 if (res[i].register_op >= IORING_REGISTER_LAST) {
10019 ret = -EINVAL;
10020 goto out;
10021 }
10022
10023 __set_bit(res[i].register_op,
10024 ctx->restrictions.register_op);
10025 break;
10026 case IORING_RESTRICTION_SQE_OP:
10027 if (res[i].sqe_op >= IORING_OP_LAST) {
10028 ret = -EINVAL;
10029 goto out;
10030 }
10031
10032 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10033 break;
10034 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10035 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10036 break;
10037 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10038 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10039 break;
10040 default:
10041 ret = -EINVAL;
10042 goto out;
10043 }
10044 }
10045
10046out:
10047 /* Reset all restrictions if an error happened */
10048 if (ret != 0)
10049 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10050 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010051 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010052
10053 kfree(res);
10054 return ret;
10055}
10056
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010057static int io_register_enable_rings(struct io_ring_ctx *ctx)
10058{
10059 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10060 return -EBADFD;
10061
10062 if (ctx->restrictions.registered)
10063 ctx->restricted = 1;
10064
10065 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10066
10067 io_sq_offload_start(ctx);
10068
10069 return 0;
10070}
10071
Jens Axboe071698e2020-01-28 10:04:42 -070010072static bool io_register_op_must_quiesce(int op)
10073{
10074 switch (op) {
10075 case IORING_UNREGISTER_FILES:
10076 case IORING_REGISTER_FILES_UPDATE:
10077 case IORING_REGISTER_PROBE:
10078 case IORING_REGISTER_PERSONALITY:
10079 case IORING_UNREGISTER_PERSONALITY:
10080 return false;
10081 default:
10082 return true;
10083 }
10084}
10085
Jens Axboeedafcce2019-01-09 09:16:05 -070010086static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10087 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010088 __releases(ctx->uring_lock)
10089 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010090{
10091 int ret;
10092
Jens Axboe35fa71a2019-04-22 10:23:23 -060010093 /*
10094 * We're inside the ring mutex, if the ref is already dying, then
10095 * someone else killed the ctx or is already going through
10096 * io_uring_register().
10097 */
10098 if (percpu_ref_is_dying(&ctx->refs))
10099 return -ENXIO;
10100
Jens Axboe071698e2020-01-28 10:04:42 -070010101 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010102 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010103
Jens Axboe05f3fb32019-12-09 11:22:50 -070010104 /*
10105 * Drop uring mutex before waiting for references to exit. If
10106 * another thread is currently inside io_uring_enter() it might
10107 * need to grab the uring_lock to make progress. If we hold it
10108 * here across the drain wait, then we can deadlock. It's safe
10109 * to drop the mutex here, since no new references will come in
10110 * after we've killed the percpu ref.
10111 */
10112 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010113 do {
10114 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10115 if (!ret)
10116 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010117 ret = io_run_task_work_sig();
10118 if (ret < 0)
10119 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010120 } while (1);
10121
Jens Axboe05f3fb32019-12-09 11:22:50 -070010122 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010123
Jens Axboec1503682020-01-08 08:26:07 -070010124 if (ret) {
10125 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010126 goto out_quiesce;
10127 }
10128 }
10129
10130 if (ctx->restricted) {
10131 if (opcode >= IORING_REGISTER_LAST) {
10132 ret = -EINVAL;
10133 goto out;
10134 }
10135
10136 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10137 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010138 goto out;
10139 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010140 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010141
10142 switch (opcode) {
10143 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010144 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010145 break;
10146 case IORING_UNREGISTER_BUFFERS:
10147 ret = -EINVAL;
10148 if (arg || nr_args)
10149 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010150 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010151 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010152 case IORING_REGISTER_FILES:
10153 ret = io_sqe_files_register(ctx, arg, nr_args);
10154 break;
10155 case IORING_UNREGISTER_FILES:
10156 ret = -EINVAL;
10157 if (arg || nr_args)
10158 break;
10159 ret = io_sqe_files_unregister(ctx);
10160 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010161 case IORING_REGISTER_FILES_UPDATE:
10162 ret = io_sqe_files_update(ctx, arg, nr_args);
10163 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010164 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010165 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010166 ret = -EINVAL;
10167 if (nr_args != 1)
10168 break;
10169 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010170 if (ret)
10171 break;
10172 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10173 ctx->eventfd_async = 1;
10174 else
10175 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010176 break;
10177 case IORING_UNREGISTER_EVENTFD:
10178 ret = -EINVAL;
10179 if (arg || nr_args)
10180 break;
10181 ret = io_eventfd_unregister(ctx);
10182 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010183 case IORING_REGISTER_PROBE:
10184 ret = -EINVAL;
10185 if (!arg || nr_args > 256)
10186 break;
10187 ret = io_probe(ctx, arg, nr_args);
10188 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010189 case IORING_REGISTER_PERSONALITY:
10190 ret = -EINVAL;
10191 if (arg || nr_args)
10192 break;
10193 ret = io_register_personality(ctx);
10194 break;
10195 case IORING_UNREGISTER_PERSONALITY:
10196 ret = -EINVAL;
10197 if (arg)
10198 break;
10199 ret = io_unregister_personality(ctx, nr_args);
10200 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010201 case IORING_REGISTER_ENABLE_RINGS:
10202 ret = -EINVAL;
10203 if (arg || nr_args)
10204 break;
10205 ret = io_register_enable_rings(ctx);
10206 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010207 case IORING_REGISTER_RESTRICTIONS:
10208 ret = io_register_restrictions(ctx, arg, nr_args);
10209 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010210 default:
10211 ret = -EINVAL;
10212 break;
10213 }
10214
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010215out:
Jens Axboe071698e2020-01-28 10:04:42 -070010216 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010217 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010218 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010219out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010220 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010221 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010222 return ret;
10223}
10224
10225SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10226 void __user *, arg, unsigned int, nr_args)
10227{
10228 struct io_ring_ctx *ctx;
10229 long ret = -EBADF;
10230 struct fd f;
10231
10232 f = fdget(fd);
10233 if (!f.file)
10234 return -EBADF;
10235
10236 ret = -EOPNOTSUPP;
10237 if (f.file->f_op != &io_uring_fops)
10238 goto out_fput;
10239
10240 ctx = f.file->private_data;
10241
10242 mutex_lock(&ctx->uring_lock);
10243 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10244 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010245 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10246 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010247out_fput:
10248 fdput(f);
10249 return ret;
10250}
10251
Jens Axboe2b188cc2019-01-07 10:46:33 -070010252static int __init io_uring_init(void)
10253{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010254#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10255 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10256 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10257} while (0)
10258
10259#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10260 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10261 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10262 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10263 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10264 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10265 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10266 BUILD_BUG_SQE_ELEM(8, __u64, off);
10267 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10268 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010269 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010270 BUILD_BUG_SQE_ELEM(24, __u32, len);
10271 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10272 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10273 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10274 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010275 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10276 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010277 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10278 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10279 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10280 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10281 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10282 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10283 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10284 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010285 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010286 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10287 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10288 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010289 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010290
Jens Axboed3656342019-12-18 09:50:26 -070010291 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010292 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010293 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10294 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010295 return 0;
10296};
10297__initcall(io_uring_init);