blob: 2c7ff0b1b086af819c2435a2ec49bf6c9e3c151a [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
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001020enum io_mem_account {
1021 ACCT_LOCKED,
1022 ACCT_PINNED,
1023};
1024
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001025static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1026 struct task_struct *task,
1027 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001028static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001029static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001030 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001031static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1032 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001033
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001034static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001035 unsigned int issue_flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001036static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001037static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001038static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001039static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001040static void io_dismantle_req(struct io_kiocb *req);
1041static void io_put_task(struct task_struct *task, int nr);
1042static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001043static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001044static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001045static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001046static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001047 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001048 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001049static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001050static struct file *io_file_get(struct io_submit_state *state,
1051 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001052static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001053static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001054
Pavel Begunkov847595d2021-02-04 13:52:06 +00001055static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1056 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001057static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1058 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001059 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001060static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001061static void io_submit_flush_completions(struct io_comp_state *cs,
1062 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001063
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064static struct kmem_cache *req_cachep;
1065
Jens Axboe09186822020-10-13 15:01:40 -06001066static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001067
1068struct sock *io_uring_get_socket(struct file *file)
1069{
1070#if defined(CONFIG_UNIX)
1071 if (file->f_op == &io_uring_fops) {
1072 struct io_ring_ctx *ctx = file->private_data;
1073
1074 return ctx->ring_sock->sk;
1075 }
1076#endif
1077 return NULL;
1078}
1079EXPORT_SYMBOL(io_uring_get_socket);
1080
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001081#define io_for_each_link(pos, head) \
1082 for (pos = (head); pos; pos = pos->link)
1083
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001084static inline void io_clean_op(struct io_kiocb *req)
1085{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001086 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001087 __io_clean_op(req);
1088}
1089
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001090static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001091{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001092 struct io_ring_ctx *ctx = req->ctx;
1093
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001094 if (!req->fixed_rsrc_refs) {
1095 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1096 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001097 }
1098}
1099
Pavel Begunkov08d23632020-11-06 13:00:22 +00001100static bool io_match_task(struct io_kiocb *head,
1101 struct task_struct *task,
1102 struct files_struct *files)
1103{
1104 struct io_kiocb *req;
1105
Jens Axboe84965ff2021-01-23 15:51:11 -07001106 if (task && head->task != task) {
1107 /* in terms of cancelation, always match if req task is dead */
1108 if (head->task->flags & PF_EXITING)
1109 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001110 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001111 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001112 if (!files)
1113 return true;
1114
1115 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001116 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1117 continue;
1118 if (req->file && req->file->f_op == &io_uring_fops)
1119 return true;
1120 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001121 req->work.identity->files == files)
1122 return true;
1123 }
1124 return false;
1125}
1126
Jens Axboe28cea78a2020-09-14 10:51:17 -06001127static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001128{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001129 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001130 struct mm_struct *mm = current->mm;
1131
1132 if (mm) {
1133 kthread_unuse_mm(mm);
1134 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001135 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001136 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001137 if (files) {
1138 struct nsproxy *nsproxy = current->nsproxy;
1139
1140 task_lock(current);
1141 current->files = NULL;
1142 current->nsproxy = NULL;
1143 task_unlock(current);
1144 put_files_struct(files);
1145 put_nsproxy(nsproxy);
1146 }
1147}
1148
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001149static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001150{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001151 if (current->flags & PF_EXITING)
1152 return -EFAULT;
1153
Jens Axboe28cea78a2020-09-14 10:51:17 -06001154 if (!current->files) {
1155 struct files_struct *files;
1156 struct nsproxy *nsproxy;
1157
1158 task_lock(ctx->sqo_task);
1159 files = ctx->sqo_task->files;
1160 if (!files) {
1161 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001162 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001163 }
1164 atomic_inc(&files->count);
1165 get_nsproxy(ctx->sqo_task->nsproxy);
1166 nsproxy = ctx->sqo_task->nsproxy;
1167 task_unlock(ctx->sqo_task);
1168
1169 task_lock(current);
1170 current->files = files;
1171 current->nsproxy = nsproxy;
1172 task_unlock(current);
1173 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001174 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001175}
1176
1177static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1178{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001179 struct mm_struct *mm;
1180
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001181 if (current->flags & PF_EXITING)
1182 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001183 if (current->mm)
1184 return 0;
1185
1186 /* Should never happen */
1187 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1188 return -EFAULT;
1189
1190 task_lock(ctx->sqo_task);
1191 mm = ctx->sqo_task->mm;
1192 if (unlikely(!mm || !mmget_not_zero(mm)))
1193 mm = NULL;
1194 task_unlock(ctx->sqo_task);
1195
1196 if (mm) {
1197 kthread_use_mm(mm);
1198 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001199 }
1200
Jens Axboe4b70cf92020-11-02 10:39:05 -07001201 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001202}
1203
Jens Axboe28cea78a2020-09-14 10:51:17 -06001204static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1205 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001206{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001207 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001208 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001209
1210 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001211 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001212 if (unlikely(ret))
1213 return ret;
1214 }
1215
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001216 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1217 ret = __io_sq_thread_acquire_files(ctx);
1218 if (unlikely(ret))
1219 return ret;
1220 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001221
1222 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001223}
1224
Dennis Zhou91d8f512020-09-16 13:41:05 -07001225static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1226 struct cgroup_subsys_state **cur_css)
1227
1228{
1229#ifdef CONFIG_BLK_CGROUP
1230 /* puts the old one when swapping */
1231 if (*cur_css != ctx->sqo_blkcg_css) {
1232 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1233 *cur_css = ctx->sqo_blkcg_css;
1234 }
1235#endif
1236}
1237
1238static void io_sq_thread_unassociate_blkcg(void)
1239{
1240#ifdef CONFIG_BLK_CGROUP
1241 kthread_associate_blkcg(NULL);
1242#endif
1243}
1244
Jens Axboec40f6372020-06-25 15:39:59 -06001245static inline void req_set_fail_links(struct io_kiocb *req)
1246{
1247 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1248 req->flags |= REQ_F_FAIL_LINK;
1249}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001250
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001251/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001252 * None of these are dereferenced, they are simply used to check if any of
1253 * them have changed. If we're under current and check they are still the
1254 * same, we're fine to grab references to them for actual out-of-line use.
1255 */
1256static void io_init_identity(struct io_identity *id)
1257{
1258 id->files = current->files;
1259 id->mm = current->mm;
1260#ifdef CONFIG_BLK_CGROUP
1261 rcu_read_lock();
1262 id->blkcg_css = blkcg_css();
1263 rcu_read_unlock();
1264#endif
1265 id->creds = current_cred();
1266 id->nsproxy = current->nsproxy;
1267 id->fs = current->fs;
1268 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001269#ifdef CONFIG_AUDIT
1270 id->loginuid = current->loginuid;
1271 id->sessionid = current->sessionid;
1272#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001273 refcount_set(&id->count, 1);
1274}
1275
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001276static inline void __io_req_init_async(struct io_kiocb *req)
1277{
1278 memset(&req->work, 0, sizeof(req->work));
1279 req->flags |= REQ_F_WORK_INITIALIZED;
1280}
1281
Jens Axboe1e6fa522020-10-15 08:46:24 -06001282/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001283 * Note: must call io_req_init_async() for the first time you
1284 * touch any members of io_wq_work.
1285 */
1286static inline void io_req_init_async(struct io_kiocb *req)
1287{
Jens Axboe500a3732020-10-15 17:38:03 -06001288 struct io_uring_task *tctx = current->io_uring;
1289
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001290 if (req->flags & REQ_F_WORK_INITIALIZED)
1291 return;
1292
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001293 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001294
1295 /* Grab a ref if this isn't our static identity */
1296 req->work.identity = tctx->identity;
1297 if (tctx->identity != &tctx->__identity)
1298 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001299}
1300
Jens Axboe2b188cc2019-01-07 10:46:33 -07001301static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1302{
1303 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1304
Jens Axboe0f158b42020-05-14 17:18:39 -06001305 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306}
1307
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001308static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1309{
1310 return !req->timeout.off;
1311}
1312
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1314{
1315 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001316 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317
1318 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1319 if (!ctx)
1320 return NULL;
1321
Jens Axboe78076bb2019-12-04 19:56:40 -07001322 /*
1323 * Use 5 bits less than the max cq entries, that should give us around
1324 * 32 entries per hash list if totally full and uniformly spread.
1325 */
1326 hash_bits = ilog2(p->cq_entries);
1327 hash_bits -= 5;
1328 if (hash_bits <= 0)
1329 hash_bits = 1;
1330 ctx->cancel_hash_bits = hash_bits;
1331 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1332 GFP_KERNEL);
1333 if (!ctx->cancel_hash)
1334 goto err;
1335 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1336
Roman Gushchin21482892019-05-07 10:01:48 -07001337 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001338 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1339 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340
1341 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001342 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001343 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001344 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001345 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001346 init_completion(&ctx->ref_comp);
1347 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001348 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001349 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350 mutex_init(&ctx->uring_lock);
1351 init_waitqueue_head(&ctx->wait);
1352 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001353 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001354 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001355 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001356 spin_lock_init(&ctx->inflight_lock);
1357 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001358 spin_lock_init(&ctx->rsrc_ref_lock);
1359 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001360 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1361 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001362 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001363 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001365err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001366 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001367 kfree(ctx);
1368 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001369}
1370
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001371static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001372{
Jens Axboe2bc99302020-07-09 09:43:27 -06001373 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1374 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001375
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001376 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001377 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001378 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001379
Bob Liu9d858b22019-11-13 18:06:25 +08001380 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001381}
1382
Jens Axboe5c3462c2020-10-15 09:02:33 -06001383static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001384{
Jens Axboe500a3732020-10-15 17:38:03 -06001385 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001386 return;
1387 if (refcount_dec_and_test(&req->work.identity->count))
1388 kfree(req->work.identity);
1389}
1390
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001391static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001392{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001393 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001394 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001395
Pavel Begunkove86d0042021-02-01 18:59:54 +00001396 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001397 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001398#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001399 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001400 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001401#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001402 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001403 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001404 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001405 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001406
Jens Axboe98447d62020-10-14 10:48:51 -06001407 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001408 if (--fs->users)
1409 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001410 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001411 if (fs)
1412 free_fs_struct(fs);
1413 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001414 if (req->work.flags & IO_WQ_WORK_FILES) {
1415 put_files_struct(req->work.identity->files);
1416 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001417 }
1418 if (req->flags & REQ_F_INFLIGHT) {
1419 struct io_ring_ctx *ctx = req->ctx;
1420 struct io_uring_task *tctx = req->task->io_uring;
1421 unsigned long flags;
1422
1423 spin_lock_irqsave(&ctx->inflight_lock, flags);
1424 list_del(&req->inflight_entry);
1425 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1426 req->flags &= ~REQ_F_INFLIGHT;
1427 if (atomic_read(&tctx->in_idle))
1428 wake_up(&tctx->wait);
1429 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001430
Pavel Begunkove86d0042021-02-01 18:59:54 +00001431 req->flags &= ~REQ_F_WORK_INITIALIZED;
1432 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1433 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001434 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001435}
1436
1437/*
1438 * Create a private copy of io_identity, since some fields don't match
1439 * the current context.
1440 */
1441static bool io_identity_cow(struct io_kiocb *req)
1442{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001443 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001444 const struct cred *creds = NULL;
1445 struct io_identity *id;
1446
1447 if (req->work.flags & IO_WQ_WORK_CREDS)
1448 creds = req->work.identity->creds;
1449
1450 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1451 if (unlikely(!id)) {
1452 req->work.flags |= IO_WQ_WORK_CANCEL;
1453 return false;
1454 }
1455
1456 /*
1457 * We can safely just re-init the creds we copied Either the field
1458 * matches the current one, or we haven't grabbed it yet. The only
1459 * exception is ->creds, through registered personalities, so handle
1460 * that one separately.
1461 */
1462 io_init_identity(id);
1463 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001464 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001465
1466 /* add one for this request */
1467 refcount_inc(&id->count);
1468
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001469 /* drop tctx and req identity references, if needed */
1470 if (tctx->identity != &tctx->__identity &&
1471 refcount_dec_and_test(&tctx->identity->count))
1472 kfree(tctx->identity);
1473 if (req->work.identity != &tctx->__identity &&
1474 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001475 kfree(req->work.identity);
1476
1477 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001478 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001479 return true;
1480}
1481
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001482static void io_req_track_inflight(struct io_kiocb *req)
1483{
1484 struct io_ring_ctx *ctx = req->ctx;
1485
1486 if (!(req->flags & REQ_F_INFLIGHT)) {
1487 io_req_init_async(req);
1488 req->flags |= REQ_F_INFLIGHT;
1489
1490 spin_lock_irq(&ctx->inflight_lock);
1491 list_add(&req->inflight_entry, &ctx->inflight_list);
1492 spin_unlock_irq(&ctx->inflight_lock);
1493 }
1494}
1495
Jens Axboe1e6fa522020-10-15 08:46:24 -06001496static bool io_grab_identity(struct io_kiocb *req)
1497{
1498 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001499 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001500
Jens Axboe69228332020-10-20 14:28:41 -06001501 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1502 if (id->fsize != rlimit(RLIMIT_FSIZE))
1503 return false;
1504 req->work.flags |= IO_WQ_WORK_FSIZE;
1505 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001506#ifdef CONFIG_BLK_CGROUP
1507 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1508 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1509 rcu_read_lock();
1510 if (id->blkcg_css != blkcg_css()) {
1511 rcu_read_unlock();
1512 return false;
1513 }
1514 /*
1515 * This should be rare, either the cgroup is dying or the task
1516 * is moving cgroups. Just punt to root for the handful of ios.
1517 */
1518 if (css_tryget_online(id->blkcg_css))
1519 req->work.flags |= IO_WQ_WORK_BLKCG;
1520 rcu_read_unlock();
1521 }
1522#endif
1523 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1524 if (id->creds != current_cred())
1525 return false;
1526 get_cred(id->creds);
1527 req->work.flags |= IO_WQ_WORK_CREDS;
1528 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001529#ifdef CONFIG_AUDIT
1530 if (!uid_eq(current->loginuid, id->loginuid) ||
1531 current->sessionid != id->sessionid)
1532 return false;
1533#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001534 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1535 (def->work_flags & IO_WQ_WORK_FS)) {
1536 if (current->fs != id->fs)
1537 return false;
1538 spin_lock(&id->fs->lock);
1539 if (!id->fs->in_exec) {
1540 id->fs->users++;
1541 req->work.flags |= IO_WQ_WORK_FS;
1542 } else {
1543 req->work.flags |= IO_WQ_WORK_CANCEL;
1544 }
1545 spin_unlock(&current->fs->lock);
1546 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001547 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1548 (def->work_flags & IO_WQ_WORK_FILES) &&
1549 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1550 if (id->files != current->files ||
1551 id->nsproxy != current->nsproxy)
1552 return false;
1553 atomic_inc(&id->files->count);
1554 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001555 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001556 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001557 }
Jens Axboe77788772020-12-29 10:50:46 -07001558 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1559 (def->work_flags & IO_WQ_WORK_MM)) {
1560 if (id->mm != current->mm)
1561 return false;
1562 mmgrab(id->mm);
1563 req->work.flags |= IO_WQ_WORK_MM;
1564 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001565
1566 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001567}
1568
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001569static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001570{
Jens Axboed3656342019-12-18 09:50:26 -07001571 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001572 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001573
Pavel Begunkov16d59802020-07-12 16:16:47 +03001574 io_req_init_async(req);
1575
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001576 if (req->flags & REQ_F_FORCE_ASYNC)
1577 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1578
Jens Axboed3656342019-12-18 09:50:26 -07001579 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001580 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001581 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001582 } else {
1583 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001584 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001585 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001586
Jens Axboe1e6fa522020-10-15 08:46:24 -06001587 /* if we fail grabbing identity, we must COW, regrab, and retry */
1588 if (io_grab_identity(req))
1589 return;
1590
1591 if (!io_identity_cow(req))
1592 return;
1593
1594 /* can't fail at this point */
1595 if (!io_grab_identity(req))
1596 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001597}
1598
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001599static void io_prep_async_link(struct io_kiocb *req)
1600{
1601 struct io_kiocb *cur;
1602
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001603 io_for_each_link(cur, req)
1604 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001605}
1606
Jens Axboe7271ef32020-08-10 09:55:22 -06001607static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001608{
Jackie Liua197f662019-11-08 08:09:12 -07001609 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001610 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001611
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001612 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1613 &req->work, req->flags);
1614 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001615 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001616}
1617
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001618static void io_queue_async_work(struct io_kiocb *req)
1619{
Jens Axboe7271ef32020-08-10 09:55:22 -06001620 struct io_kiocb *link;
1621
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001622 /* init ->work of the whole link before punting */
1623 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001624 link = __io_queue_async_work(req);
1625
1626 if (link)
1627 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001628}
1629
Jens Axboe5262f562019-09-17 12:26:57 -06001630static void io_kill_timeout(struct io_kiocb *req)
1631{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001632 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001633 int ret;
1634
Jens Axboee8c2bc12020-08-15 18:44:09 -07001635 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001636 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001637 atomic_set(&req->ctx->cq_timeouts,
1638 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001639 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001640 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001641 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001642 }
1643}
1644
Jens Axboe76e1b642020-09-26 15:05:03 -06001645/*
1646 * Returns true if we found and killed one or more timeouts
1647 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001648static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1649 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001650{
1651 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001652 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001653
1654 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001655 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001656 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001657 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001658 canceled++;
1659 }
Jens Axboef3606e32020-09-22 08:18:24 -06001660 }
Jens Axboe5262f562019-09-17 12:26:57 -06001661 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001662 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001663}
1664
Pavel Begunkov04518942020-05-26 20:34:05 +03001665static void __io_queue_deferred(struct io_ring_ctx *ctx)
1666{
1667 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001668 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1669 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001670
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001671 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001672 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001673 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001674 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001675 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001676 } while (!list_empty(&ctx->defer_list));
1677}
1678
Pavel Begunkov360428f2020-05-30 14:54:17 +03001679static void io_flush_timeouts(struct io_ring_ctx *ctx)
1680{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001681 u32 seq;
1682
1683 if (list_empty(&ctx->timeout_list))
1684 return;
1685
1686 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1687
1688 do {
1689 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001690 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001691 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001692
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001693 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001694 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001695
1696 /*
1697 * Since seq can easily wrap around over time, subtract
1698 * the last seq at which timeouts were flushed before comparing.
1699 * Assuming not more than 2^31-1 events have happened since,
1700 * these subtractions won't have wrapped, so we can check if
1701 * target is in [last_seq, current_seq] by comparing the two.
1702 */
1703 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1704 events_got = seq - ctx->cq_last_tm_flush;
1705 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001706 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001707
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001708 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001709 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001710 } while (!list_empty(&ctx->timeout_list));
1711
1712 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001713}
1714
Jens Axboede0617e2019-04-06 21:51:27 -06001715static void io_commit_cqring(struct io_ring_ctx *ctx)
1716{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001717 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001718
1719 /* order cqe stores with ring update */
1720 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001721
Pavel Begunkov04518942020-05-26 20:34:05 +03001722 if (unlikely(!list_empty(&ctx->defer_list)))
1723 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001724}
1725
Jens Axboe90554202020-09-03 12:12:41 -06001726static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1727{
1728 struct io_rings *r = ctx->rings;
1729
1730 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1731}
1732
Pavel Begunkov888aae22021-01-19 13:32:39 +00001733static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1734{
1735 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1736}
1737
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1739{
Hristo Venev75b28af2019-08-26 17:23:46 +00001740 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741 unsigned tail;
1742
Stefan Bühler115e12e2019-04-24 23:54:18 +02001743 /*
1744 * writes to the cq entry need to come after reading head; the
1745 * control dependency is enough as we're using WRITE_ONCE to
1746 * fill the cq entry
1747 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001748 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001749 return NULL;
1750
Pavel Begunkov888aae22021-01-19 13:32:39 +00001751 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001752 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001753}
1754
Jens Axboef2842ab2020-01-08 11:04:00 -07001755static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1756{
Jens Axboef0b493e2020-02-01 21:30:11 -07001757 if (!ctx->cq_ev_fd)
1758 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001759 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1760 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001761 if (!ctx->eventfd_async)
1762 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001763 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001764}
1765
Jens Axboeb41e9852020-02-17 09:52:41 -07001766static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001767{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001768 /* see waitqueue_active() comment */
1769 smp_mb();
1770
Jens Axboe8c838782019-03-12 15:48:16 -06001771 if (waitqueue_active(&ctx->wait))
1772 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001773 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1774 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001775 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001776 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001777 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001778 wake_up_interruptible(&ctx->cq_wait);
1779 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1780 }
Jens Axboe8c838782019-03-12 15:48:16 -06001781}
1782
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001783static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1784{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001785 /* see waitqueue_active() comment */
1786 smp_mb();
1787
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001788 if (ctx->flags & IORING_SETUP_SQPOLL) {
1789 if (waitqueue_active(&ctx->wait))
1790 wake_up(&ctx->wait);
1791 }
1792 if (io_should_trigger_evfd(ctx))
1793 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001794 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001795 wake_up_interruptible(&ctx->cq_wait);
1796 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1797 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001798}
1799
Jens Axboec4a2ed72019-11-21 21:01:26 -07001800/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001801static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1802 struct task_struct *tsk,
1803 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001804{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001805 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001806 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001807 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001808 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001809 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001810 LIST_HEAD(list);
1811
Pavel Begunkove23de152020-12-17 00:24:37 +00001812 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1813 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001814
Jens Axboeb18032b2021-01-24 16:58:56 -07001815 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001816 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001817 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001818 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001819 continue;
1820
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001821 cqe = io_get_cqring(ctx);
1822 if (!cqe && !force)
1823 break;
1824
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001825 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001826 if (cqe) {
1827 WRITE_ONCE(cqe->user_data, req->user_data);
1828 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001829 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001830 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001831 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001832 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001833 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001834 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001835 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001836 }
1837
Pavel Begunkov09e88402020-12-17 00:24:38 +00001838 all_flushed = list_empty(&ctx->cq_overflow_list);
1839 if (all_flushed) {
1840 clear_bit(0, &ctx->sq_check_overflow);
1841 clear_bit(0, &ctx->cq_check_overflow);
1842 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1843 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001844
Jens Axboeb18032b2021-01-24 16:58:56 -07001845 if (posted)
1846 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001847 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001848 if (posted)
1849 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001850
1851 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001852 req = list_first_entry(&list, struct io_kiocb, compl.list);
1853 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001854 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001855 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001856
Pavel Begunkov09e88402020-12-17 00:24:38 +00001857 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001858}
1859
Pavel Begunkov6c503152021-01-04 20:36:36 +00001860static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1861 struct task_struct *tsk,
1862 struct files_struct *files)
1863{
1864 if (test_bit(0, &ctx->cq_check_overflow)) {
1865 /* iopoll syncs against uring_lock, not completion_lock */
1866 if (ctx->flags & IORING_SETUP_IOPOLL)
1867 mutex_lock(&ctx->uring_lock);
1868 __io_cqring_overflow_flush(ctx, force, tsk, files);
1869 if (ctx->flags & IORING_SETUP_IOPOLL)
1870 mutex_unlock(&ctx->uring_lock);
1871 }
1872}
1873
Jens Axboebcda7ba2020-02-23 16:42:51 -07001874static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001876 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877 struct io_uring_cqe *cqe;
1878
Jens Axboe78e19bb2019-11-06 15:21:34 -07001879 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001880
Jens Axboe2b188cc2019-01-07 10:46:33 -07001881 /*
1882 * If we can't get a cq entry, userspace overflowed the
1883 * submission (by quite a lot). Increment the overflow count in
1884 * the ring.
1885 */
1886 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001887 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001888 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001890 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001891 } else if (ctx->cq_overflow_flushed ||
1892 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001893 /*
1894 * If we're in ring overflow flush mode, or in task cancel mode,
1895 * then we cannot store the request for later flushing, we need
1896 * to drop it on the floor.
1897 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001898 ctx->cached_cq_overflow++;
1899 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001900 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001901 if (list_empty(&ctx->cq_overflow_list)) {
1902 set_bit(0, &ctx->sq_check_overflow);
1903 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001904 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001905 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001906 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001907 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001908 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001909 refcount_inc(&req->refs);
1910 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911 }
1912}
1913
Jens Axboebcda7ba2020-02-23 16:42:51 -07001914static void io_cqring_fill_event(struct io_kiocb *req, long res)
1915{
1916 __io_cqring_fill_event(req, res, 0);
1917}
1918
Jens Axboec7dae4b2021-02-09 19:53:37 -07001919static inline void io_req_complete_post(struct io_kiocb *req, long res,
1920 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001921{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001922 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923 unsigned long flags;
1924
1925 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001926 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001927 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001928 /*
1929 * If we're the last reference to this request, add to our locked
1930 * free_list cache.
1931 */
1932 if (refcount_dec_and_test(&req->refs)) {
1933 struct io_comp_state *cs = &ctx->submit_state.comp;
1934
1935 io_dismantle_req(req);
1936 io_put_task(req->task, 1);
1937 list_add(&req->compl.list, &cs->locked_free_list);
1938 cs->locked_free_nr++;
1939 } else
1940 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1942
Jens Axboe8c838782019-03-12 15:48:16 -06001943 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001944 if (req) {
1945 io_queue_next(req);
1946 percpu_ref_put(&ctx->refs);
1947 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001948}
1949
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001950static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001951 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001952{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001953 io_clean_op(req);
1954 req->result = res;
1955 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001956 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001957}
1958
Pavel Begunkov889fca72021-02-10 00:03:09 +00001959static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1960 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001961{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001962 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1963 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001964 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001965 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001966}
1967
1968static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001969{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001970 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001971}
1972
Jens Axboec7dae4b2021-02-09 19:53:37 -07001973static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001974{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001975 struct io_submit_state *state = &ctx->submit_state;
1976 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001977 struct io_kiocb *req = NULL;
1978
Jens Axboec7dae4b2021-02-09 19:53:37 -07001979 /*
1980 * If we have more than a batch's worth of requests in our IRQ side
1981 * locked cache, grab the lock and move them over to our submission
1982 * side cache.
1983 */
1984 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1985 spin_lock_irq(&ctx->completion_lock);
1986 list_splice_init(&cs->locked_free_list, &cs->free_list);
1987 cs->locked_free_nr = 0;
1988 spin_unlock_irq(&ctx->completion_lock);
1989 }
1990
1991 while (!list_empty(&cs->free_list)) {
1992 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001993 compl.list);
1994 list_del(&req->compl.list);
1995 state->reqs[state->free_reqs++] = req;
1996 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1997 break;
1998 }
1999
2000 return req != NULL;
2001}
2002
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002003static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002004{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002005 struct io_submit_state *state = &ctx->submit_state;
2006
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002007 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
2008
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002009 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002010 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002011 int ret;
2012
Jens Axboec7dae4b2021-02-09 19:53:37 -07002013 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002014 goto got_req;
2015
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002016 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
2017 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06002018
2019 /*
2020 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2021 * retry single alloc to be on the safe side.
2022 */
2023 if (unlikely(ret <= 0)) {
2024 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2025 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00002026 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06002027 ret = 1;
2028 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002029 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002031got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03002032 state->free_reqs--;
2033 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034}
2035
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002036static inline void io_put_file(struct io_kiocb *req, struct file *file,
2037 bool fixed)
2038{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002039 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002040 fput(file);
2041}
2042
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002043static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002044{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002045 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002046
Jens Axboee8c2bc12020-08-15 18:44:09 -07002047 if (req->async_data)
2048 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002049 if (req->file)
2050 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002051 if (req->fixed_rsrc_refs)
2052 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002053 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002054}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002055
Pavel Begunkov7c660732021-01-25 11:42:21 +00002056static inline void io_put_task(struct task_struct *task, int nr)
2057{
2058 struct io_uring_task *tctx = task->io_uring;
2059
2060 percpu_counter_sub(&tctx->inflight, nr);
2061 if (unlikely(atomic_read(&tctx->in_idle)))
2062 wake_up(&tctx->wait);
2063 put_task_struct_many(task, nr);
2064}
2065
Pavel Begunkov216578e2020-10-13 09:44:00 +01002066static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002067{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002068 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002069
Pavel Begunkov216578e2020-10-13 09:44:00 +01002070 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002071 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002072
Pavel Begunkov3893f392021-02-10 00:03:15 +00002073 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002074 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002075}
2076
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002077static inline void io_remove_next_linked(struct io_kiocb *req)
2078{
2079 struct io_kiocb *nxt = req->link;
2080
2081 req->link = nxt->link;
2082 nxt->link = NULL;
2083}
2084
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002085static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002086{
Jackie Liua197f662019-11-08 08:09:12 -07002087 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002088 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002089 bool cancelled = false;
2090 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002091
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002092 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002093 link = req->link;
2094
Pavel Begunkov900fad42020-10-19 16:39:16 +01002095 /*
2096 * Can happen if a linked timeout fired and link had been like
2097 * req -> link t-out -> link t-out [-> ...]
2098 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002099 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2100 struct io_timeout_data *io = link->async_data;
2101 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002102
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002103 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002104 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002105 ret = hrtimer_try_to_cancel(&io->timer);
2106 if (ret != -1) {
2107 io_cqring_fill_event(link, -ECANCELED);
2108 io_commit_cqring(ctx);
2109 cancelled = true;
2110 }
2111 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002112 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002113 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002114
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002115 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002116 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002117 io_put_req(link);
2118 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002119}
2120
Jens Axboe4d7dd462019-11-20 13:03:52 -07002121
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002122static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002123{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002124 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002125 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002126 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002127
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002128 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002129 link = req->link;
2130 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002131
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002132 while (link) {
2133 nxt = link->link;
2134 link->link = NULL;
2135
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002136 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002137 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002138
2139 /*
2140 * It's ok to free under spinlock as they're not linked anymore,
2141 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2142 * work.fs->lock.
2143 */
2144 if (link->flags & REQ_F_WORK_INITIALIZED)
2145 io_put_req_deferred(link, 2);
2146 else
2147 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002148 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002149 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002150 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002151 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002152
Jens Axboe2665abf2019-11-05 12:40:47 -07002153 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002154}
2155
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002156static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002157{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002158 if (req->flags & REQ_F_LINK_TIMEOUT)
2159 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002160
Jens Axboe9e645e112019-05-10 16:07:28 -06002161 /*
2162 * If LINK is set, we have dependent requests in this chain. If we
2163 * didn't fail this request, queue the first one up, moving any other
2164 * dependencies to the next request. In case of failure, fail the rest
2165 * of the chain.
2166 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002167 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2168 struct io_kiocb *nxt = req->link;
2169
2170 req->link = NULL;
2171 return nxt;
2172 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002173 io_fail_links(req);
2174 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002175}
Jens Axboe2665abf2019-11-05 12:40:47 -07002176
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002177static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002178{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002179 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002180 return NULL;
2181 return __io_req_find_next(req);
2182}
2183
Jens Axboe7cbf1722021-02-10 00:03:20 +00002184static bool __tctx_task_work(struct io_uring_task *tctx)
2185{
Jens Axboe65453d12021-02-10 00:03:21 +00002186 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002187 struct io_wq_work_list list;
2188 struct io_wq_work_node *node;
2189
2190 if (wq_list_empty(&tctx->task_list))
2191 return false;
2192
2193 spin_lock(&tctx->task_lock);
2194 list = tctx->task_list;
2195 INIT_WQ_LIST(&tctx->task_list);
2196 spin_unlock(&tctx->task_lock);
2197
2198 node = list.first;
2199 while (node) {
2200 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00002201 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002202 struct io_kiocb *req;
2203
2204 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00002205 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002206 req->task_work.func(&req->task_work);
2207 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00002208
2209 if (!ctx) {
2210 ctx = this_ctx;
2211 } else if (ctx != this_ctx) {
2212 mutex_lock(&ctx->uring_lock);
2213 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2214 mutex_unlock(&ctx->uring_lock);
2215 ctx = this_ctx;
2216 }
2217 }
2218
2219 if (ctx && ctx->submit_state.comp.nr) {
2220 mutex_lock(&ctx->uring_lock);
2221 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2222 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002223 }
2224
2225 return list.first != NULL;
2226}
2227
2228static void tctx_task_work(struct callback_head *cb)
2229{
2230 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2231
2232 while (__tctx_task_work(tctx))
2233 cond_resched();
2234
2235 clear_bit(0, &tctx->task_state);
2236}
2237
2238static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2239 enum task_work_notify_mode notify)
2240{
2241 struct io_uring_task *tctx = tsk->io_uring;
2242 struct io_wq_work_node *node, *prev;
2243 int ret;
2244
2245 WARN_ON_ONCE(!tctx);
2246
2247 spin_lock(&tctx->task_lock);
2248 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
2249 spin_unlock(&tctx->task_lock);
2250
2251 /* task_work already pending, we're done */
2252 if (test_bit(0, &tctx->task_state) ||
2253 test_and_set_bit(0, &tctx->task_state))
2254 return 0;
2255
2256 if (!task_work_add(tsk, &tctx->task_work, notify))
2257 return 0;
2258
2259 /*
2260 * Slow path - we failed, find and delete work. if the work is not
2261 * in the list, it got run and we're fine.
2262 */
2263 ret = 0;
2264 spin_lock(&tctx->task_lock);
2265 wq_list_for_each(node, prev, &tctx->task_list) {
2266 if (&req->io_task_work.node == node) {
2267 wq_list_del(&tctx->task_list, node, prev);
2268 ret = 1;
2269 break;
2270 }
2271 }
2272 spin_unlock(&tctx->task_lock);
2273 clear_bit(0, &tctx->task_state);
2274 return ret;
2275}
2276
Jens Axboe355fb9e2020-10-22 20:19:35 -06002277static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002278{
2279 struct task_struct *tsk = req->task;
2280 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002281 enum task_work_notify_mode notify;
2282 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002283
Jens Axboe6200b0a2020-09-13 14:38:30 -06002284 if (tsk->flags & PF_EXITING)
2285 return -ESRCH;
2286
Jens Axboec2c4c832020-07-01 15:37:11 -06002287 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002288 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2289 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2290 * processing task_work. There's no reliable way to tell if TWA_RESUME
2291 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002292 */
Jens Axboe91989c72020-10-16 09:02:26 -06002293 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002294 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002295 notify = TWA_SIGNAL;
2296
Jens Axboe7cbf1722021-02-10 00:03:20 +00002297 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002298 if (!ret)
2299 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002300
Jens Axboec2c4c832020-07-01 15:37:11 -06002301 return ret;
2302}
2303
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002304static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002305 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002306{
2307 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2308
2309 init_task_work(&req->task_work, cb);
2310 task_work_add(tsk, &req->task_work, TWA_NONE);
2311 wake_up_process(tsk);
2312}
2313
Jens Axboec40f6372020-06-25 15:39:59 -06002314static void __io_req_task_cancel(struct io_kiocb *req, int error)
2315{
2316 struct io_ring_ctx *ctx = req->ctx;
2317
2318 spin_lock_irq(&ctx->completion_lock);
2319 io_cqring_fill_event(req, error);
2320 io_commit_cqring(ctx);
2321 spin_unlock_irq(&ctx->completion_lock);
2322
2323 io_cqring_ev_posted(ctx);
2324 req_set_fail_links(req);
2325 io_double_put_req(req);
2326}
2327
2328static void io_req_task_cancel(struct callback_head *cb)
2329{
2330 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002331 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002332
2333 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002334 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002335}
2336
2337static void __io_req_task_submit(struct io_kiocb *req)
2338{
2339 struct io_ring_ctx *ctx = req->ctx;
2340
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002341 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002342 if (!ctx->sqo_dead &&
2343 !__io_sq_thread_acquire_mm(ctx) &&
2344 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002345 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002346 else
Jens Axboec40f6372020-06-25 15:39:59 -06002347 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002348 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002349}
2350
Jens Axboec40f6372020-06-25 15:39:59 -06002351static void io_req_task_submit(struct callback_head *cb)
2352{
2353 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002354 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002355
2356 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002357 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002358}
2359
2360static void io_req_task_queue(struct io_kiocb *req)
2361{
Jens Axboec40f6372020-06-25 15:39:59 -06002362 int ret;
2363
Jens Axboe7cbf1722021-02-10 00:03:20 +00002364 req->task_work.func = io_req_task_submit;
Jens Axboe6d816e02020-08-11 08:04:14 -06002365 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002366
Jens Axboe355fb9e2020-10-22 20:19:35 -06002367 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002368 if (unlikely(ret))
2369 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002370}
2371
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002372static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002373{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002374 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002375
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002376 if (nxt)
2377 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002378}
2379
Jens Axboe9e645e112019-05-10 16:07:28 -06002380static void io_free_req(struct io_kiocb *req)
2381{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002382 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002383 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002384}
2385
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002386struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002387 struct task_struct *task;
2388 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002389 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002390};
2391
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002392static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002393{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002394 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002395 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002396 rb->task = NULL;
2397}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002398
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002399static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2400 struct req_batch *rb)
2401{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002402 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002403 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002404 rb->task = NULL;
2405 }
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002406 if (rb->ctx_refs)
2407 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002408}
2409
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002410static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2411 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002412{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002413 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002414
Jens Axboee3bc8e92020-09-24 08:45:57 -06002415 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002416 if (rb->task)
2417 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002418 rb->task = req->task;
2419 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002420 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002421 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002422 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002423
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002424 io_dismantle_req(req);
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002425 if (state->free_reqs != ARRAY_SIZE(state->reqs)) {
2426 state->reqs[state->free_reqs++] = req;
2427 } else {
Jens Axboe1b4c3512021-02-10 00:03:19 +00002428 struct io_comp_state *cs = &req->ctx->submit_state.comp;
2429
2430 list_add(&req->compl.list, &cs->free_list);
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002431 }
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002432}
2433
Pavel Begunkov905c1722021-02-10 00:03:14 +00002434static void io_submit_flush_completions(struct io_comp_state *cs,
2435 struct io_ring_ctx *ctx)
2436{
2437 int i, nr = cs->nr;
2438 struct io_kiocb *req;
2439 struct req_batch rb;
2440
2441 io_init_req_batch(&rb);
2442 spin_lock_irq(&ctx->completion_lock);
2443 for (i = 0; i < nr; i++) {
2444 req = cs->reqs[i];
2445 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2446 }
2447 io_commit_cqring(ctx);
2448 spin_unlock_irq(&ctx->completion_lock);
2449
2450 io_cqring_ev_posted(ctx);
2451 for (i = 0; i < nr; i++) {
2452 req = cs->reqs[i];
2453
2454 /* submission and completion refs */
2455 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002456 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002457 }
2458
2459 io_req_free_batch_finish(ctx, &rb);
2460 cs->nr = 0;
2461}
2462
Jens Axboeba816ad2019-09-28 11:36:45 -06002463/*
2464 * Drop reference to request, return next in chain (if there is one) if this
2465 * was the last reference to this request.
2466 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002467static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002468{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002469 struct io_kiocb *nxt = NULL;
2470
Jens Axboe2a44f462020-02-25 13:25:41 -07002471 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002472 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002473 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002474 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002475 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002476}
2477
Jens Axboe2b188cc2019-01-07 10:46:33 -07002478static void io_put_req(struct io_kiocb *req)
2479{
Jens Axboedef596e2019-01-09 08:59:42 -07002480 if (refcount_dec_and_test(&req->refs))
2481 io_free_req(req);
2482}
2483
Pavel Begunkov216578e2020-10-13 09:44:00 +01002484static void io_put_req_deferred_cb(struct callback_head *cb)
2485{
2486 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2487
2488 io_free_req(req);
2489}
2490
2491static void io_free_req_deferred(struct io_kiocb *req)
2492{
2493 int ret;
2494
Jens Axboe7cbf1722021-02-10 00:03:20 +00002495 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002496 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002497 if (unlikely(ret))
2498 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002499}
2500
2501static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2502{
2503 if (refcount_sub_and_test(refs, &req->refs))
2504 io_free_req_deferred(req);
2505}
2506
Jens Axboe978db572019-11-14 22:39:04 -07002507static void io_double_put_req(struct io_kiocb *req)
2508{
2509 /* drop both submit and complete references */
2510 if (refcount_sub_and_test(2, &req->refs))
2511 io_free_req(req);
2512}
2513
Pavel Begunkov6c503152021-01-04 20:36:36 +00002514static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002515{
2516 /* See comment at the top of this file */
2517 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002518 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002519}
2520
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002521static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2522{
2523 struct io_rings *rings = ctx->rings;
2524
2525 /* make sure SQ entry isn't read before tail */
2526 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2527}
2528
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002529static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002530{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002531 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002532
Jens Axboebcda7ba2020-02-23 16:42:51 -07002533 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2534 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002535 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002536 kfree(kbuf);
2537 return cflags;
2538}
2539
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002540static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2541{
2542 struct io_buffer *kbuf;
2543
2544 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2545 return io_put_kbuf(req, kbuf);
2546}
2547
Jens Axboe4c6e2772020-07-01 11:29:10 -06002548static inline bool io_run_task_work(void)
2549{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002550 /*
2551 * Not safe to run on exiting task, and the task_work handling will
2552 * not add work to such a task.
2553 */
2554 if (unlikely(current->flags & PF_EXITING))
2555 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002556 if (current->task_works) {
2557 __set_current_state(TASK_RUNNING);
2558 task_work_run();
2559 return true;
2560 }
2561
2562 return false;
2563}
2564
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002565static void io_iopoll_queue(struct list_head *again)
2566{
2567 struct io_kiocb *req;
2568
2569 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002570 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2571 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002572 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002573 } while (!list_empty(again));
2574}
2575
Jens Axboedef596e2019-01-09 08:59:42 -07002576/*
2577 * Find and free completed poll iocbs
2578 */
2579static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2580 struct list_head *done)
2581{
Jens Axboe8237e042019-12-28 10:48:22 -07002582 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002583 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002584 LIST_HEAD(again);
2585
2586 /* order with ->result store in io_complete_rw_iopoll() */
2587 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002588
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002589 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002590 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002591 int cflags = 0;
2592
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002593 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002594 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002595 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002596 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002597 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002598 continue;
2599 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002600 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002601
Jens Axboebcda7ba2020-02-23 16:42:51 -07002602 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002603 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002604
2605 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002606 (*nr_events)++;
2607
Pavel Begunkovc3524382020-06-28 12:52:32 +03002608 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002609 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002610 }
Jens Axboedef596e2019-01-09 08:59:42 -07002611
Jens Axboe09bb8392019-03-13 12:39:28 -06002612 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002613 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002614 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002615
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002616 if (!list_empty(&again))
2617 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002618}
2619
Jens Axboedef596e2019-01-09 08:59:42 -07002620static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2621 long min)
2622{
2623 struct io_kiocb *req, *tmp;
2624 LIST_HEAD(done);
2625 bool spin;
2626 int ret;
2627
2628 /*
2629 * Only spin for completions if we don't have multiple devices hanging
2630 * off our complete list, and we're under the requested amount.
2631 */
2632 spin = !ctx->poll_multi_file && *nr_events < min;
2633
2634 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002635 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002636 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002637
2638 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002639 * Move completed and retryable entries to our local lists.
2640 * If we find a request that requires polling, break out
2641 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002642 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002643 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002644 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002645 continue;
2646 }
2647 if (!list_empty(&done))
2648 break;
2649
2650 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2651 if (ret < 0)
2652 break;
2653
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002654 /* iopoll may have completed current req */
2655 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002656 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002657
Jens Axboedef596e2019-01-09 08:59:42 -07002658 if (ret && spin)
2659 spin = false;
2660 ret = 0;
2661 }
2662
2663 if (!list_empty(&done))
2664 io_iopoll_complete(ctx, nr_events, &done);
2665
2666 return ret;
2667}
2668
2669/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002670 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002671 * non-spinning poll check - we'll still enter the driver poll loop, but only
2672 * as a non-spinning completion check.
2673 */
2674static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2675 long min)
2676{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002677 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002678 int ret;
2679
2680 ret = io_do_iopoll(ctx, nr_events, min);
2681 if (ret < 0)
2682 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002683 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002684 return 0;
2685 }
2686
2687 return 1;
2688}
2689
2690/*
2691 * We can't just wait for polled events to come to us, we have to actively
2692 * find and complete them.
2693 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002694static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002695{
2696 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2697 return;
2698
2699 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002700 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002701 unsigned int nr_events = 0;
2702
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002703 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002704
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002705 /* let it sleep and repeat later if can't complete a request */
2706 if (nr_events == 0)
2707 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002708 /*
2709 * Ensure we allow local-to-the-cpu processing to take place,
2710 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002711 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002712 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002713 if (need_resched()) {
2714 mutex_unlock(&ctx->uring_lock);
2715 cond_resched();
2716 mutex_lock(&ctx->uring_lock);
2717 }
Jens Axboedef596e2019-01-09 08:59:42 -07002718 }
2719 mutex_unlock(&ctx->uring_lock);
2720}
2721
Pavel Begunkov7668b922020-07-07 16:36:21 +03002722static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002723{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002724 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002725 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002726
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002727 /*
2728 * We disallow the app entering submit/complete with polling, but we
2729 * still need to lock the ring to prevent racing with polled issue
2730 * that got punted to a workqueue.
2731 */
2732 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002733 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002734 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002735 * Don't enter poll loop if we already have events pending.
2736 * If we do, we can potentially be spinning for commands that
2737 * already triggered a CQE (eg in error).
2738 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002739 if (test_bit(0, &ctx->cq_check_overflow))
2740 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2741 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002742 break;
2743
2744 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002745 * If a submit got punted to a workqueue, we can have the
2746 * application entering polling for a command before it gets
2747 * issued. That app will hold the uring_lock for the duration
2748 * of the poll right here, so we need to take a breather every
2749 * now and then to ensure that the issue has a chance to add
2750 * the poll to the issued list. Otherwise we can spin here
2751 * forever, while the workqueue is stuck trying to acquire the
2752 * very same mutex.
2753 */
2754 if (!(++iters & 7)) {
2755 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002756 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002757 mutex_lock(&ctx->uring_lock);
2758 }
2759
Pavel Begunkov7668b922020-07-07 16:36:21 +03002760 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002761 if (ret <= 0)
2762 break;
2763 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002764 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002765
Jens Axboe500f9fb2019-08-19 12:15:59 -06002766 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002767 return ret;
2768}
2769
Jens Axboe491381ce2019-10-17 09:20:46 -06002770static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002771{
Jens Axboe491381ce2019-10-17 09:20:46 -06002772 /*
2773 * Tell lockdep we inherited freeze protection from submission
2774 * thread.
2775 */
2776 if (req->flags & REQ_F_ISREG) {
2777 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002778
Jens Axboe491381ce2019-10-17 09:20:46 -06002779 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002780 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002781 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002782}
2783
Jens Axboea1d7c392020-06-22 11:09:46 -06002784static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002785 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002786{
Jens Axboe9adbd452019-12-20 08:45:55 -07002787 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002788 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002789
Jens Axboe491381ce2019-10-17 09:20:46 -06002790 if (kiocb->ki_flags & IOCB_WRITE)
2791 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002793 if (res != req->result)
2794 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002795 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002796 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002797 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002798}
2799
Jens Axboeb63534c2020-06-04 11:28:00 -06002800#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002801static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002802{
2803 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002804 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002805 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002806
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002807 /* already prepared */
2808 if (req->async_data)
2809 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002810
2811 switch (req->opcode) {
2812 case IORING_OP_READV:
2813 case IORING_OP_READ_FIXED:
2814 case IORING_OP_READ:
2815 rw = READ;
2816 break;
2817 case IORING_OP_WRITEV:
2818 case IORING_OP_WRITE_FIXED:
2819 case IORING_OP_WRITE:
2820 rw = WRITE;
2821 break;
2822 default:
2823 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2824 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002825 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002826 }
2827
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002828 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2829 if (ret < 0)
2830 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002831 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002832}
Jens Axboeb63534c2020-06-04 11:28:00 -06002833#endif
2834
2835static bool io_rw_reissue(struct io_kiocb *req, long res)
2836{
2837#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002838 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002839 int ret;
2840
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002841 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002842 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002843 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002844 if (!S_ISBLK(mode) && !S_ISREG(mode))
2845 return false;
2846 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002847 return false;
2848
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002849 lockdep_assert_held(&req->ctx->uring_lock);
2850
Jens Axboe28cea78a2020-09-14 10:51:17 -06002851 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002852
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002853 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002854 refcount_inc(&req->refs);
2855 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002856 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002857 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002858 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002859#endif
2860 return false;
2861}
2862
Jens Axboea1d7c392020-06-22 11:09:46 -06002863static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002864 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002865{
2866 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002867 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002868}
2869
2870static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2871{
Jens Axboe9adbd452019-12-20 08:45:55 -07002872 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002873
Pavel Begunkov889fca72021-02-10 00:03:09 +00002874 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002875}
2876
Jens Axboedef596e2019-01-09 08:59:42 -07002877static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2878{
Jens Axboe9adbd452019-12-20 08:45:55 -07002879 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002880
Jens Axboe491381ce2019-10-17 09:20:46 -06002881 if (kiocb->ki_flags & IOCB_WRITE)
2882 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002883
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002884 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002885 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002886
2887 WRITE_ONCE(req->result, res);
2888 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002889 smp_wmb();
2890 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002891}
2892
2893/*
2894 * After the iocb has been issued, it's safe to be found on the poll list.
2895 * Adding the kiocb to the list AFTER submission ensures that we don't
2896 * find it from a io_iopoll_getevents() thread before the issuer is done
2897 * accessing the kiocb cookie.
2898 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002899static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002900{
2901 struct io_ring_ctx *ctx = req->ctx;
2902
2903 /*
2904 * Track whether we have multiple files in our lists. This will impact
2905 * how we do polling eventually, not spinning if we're on potentially
2906 * different devices.
2907 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002908 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002909 ctx->poll_multi_file = false;
2910 } else if (!ctx->poll_multi_file) {
2911 struct io_kiocb *list_req;
2912
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002913 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002914 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002915 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002916 ctx->poll_multi_file = true;
2917 }
2918
2919 /*
2920 * For fast devices, IO may have already completed. If it has, add
2921 * it to the front so we find it first.
2922 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002923 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002924 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002925 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002926 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002927
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002928 /*
2929 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2930 * task context or in io worker task context. If current task context is
2931 * sq thread, we don't need to check whether should wake up sq thread.
2932 */
2933 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002934 wq_has_sleeper(&ctx->sq_data->wait))
2935 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002936}
2937
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002938static inline void io_state_file_put(struct io_submit_state *state)
2939{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002940 if (state->file_refs) {
2941 fput_many(state->file, state->file_refs);
2942 state->file_refs = 0;
2943 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002944}
2945
2946/*
2947 * Get as many references to a file as we have IOs left in this submission,
2948 * assuming most submissions are for one file, or at least that each file
2949 * has more than one submission.
2950 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002951static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002952{
2953 if (!state)
2954 return fget(fd);
2955
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002956 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002957 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002958 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002959 return state->file;
2960 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002961 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002962 }
2963 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002964 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002965 return NULL;
2966
2967 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002968 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002969 return state->file;
2970}
2971
Jens Axboe4503b762020-06-01 10:00:27 -06002972static bool io_bdev_nowait(struct block_device *bdev)
2973{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002974 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002975}
2976
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977/*
2978 * If we tracked the file through the SCM inflight mechanism, we could support
2979 * any file. For now, just ensure that anything potentially problematic is done
2980 * inline.
2981 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002982static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002983{
2984 umode_t mode = file_inode(file)->i_mode;
2985
Jens Axboe4503b762020-06-01 10:00:27 -06002986 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002987 if (IS_ENABLED(CONFIG_BLOCK) &&
2988 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002989 return true;
2990 return false;
2991 }
2992 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002994 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002995 if (IS_ENABLED(CONFIG_BLOCK) &&
2996 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002997 file->f_op != &io_uring_fops)
2998 return true;
2999 return false;
3000 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003001
Jens Axboec5b85622020-06-09 19:23:05 -06003002 /* any ->read/write should understand O_NONBLOCK */
3003 if (file->f_flags & O_NONBLOCK)
3004 return true;
3005
Jens Axboeaf197f52020-04-28 13:15:06 -06003006 if (!(file->f_mode & FMODE_NOWAIT))
3007 return false;
3008
3009 if (rw == READ)
3010 return file->f_op->read_iter != NULL;
3011
3012 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003013}
3014
Pavel Begunkova88fc402020-09-30 22:57:53 +03003015static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003016{
Jens Axboedef596e2019-01-09 08:59:42 -07003017 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07003018 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003019 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06003020 unsigned ioprio;
3021 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003022
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003023 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06003024 req->flags |= REQ_F_ISREG;
3025
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003027 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07003028 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003029 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07003030 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003031 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003032 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
3033 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3034 if (unlikely(ret))
3035 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003036
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003037 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
3038 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
3039 req->flags |= REQ_F_NOWAIT;
3040
Jens Axboe2b188cc2019-01-07 10:46:33 -07003041 ioprio = READ_ONCE(sqe->ioprio);
3042 if (ioprio) {
3043 ret = ioprio_check_cap(ioprio);
3044 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06003045 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003046
3047 kiocb->ki_ioprio = ioprio;
3048 } else
3049 kiocb->ki_ioprio = get_current_ioprio();
3050
Jens Axboedef596e2019-01-09 08:59:42 -07003051 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003052 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
3053 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003054 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003055
Jens Axboedef596e2019-01-09 08:59:42 -07003056 kiocb->ki_flags |= IOCB_HIPRI;
3057 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003058 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003059 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003060 if (kiocb->ki_flags & IOCB_HIPRI)
3061 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003062 kiocb->ki_complete = io_complete_rw;
3063 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003064
Jens Axboe3529d8c2019-12-19 18:24:38 -07003065 req->rw.addr = READ_ONCE(sqe->addr);
3066 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003067 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003068 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003069}
3070
3071static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3072{
3073 switch (ret) {
3074 case -EIOCBQUEUED:
3075 break;
3076 case -ERESTARTSYS:
3077 case -ERESTARTNOINTR:
3078 case -ERESTARTNOHAND:
3079 case -ERESTART_RESTARTBLOCK:
3080 /*
3081 * We can't just restart the syscall, since previously
3082 * submitted sqes may already be in progress. Just fail this
3083 * IO with EINTR.
3084 */
3085 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003086 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003087 default:
3088 kiocb->ki_complete(kiocb, ret, 0);
3089 }
3090}
3091
Jens Axboea1d7c392020-06-22 11:09:46 -06003092static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003093 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003094{
Jens Axboeba042912019-12-25 16:33:42 -07003095 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07003096 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003097
Jens Axboe227c0c92020-08-13 11:51:40 -06003098 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003099 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003100 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003101 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003102 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003103 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003104 }
3105
Jens Axboeba042912019-12-25 16:33:42 -07003106 if (req->flags & REQ_F_CUR_POS)
3107 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003108 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00003109 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003110 else
3111 io_rw_done(kiocb, ret);
3112}
3113
Pavel Begunkov847595d2021-02-04 13:52:06 +00003114static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003115{
Jens Axboe9adbd452019-12-20 08:45:55 -07003116 struct io_ring_ctx *ctx = req->ctx;
3117 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003118 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003119 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003120 size_t offset;
3121 u64 buf_addr;
3122
Jens Axboeedafcce2019-01-09 09:16:05 -07003123 if (unlikely(buf_index >= ctx->nr_user_bufs))
3124 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003125 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3126 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003127 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003128
3129 /* overflow */
3130 if (buf_addr + len < buf_addr)
3131 return -EFAULT;
3132 /* not inside the mapped region */
3133 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3134 return -EFAULT;
3135
3136 /*
3137 * May not be a start of buffer, set size appropriately
3138 * and advance us to the beginning.
3139 */
3140 offset = buf_addr - imu->ubuf;
3141 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003142
3143 if (offset) {
3144 /*
3145 * Don't use iov_iter_advance() here, as it's really slow for
3146 * using the latter parts of a big fixed buffer - it iterates
3147 * over each segment manually. We can cheat a bit here, because
3148 * we know that:
3149 *
3150 * 1) it's a BVEC iter, we set it up
3151 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3152 * first and last bvec
3153 *
3154 * So just find our index, and adjust the iterator afterwards.
3155 * If the offset is within the first bvec (or the whole first
3156 * bvec, just use iov_iter_advance(). This makes it easier
3157 * since we can just skip the first segment, which may not
3158 * be PAGE_SIZE aligned.
3159 */
3160 const struct bio_vec *bvec = imu->bvec;
3161
3162 if (offset <= bvec->bv_len) {
3163 iov_iter_advance(iter, offset);
3164 } else {
3165 unsigned long seg_skip;
3166
3167 /* skip first vec */
3168 offset -= bvec->bv_len;
3169 seg_skip = 1 + (offset >> PAGE_SHIFT);
3170
3171 iter->bvec = bvec + seg_skip;
3172 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003173 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003174 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003175 }
3176 }
3177
Pavel Begunkov847595d2021-02-04 13:52:06 +00003178 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003179}
3180
Jens Axboebcda7ba2020-02-23 16:42:51 -07003181static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3182{
3183 if (needs_lock)
3184 mutex_unlock(&ctx->uring_lock);
3185}
3186
3187static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3188{
3189 /*
3190 * "Normal" inline submissions always hold the uring_lock, since we
3191 * grab it from the system call. Same is true for the SQPOLL offload.
3192 * The only exception is when we've detached the request and issue it
3193 * from an async worker thread, grab the lock for that case.
3194 */
3195 if (needs_lock)
3196 mutex_lock(&ctx->uring_lock);
3197}
3198
3199static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3200 int bgid, struct io_buffer *kbuf,
3201 bool needs_lock)
3202{
3203 struct io_buffer *head;
3204
3205 if (req->flags & REQ_F_BUFFER_SELECTED)
3206 return kbuf;
3207
3208 io_ring_submit_lock(req->ctx, needs_lock);
3209
3210 lockdep_assert_held(&req->ctx->uring_lock);
3211
3212 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3213 if (head) {
3214 if (!list_empty(&head->list)) {
3215 kbuf = list_last_entry(&head->list, struct io_buffer,
3216 list);
3217 list_del(&kbuf->list);
3218 } else {
3219 kbuf = head;
3220 idr_remove(&req->ctx->io_buffer_idr, bgid);
3221 }
3222 if (*len > kbuf->len)
3223 *len = kbuf->len;
3224 } else {
3225 kbuf = ERR_PTR(-ENOBUFS);
3226 }
3227
3228 io_ring_submit_unlock(req->ctx, needs_lock);
3229
3230 return kbuf;
3231}
3232
Jens Axboe4d954c22020-02-27 07:31:19 -07003233static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3234 bool needs_lock)
3235{
3236 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003237 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003238
3239 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003240 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003241 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3242 if (IS_ERR(kbuf))
3243 return kbuf;
3244 req->rw.addr = (u64) (unsigned long) kbuf;
3245 req->flags |= REQ_F_BUFFER_SELECTED;
3246 return u64_to_user_ptr(kbuf->addr);
3247}
3248
3249#ifdef CONFIG_COMPAT
3250static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3251 bool needs_lock)
3252{
3253 struct compat_iovec __user *uiov;
3254 compat_ssize_t clen;
3255 void __user *buf;
3256 ssize_t len;
3257
3258 uiov = u64_to_user_ptr(req->rw.addr);
3259 if (!access_ok(uiov, sizeof(*uiov)))
3260 return -EFAULT;
3261 if (__get_user(clen, &uiov->iov_len))
3262 return -EFAULT;
3263 if (clen < 0)
3264 return -EINVAL;
3265
3266 len = clen;
3267 buf = io_rw_buffer_select(req, &len, needs_lock);
3268 if (IS_ERR(buf))
3269 return PTR_ERR(buf);
3270 iov[0].iov_base = buf;
3271 iov[0].iov_len = (compat_size_t) len;
3272 return 0;
3273}
3274#endif
3275
3276static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3277 bool needs_lock)
3278{
3279 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3280 void __user *buf;
3281 ssize_t len;
3282
3283 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3284 return -EFAULT;
3285
3286 len = iov[0].iov_len;
3287 if (len < 0)
3288 return -EINVAL;
3289 buf = io_rw_buffer_select(req, &len, needs_lock);
3290 if (IS_ERR(buf))
3291 return PTR_ERR(buf);
3292 iov[0].iov_base = buf;
3293 iov[0].iov_len = len;
3294 return 0;
3295}
3296
3297static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3298 bool needs_lock)
3299{
Jens Axboedddb3e22020-06-04 11:27:01 -06003300 if (req->flags & REQ_F_BUFFER_SELECTED) {
3301 struct io_buffer *kbuf;
3302
3303 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3304 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3305 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003306 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003307 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003308 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003309 return -EINVAL;
3310
3311#ifdef CONFIG_COMPAT
3312 if (req->ctx->compat)
3313 return io_compat_import(req, iov, needs_lock);
3314#endif
3315
3316 return __io_iov_buffer_select(req, iov, needs_lock);
3317}
3318
Pavel Begunkov847595d2021-02-04 13:52:06 +00003319static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3320 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003321{
Jens Axboe9adbd452019-12-20 08:45:55 -07003322 void __user *buf = u64_to_user_ptr(req->rw.addr);
3323 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003324 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003325 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003326
Pavel Begunkov7d009162019-11-25 23:14:40 +03003327 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003328 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003329 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003330 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003331
Jens Axboebcda7ba2020-02-23 16:42:51 -07003332 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003333 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003334 return -EINVAL;
3335
Jens Axboe3a6820f2019-12-22 15:19:35 -07003336 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003337 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003338 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003339 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003340 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003341 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003342 }
3343
Jens Axboe3a6820f2019-12-22 15:19:35 -07003344 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3345 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003346 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003347 }
3348
Jens Axboe4d954c22020-02-27 07:31:19 -07003349 if (req->flags & REQ_F_BUFFER_SELECT) {
3350 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003351 if (!ret)
3352 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003353 *iovec = NULL;
3354 return ret;
3355 }
3356
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003357 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3358 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003359}
3360
Jens Axboe0fef9482020-08-26 10:36:20 -06003361static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3362{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003363 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003364}
3365
Jens Axboe32960612019-09-23 11:05:34 -06003366/*
3367 * For files that don't have ->read_iter() and ->write_iter(), handle them
3368 * by looping over ->read() or ->write() manually.
3369 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003370static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003371{
Jens Axboe4017eb92020-10-22 14:14:12 -06003372 struct kiocb *kiocb = &req->rw.kiocb;
3373 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003374 ssize_t ret = 0;
3375
3376 /*
3377 * Don't support polled IO through this interface, and we can't
3378 * support non-blocking either. For the latter, this just causes
3379 * the kiocb to be handled from an async context.
3380 */
3381 if (kiocb->ki_flags & IOCB_HIPRI)
3382 return -EOPNOTSUPP;
3383 if (kiocb->ki_flags & IOCB_NOWAIT)
3384 return -EAGAIN;
3385
3386 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003387 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003388 ssize_t nr;
3389
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003390 if (!iov_iter_is_bvec(iter)) {
3391 iovec = iov_iter_iovec(iter);
3392 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003393 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3394 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003395 }
3396
Jens Axboe32960612019-09-23 11:05:34 -06003397 if (rw == READ) {
3398 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003399 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003400 } else {
3401 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003402 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003403 }
3404
3405 if (nr < 0) {
3406 if (!ret)
3407 ret = nr;
3408 break;
3409 }
3410 ret += nr;
3411 if (nr != iovec.iov_len)
3412 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003413 req->rw.len -= nr;
3414 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003415 iov_iter_advance(iter, nr);
3416 }
3417
3418 return ret;
3419}
3420
Jens Axboeff6165b2020-08-13 09:47:43 -06003421static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3422 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003423{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003424 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003425
Jens Axboeff6165b2020-08-13 09:47:43 -06003426 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003427 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003428 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003429 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003430 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003431 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003432 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003433 unsigned iov_off = 0;
3434
3435 rw->iter.iov = rw->fast_iov;
3436 if (iter->iov != fast_iov) {
3437 iov_off = iter->iov - fast_iov;
3438 rw->iter.iov += iov_off;
3439 }
3440 if (rw->fast_iov != fast_iov)
3441 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003442 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003443 } else {
3444 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003445 }
3446}
3447
Jens Axboee8c2bc12020-08-15 18:44:09 -07003448static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003449{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003450 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3451 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3452 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003453}
3454
Jens Axboee8c2bc12020-08-15 18:44:09 -07003455static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003456{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003457 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003458 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003459
Jens Axboee8c2bc12020-08-15 18:44:09 -07003460 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003461}
3462
Jens Axboeff6165b2020-08-13 09:47:43 -06003463static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3464 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003465 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003466{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003467 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003468 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003469 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003470 if (__io_alloc_async_data(req)) {
3471 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003472 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003473 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003474
Jens Axboeff6165b2020-08-13 09:47:43 -06003475 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003476 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003477 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003478}
3479
Pavel Begunkov73debe62020-09-30 22:57:54 +03003480static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003481{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003482 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003483 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003484 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003485
Pavel Begunkov2846c482020-11-07 13:16:27 +00003486 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003487 if (unlikely(ret < 0))
3488 return ret;
3489
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003490 iorw->bytes_done = 0;
3491 iorw->free_iovec = iov;
3492 if (iov)
3493 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003494 return 0;
3495}
3496
Pavel Begunkov73debe62020-09-30 22:57:54 +03003497static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003498{
3499 ssize_t ret;
3500
Pavel Begunkova88fc402020-09-30 22:57:53 +03003501 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003502 if (ret)
3503 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003504
Jens Axboe3529d8c2019-12-19 18:24:38 -07003505 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3506 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003507
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003508 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003509 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003510 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003511 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003512}
3513
Jens Axboec1dd91d2020-08-03 16:43:59 -06003514/*
3515 * This is our waitqueue callback handler, registered through lock_page_async()
3516 * when we initially tried to do the IO with the iocb armed our waitqueue.
3517 * This gets called when the page is unlocked, and we generally expect that to
3518 * happen when the page IO is completed and the page is now uptodate. This will
3519 * queue a task_work based retry of the operation, attempting to copy the data
3520 * again. If the latter fails because the page was NOT uptodate, then we will
3521 * do a thread based blocking retry of the operation. That's the unexpected
3522 * slow path.
3523 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003524static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3525 int sync, void *arg)
3526{
3527 struct wait_page_queue *wpq;
3528 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003529 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003530 int ret;
3531
3532 wpq = container_of(wait, struct wait_page_queue, wait);
3533
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003534 if (!wake_page_match(wpq, key))
3535 return 0;
3536
Hao Xuc8d317a2020-09-29 20:00:45 +08003537 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003538 list_del_init(&wait->entry);
3539
Jens Axboe7cbf1722021-02-10 00:03:20 +00003540 req->task_work.func = io_req_task_submit;
Jens Axboe6d816e02020-08-11 08:04:14 -06003541 percpu_ref_get(&req->ctx->refs);
3542
Jens Axboebcf5a062020-05-22 09:24:42 -06003543 /* submit ref gets dropped, acquire a new one */
3544 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003545 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003546 if (unlikely(ret))
3547 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003548 return 1;
3549}
3550
Jens Axboec1dd91d2020-08-03 16:43:59 -06003551/*
3552 * This controls whether a given IO request should be armed for async page
3553 * based retry. If we return false here, the request is handed to the async
3554 * worker threads for retry. If we're doing buffered reads on a regular file,
3555 * we prepare a private wait_page_queue entry and retry the operation. This
3556 * will either succeed because the page is now uptodate and unlocked, or it
3557 * will register a callback when the page is unlocked at IO completion. Through
3558 * that callback, io_uring uses task_work to setup a retry of the operation.
3559 * That retry will attempt the buffered read again. The retry will generally
3560 * succeed, or in rare cases where it fails, we then fall back to using the
3561 * async worker threads for a blocking retry.
3562 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003563static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003564{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003565 struct io_async_rw *rw = req->async_data;
3566 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003567 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003568
3569 /* never retry for NOWAIT, we just complete with -EAGAIN */
3570 if (req->flags & REQ_F_NOWAIT)
3571 return false;
3572
Jens Axboe227c0c92020-08-13 11:51:40 -06003573 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003574 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003575 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003576
Jens Axboebcf5a062020-05-22 09:24:42 -06003577 /*
3578 * just use poll if we can, and don't attempt if the fs doesn't
3579 * support callback based unlocks
3580 */
3581 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3582 return false;
3583
Jens Axboe3b2a4432020-08-16 10:58:43 -07003584 wait->wait.func = io_async_buf_func;
3585 wait->wait.private = req;
3586 wait->wait.flags = 0;
3587 INIT_LIST_HEAD(&wait->wait.entry);
3588 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003589 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003590 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003591 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003592}
3593
3594static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3595{
3596 if (req->file->f_op->read_iter)
3597 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003598 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003599 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003600 else
3601 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003602}
3603
Pavel Begunkov889fca72021-02-10 00:03:09 +00003604static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003605{
3606 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003607 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003608 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003609 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003610 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003611 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003612
Pavel Begunkov2846c482020-11-07 13:16:27 +00003613 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003614 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003615 iovec = NULL;
3616 } else {
3617 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3618 if (ret < 0)
3619 return ret;
3620 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003621 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003622 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003623
Jens Axboefd6c2e42019-12-18 12:19:41 -07003624 /* Ensure we clear previously set non-block flag */
3625 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003626 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003627 else
3628 kiocb->ki_flags |= IOCB_NOWAIT;
3629
Pavel Begunkov24c74672020-06-21 13:09:51 +03003630 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003631 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3632 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003633 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003634 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003635
Pavel Begunkov632546c2020-11-07 13:16:26 +00003636 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003637 if (unlikely(ret)) {
3638 kfree(iovec);
3639 return ret;
3640 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003641
Jens Axboe227c0c92020-08-13 11:51:40 -06003642 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003643
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003644 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003645 /* it's faster to check here then delegate to kfree */
3646 if (iovec)
3647 kfree(iovec);
3648 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003649 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003650 /* IOPOLL retry should happen for io-wq threads */
3651 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003652 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003653 /* no retry on NONBLOCK nor RWF_NOWAIT */
3654 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003655 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003656 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003657 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003658 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003659 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003660 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003661 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003662 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003663 }
3664
Jens Axboe227c0c92020-08-13 11:51:40 -06003665 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003666 if (ret2)
3667 return ret2;
3668
Jens Axboee8c2bc12020-08-15 18:44:09 -07003669 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003670 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003671 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003672
Pavel Begunkovb23df912021-02-04 13:52:04 +00003673 do {
3674 io_size -= ret;
3675 rw->bytes_done += ret;
3676 /* if we can retry, do so with the callbacks armed */
3677 if (!io_rw_should_retry(req)) {
3678 kiocb->ki_flags &= ~IOCB_WAITQ;
3679 return -EAGAIN;
3680 }
3681
3682 /*
3683 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3684 * we get -EIOCBQUEUED, then we'll get a notification when the
3685 * desired page gets unlocked. We can also get a partial read
3686 * here, and if we do, then just retry at the new offset.
3687 */
3688 ret = io_iter_do_read(req, iter);
3689 if (ret == -EIOCBQUEUED)
3690 return 0;
3691 /* we got some bytes, but not all. retry. */
3692 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003693done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003694 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003695 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003696}
3697
Pavel Begunkov73debe62020-09-30 22:57:54 +03003698static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003699{
3700 ssize_t ret;
3701
Pavel Begunkova88fc402020-09-30 22:57:53 +03003702 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003703 if (ret)
3704 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003705
Jens Axboe3529d8c2019-12-19 18:24:38 -07003706 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3707 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003708
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003709 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003710 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003711 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003712 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003713}
3714
Pavel Begunkov889fca72021-02-10 00:03:09 +00003715static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003716{
3717 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003718 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003719 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003720 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003721 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003722 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003723
Pavel Begunkov2846c482020-11-07 13:16:27 +00003724 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003725 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003726 iovec = NULL;
3727 } else {
3728 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3729 if (ret < 0)
3730 return ret;
3731 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003732 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003733 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003734
Jens Axboefd6c2e42019-12-18 12:19:41 -07003735 /* Ensure we clear previously set non-block flag */
3736 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003737 kiocb->ki_flags &= ~IOCB_NOWAIT;
3738 else
3739 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003740
Pavel Begunkov24c74672020-06-21 13:09:51 +03003741 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003742 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003743 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003744
Jens Axboe10d59342019-12-09 20:16:22 -07003745 /* file path doesn't support NOWAIT for non-direct_IO */
3746 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3747 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003748 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003749
Pavel Begunkov632546c2020-11-07 13:16:26 +00003750 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003751 if (unlikely(ret))
3752 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003753
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003754 /*
3755 * Open-code file_start_write here to grab freeze protection,
3756 * which will be released by another thread in
3757 * io_complete_rw(). Fool lockdep by telling it the lock got
3758 * released so that it doesn't complain about the held lock when
3759 * we return to userspace.
3760 */
3761 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003762 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003763 __sb_writers_release(file_inode(req->file)->i_sb,
3764 SB_FREEZE_WRITE);
3765 }
3766 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003767
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003768 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003769 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003770 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003771 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003772 else
3773 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003774
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003775 /*
3776 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3777 * retry them without IOCB_NOWAIT.
3778 */
3779 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3780 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003781 /* no retry on NONBLOCK nor RWF_NOWAIT */
3782 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003783 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003784 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003785 /* IOPOLL retry should happen for io-wq threads */
3786 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3787 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003788done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003789 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003790 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003791copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003792 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003793 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003794 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003795 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003796 }
Jens Axboe31b51512019-01-18 22:56:34 -07003797out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003798 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003799 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003800 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003801 return ret;
3802}
3803
Jens Axboe80a261f2020-09-28 14:23:58 -06003804static int io_renameat_prep(struct io_kiocb *req,
3805 const struct io_uring_sqe *sqe)
3806{
3807 struct io_rename *ren = &req->rename;
3808 const char __user *oldf, *newf;
3809
3810 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3811 return -EBADF;
3812
3813 ren->old_dfd = READ_ONCE(sqe->fd);
3814 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3815 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3816 ren->new_dfd = READ_ONCE(sqe->len);
3817 ren->flags = READ_ONCE(sqe->rename_flags);
3818
3819 ren->oldpath = getname(oldf);
3820 if (IS_ERR(ren->oldpath))
3821 return PTR_ERR(ren->oldpath);
3822
3823 ren->newpath = getname(newf);
3824 if (IS_ERR(ren->newpath)) {
3825 putname(ren->oldpath);
3826 return PTR_ERR(ren->newpath);
3827 }
3828
3829 req->flags |= REQ_F_NEED_CLEANUP;
3830 return 0;
3831}
3832
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003833static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003834{
3835 struct io_rename *ren = &req->rename;
3836 int ret;
3837
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003838 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003839 return -EAGAIN;
3840
3841 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3842 ren->newpath, ren->flags);
3843
3844 req->flags &= ~REQ_F_NEED_CLEANUP;
3845 if (ret < 0)
3846 req_set_fail_links(req);
3847 io_req_complete(req, ret);
3848 return 0;
3849}
3850
Jens Axboe14a11432020-09-28 14:27:37 -06003851static int io_unlinkat_prep(struct io_kiocb *req,
3852 const struct io_uring_sqe *sqe)
3853{
3854 struct io_unlink *un = &req->unlink;
3855 const char __user *fname;
3856
3857 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3858 return -EBADF;
3859
3860 un->dfd = READ_ONCE(sqe->fd);
3861
3862 un->flags = READ_ONCE(sqe->unlink_flags);
3863 if (un->flags & ~AT_REMOVEDIR)
3864 return -EINVAL;
3865
3866 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3867 un->filename = getname(fname);
3868 if (IS_ERR(un->filename))
3869 return PTR_ERR(un->filename);
3870
3871 req->flags |= REQ_F_NEED_CLEANUP;
3872 return 0;
3873}
3874
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003875static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003876{
3877 struct io_unlink *un = &req->unlink;
3878 int ret;
3879
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003880 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003881 return -EAGAIN;
3882
3883 if (un->flags & AT_REMOVEDIR)
3884 ret = do_rmdir(un->dfd, un->filename);
3885 else
3886 ret = do_unlinkat(un->dfd, un->filename);
3887
3888 req->flags &= ~REQ_F_NEED_CLEANUP;
3889 if (ret < 0)
3890 req_set_fail_links(req);
3891 io_req_complete(req, ret);
3892 return 0;
3893}
3894
Jens Axboe36f4fa62020-09-05 11:14:22 -06003895static int io_shutdown_prep(struct io_kiocb *req,
3896 const struct io_uring_sqe *sqe)
3897{
3898#if defined(CONFIG_NET)
3899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3900 return -EINVAL;
3901 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3902 sqe->buf_index)
3903 return -EINVAL;
3904
3905 req->shutdown.how = READ_ONCE(sqe->len);
3906 return 0;
3907#else
3908 return -EOPNOTSUPP;
3909#endif
3910}
3911
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003912static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003913{
3914#if defined(CONFIG_NET)
3915 struct socket *sock;
3916 int ret;
3917
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003918 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003919 return -EAGAIN;
3920
Linus Torvalds48aba792020-12-16 12:44:05 -08003921 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003922 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003923 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003924
3925 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003926 if (ret < 0)
3927 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003928 io_req_complete(req, ret);
3929 return 0;
3930#else
3931 return -EOPNOTSUPP;
3932#endif
3933}
3934
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003935static int __io_splice_prep(struct io_kiocb *req,
3936 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003937{
3938 struct io_splice* sp = &req->splice;
3939 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003940
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003941 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3942 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003943
3944 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003945 sp->len = READ_ONCE(sqe->len);
3946 sp->flags = READ_ONCE(sqe->splice_flags);
3947
3948 if (unlikely(sp->flags & ~valid_flags))
3949 return -EINVAL;
3950
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003951 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3952 (sp->flags & SPLICE_F_FD_IN_FIXED));
3953 if (!sp->file_in)
3954 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003955 req->flags |= REQ_F_NEED_CLEANUP;
3956
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003957 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3958 /*
3959 * Splice operation will be punted aync, and here need to
3960 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3961 */
3962 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003963 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003964 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003965
3966 return 0;
3967}
3968
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003969static int io_tee_prep(struct io_kiocb *req,
3970 const struct io_uring_sqe *sqe)
3971{
3972 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3973 return -EINVAL;
3974 return __io_splice_prep(req, sqe);
3975}
3976
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003977static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003978{
3979 struct io_splice *sp = &req->splice;
3980 struct file *in = sp->file_in;
3981 struct file *out = sp->file_out;
3982 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3983 long ret = 0;
3984
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003985 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003986 return -EAGAIN;
3987 if (sp->len)
3988 ret = do_tee(in, out, sp->len, flags);
3989
3990 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3991 req->flags &= ~REQ_F_NEED_CLEANUP;
3992
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003993 if (ret != sp->len)
3994 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003995 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003996 return 0;
3997}
3998
3999static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4000{
4001 struct io_splice* sp = &req->splice;
4002
4003 sp->off_in = READ_ONCE(sqe->splice_off_in);
4004 sp->off_out = READ_ONCE(sqe->off);
4005 return __io_splice_prep(req, sqe);
4006}
4007
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004008static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004009{
4010 struct io_splice *sp = &req->splice;
4011 struct file *in = sp->file_in;
4012 struct file *out = sp->file_out;
4013 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4014 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004015 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004016
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004017 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004018 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004019
4020 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4021 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004022
Jens Axboe948a7742020-05-17 14:21:38 -06004023 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004024 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004025
4026 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
4027 req->flags &= ~REQ_F_NEED_CLEANUP;
4028
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004029 if (ret != sp->len)
4030 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004031 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004032 return 0;
4033}
4034
Jens Axboe2b188cc2019-01-07 10:46:33 -07004035/*
4036 * IORING_OP_NOP just posts a completion event, nothing else.
4037 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004038static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004039{
4040 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004041
Jens Axboedef596e2019-01-09 08:59:42 -07004042 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4043 return -EINVAL;
4044
Pavel Begunkov889fca72021-02-10 00:03:09 +00004045 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004046 return 0;
4047}
4048
Jens Axboe3529d8c2019-12-19 18:24:38 -07004049static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004050{
Jens Axboe6b063142019-01-10 22:13:58 -07004051 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004052
Jens Axboe09bb8392019-03-13 12:39:28 -06004053 if (!req->file)
4054 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004055
Jens Axboe6b063142019-01-10 22:13:58 -07004056 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004057 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07004058 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004059 return -EINVAL;
4060
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004061 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4062 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4063 return -EINVAL;
4064
4065 req->sync.off = READ_ONCE(sqe->off);
4066 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004067 return 0;
4068}
4069
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004070static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004071{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004072 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004073 int ret;
4074
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004075 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004076 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004077 return -EAGAIN;
4078
Jens Axboe9adbd452019-12-20 08:45:55 -07004079 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004080 end > 0 ? end : LLONG_MAX,
4081 req->sync.flags & IORING_FSYNC_DATASYNC);
4082 if (ret < 0)
4083 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004084 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004085 return 0;
4086}
4087
Jens Axboed63d1b52019-12-10 10:38:56 -07004088static int io_fallocate_prep(struct io_kiocb *req,
4089 const struct io_uring_sqe *sqe)
4090{
4091 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4092 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004093 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4094 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004095
4096 req->sync.off = READ_ONCE(sqe->off);
4097 req->sync.len = READ_ONCE(sqe->addr);
4098 req->sync.mode = READ_ONCE(sqe->len);
4099 return 0;
4100}
4101
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004102static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004103{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004104 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004105
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004106 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004107 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004108 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004109 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4110 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004111 if (ret < 0)
4112 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004113 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004114 return 0;
4115}
4116
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004117static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004118{
Jens Axboef8748882020-01-08 17:47:02 -07004119 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004120 int ret;
4121
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004122 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004123 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004124 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004125 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004126
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004127 /* open.how should be already initialised */
4128 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004129 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004130
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004131 req->open.dfd = READ_ONCE(sqe->fd);
4132 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004133 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004134 if (IS_ERR(req->open.filename)) {
4135 ret = PTR_ERR(req->open.filename);
4136 req->open.filename = NULL;
4137 return ret;
4138 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004139 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004140 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004141 return 0;
4142}
4143
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004144static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4145{
4146 u64 flags, mode;
4147
Jens Axboe14587a462020-09-05 11:36:08 -06004148 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004149 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004150 mode = READ_ONCE(sqe->len);
4151 flags = READ_ONCE(sqe->open_flags);
4152 req->open.how = build_open_how(flags, mode);
4153 return __io_openat_prep(req, sqe);
4154}
4155
Jens Axboecebdb982020-01-08 17:59:24 -07004156static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4157{
4158 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004159 size_t len;
4160 int ret;
4161
Jens Axboe14587a462020-09-05 11:36:08 -06004162 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004163 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004164 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4165 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004166 if (len < OPEN_HOW_SIZE_VER0)
4167 return -EINVAL;
4168
4169 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4170 len);
4171 if (ret)
4172 return ret;
4173
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004174 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004175}
4176
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004177static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004178{
4179 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004180 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004181 bool nonblock_set;
4182 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004183 int ret;
4184
Jens Axboecebdb982020-01-08 17:59:24 -07004185 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004186 if (ret)
4187 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004188 nonblock_set = op.open_flag & O_NONBLOCK;
4189 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004190 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004191 /*
4192 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4193 * it'll always -EAGAIN
4194 */
4195 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4196 return -EAGAIN;
4197 op.lookup_flags |= LOOKUP_CACHED;
4198 op.open_flag |= O_NONBLOCK;
4199 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004200
Jens Axboe4022e7a2020-03-19 19:23:18 -06004201 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004202 if (ret < 0)
4203 goto err;
4204
4205 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004206 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004207 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4208 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004209 /*
4210 * We could hang on to this 'fd', but seems like marginal
4211 * gain for something that is now known to be a slower path.
4212 * So just put it, and we'll get a new one when we retry.
4213 */
4214 put_unused_fd(ret);
4215 return -EAGAIN;
4216 }
4217
Jens Axboe15b71ab2019-12-11 11:20:36 -07004218 if (IS_ERR(file)) {
4219 put_unused_fd(ret);
4220 ret = PTR_ERR(file);
4221 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004222 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004223 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004224 fsnotify_open(file);
4225 fd_install(ret, file);
4226 }
4227err:
4228 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004229 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004230 if (ret < 0)
4231 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004232 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004233 return 0;
4234}
4235
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004236static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004237{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004238 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004239}
4240
Jens Axboe067524e2020-03-02 16:32:28 -07004241static int io_remove_buffers_prep(struct io_kiocb *req,
4242 const struct io_uring_sqe *sqe)
4243{
4244 struct io_provide_buf *p = &req->pbuf;
4245 u64 tmp;
4246
4247 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4248 return -EINVAL;
4249
4250 tmp = READ_ONCE(sqe->fd);
4251 if (!tmp || tmp > USHRT_MAX)
4252 return -EINVAL;
4253
4254 memset(p, 0, sizeof(*p));
4255 p->nbufs = tmp;
4256 p->bgid = READ_ONCE(sqe->buf_group);
4257 return 0;
4258}
4259
4260static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4261 int bgid, unsigned nbufs)
4262{
4263 unsigned i = 0;
4264
4265 /* shouldn't happen */
4266 if (!nbufs)
4267 return 0;
4268
4269 /* the head kbuf is the list itself */
4270 while (!list_empty(&buf->list)) {
4271 struct io_buffer *nxt;
4272
4273 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4274 list_del(&nxt->list);
4275 kfree(nxt);
4276 if (++i == nbufs)
4277 return i;
4278 }
4279 i++;
4280 kfree(buf);
4281 idr_remove(&ctx->io_buffer_idr, bgid);
4282
4283 return i;
4284}
4285
Pavel Begunkov889fca72021-02-10 00:03:09 +00004286static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004287{
4288 struct io_provide_buf *p = &req->pbuf;
4289 struct io_ring_ctx *ctx = req->ctx;
4290 struct io_buffer *head;
4291 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004292 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004293
4294 io_ring_submit_lock(ctx, !force_nonblock);
4295
4296 lockdep_assert_held(&ctx->uring_lock);
4297
4298 ret = -ENOENT;
4299 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4300 if (head)
4301 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004302 if (ret < 0)
4303 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004304
4305 /* need to hold the lock to complete IOPOLL requests */
4306 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004307 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004308 io_ring_submit_unlock(ctx, !force_nonblock);
4309 } else {
4310 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004311 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004312 }
Jens Axboe067524e2020-03-02 16:32:28 -07004313 return 0;
4314}
4315
Jens Axboeddf0322d2020-02-23 16:41:33 -07004316static int io_provide_buffers_prep(struct io_kiocb *req,
4317 const struct io_uring_sqe *sqe)
4318{
4319 struct io_provide_buf *p = &req->pbuf;
4320 u64 tmp;
4321
4322 if (sqe->ioprio || sqe->rw_flags)
4323 return -EINVAL;
4324
4325 tmp = READ_ONCE(sqe->fd);
4326 if (!tmp || tmp > USHRT_MAX)
4327 return -E2BIG;
4328 p->nbufs = tmp;
4329 p->addr = READ_ONCE(sqe->addr);
4330 p->len = READ_ONCE(sqe->len);
4331
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004332 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004333 return -EFAULT;
4334
4335 p->bgid = READ_ONCE(sqe->buf_group);
4336 tmp = READ_ONCE(sqe->off);
4337 if (tmp > USHRT_MAX)
4338 return -E2BIG;
4339 p->bid = tmp;
4340 return 0;
4341}
4342
4343static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4344{
4345 struct io_buffer *buf;
4346 u64 addr = pbuf->addr;
4347 int i, bid = pbuf->bid;
4348
4349 for (i = 0; i < pbuf->nbufs; i++) {
4350 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4351 if (!buf)
4352 break;
4353
4354 buf->addr = addr;
4355 buf->len = pbuf->len;
4356 buf->bid = bid;
4357 addr += pbuf->len;
4358 bid++;
4359 if (!*head) {
4360 INIT_LIST_HEAD(&buf->list);
4361 *head = buf;
4362 } else {
4363 list_add_tail(&buf->list, &(*head)->list);
4364 }
4365 }
4366
4367 return i ? i : -ENOMEM;
4368}
4369
Pavel Begunkov889fca72021-02-10 00:03:09 +00004370static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004371{
4372 struct io_provide_buf *p = &req->pbuf;
4373 struct io_ring_ctx *ctx = req->ctx;
4374 struct io_buffer *head, *list;
4375 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004376 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004377
4378 io_ring_submit_lock(ctx, !force_nonblock);
4379
4380 lockdep_assert_held(&ctx->uring_lock);
4381
4382 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4383
4384 ret = io_add_buffers(p, &head);
4385 if (ret < 0)
4386 goto out;
4387
4388 if (!list) {
4389 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4390 GFP_KERNEL);
4391 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004392 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004393 goto out;
4394 }
4395 }
4396out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004397 if (ret < 0)
4398 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004399
4400 /* need to hold the lock to complete IOPOLL requests */
4401 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004402 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004403 io_ring_submit_unlock(ctx, !force_nonblock);
4404 } else {
4405 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004406 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004407 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004408 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004409}
4410
Jens Axboe3e4827b2020-01-08 15:18:09 -07004411static int io_epoll_ctl_prep(struct io_kiocb *req,
4412 const struct io_uring_sqe *sqe)
4413{
4414#if defined(CONFIG_EPOLL)
4415 if (sqe->ioprio || sqe->buf_index)
4416 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004417 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004418 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004419
4420 req->epoll.epfd = READ_ONCE(sqe->fd);
4421 req->epoll.op = READ_ONCE(sqe->len);
4422 req->epoll.fd = READ_ONCE(sqe->off);
4423
4424 if (ep_op_has_event(req->epoll.op)) {
4425 struct epoll_event __user *ev;
4426
4427 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4428 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4429 return -EFAULT;
4430 }
4431
4432 return 0;
4433#else
4434 return -EOPNOTSUPP;
4435#endif
4436}
4437
Pavel Begunkov889fca72021-02-10 00:03:09 +00004438static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004439{
4440#if defined(CONFIG_EPOLL)
4441 struct io_epoll *ie = &req->epoll;
4442 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004443 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004444
4445 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4446 if (force_nonblock && ret == -EAGAIN)
4447 return -EAGAIN;
4448
4449 if (ret < 0)
4450 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004451 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004452 return 0;
4453#else
4454 return -EOPNOTSUPP;
4455#endif
4456}
4457
Jens Axboec1ca7572019-12-25 22:18:28 -07004458static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4459{
4460#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4461 if (sqe->ioprio || sqe->buf_index || sqe->off)
4462 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004463 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4464 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004465
4466 req->madvise.addr = READ_ONCE(sqe->addr);
4467 req->madvise.len = READ_ONCE(sqe->len);
4468 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4469 return 0;
4470#else
4471 return -EOPNOTSUPP;
4472#endif
4473}
4474
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004475static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004476{
4477#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4478 struct io_madvise *ma = &req->madvise;
4479 int ret;
4480
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004481 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004482 return -EAGAIN;
4483
Minchan Kim0726b012020-10-17 16:14:50 -07004484 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004485 if (ret < 0)
4486 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004487 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004488 return 0;
4489#else
4490 return -EOPNOTSUPP;
4491#endif
4492}
4493
Jens Axboe4840e412019-12-25 22:03:45 -07004494static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4495{
4496 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4497 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004498 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4499 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004500
4501 req->fadvise.offset = READ_ONCE(sqe->off);
4502 req->fadvise.len = READ_ONCE(sqe->len);
4503 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4504 return 0;
4505}
4506
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004507static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004508{
4509 struct io_fadvise *fa = &req->fadvise;
4510 int ret;
4511
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004512 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004513 switch (fa->advice) {
4514 case POSIX_FADV_NORMAL:
4515 case POSIX_FADV_RANDOM:
4516 case POSIX_FADV_SEQUENTIAL:
4517 break;
4518 default:
4519 return -EAGAIN;
4520 }
4521 }
Jens Axboe4840e412019-12-25 22:03:45 -07004522
4523 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4524 if (ret < 0)
4525 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004526 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004527 return 0;
4528}
4529
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004530static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4531{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004532 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004533 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004534 if (sqe->ioprio || sqe->buf_index)
4535 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004536 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004537 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004538
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004539 req->statx.dfd = READ_ONCE(sqe->fd);
4540 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004541 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004542 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4543 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004544
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004545 return 0;
4546}
4547
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004548static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004549{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004550 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004551 int ret;
4552
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004553 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004554 /* only need file table for an actual valid fd */
4555 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4556 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004557 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004558 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004559
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004560 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4561 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004562
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004563 if (ret < 0)
4564 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004565 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004566 return 0;
4567}
4568
Jens Axboeb5dba592019-12-11 14:02:38 -07004569static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4570{
Jens Axboe14587a462020-09-05 11:36:08 -06004571 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004572 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004573 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4574 sqe->rw_flags || sqe->buf_index)
4575 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004576 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004577 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004578
4579 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004580 return 0;
4581}
4582
Pavel Begunkov889fca72021-02-10 00:03:09 +00004583static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004584{
Jens Axboe9eac1902021-01-19 15:50:37 -07004585 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004586 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004587 struct fdtable *fdt;
4588 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004589 int ret;
4590
Jens Axboe9eac1902021-01-19 15:50:37 -07004591 file = NULL;
4592 ret = -EBADF;
4593 spin_lock(&files->file_lock);
4594 fdt = files_fdtable(files);
4595 if (close->fd >= fdt->max_fds) {
4596 spin_unlock(&files->file_lock);
4597 goto err;
4598 }
4599 file = fdt->fd[close->fd];
4600 if (!file) {
4601 spin_unlock(&files->file_lock);
4602 goto err;
4603 }
4604
4605 if (file->f_op == &io_uring_fops) {
4606 spin_unlock(&files->file_lock);
4607 file = NULL;
4608 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004609 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004610
4611 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004612 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004613 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004614 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004615 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004616
Jens Axboe9eac1902021-01-19 15:50:37 -07004617 ret = __close_fd_get_file(close->fd, &file);
4618 spin_unlock(&files->file_lock);
4619 if (ret < 0) {
4620 if (ret == -ENOENT)
4621 ret = -EBADF;
4622 goto err;
4623 }
4624
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004625 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004626 ret = filp_close(file, current->files);
4627err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004628 if (ret < 0)
4629 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004630 if (file)
4631 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004632 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004633 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004634}
4635
Jens Axboe3529d8c2019-12-19 18:24:38 -07004636static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004637{
4638 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004639
4640 if (!req->file)
4641 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004642
4643 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4644 return -EINVAL;
4645 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4646 return -EINVAL;
4647
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004648 req->sync.off = READ_ONCE(sqe->off);
4649 req->sync.len = READ_ONCE(sqe->len);
4650 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004651 return 0;
4652}
4653
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004654static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004655{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004656 int ret;
4657
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004658 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004659 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004660 return -EAGAIN;
4661
Jens Axboe9adbd452019-12-20 08:45:55 -07004662 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004663 req->sync.flags);
4664 if (ret < 0)
4665 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004666 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004667 return 0;
4668}
4669
YueHaibing469956e2020-03-04 15:53:52 +08004670#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004671static int io_setup_async_msg(struct io_kiocb *req,
4672 struct io_async_msghdr *kmsg)
4673{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004674 struct io_async_msghdr *async_msg = req->async_data;
4675
4676 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004677 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004678 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004679 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004680 return -ENOMEM;
4681 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004682 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004683 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004684 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004685 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004686 /* if were using fast_iov, set it to the new one */
4687 if (!async_msg->free_iov)
4688 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4689
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004690 return -EAGAIN;
4691}
4692
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004693static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4694 struct io_async_msghdr *iomsg)
4695{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004696 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004697 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004698 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004699 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004700}
4701
Jens Axboe3529d8c2019-12-19 18:24:38 -07004702static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004703{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004704 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004705 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004706 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004707
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004708 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4709 return -EINVAL;
4710
Jens Axboee47293f2019-12-20 08:58:21 -07004711 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004712 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004713 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004714
Jens Axboed8768362020-02-27 14:17:49 -07004715#ifdef CONFIG_COMPAT
4716 if (req->ctx->compat)
4717 sr->msg_flags |= MSG_CMSG_COMPAT;
4718#endif
4719
Jens Axboee8c2bc12020-08-15 18:44:09 -07004720 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004721 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004722 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004723 if (!ret)
4724 req->flags |= REQ_F_NEED_CLEANUP;
4725 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004726}
4727
Pavel Begunkov889fca72021-02-10 00:03:09 +00004728static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004729{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004730 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004731 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004732 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004733 int ret;
4734
Florent Revestdba4a922020-12-04 12:36:04 +01004735 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004736 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004737 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004738
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004739 kmsg = req->async_data;
4740 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004741 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004742 if (ret)
4743 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004744 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004745 }
4746
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004747 flags = req->sr_msg.msg_flags;
4748 if (flags & MSG_DONTWAIT)
4749 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004750 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004751 flags |= MSG_DONTWAIT;
4752
4753 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004754 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004755 return io_setup_async_msg(req, kmsg);
4756 if (ret == -ERESTARTSYS)
4757 ret = -EINTR;
4758
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004759 /* fast path, check for non-NULL to avoid function call */
4760 if (kmsg->free_iov)
4761 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004762 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004763 if (ret < 0)
4764 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004765 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004766 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004767}
4768
Pavel Begunkov889fca72021-02-10 00:03:09 +00004769static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004770{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004771 struct io_sr_msg *sr = &req->sr_msg;
4772 struct msghdr msg;
4773 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004774 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004775 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004776 int ret;
4777
Florent Revestdba4a922020-12-04 12:36:04 +01004778 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004779 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004780 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004781
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004782 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4783 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004784 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004785
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004786 msg.msg_name = NULL;
4787 msg.msg_control = NULL;
4788 msg.msg_controllen = 0;
4789 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004790
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004791 flags = req->sr_msg.msg_flags;
4792 if (flags & MSG_DONTWAIT)
4793 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004794 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004795 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004796
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004797 msg.msg_flags = flags;
4798 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004799 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004800 return -EAGAIN;
4801 if (ret == -ERESTARTSYS)
4802 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004803
Jens Axboe03b12302019-12-02 18:50:25 -07004804 if (ret < 0)
4805 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004806 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004807 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004808}
4809
Pavel Begunkov1400e692020-07-12 20:41:05 +03004810static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4811 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004812{
4813 struct io_sr_msg *sr = &req->sr_msg;
4814 struct iovec __user *uiov;
4815 size_t iov_len;
4816 int ret;
4817
Pavel Begunkov1400e692020-07-12 20:41:05 +03004818 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4819 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004820 if (ret)
4821 return ret;
4822
4823 if (req->flags & REQ_F_BUFFER_SELECT) {
4824 if (iov_len > 1)
4825 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004826 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004827 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004828 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004829 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004830 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004831 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004832 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004833 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004834 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004835 if (ret > 0)
4836 ret = 0;
4837 }
4838
4839 return ret;
4840}
4841
4842#ifdef CONFIG_COMPAT
4843static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004844 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004845{
4846 struct compat_msghdr __user *msg_compat;
4847 struct io_sr_msg *sr = &req->sr_msg;
4848 struct compat_iovec __user *uiov;
4849 compat_uptr_t ptr;
4850 compat_size_t len;
4851 int ret;
4852
Pavel Begunkov270a5942020-07-12 20:41:04 +03004853 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004854 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004855 &ptr, &len);
4856 if (ret)
4857 return ret;
4858
4859 uiov = compat_ptr(ptr);
4860 if (req->flags & REQ_F_BUFFER_SELECT) {
4861 compat_ssize_t clen;
4862
4863 if (len > 1)
4864 return -EINVAL;
4865 if (!access_ok(uiov, sizeof(*uiov)))
4866 return -EFAULT;
4867 if (__get_user(clen, &uiov->iov_len))
4868 return -EFAULT;
4869 if (clen < 0)
4870 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004871 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004872 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004873 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004874 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004875 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004876 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004877 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004878 if (ret < 0)
4879 return ret;
4880 }
4881
4882 return 0;
4883}
Jens Axboe03b12302019-12-02 18:50:25 -07004884#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004885
Pavel Begunkov1400e692020-07-12 20:41:05 +03004886static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4887 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004888{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004889 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004890
4891#ifdef CONFIG_COMPAT
4892 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004893 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004894#endif
4895
Pavel Begunkov1400e692020-07-12 20:41:05 +03004896 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004897}
4898
Jens Axboebcda7ba2020-02-23 16:42:51 -07004899static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004900 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004901{
4902 struct io_sr_msg *sr = &req->sr_msg;
4903 struct io_buffer *kbuf;
4904
Jens Axboebcda7ba2020-02-23 16:42:51 -07004905 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4906 if (IS_ERR(kbuf))
4907 return kbuf;
4908
4909 sr->kbuf = kbuf;
4910 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004911 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004912}
4913
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004914static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4915{
4916 return io_put_kbuf(req, req->sr_msg.kbuf);
4917}
4918
Jens Axboe3529d8c2019-12-19 18:24:38 -07004919static int io_recvmsg_prep(struct io_kiocb *req,
4920 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004921{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004922 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004923 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004924 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004925
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004926 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4927 return -EINVAL;
4928
Jens Axboe3529d8c2019-12-19 18:24:38 -07004929 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004930 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004931 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004932 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004933
Jens Axboed8768362020-02-27 14:17:49 -07004934#ifdef CONFIG_COMPAT
4935 if (req->ctx->compat)
4936 sr->msg_flags |= MSG_CMSG_COMPAT;
4937#endif
4938
Jens Axboee8c2bc12020-08-15 18:44:09 -07004939 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004940 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004941 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004942 if (!ret)
4943 req->flags |= REQ_F_NEED_CLEANUP;
4944 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004945}
4946
Pavel Begunkov889fca72021-02-10 00:03:09 +00004947static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004948{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004949 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004950 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004951 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004952 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004953 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004954 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004955
Florent Revestdba4a922020-12-04 12:36:04 +01004956 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004957 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004958 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004959
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004960 kmsg = req->async_data;
4961 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004962 ret = io_recvmsg_copy_hdr(req, &iomsg);
4963 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004964 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004965 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004966 }
4967
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004968 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004969 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004970 if (IS_ERR(kbuf))
4971 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004972 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004973 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4974 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004975 1, req->sr_msg.len);
4976 }
4977
4978 flags = req->sr_msg.msg_flags;
4979 if (flags & MSG_DONTWAIT)
4980 req->flags |= REQ_F_NOWAIT;
4981 else if (force_nonblock)
4982 flags |= MSG_DONTWAIT;
4983
4984 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4985 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004986 if (force_nonblock && ret == -EAGAIN)
4987 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004988 if (ret == -ERESTARTSYS)
4989 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004990
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004991 if (req->flags & REQ_F_BUFFER_SELECTED)
4992 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004993 /* fast path, check for non-NULL to avoid function call */
4994 if (kmsg->free_iov)
4995 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004996 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004997 if (ret < 0)
4998 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004999 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005000 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005001}
5002
Pavel Begunkov889fca72021-02-10 00:03:09 +00005003static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005004{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005005 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005006 struct io_sr_msg *sr = &req->sr_msg;
5007 struct msghdr msg;
5008 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005009 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005010 struct iovec iov;
5011 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005012 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005013 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005014
Florent Revestdba4a922020-12-04 12:36:04 +01005015 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005016 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005017 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005018
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005019 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005020 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005021 if (IS_ERR(kbuf))
5022 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005023 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005024 }
5025
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005026 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005027 if (unlikely(ret))
5028 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005029
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005030 msg.msg_name = NULL;
5031 msg.msg_control = NULL;
5032 msg.msg_controllen = 0;
5033 msg.msg_namelen = 0;
5034 msg.msg_iocb = NULL;
5035 msg.msg_flags = 0;
5036
5037 flags = req->sr_msg.msg_flags;
5038 if (flags & MSG_DONTWAIT)
5039 req->flags |= REQ_F_NOWAIT;
5040 else if (force_nonblock)
5041 flags |= MSG_DONTWAIT;
5042
5043 ret = sock_recvmsg(sock, &msg, flags);
5044 if (force_nonblock && ret == -EAGAIN)
5045 return -EAGAIN;
5046 if (ret == -ERESTARTSYS)
5047 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005048out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005049 if (req->flags & REQ_F_BUFFER_SELECTED)
5050 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07005051 if (ret < 0)
5052 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005053 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005054 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005055}
5056
Jens Axboe3529d8c2019-12-19 18:24:38 -07005057static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005058{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005059 struct io_accept *accept = &req->accept;
5060
Jens Axboe14587a462020-09-05 11:36:08 -06005061 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005062 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05005063 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005064 return -EINVAL;
5065
Jens Axboed55e5f52019-12-11 16:12:15 -07005066 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5067 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005068 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005069 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005070 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005071}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005072
Pavel Begunkov889fca72021-02-10 00:03:09 +00005073static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005074{
5075 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005076 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005077 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005078 int ret;
5079
Jiufei Xuee697dee2020-06-10 13:41:59 +08005080 if (req->file->f_flags & O_NONBLOCK)
5081 req->flags |= REQ_F_NOWAIT;
5082
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005083 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005084 accept->addr_len, accept->flags,
5085 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005086 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005087 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005088 if (ret < 0) {
5089 if (ret == -ERESTARTSYS)
5090 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005091 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005092 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005093 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005094 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005095}
5096
Jens Axboe3529d8c2019-12-19 18:24:38 -07005097static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005098{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005099 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005100 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005101
Jens Axboe14587a462020-09-05 11:36:08 -06005102 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005103 return -EINVAL;
5104 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5105 return -EINVAL;
5106
Jens Axboe3529d8c2019-12-19 18:24:38 -07005107 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5108 conn->addr_len = READ_ONCE(sqe->addr2);
5109
5110 if (!io)
5111 return 0;
5112
5113 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005114 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005115}
5116
Pavel Begunkov889fca72021-02-10 00:03:09 +00005117static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005118{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005119 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005120 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005121 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005122 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005123
Jens Axboee8c2bc12020-08-15 18:44:09 -07005124 if (req->async_data) {
5125 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005126 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005127 ret = move_addr_to_kernel(req->connect.addr,
5128 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005129 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005130 if (ret)
5131 goto out;
5132 io = &__io;
5133 }
5134
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005135 file_flags = force_nonblock ? O_NONBLOCK : 0;
5136
Jens Axboee8c2bc12020-08-15 18:44:09 -07005137 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005138 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005139 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005140 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005141 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005142 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005143 ret = -ENOMEM;
5144 goto out;
5145 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005146 io = req->async_data;
5147 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005148 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005149 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005150 if (ret == -ERESTARTSYS)
5151 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005152out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005153 if (ret < 0)
5154 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005155 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005156 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005157}
YueHaibing469956e2020-03-04 15:53:52 +08005158#else /* !CONFIG_NET */
5159static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5160{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005161 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005162}
5163
Pavel Begunkov889fca72021-02-10 00:03:09 +00005164static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005165{
YueHaibing469956e2020-03-04 15:53:52 +08005166 return -EOPNOTSUPP;
5167}
5168
Pavel Begunkov889fca72021-02-10 00:03:09 +00005169static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005170{
5171 return -EOPNOTSUPP;
5172}
5173
5174static int io_recvmsg_prep(struct io_kiocb *req,
5175 const struct io_uring_sqe *sqe)
5176{
5177 return -EOPNOTSUPP;
5178}
5179
Pavel Begunkov889fca72021-02-10 00:03:09 +00005180static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005181{
5182 return -EOPNOTSUPP;
5183}
5184
Pavel Begunkov889fca72021-02-10 00:03:09 +00005185static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005186{
5187 return -EOPNOTSUPP;
5188}
5189
5190static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5191{
5192 return -EOPNOTSUPP;
5193}
5194
Pavel Begunkov889fca72021-02-10 00:03:09 +00005195static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005196{
5197 return -EOPNOTSUPP;
5198}
5199
5200static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5201{
5202 return -EOPNOTSUPP;
5203}
5204
Pavel Begunkov889fca72021-02-10 00:03:09 +00005205static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005206{
5207 return -EOPNOTSUPP;
5208}
5209#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005210
Jens Axboed7718a92020-02-14 22:23:12 -07005211struct io_poll_table {
5212 struct poll_table_struct pt;
5213 struct io_kiocb *req;
5214 int error;
5215};
5216
Jens Axboed7718a92020-02-14 22:23:12 -07005217static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5218 __poll_t mask, task_work_func_t func)
5219{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005220 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005221
5222 /* for instances that support it check for an event match first: */
5223 if (mask && !(mask & poll->events))
5224 return 0;
5225
5226 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5227
5228 list_del_init(&poll->wait.entry);
5229
Jens Axboed7718a92020-02-14 22:23:12 -07005230 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00005231 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005232 percpu_ref_get(&req->ctx->refs);
5233
Jens Axboed7718a92020-02-14 22:23:12 -07005234 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005235 * If this fails, then the task is exiting. When a task exits, the
5236 * work gets canceled, so just cancel this request as well instead
5237 * of executing it. We can't safely execute it anyway, as we may not
5238 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005239 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005240 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005241 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005242 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005243 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005244 }
Jens Axboed7718a92020-02-14 22:23:12 -07005245 return 1;
5246}
5247
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005248static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5249 __acquires(&req->ctx->completion_lock)
5250{
5251 struct io_ring_ctx *ctx = req->ctx;
5252
5253 if (!req->result && !READ_ONCE(poll->canceled)) {
5254 struct poll_table_struct pt = { ._key = poll->events };
5255
5256 req->result = vfs_poll(req->file, &pt) & poll->events;
5257 }
5258
5259 spin_lock_irq(&ctx->completion_lock);
5260 if (!req->result && !READ_ONCE(poll->canceled)) {
5261 add_wait_queue(poll->head, &poll->wait);
5262 return true;
5263 }
5264
5265 return false;
5266}
5267
Jens Axboed4e7cd32020-08-15 11:44:50 -07005268static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005269{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005270 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005271 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005272 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005273 return req->apoll->double_poll;
5274}
5275
5276static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5277{
5278 if (req->opcode == IORING_OP_POLL_ADD)
5279 return &req->poll;
5280 return &req->apoll->poll;
5281}
5282
5283static void io_poll_remove_double(struct io_kiocb *req)
5284{
5285 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005286
5287 lockdep_assert_held(&req->ctx->completion_lock);
5288
5289 if (poll && poll->head) {
5290 struct wait_queue_head *head = poll->head;
5291
5292 spin_lock(&head->lock);
5293 list_del_init(&poll->wait.entry);
5294 if (poll->wait.private)
5295 refcount_dec(&req->refs);
5296 poll->head = NULL;
5297 spin_unlock(&head->lock);
5298 }
5299}
5300
5301static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5302{
5303 struct io_ring_ctx *ctx = req->ctx;
5304
Jens Axboed4e7cd32020-08-15 11:44:50 -07005305 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005306 req->poll.done = true;
5307 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5308 io_commit_cqring(ctx);
5309}
5310
Jens Axboe18bceab2020-05-15 11:56:54 -06005311static void io_poll_task_func(struct callback_head *cb)
5312{
5313 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005314 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005315 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005316
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005317 if (io_poll_rewait(req, &req->poll)) {
5318 spin_unlock_irq(&ctx->completion_lock);
5319 } else {
5320 hash_del(&req->hash_node);
5321 io_poll_complete(req, req->result, 0);
5322 spin_unlock_irq(&ctx->completion_lock);
5323
5324 nxt = io_put_req_find_next(req);
5325 io_cqring_ev_posted(ctx);
5326 if (nxt)
5327 __io_req_task_submit(nxt);
5328 }
5329
Jens Axboe6d816e02020-08-11 08:04:14 -06005330 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005331}
5332
5333static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5334 int sync, void *key)
5335{
5336 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005337 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005338 __poll_t mask = key_to_poll(key);
5339
5340 /* for instances that support it check for an event match first: */
5341 if (mask && !(mask & poll->events))
5342 return 0;
5343
Jens Axboe8706e042020-09-28 08:38:54 -06005344 list_del_init(&wait->entry);
5345
Jens Axboe807abcb2020-07-17 17:09:27 -06005346 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005347 bool done;
5348
Jens Axboe807abcb2020-07-17 17:09:27 -06005349 spin_lock(&poll->head->lock);
5350 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005351 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005352 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005353 /* make sure double remove sees this as being gone */
5354 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005355 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005356 if (!done) {
5357 /* use wait func handler, so it matches the rq type */
5358 poll->wait.func(&poll->wait, mode, sync, key);
5359 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005360 }
5361 refcount_dec(&req->refs);
5362 return 1;
5363}
5364
5365static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5366 wait_queue_func_t wake_func)
5367{
5368 poll->head = NULL;
5369 poll->done = false;
5370 poll->canceled = false;
5371 poll->events = events;
5372 INIT_LIST_HEAD(&poll->wait.entry);
5373 init_waitqueue_func_entry(&poll->wait, wake_func);
5374}
5375
5376static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005377 struct wait_queue_head *head,
5378 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005379{
5380 struct io_kiocb *req = pt->req;
5381
5382 /*
5383 * If poll->head is already set, it's because the file being polled
5384 * uses multiple waitqueues for poll handling (eg one for read, one
5385 * for write). Setup a separate io_poll_iocb if this happens.
5386 */
5387 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005388 struct io_poll_iocb *poll_one = poll;
5389
Jens Axboe18bceab2020-05-15 11:56:54 -06005390 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005391 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005392 pt->error = -EINVAL;
5393 return;
5394 }
5395 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5396 if (!poll) {
5397 pt->error = -ENOMEM;
5398 return;
5399 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005400 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005401 refcount_inc(&req->refs);
5402 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005403 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005404 }
5405
5406 pt->error = 0;
5407 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005408
5409 if (poll->events & EPOLLEXCLUSIVE)
5410 add_wait_queue_exclusive(head, &poll->wait);
5411 else
5412 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005413}
5414
5415static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5416 struct poll_table_struct *p)
5417{
5418 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005419 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005420
Jens Axboe807abcb2020-07-17 17:09:27 -06005421 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005422}
5423
Jens Axboed7718a92020-02-14 22:23:12 -07005424static void io_async_task_func(struct callback_head *cb)
5425{
5426 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5427 struct async_poll *apoll = req->apoll;
5428 struct io_ring_ctx *ctx = req->ctx;
5429
5430 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5431
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005432 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005433 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005434 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005435 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005436 }
5437
Jens Axboe31067252020-05-17 17:43:31 -06005438 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005439 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005440 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005441
Jens Axboed4e7cd32020-08-15 11:44:50 -07005442 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005443 spin_unlock_irq(&ctx->completion_lock);
5444
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005445 if (!READ_ONCE(apoll->poll.canceled))
5446 __io_req_task_submit(req);
5447 else
5448 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005449
Jens Axboe6d816e02020-08-11 08:04:14 -06005450 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005451 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005452 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005453}
5454
5455static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5456 void *key)
5457{
5458 struct io_kiocb *req = wait->private;
5459 struct io_poll_iocb *poll = &req->apoll->poll;
5460
5461 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5462 key_to_poll(key));
5463
5464 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5465}
5466
5467static void io_poll_req_insert(struct io_kiocb *req)
5468{
5469 struct io_ring_ctx *ctx = req->ctx;
5470 struct hlist_head *list;
5471
5472 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5473 hlist_add_head(&req->hash_node, list);
5474}
5475
5476static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5477 struct io_poll_iocb *poll,
5478 struct io_poll_table *ipt, __poll_t mask,
5479 wait_queue_func_t wake_func)
5480 __acquires(&ctx->completion_lock)
5481{
5482 struct io_ring_ctx *ctx = req->ctx;
5483 bool cancel = false;
5484
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005485 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005486 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005487 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005488 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005489
5490 ipt->pt._key = mask;
5491 ipt->req = req;
5492 ipt->error = -EINVAL;
5493
Jens Axboed7718a92020-02-14 22:23:12 -07005494 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5495
5496 spin_lock_irq(&ctx->completion_lock);
5497 if (likely(poll->head)) {
5498 spin_lock(&poll->head->lock);
5499 if (unlikely(list_empty(&poll->wait.entry))) {
5500 if (ipt->error)
5501 cancel = true;
5502 ipt->error = 0;
5503 mask = 0;
5504 }
5505 if (mask || ipt->error)
5506 list_del_init(&poll->wait.entry);
5507 else if (cancel)
5508 WRITE_ONCE(poll->canceled, true);
5509 else if (!poll->done) /* actually waiting for an event */
5510 io_poll_req_insert(req);
5511 spin_unlock(&poll->head->lock);
5512 }
5513
5514 return mask;
5515}
5516
5517static bool io_arm_poll_handler(struct io_kiocb *req)
5518{
5519 const struct io_op_def *def = &io_op_defs[req->opcode];
5520 struct io_ring_ctx *ctx = req->ctx;
5521 struct async_poll *apoll;
5522 struct io_poll_table ipt;
5523 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005524 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005525
5526 if (!req->file || !file_can_poll(req->file))
5527 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005528 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005529 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005530 if (def->pollin)
5531 rw = READ;
5532 else if (def->pollout)
5533 rw = WRITE;
5534 else
5535 return false;
5536 /* if we can't nonblock try, then no point in arming a poll handler */
5537 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005538 return false;
5539
5540 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5541 if (unlikely(!apoll))
5542 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005543 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005544
5545 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005546 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005547
Nathan Chancellor8755d972020-03-02 16:01:19 -07005548 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005549 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005550 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005551 if (def->pollout)
5552 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005553
5554 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5555 if ((req->opcode == IORING_OP_RECVMSG) &&
5556 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5557 mask &= ~POLLIN;
5558
Jens Axboed7718a92020-02-14 22:23:12 -07005559 mask |= POLLERR | POLLPRI;
5560
5561 ipt.pt._qproc = io_async_queue_proc;
5562
5563 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5564 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005565 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005566 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005567 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005568 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005569 kfree(apoll);
5570 return false;
5571 }
5572 spin_unlock_irq(&ctx->completion_lock);
5573 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5574 apoll->poll.events);
5575 return true;
5576}
5577
5578static bool __io_poll_remove_one(struct io_kiocb *req,
5579 struct io_poll_iocb *poll)
5580{
Jens Axboeb41e9852020-02-17 09:52:41 -07005581 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005582
5583 spin_lock(&poll->head->lock);
5584 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005585 if (!list_empty(&poll->wait.entry)) {
5586 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005587 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005588 }
5589 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005590 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005591 return do_complete;
5592}
5593
5594static bool io_poll_remove_one(struct io_kiocb *req)
5595{
5596 bool do_complete;
5597
Jens Axboed4e7cd32020-08-15 11:44:50 -07005598 io_poll_remove_double(req);
5599
Jens Axboed7718a92020-02-14 22:23:12 -07005600 if (req->opcode == IORING_OP_POLL_ADD) {
5601 do_complete = __io_poll_remove_one(req, &req->poll);
5602 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005603 struct async_poll *apoll = req->apoll;
5604
Jens Axboed7718a92020-02-14 22:23:12 -07005605 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005606 do_complete = __io_poll_remove_one(req, &apoll->poll);
5607 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005608 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005609 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005610 kfree(apoll);
5611 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005612 }
5613
Jens Axboeb41e9852020-02-17 09:52:41 -07005614 if (do_complete) {
5615 io_cqring_fill_event(req, -ECANCELED);
5616 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005617 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005618 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005619 }
5620
5621 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005622}
5623
Jens Axboe76e1b642020-09-26 15:05:03 -06005624/*
5625 * Returns true if we found and killed one or more poll requests
5626 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005627static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5628 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005629{
Jens Axboe78076bb2019-12-04 19:56:40 -07005630 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005631 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005632 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005633
5634 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005635 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5636 struct hlist_head *list;
5637
5638 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005639 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005640 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005641 posted += io_poll_remove_one(req);
5642 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005643 }
5644 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005645
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005646 if (posted)
5647 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005648
5649 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005650}
5651
Jens Axboe47f46762019-11-09 17:43:02 -07005652static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5653{
Jens Axboe78076bb2019-12-04 19:56:40 -07005654 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005655 struct io_kiocb *req;
5656
Jens Axboe78076bb2019-12-04 19:56:40 -07005657 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5658 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005659 if (sqe_addr != req->user_data)
5660 continue;
5661 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005662 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005663 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005664 }
5665
5666 return -ENOENT;
5667}
5668
Jens Axboe3529d8c2019-12-19 18:24:38 -07005669static int io_poll_remove_prep(struct io_kiocb *req,
5670 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005671{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005672 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5673 return -EINVAL;
5674 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5675 sqe->poll_events)
5676 return -EINVAL;
5677
Pavel Begunkov018043b2020-10-27 23:17:18 +00005678 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005679 return 0;
5680}
5681
5682/*
5683 * Find a running poll command that matches one specified in sqe->addr,
5684 * and remove it if found.
5685 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005686static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005687{
5688 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005689 int ret;
5690
Jens Axboe221c5eb2019-01-17 09:41:58 -07005691 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005692 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005693 spin_unlock_irq(&ctx->completion_lock);
5694
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005695 if (ret < 0)
5696 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005697 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005698 return 0;
5699}
5700
Jens Axboe221c5eb2019-01-17 09:41:58 -07005701static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5702 void *key)
5703{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005704 struct io_kiocb *req = wait->private;
5705 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005706
Jens Axboed7718a92020-02-14 22:23:12 -07005707 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005708}
5709
Jens Axboe221c5eb2019-01-17 09:41:58 -07005710static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5711 struct poll_table_struct *p)
5712{
5713 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5714
Jens Axboee8c2bc12020-08-15 18:44:09 -07005715 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005716}
5717
Jens Axboe3529d8c2019-12-19 18:24:38 -07005718static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005719{
5720 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005721 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005722
5723 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5724 return -EINVAL;
5725 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5726 return -EINVAL;
5727
Jiufei Xue5769a352020-06-17 17:53:55 +08005728 events = READ_ONCE(sqe->poll32_events);
5729#ifdef __BIG_ENDIAN
5730 events = swahw32(events);
5731#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005732 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5733 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005734 return 0;
5735}
5736
Pavel Begunkov61e98202021-02-10 00:03:08 +00005737static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005738{
5739 struct io_poll_iocb *poll = &req->poll;
5740 struct io_ring_ctx *ctx = req->ctx;
5741 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005742 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005743
Jens Axboed7718a92020-02-14 22:23:12 -07005744 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005745
Jens Axboed7718a92020-02-14 22:23:12 -07005746 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5747 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005748
Jens Axboe8c838782019-03-12 15:48:16 -06005749 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005750 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005751 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005752 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005753 spin_unlock_irq(&ctx->completion_lock);
5754
Jens Axboe8c838782019-03-12 15:48:16 -06005755 if (mask) {
5756 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005757 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005758 }
Jens Axboe8c838782019-03-12 15:48:16 -06005759 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005760}
5761
Jens Axboe5262f562019-09-17 12:26:57 -06005762static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5763{
Jens Axboead8a48a2019-11-15 08:49:11 -07005764 struct io_timeout_data *data = container_of(timer,
5765 struct io_timeout_data, timer);
5766 struct io_kiocb *req = data->req;
5767 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005768 unsigned long flags;
5769
Jens Axboe5262f562019-09-17 12:26:57 -06005770 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005771 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005772 atomic_set(&req->ctx->cq_timeouts,
5773 atomic_read(&req->ctx->cq_timeouts) + 1);
5774
Jens Axboe78e19bb2019-11-06 15:21:34 -07005775 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005776 io_commit_cqring(ctx);
5777 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5778
5779 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005780 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005781 io_put_req(req);
5782 return HRTIMER_NORESTART;
5783}
5784
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005785static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5786 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005787{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005788 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005789 struct io_kiocb *req;
5790 int ret = -ENOENT;
5791
5792 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5793 if (user_data == req->user_data) {
5794 ret = 0;
5795 break;
5796 }
5797 }
5798
5799 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005800 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005801
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005802 io = req->async_data;
5803 ret = hrtimer_try_to_cancel(&io->timer);
5804 if (ret == -1)
5805 return ERR_PTR(-EALREADY);
5806 list_del_init(&req->timeout.list);
5807 return req;
5808}
5809
5810static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5811{
5812 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5813
5814 if (IS_ERR(req))
5815 return PTR_ERR(req);
5816
5817 req_set_fail_links(req);
5818 io_cqring_fill_event(req, -ECANCELED);
5819 io_put_req_deferred(req, 1);
5820 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005821}
5822
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005823static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5824 struct timespec64 *ts, enum hrtimer_mode mode)
5825{
5826 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5827 struct io_timeout_data *data;
5828
5829 if (IS_ERR(req))
5830 return PTR_ERR(req);
5831
5832 req->timeout.off = 0; /* noseq */
5833 data = req->async_data;
5834 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5835 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5836 data->timer.function = io_timeout_fn;
5837 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5838 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005839}
5840
Jens Axboe3529d8c2019-12-19 18:24:38 -07005841static int io_timeout_remove_prep(struct io_kiocb *req,
5842 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005843{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005844 struct io_timeout_rem *tr = &req->timeout_rem;
5845
Jens Axboeb29472e2019-12-17 18:50:29 -07005846 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5847 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005848 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5849 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005850 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005851 return -EINVAL;
5852
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005853 tr->addr = READ_ONCE(sqe->addr);
5854 tr->flags = READ_ONCE(sqe->timeout_flags);
5855 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5856 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5857 return -EINVAL;
5858 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5859 return -EFAULT;
5860 } else if (tr->flags) {
5861 /* timeout removal doesn't support flags */
5862 return -EINVAL;
5863 }
5864
Jens Axboeb29472e2019-12-17 18:50:29 -07005865 return 0;
5866}
5867
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005868static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5869{
5870 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5871 : HRTIMER_MODE_REL;
5872}
5873
Jens Axboe11365042019-10-16 09:08:32 -06005874/*
5875 * Remove or update an existing timeout command
5876 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005877static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005878{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005879 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005881 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005882
Jens Axboe11365042019-10-16 09:08:32 -06005883 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005884 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005885 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005886 else
5887 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5888 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005889
Jens Axboe47f46762019-11-09 17:43:02 -07005890 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005891 io_commit_cqring(ctx);
5892 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005893 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005894 if (ret < 0)
5895 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005896 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005897 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005898}
5899
Jens Axboe3529d8c2019-12-19 18:24:38 -07005900static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005901 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005902{
Jens Axboead8a48a2019-11-15 08:49:11 -07005903 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005904 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005905 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005906
Jens Axboead8a48a2019-11-15 08:49:11 -07005907 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005908 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005909 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005910 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005911 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005912 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005913 flags = READ_ONCE(sqe->timeout_flags);
5914 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005915 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005916
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005917 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005918
Jens Axboee8c2bc12020-08-15 18:44:09 -07005919 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005920 return -ENOMEM;
5921
Jens Axboee8c2bc12020-08-15 18:44:09 -07005922 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005923 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005924
5925 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005926 return -EFAULT;
5927
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005928 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005929 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5930 return 0;
5931}
5932
Pavel Begunkov61e98202021-02-10 00:03:08 +00005933static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005934{
Jens Axboead8a48a2019-11-15 08:49:11 -07005935 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005936 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005937 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005938 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005939
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005940 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005941
Jens Axboe5262f562019-09-17 12:26:57 -06005942 /*
5943 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005944 * timeout event to be satisfied. If it isn't set, then this is
5945 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005946 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005947 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005948 entry = ctx->timeout_list.prev;
5949 goto add;
5950 }
Jens Axboe5262f562019-09-17 12:26:57 -06005951
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005952 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5953 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005954
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005955 /* Update the last seq here in case io_flush_timeouts() hasn't.
5956 * This is safe because ->completion_lock is held, and submissions
5957 * and completions are never mixed in the same ->completion_lock section.
5958 */
5959 ctx->cq_last_tm_flush = tail;
5960
Jens Axboe5262f562019-09-17 12:26:57 -06005961 /*
5962 * Insertion sort, ensuring the first entry in the list is always
5963 * the one we need first.
5964 */
Jens Axboe5262f562019-09-17 12:26:57 -06005965 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005966 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5967 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005968
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005969 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005970 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005971 /* nxt.seq is behind @tail, otherwise would've been completed */
5972 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005973 break;
5974 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005975add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005976 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005977 data->timer.function = io_timeout_fn;
5978 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005979 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005980 return 0;
5981}
5982
Jens Axboe62755e32019-10-28 21:49:21 -06005983static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005984{
Jens Axboe62755e32019-10-28 21:49:21 -06005985 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005986
Jens Axboe62755e32019-10-28 21:49:21 -06005987 return req->user_data == (unsigned long) data;
5988}
5989
Jens Axboee977d6d2019-11-05 12:39:45 -07005990static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005991{
Jens Axboe62755e32019-10-28 21:49:21 -06005992 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005993 int ret = 0;
5994
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005995 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005996 switch (cancel_ret) {
5997 case IO_WQ_CANCEL_OK:
5998 ret = 0;
5999 break;
6000 case IO_WQ_CANCEL_RUNNING:
6001 ret = -EALREADY;
6002 break;
6003 case IO_WQ_CANCEL_NOTFOUND:
6004 ret = -ENOENT;
6005 break;
6006 }
6007
Jens Axboee977d6d2019-11-05 12:39:45 -07006008 return ret;
6009}
6010
Jens Axboe47f46762019-11-09 17:43:02 -07006011static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
6012 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03006013 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07006014{
6015 unsigned long flags;
6016 int ret;
6017
6018 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
6019 if (ret != -ENOENT) {
6020 spin_lock_irqsave(&ctx->completion_lock, flags);
6021 goto done;
6022 }
6023
6024 spin_lock_irqsave(&ctx->completion_lock, flags);
6025 ret = io_timeout_cancel(ctx, sqe_addr);
6026 if (ret != -ENOENT)
6027 goto done;
6028 ret = io_poll_cancel(ctx, sqe_addr);
6029done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07006030 if (!ret)
6031 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006032 io_cqring_fill_event(req, ret);
6033 io_commit_cqring(ctx);
6034 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6035 io_cqring_ev_posted(ctx);
6036
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006037 if (ret < 0)
6038 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03006039 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07006040}
6041
Jens Axboe3529d8c2019-12-19 18:24:38 -07006042static int io_async_cancel_prep(struct io_kiocb *req,
6043 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006044{
Jens Axboefbf23842019-12-17 18:45:56 -07006045 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006046 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006047 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6048 return -EINVAL;
6049 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07006050 return -EINVAL;
6051
Jens Axboefbf23842019-12-17 18:45:56 -07006052 req->cancel.addr = READ_ONCE(sqe->addr);
6053 return 0;
6054}
6055
Pavel Begunkov61e98202021-02-10 00:03:08 +00006056static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006057{
6058 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07006059
Pavel Begunkov014db002020-03-03 21:33:12 +03006060 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006061 return 0;
6062}
6063
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006064static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006065 const struct io_uring_sqe *sqe)
6066{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006067 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6068 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006069 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6070 return -EINVAL;
6071 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006072 return -EINVAL;
6073
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006074 req->rsrc_update.offset = READ_ONCE(sqe->off);
6075 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6076 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006077 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006078 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006079 return 0;
6080}
6081
Pavel Begunkov889fca72021-02-10 00:03:09 +00006082static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006083{
6084 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006085 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006086 int ret;
6087
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006088 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006089 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006090
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006091 up.offset = req->rsrc_update.offset;
6092 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006093
6094 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006095 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006096 mutex_unlock(&ctx->uring_lock);
6097
6098 if (ret < 0)
6099 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006100 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006101 return 0;
6102}
6103
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006104static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006105{
Jens Axboed625c6e2019-12-17 19:53:05 -07006106 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006107 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006108 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006109 case IORING_OP_READV:
6110 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006111 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006112 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006113 case IORING_OP_WRITEV:
6114 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006115 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006116 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006117 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006118 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006119 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006120 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006121 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006122 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006123 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006124 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006125 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006126 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006127 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006128 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006129 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006130 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006131 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006132 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006133 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006134 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006135 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006136 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006137 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006138 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006139 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006140 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006141 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006142 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006143 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006144 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006145 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006146 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006147 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006148 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006149 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006150 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006151 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006152 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006153 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006154 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006155 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006156 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006157 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006158 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006159 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006160 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006161 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006162 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006163 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006164 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006165 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006166 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006167 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006168 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006169 case IORING_OP_SHUTDOWN:
6170 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006171 case IORING_OP_RENAMEAT:
6172 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006173 case IORING_OP_UNLINKAT:
6174 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006175 }
6176
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006177 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6178 req->opcode);
6179 return-EINVAL;
6180}
6181
Jens Axboedef596e2019-01-09 08:59:42 -07006182static int io_req_defer_prep(struct io_kiocb *req,
6183 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006184{
Jens Axboedef596e2019-01-09 08:59:42 -07006185 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006186 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006187 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006188 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006189 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006190}
6191
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006192static u32 io_get_sequence(struct io_kiocb *req)
6193{
6194 struct io_kiocb *pos;
6195 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006196 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006197
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006198 io_for_each_link(pos, req)
6199 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006200
6201 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6202 return total_submitted - nr_reqs;
6203}
6204
Jens Axboe3529d8c2019-12-19 18:24:38 -07006205static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206{
6207 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006208 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006210 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006211
6212 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006213 if (likely(list_empty_careful(&ctx->defer_list) &&
6214 !(req->flags & REQ_F_IO_DRAIN)))
6215 return 0;
6216
6217 seq = io_get_sequence(req);
6218 /* Still a chance to pass the sequence check */
6219 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220 return 0;
6221
Jens Axboee8c2bc12020-08-15 18:44:09 -07006222 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006223 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006224 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006225 return ret;
6226 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006227 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006228 de = kmalloc(sizeof(*de), GFP_KERNEL);
6229 if (!de)
6230 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006231
6232 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006233 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006234 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006235 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006236 io_queue_async_work(req);
6237 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006238 }
6239
6240 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006241 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006242 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006243 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006244 spin_unlock_irq(&ctx->completion_lock);
6245 return -EIOCBQUEUED;
6246}
Jens Axboeedafcce2019-01-09 09:16:05 -07006247
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006248static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006249{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006250 if (req->flags & REQ_F_BUFFER_SELECTED) {
6251 switch (req->opcode) {
6252 case IORING_OP_READV:
6253 case IORING_OP_READ_FIXED:
6254 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006255 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006256 break;
6257 case IORING_OP_RECVMSG:
6258 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006259 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006260 break;
6261 }
6262 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006263 }
6264
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006265 if (req->flags & REQ_F_NEED_CLEANUP) {
6266 switch (req->opcode) {
6267 case IORING_OP_READV:
6268 case IORING_OP_READ_FIXED:
6269 case IORING_OP_READ:
6270 case IORING_OP_WRITEV:
6271 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006272 case IORING_OP_WRITE: {
6273 struct io_async_rw *io = req->async_data;
6274 if (io->free_iovec)
6275 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006276 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006277 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006278 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006279 case IORING_OP_SENDMSG: {
6280 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006281
6282 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006283 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006284 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006285 case IORING_OP_SPLICE:
6286 case IORING_OP_TEE:
6287 io_put_file(req, req->splice.file_in,
6288 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6289 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006290 case IORING_OP_OPENAT:
6291 case IORING_OP_OPENAT2:
6292 if (req->open.filename)
6293 putname(req->open.filename);
6294 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006295 case IORING_OP_RENAMEAT:
6296 putname(req->rename.oldpath);
6297 putname(req->rename.newpath);
6298 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006299 case IORING_OP_UNLINKAT:
6300 putname(req->unlink.filename);
6301 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006302 }
6303 req->flags &= ~REQ_F_NEED_CLEANUP;
6304 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006305}
6306
Pavel Begunkov889fca72021-02-10 00:03:09 +00006307static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006308{
Jens Axboeedafcce2019-01-09 09:16:05 -07006309 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006310 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006311
Jens Axboed625c6e2019-12-17 19:53:05 -07006312 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006313 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006314 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006315 break;
6316 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006317 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006318 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006319 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320 break;
6321 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006322 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006323 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006324 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006325 break;
6326 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006327 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006328 break;
6329 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006330 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006331 break;
6332 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006333 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006334 break;
6335 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006336 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006337 break;
6338 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006339 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006340 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006341 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006342 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006343 break;
6344 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006345 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006346 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006347 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006348 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006349 break;
6350 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006351 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006352 break;
6353 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006354 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006355 break;
6356 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006357 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006358 break;
6359 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006360 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006361 break;
6362 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006363 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006364 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006365 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006366 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006367 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006368 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006369 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006370 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006371 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006372 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006373 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006374 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006375 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006376 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006377 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006378 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006379 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006380 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006381 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006382 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006383 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006384 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006385 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006386 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006387 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006388 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006389 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006390 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006391 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006392 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006393 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006394 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006395 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006396 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006397 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006398 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006399 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006401 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006402 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006403 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006404 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006405 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006406 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006407 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006408 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006409 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006410 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006411 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006412 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006413 default:
6414 ret = -EINVAL;
6415 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006416 }
6417
6418 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006419 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006420
Jens Axboeb5325762020-05-19 21:20:27 -06006421 /* If the op doesn't have a file, we're not polling for it */
6422 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006423 const bool in_async = io_wq_current_is_worker();
6424
Jens Axboe11ba8202020-01-15 21:51:17 -07006425 /* workqueue context doesn't hold uring_lock, grab it now */
6426 if (in_async)
6427 mutex_lock(&ctx->uring_lock);
6428
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006429 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006430
6431 if (in_async)
6432 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006433 }
6434
6435 return 0;
6436}
6437
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006438static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006439{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006440 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006441 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006442 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006444 timeout = io_prep_linked_timeout(req);
6445 if (timeout)
6446 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006447
Jens Axboe4014d942021-01-19 15:53:54 -07006448 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006449 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006450
Jens Axboe561fb042019-10-24 07:25:42 -06006451 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006452 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006453 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006454 /*
6455 * We can get EAGAIN for polled IO even though we're
6456 * forcing a sync submission from here, since we can't
6457 * wait for request slots on the block side.
6458 */
6459 if (ret != -EAGAIN)
6460 break;
6461 cond_resched();
6462 } while (1);
6463 }
Jens Axboe31b51512019-01-18 22:56:34 -07006464
Jens Axboe561fb042019-10-24 07:25:42 -06006465 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006466 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006467
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006468 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6469 lock_ctx = req->ctx;
6470
6471 /*
6472 * io_iopoll_complete() does not hold completion_lock to
6473 * complete polled io, so here for polled io, we can not call
6474 * io_req_complete() directly, otherwise there maybe concurrent
6475 * access to cqring, defer_list, etc, which is not safe. Given
6476 * that io_iopoll_complete() is always called under uring_lock,
6477 * so here for polled io, we also get uring_lock to complete
6478 * it.
6479 */
6480 if (lock_ctx)
6481 mutex_lock(&lock_ctx->uring_lock);
6482
6483 req_set_fail_links(req);
6484 io_req_complete(req, ret);
6485
6486 if (lock_ctx)
6487 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006488 }
Jens Axboe31b51512019-01-18 22:56:34 -07006489}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006490
Jens Axboe65e19f52019-10-26 07:20:21 -06006491static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6492 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006493{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006494 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006495
Jens Axboe05f3fb32019-12-09 11:22:50 -07006496 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006497 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006498}
6499
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006500static struct file *io_file_get(struct io_submit_state *state,
6501 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006502{
6503 struct io_ring_ctx *ctx = req->ctx;
6504 struct file *file;
6505
6506 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006507 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006508 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006509 fd = array_index_nospec(fd, ctx->nr_user_files);
6510 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006511 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006512 } else {
6513 trace_io_uring_file_get(ctx, fd);
6514 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006515 }
6516
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006517 if (file && unlikely(file->f_op == &io_uring_fops))
6518 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006519 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006520}
6521
Jens Axboe2665abf2019-11-05 12:40:47 -07006522static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6523{
Jens Axboead8a48a2019-11-15 08:49:11 -07006524 struct io_timeout_data *data = container_of(timer,
6525 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006526 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006527 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006528 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006529
6530 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006531 prev = req->timeout.head;
6532 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006533
6534 /*
6535 * We don't expect the list to be empty, that will only happen if we
6536 * race with the completion of the linked work.
6537 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006538 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006539 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006540 else
6541 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006542 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6543
6544 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006545 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006546 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006547 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006548 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006549 io_req_complete_post(req, -ETIME, 0);
6550 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006551 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006552 return HRTIMER_NORESTART;
6553}
6554
Jens Axboe7271ef32020-08-10 09:55:22 -06006555static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006556{
Jens Axboe76a46e02019-11-10 23:34:16 -07006557 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006558 * If the back reference is NULL, then our linked request finished
6559 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006560 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006561 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006562 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006563
Jens Axboead8a48a2019-11-15 08:49:11 -07006564 data->timer.function = io_link_timeout_fn;
6565 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6566 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006567 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006568}
6569
6570static void io_queue_linked_timeout(struct io_kiocb *req)
6571{
6572 struct io_ring_ctx *ctx = req->ctx;
6573
6574 spin_lock_irq(&ctx->completion_lock);
6575 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006576 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006577
Jens Axboe2665abf2019-11-05 12:40:47 -07006578 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006579 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006580}
6581
Jens Axboead8a48a2019-11-15 08:49:11 -07006582static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006583{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006584 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006586 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6587 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006588 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006589
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006590 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006591 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006592 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006593 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006594}
6595
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006596static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006597{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006598 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006599 const struct cred *old_creds = NULL;
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006600 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006601
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006602again:
6603 linked_timeout = io_prep_linked_timeout(req);
6604
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006605 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6606 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006607 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006608 if (old_creds)
6609 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006610 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006611 old_creds = NULL; /* restored original creds */
6612 else
Jens Axboe98447d62020-10-14 10:48:51 -06006613 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006614 }
6615
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006616 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006617
6618 /*
6619 * We async punt it if the file wasn't marked NOWAIT, or if the file
6620 * doesn't support non-blocking read/write attempts
6621 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006622 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006623 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006624 /*
6625 * Queued up for async execution, worker will release
6626 * submit reference when the iocb is actually submitted.
6627 */
6628 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006629 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006630
Pavel Begunkovf063c542020-07-25 14:41:59 +03006631 if (linked_timeout)
6632 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006633 } else if (likely(!ret)) {
6634 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006635 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006636 struct io_ring_ctx *ctx = req->ctx;
6637 struct io_comp_state *cs = &ctx->submit_state.comp;
6638
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006639 cs->reqs[cs->nr++] = req;
6640 if (cs->nr == IO_COMPL_BATCH)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006641 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006642 req = NULL;
6643 } else {
6644 req = io_put_req_find_next(req);
6645 }
6646
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006647 if (linked_timeout)
6648 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006649
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006650 if (req) {
6651 if (!(req->flags & REQ_F_FORCE_ASYNC))
6652 goto again;
6653 io_queue_async_work(req);
6654 }
6655 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006656 /* un-prep timeout, so it'll be killed as any other linked */
6657 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006658 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006659 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006660 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006661 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006662
Jens Axboe193155c2020-02-22 23:22:19 -07006663 if (old_creds)
6664 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006665}
6666
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006667static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006668{
6669 int ret;
6670
Jens Axboe3529d8c2019-12-19 18:24:38 -07006671 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006672 if (ret) {
6673 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006674fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006675 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006676 io_put_req(req);
6677 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006678 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006679 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006680 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006681 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006682 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006683 goto fail_req;
6684 }
Jens Axboece35a472019-12-17 08:04:44 -07006685 io_queue_async_work(req);
6686 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006687 if (sqe) {
6688 ret = io_req_prep(req, sqe);
6689 if (unlikely(ret))
6690 goto fail_req;
6691 }
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006692 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006693 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006694}
6695
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006696static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006697{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006698 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006699 io_put_req(req);
6700 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006701 } else
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006702 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006703}
6704
Pavel Begunkov863e0562020-10-27 23:25:35 +00006705struct io_submit_link {
6706 struct io_kiocb *head;
6707 struct io_kiocb *last;
6708};
6709
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006710static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006711 struct io_submit_link *link)
Jens Axboe9e645e112019-05-10 16:07:28 -06006712{
Jackie Liua197f662019-11-08 08:09:12 -07006713 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006714 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006715
Jens Axboe9e645e112019-05-10 16:07:28 -06006716 /*
6717 * If we already have a head request, queue this one for async
6718 * submittal once the head completes. If we don't have a head but
6719 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6720 * submitted sync once the chain is complete. If none of those
6721 * conditions are true (normal request), then just queue it.
6722 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006723 if (link->head) {
6724 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006725
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006726 /*
6727 * Taking sequential execution of a link, draining both sides
6728 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6729 * requests in the link. So, it drains the head and the
6730 * next after the link request. The last one is done via
6731 * drain_next flag to persist the effect across calls.
6732 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006733 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006734 head->flags |= REQ_F_IO_DRAIN;
6735 ctx->drain_next = 1;
6736 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006737 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006738 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006739 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006740 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006741 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006742 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006743 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006744 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006745 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006746
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006747 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006748 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006749 io_queue_link_head(head);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006750 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006751 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006752 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006753 if (unlikely(ctx->drain_next)) {
6754 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006755 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006756 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006757 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006758 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006759 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006760 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006761 link->head = req;
6762 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006763 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006764 io_queue_sqe(req, sqe);
Pavel Begunkov711be032020-01-17 03:57:59 +03006765 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006766 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006767
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006768 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006769}
6770
Jens Axboe9a56a232019-01-09 09:06:50 -07006771/*
6772 * Batched submission is done, ensure local IO is flushed out.
6773 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006774static void io_submit_state_end(struct io_submit_state *state,
6775 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006776{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006777 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006778 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006779 if (state->plug_started)
6780 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006781 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07006782}
6783
6784/*
6785 * Start submission side cache.
6786 */
6787static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006788 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006789{
Jens Axboe27926b62020-10-28 09:33:23 -06006790 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006791 state->ios_left = max_ios;
6792}
6793
Jens Axboe2b188cc2019-01-07 10:46:33 -07006794static void io_commit_sqring(struct io_ring_ctx *ctx)
6795{
Hristo Venev75b28af2019-08-26 17:23:46 +00006796 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006798 /*
6799 * Ensure any loads from the SQEs are done at this point,
6800 * since once we write the new head, the application could
6801 * write new data to them.
6802 */
6803 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006804}
6805
6806/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006807 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006808 * that is mapped by userspace. This means that care needs to be taken to
6809 * ensure that reads are stable, as we cannot rely on userspace always
6810 * being a good citizen. If members of the sqe are validated and then later
6811 * used, it's important that those reads are done through READ_ONCE() to
6812 * prevent a re-load down the line.
6813 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006814static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006815{
Hristo Venev75b28af2019-08-26 17:23:46 +00006816 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006817 unsigned head;
6818
6819 /*
6820 * The cached sq head (or cq tail) serves two purposes:
6821 *
6822 * 1) allows us to batch the cost of updating the user visible
6823 * head updates.
6824 * 2) allows the kernel side to track the head on its own, even
6825 * though the application is the one updating it.
6826 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006827 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006828 if (likely(head < ctx->sq_entries))
6829 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006830
6831 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006832 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006833 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006834 return NULL;
6835}
6836
6837static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6838{
6839 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006840}
6841
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006842/*
6843 * Check SQE restrictions (opcode and flags).
6844 *
6845 * Returns 'true' if SQE is allowed, 'false' otherwise.
6846 */
6847static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6848 struct io_kiocb *req,
6849 unsigned int sqe_flags)
6850{
6851 if (!ctx->restricted)
6852 return true;
6853
6854 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6855 return false;
6856
6857 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6858 ctx->restrictions.sqe_flags_required)
6859 return false;
6860
6861 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6862 ctx->restrictions.sqe_flags_required))
6863 return false;
6864
6865 return true;
6866}
6867
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006868#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6869 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6870 IOSQE_BUFFER_SELECT)
6871
6872static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006873 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006874{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006875 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006876 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006877 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006878
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006879 req->opcode = READ_ONCE(sqe->opcode);
6880 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006881 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006882 req->file = NULL;
6883 req->ctx = ctx;
6884 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006885 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006886 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006887 /* one is dropped after submission, the other at completion */
6888 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006889 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006890 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006891
6892 if (unlikely(req->opcode >= IORING_OP_LAST))
6893 return -EINVAL;
6894
Jens Axboe28cea78a2020-09-14 10:51:17 -06006895 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006896 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006897
6898 sqe_flags = READ_ONCE(sqe->flags);
6899 /* enforce forwards compatibility on users */
6900 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6901 return -EINVAL;
6902
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006903 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6904 return -EACCES;
6905
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006906 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6907 !io_op_defs[req->opcode].buffer_select)
6908 return -EOPNOTSUPP;
6909
6910 id = READ_ONCE(sqe->personality);
6911 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006912 struct io_identity *iod;
6913
Jens Axboe1e6fa522020-10-15 08:46:24 -06006914 iod = idr_find(&ctx->personality_idr, id);
6915 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006916 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006917 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006918
6919 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006920 get_cred(iod->creds);
6921 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006922 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006923 }
6924
6925 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006926 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006927 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006928
Jens Axboe27926b62020-10-28 09:33:23 -06006929 /*
6930 * Plug now if we have more than 1 IO left after this, and the target
6931 * is potentially a read/write to block based storage.
6932 */
6933 if (!state->plug_started && state->ios_left > 1 &&
6934 io_op_defs[req->opcode].plug) {
6935 blk_start_plug(&state->plug);
6936 state->plug_started = true;
6937 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006938
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006939 ret = 0;
6940 if (io_op_defs[req->opcode].needs_file) {
6941 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006942
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006943 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006944 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006945 ret = -EBADF;
6946 }
6947
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006948 state->ios_left--;
6949 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006950}
6951
Jens Axboe0f212202020-09-13 13:09:39 -06006952static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006953{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006954 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006955 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006956
Jens Axboec4a2ed72019-11-21 21:01:26 -07006957 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006958 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006959 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006960 return -EBUSY;
6961 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006962
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006963 /* make sure SQ entry isn't read before tail */
6964 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006965
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006966 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6967 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006968
Jens Axboed8a6df12020-10-15 16:24:45 -06006969 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006970 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006971
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006972 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006973 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006974
Jens Axboe6c271ce2019-01-10 11:22:30 -07006975 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006976 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006977 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006978 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006979
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006980 sqe = io_get_sqe(ctx);
6981 if (unlikely(!sqe)) {
6982 io_consume_sqe(ctx);
6983 break;
6984 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006985 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006986 if (unlikely(!req)) {
6987 if (!submitted)
6988 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006989 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006990 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006991 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006992 /* will complete beyond this point, count as submitted */
6993 submitted++;
6994
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006995 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006996 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006997fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006998 io_put_req(req);
6999 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07007000 break;
7001 }
7002
Jens Axboe354420f2020-01-08 18:55:15 -07007003 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00007004 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007005 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03007006 if (err)
7007 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007008 }
7009
Pavel Begunkov9466f432020-01-25 22:34:01 +03007010 if (unlikely(submitted != nr)) {
7011 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007012 struct io_uring_task *tctx = current->io_uring;
7013 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007014
Jens Axboed8a6df12020-10-15 16:24:45 -06007015 percpu_ref_put_many(&ctx->refs, unused);
7016 percpu_counter_sub(&tctx->inflight, unused);
7017 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03007018 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00007019 if (link.head)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007020 io_queue_link_head(link.head);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007021 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007022
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007023 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7024 io_commit_sqring(ctx);
7025
Jens Axboe6c271ce2019-01-10 11:22:30 -07007026 return submitted;
7027}
7028
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007029static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7030{
7031 /* Tell userspace we may need a wakeup call */
7032 spin_lock_irq(&ctx->completion_lock);
7033 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
7034 spin_unlock_irq(&ctx->completion_lock);
7035}
7036
7037static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7038{
7039 spin_lock_irq(&ctx->completion_lock);
7040 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
7041 spin_unlock_irq(&ctx->completion_lock);
7042}
7043
Xiaoguang Wang08369242020-11-03 14:15:59 +08007044static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007045{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007046 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007047 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007048
Jens Axboec8d1ba52020-09-14 11:07:26 -06007049 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007050 /* if we're handling multiple rings, cap submit size for fairness */
7051 if (cap_entries && to_submit > 8)
7052 to_submit = 8;
7053
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007054 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7055 unsigned nr_events = 0;
7056
Xiaoguang Wang08369242020-11-03 14:15:59 +08007057 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007058 if (!list_empty(&ctx->iopoll_list))
7059 io_do_iopoll(ctx, &nr_events, 0);
7060
Pavel Begunkovd9d05212021-01-08 20:57:25 +00007061 if (to_submit && !ctx->sqo_dead &&
7062 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007063 ret = io_submit_sqes(ctx, to_submit);
7064 mutex_unlock(&ctx->uring_lock);
7065 }
Jens Axboe90554202020-09-03 12:12:41 -06007066
7067 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
7068 wake_up(&ctx->sqo_sq_wait);
7069
Xiaoguang Wang08369242020-11-03 14:15:59 +08007070 return ret;
7071}
7072
7073static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7074{
7075 struct io_ring_ctx *ctx;
7076 unsigned sq_thread_idle = 0;
7077
7078 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7079 if (sq_thread_idle < ctx->sq_thread_idle)
7080 sq_thread_idle = ctx->sq_thread_idle;
7081 }
7082
7083 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007084}
7085
Jens Axboe69fb2132020-09-14 11:16:23 -06007086static void io_sqd_init_new(struct io_sq_data *sqd)
7087{
7088 struct io_ring_ctx *ctx;
7089
7090 while (!list_empty(&sqd->ctx_new_list)) {
7091 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007092 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7093 complete(&ctx->sq_thread_comp);
7094 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007095
7096 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007097}
7098
Jens Axboe6c271ce2019-01-10 11:22:30 -07007099static int io_sq_thread(void *data)
7100{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007101 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007102 struct files_struct *old_files = current->files;
7103 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007104 const struct cred *old_cred = NULL;
7105 struct io_sq_data *sqd = data;
7106 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007107 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007108 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007109
Jens Axboe28cea78a2020-09-14 10:51:17 -06007110 task_lock(current);
7111 current->files = NULL;
7112 current->nsproxy = NULL;
7113 task_unlock(current);
7114
Jens Axboe69fb2132020-09-14 11:16:23 -06007115 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007116 int ret;
7117 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007118
7119 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007120 * Any changes to the sqd lists are synchronized through the
7121 * kthread parking. This synchronizes the thread vs users,
7122 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007123 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007124 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007125 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007126 /*
7127 * When sq thread is unparked, in case the previous park operation
7128 * comes from io_put_sq_data(), which means that sq thread is going
7129 * to be stopped, so here needs to have a check.
7130 */
7131 if (kthread_should_stop())
7132 break;
7133 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007134
Xiaoguang Wang08369242020-11-03 14:15:59 +08007135 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007136 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007137 timeout = jiffies + sqd->sq_thread_idle;
7138 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007139
Xiaoguang Wang08369242020-11-03 14:15:59 +08007140 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007141 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007142 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7143 if (current->cred != ctx->creds) {
7144 if (old_cred)
7145 revert_creds(old_cred);
7146 old_cred = override_creds(ctx->creds);
7147 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007148 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007149#ifdef CONFIG_AUDIT
7150 current->loginuid = ctx->loginuid;
7151 current->sessionid = ctx->sessionid;
7152#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007153
Xiaoguang Wang08369242020-11-03 14:15:59 +08007154 ret = __io_sq_thread(ctx, cap_entries);
7155 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7156 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007157
Jens Axboe28cea78a2020-09-14 10:51:17 -06007158 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007159 }
7160
Xiaoguang Wang08369242020-11-03 14:15:59 +08007161 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007162 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007163 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007164 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007165 if (sqt_spin)
7166 timeout = jiffies + sqd->sq_thread_idle;
7167 continue;
7168 }
7169
Xiaoguang Wang08369242020-11-03 14:15:59 +08007170 needs_sched = true;
7171 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7172 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7173 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7174 !list_empty_careful(&ctx->iopoll_list)) {
7175 needs_sched = false;
7176 break;
7177 }
7178 if (io_sqring_entries(ctx)) {
7179 needs_sched = false;
7180 break;
7181 }
7182 }
7183
Hao Xu8b28fdf2021-01-31 22:39:04 +08007184 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007185 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7186 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007187
Jens Axboe69fb2132020-09-14 11:16:23 -06007188 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007189 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7190 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007191 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007192
7193 finish_wait(&sqd->wait, &wait);
7194 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007195 }
7196
Jens Axboe4c6e2772020-07-01 11:29:10 -06007197 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007198 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007199
Dennis Zhou91d8f512020-09-16 13:41:05 -07007200 if (cur_css)
7201 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007202 if (old_cred)
7203 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007204
Jens Axboe28cea78a2020-09-14 10:51:17 -06007205 task_lock(current);
7206 current->files = old_files;
7207 current->nsproxy = old_nsproxy;
7208 task_unlock(current);
7209
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007210 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007211
Jens Axboe6c271ce2019-01-10 11:22:30 -07007212 return 0;
7213}
7214
Jens Axboebda52162019-09-24 13:47:15 -06007215struct io_wait_queue {
7216 struct wait_queue_entry wq;
7217 struct io_ring_ctx *ctx;
7218 unsigned to_wait;
7219 unsigned nr_timeouts;
7220};
7221
Pavel Begunkov6c503152021-01-04 20:36:36 +00007222static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007223{
7224 struct io_ring_ctx *ctx = iowq->ctx;
7225
7226 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007227 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007228 * started waiting. For timeouts, we always want to return to userspace,
7229 * regardless of event count.
7230 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007231 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007232 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7233}
7234
7235static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7236 int wake_flags, void *key)
7237{
7238 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7239 wq);
7240
Pavel Begunkov6c503152021-01-04 20:36:36 +00007241 /*
7242 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7243 * the task, and the next invocation will do it.
7244 */
7245 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7246 return autoremove_wake_function(curr, mode, wake_flags, key);
7247 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007248}
7249
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007250static int io_run_task_work_sig(void)
7251{
7252 if (io_run_task_work())
7253 return 1;
7254 if (!signal_pending(current))
7255 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007256 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7257 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007258 return -EINTR;
7259}
7260
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007261/* when returns >0, the caller should retry */
7262static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7263 struct io_wait_queue *iowq,
7264 signed long *timeout)
7265{
7266 int ret;
7267
7268 /* make sure we run task_work before checking for signals */
7269 ret = io_run_task_work_sig();
7270 if (ret || io_should_wake(iowq))
7271 return ret;
7272 /* let the caller flush overflows, retry */
7273 if (test_bit(0, &ctx->cq_check_overflow))
7274 return 1;
7275
7276 *timeout = schedule_timeout(*timeout);
7277 return !*timeout ? -ETIME : 1;
7278}
7279
Jens Axboe2b188cc2019-01-07 10:46:33 -07007280/*
7281 * Wait until events become available, if we don't already have some. The
7282 * application must reap them itself, as they reside on the shared cq ring.
7283 */
7284static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007285 const sigset_t __user *sig, size_t sigsz,
7286 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007287{
Jens Axboebda52162019-09-24 13:47:15 -06007288 struct io_wait_queue iowq = {
7289 .wq = {
7290 .private = current,
7291 .func = io_wake_function,
7292 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7293 },
7294 .ctx = ctx,
7295 .to_wait = min_events,
7296 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007297 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007298 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7299 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007300
Jens Axboeb41e9852020-02-17 09:52:41 -07007301 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007302 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7303 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007304 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007305 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007306 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007307 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007308
7309 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007310#ifdef CONFIG_COMPAT
7311 if (in_compat_syscall())
7312 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007313 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007314 else
7315#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007316 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007317
Jens Axboe2b188cc2019-01-07 10:46:33 -07007318 if (ret)
7319 return ret;
7320 }
7321
Hao Xuc73ebb62020-11-03 10:54:37 +08007322 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007323 struct timespec64 ts;
7324
Hao Xuc73ebb62020-11-03 10:54:37 +08007325 if (get_timespec64(&ts, uts))
7326 return -EFAULT;
7327 timeout = timespec64_to_jiffies(&ts);
7328 }
7329
Jens Axboebda52162019-09-24 13:47:15 -06007330 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007331 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007332 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007333 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007334 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7335 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007336 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7337 finish_wait(&ctx->wait, &iowq.wq);
7338 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007339
Jens Axboeb7db41c2020-07-04 08:55:50 -06007340 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007341
Hristo Venev75b28af2019-08-26 17:23:46 +00007342 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007343}
7344
Jens Axboe6b063142019-01-10 22:13:58 -07007345static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7346{
7347#if defined(CONFIG_UNIX)
7348 if (ctx->ring_sock) {
7349 struct sock *sock = ctx->ring_sock->sk;
7350 struct sk_buff *skb;
7351
7352 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7353 kfree_skb(skb);
7354 }
7355#else
7356 int i;
7357
Jens Axboe65e19f52019-10-26 07:20:21 -06007358 for (i = 0; i < ctx->nr_user_files; i++) {
7359 struct file *file;
7360
7361 file = io_file_from_index(ctx, i);
7362 if (file)
7363 fput(file);
7364 }
Jens Axboe6b063142019-01-10 22:13:58 -07007365#endif
7366}
7367
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007368static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007369{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007370 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007371
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007372 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007373 complete(&data->done);
7374}
7375
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007376static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7377{
7378 spin_lock_bh(&ctx->rsrc_ref_lock);
7379}
7380
7381static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7382{
7383 spin_unlock_bh(&ctx->rsrc_ref_lock);
7384}
7385
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007386static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7387 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007388 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007389{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007390 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007391 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007392 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007393 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007394 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007395}
7396
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007397static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7398 struct io_ring_ctx *ctx,
7399 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007400{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007401 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007402 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007403
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007404 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007405 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007406 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007407 if (ref_node)
7408 percpu_ref_kill(&ref_node->refs);
7409
7410 percpu_ref_kill(&data->refs);
7411
7412 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007413 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007414 do {
7415 ret = wait_for_completion_interruptible(&data->done);
7416 if (!ret)
7417 break;
7418 ret = io_run_task_work_sig();
7419 if (ret < 0) {
7420 percpu_ref_resurrect(&data->refs);
7421 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007422 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007423 return ret;
7424 }
7425 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007426
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007427 destroy_fixed_rsrc_ref_node(backup_node);
7428 return 0;
7429}
7430
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007431static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7432{
7433 struct fixed_rsrc_data *data;
7434
7435 data = kzalloc(sizeof(*data), GFP_KERNEL);
7436 if (!data)
7437 return NULL;
7438
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007439 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007440 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7441 kfree(data);
7442 return NULL;
7443 }
7444 data->ctx = ctx;
7445 init_completion(&data->done);
7446 return data;
7447}
7448
7449static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7450{
7451 percpu_ref_exit(&data->refs);
7452 kfree(data->table);
7453 kfree(data);
7454}
7455
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007456static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7457{
7458 struct fixed_rsrc_data *data = ctx->file_data;
7459 struct fixed_rsrc_ref_node *backup_node;
7460 unsigned nr_tables, i;
7461 int ret;
7462
7463 if (!data)
7464 return -ENXIO;
7465 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7466 if (!backup_node)
7467 return -ENOMEM;
7468 init_fixed_file_ref_node(ctx, backup_node);
7469
7470 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7471 if (ret)
7472 return ret;
7473
Jens Axboe6b063142019-01-10 22:13:58 -07007474 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007475 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7476 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007477 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007478 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007479 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007480 ctx->nr_user_files = 0;
7481 return 0;
7482}
7483
Jens Axboe534ca6d2020-09-02 13:52:19 -06007484static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007485{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007486 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007487 /*
7488 * The park is a bit of a work-around, without it we get
7489 * warning spews on shutdown with SQPOLL set and affinity
7490 * set to a single CPU.
7491 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007492 if (sqd->thread) {
7493 kthread_park(sqd->thread);
7494 kthread_stop(sqd->thread);
7495 }
7496
7497 kfree(sqd);
7498 }
7499}
7500
Jens Axboeaa061652020-09-02 14:50:27 -06007501static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7502{
7503 struct io_ring_ctx *ctx_attach;
7504 struct io_sq_data *sqd;
7505 struct fd f;
7506
7507 f = fdget(p->wq_fd);
7508 if (!f.file)
7509 return ERR_PTR(-ENXIO);
7510 if (f.file->f_op != &io_uring_fops) {
7511 fdput(f);
7512 return ERR_PTR(-EINVAL);
7513 }
7514
7515 ctx_attach = f.file->private_data;
7516 sqd = ctx_attach->sq_data;
7517 if (!sqd) {
7518 fdput(f);
7519 return ERR_PTR(-EINVAL);
7520 }
7521
7522 refcount_inc(&sqd->refs);
7523 fdput(f);
7524 return sqd;
7525}
7526
Jens Axboe534ca6d2020-09-02 13:52:19 -06007527static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7528{
7529 struct io_sq_data *sqd;
7530
Jens Axboeaa061652020-09-02 14:50:27 -06007531 if (p->flags & IORING_SETUP_ATTACH_WQ)
7532 return io_attach_sq_data(p);
7533
Jens Axboe534ca6d2020-09-02 13:52:19 -06007534 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7535 if (!sqd)
7536 return ERR_PTR(-ENOMEM);
7537
7538 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007539 INIT_LIST_HEAD(&sqd->ctx_list);
7540 INIT_LIST_HEAD(&sqd->ctx_new_list);
7541 mutex_init(&sqd->ctx_lock);
7542 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007543 init_waitqueue_head(&sqd->wait);
7544 return sqd;
7545}
7546
Jens Axboe69fb2132020-09-14 11:16:23 -06007547static void io_sq_thread_unpark(struct io_sq_data *sqd)
7548 __releases(&sqd->lock)
7549{
7550 if (!sqd->thread)
7551 return;
7552 kthread_unpark(sqd->thread);
7553 mutex_unlock(&sqd->lock);
7554}
7555
7556static void io_sq_thread_park(struct io_sq_data *sqd)
7557 __acquires(&sqd->lock)
7558{
7559 if (!sqd->thread)
7560 return;
7561 mutex_lock(&sqd->lock);
7562 kthread_park(sqd->thread);
7563}
7564
Jens Axboe534ca6d2020-09-02 13:52:19 -06007565static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7566{
7567 struct io_sq_data *sqd = ctx->sq_data;
7568
7569 if (sqd) {
7570 if (sqd->thread) {
7571 /*
7572 * We may arrive here from the error branch in
7573 * io_sq_offload_create() where the kthread is created
7574 * without being waked up, thus wake it up now to make
7575 * sure the wait will complete.
7576 */
7577 wake_up_process(sqd->thread);
7578 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007579
7580 io_sq_thread_park(sqd);
7581 }
7582
7583 mutex_lock(&sqd->ctx_lock);
7584 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007585 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007586 mutex_unlock(&sqd->ctx_lock);
7587
Xiaoguang Wang08369242020-11-03 14:15:59 +08007588 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007589 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007590
7591 io_put_sq_data(sqd);
7592 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007593 }
7594}
7595
Jens Axboe6b063142019-01-10 22:13:58 -07007596static void io_finish_async(struct io_ring_ctx *ctx)
7597{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007598 io_sq_thread_stop(ctx);
7599
Jens Axboe561fb042019-10-24 07:25:42 -06007600 if (ctx->io_wq) {
7601 io_wq_destroy(ctx->io_wq);
7602 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007603 }
7604}
7605
7606#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007607/*
7608 * Ensure the UNIX gc is aware of our file set, so we are certain that
7609 * the io_uring can be safely unregistered on process exit, even if we have
7610 * loops in the file referencing.
7611 */
7612static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7613{
7614 struct sock *sk = ctx->ring_sock->sk;
7615 struct scm_fp_list *fpl;
7616 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007617 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007618
Jens Axboe6b063142019-01-10 22:13:58 -07007619 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7620 if (!fpl)
7621 return -ENOMEM;
7622
7623 skb = alloc_skb(0, GFP_KERNEL);
7624 if (!skb) {
7625 kfree(fpl);
7626 return -ENOMEM;
7627 }
7628
7629 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007630
Jens Axboe08a45172019-10-03 08:11:03 -06007631 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007632 fpl->user = get_uid(ctx->user);
7633 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007634 struct file *file = io_file_from_index(ctx, i + offset);
7635
7636 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007637 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007638 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007639 unix_inflight(fpl->user, fpl->fp[nr_files]);
7640 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007641 }
7642
Jens Axboe08a45172019-10-03 08:11:03 -06007643 if (nr_files) {
7644 fpl->max = SCM_MAX_FD;
7645 fpl->count = nr_files;
7646 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007647 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007648 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7649 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007650
Jens Axboe08a45172019-10-03 08:11:03 -06007651 for (i = 0; i < nr_files; i++)
7652 fput(fpl->fp[i]);
7653 } else {
7654 kfree_skb(skb);
7655 kfree(fpl);
7656 }
Jens Axboe6b063142019-01-10 22:13:58 -07007657
7658 return 0;
7659}
7660
7661/*
7662 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7663 * causes regular reference counting to break down. We rely on the UNIX
7664 * garbage collection to take care of this problem for us.
7665 */
7666static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7667{
7668 unsigned left, total;
7669 int ret = 0;
7670
7671 total = 0;
7672 left = ctx->nr_user_files;
7673 while (left) {
7674 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007675
7676 ret = __io_sqe_files_scm(ctx, this_files, total);
7677 if (ret)
7678 break;
7679 left -= this_files;
7680 total += this_files;
7681 }
7682
7683 if (!ret)
7684 return 0;
7685
7686 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007687 struct file *file = io_file_from_index(ctx, total);
7688
7689 if (file)
7690 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007691 total++;
7692 }
7693
7694 return ret;
7695}
7696#else
7697static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7698{
7699 return 0;
7700}
7701#endif
7702
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007703static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007704 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007705{
7706 int i;
7707
7708 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007709 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007710 unsigned this_files;
7711
7712 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7713 table->files = kcalloc(this_files, sizeof(struct file *),
7714 GFP_KERNEL);
7715 if (!table->files)
7716 break;
7717 nr_files -= this_files;
7718 }
7719
7720 if (i == nr_tables)
7721 return 0;
7722
7723 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007724 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007725 kfree(table->files);
7726 }
7727 return 1;
7728}
7729
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007730static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007731{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007732 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007733#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007734 struct sock *sock = ctx->ring_sock->sk;
7735 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7736 struct sk_buff *skb;
7737 int i;
7738
7739 __skb_queue_head_init(&list);
7740
7741 /*
7742 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7743 * remove this entry and rearrange the file array.
7744 */
7745 skb = skb_dequeue(head);
7746 while (skb) {
7747 struct scm_fp_list *fp;
7748
7749 fp = UNIXCB(skb).fp;
7750 for (i = 0; i < fp->count; i++) {
7751 int left;
7752
7753 if (fp->fp[i] != file)
7754 continue;
7755
7756 unix_notinflight(fp->user, fp->fp[i]);
7757 left = fp->count - 1 - i;
7758 if (left) {
7759 memmove(&fp->fp[i], &fp->fp[i + 1],
7760 left * sizeof(struct file *));
7761 }
7762 fp->count--;
7763 if (!fp->count) {
7764 kfree_skb(skb);
7765 skb = NULL;
7766 } else {
7767 __skb_queue_tail(&list, skb);
7768 }
7769 fput(file);
7770 file = NULL;
7771 break;
7772 }
7773
7774 if (!file)
7775 break;
7776
7777 __skb_queue_tail(&list, skb);
7778
7779 skb = skb_dequeue(head);
7780 }
7781
7782 if (skb_peek(&list)) {
7783 spin_lock_irq(&head->lock);
7784 while ((skb = __skb_dequeue(&list)) != NULL)
7785 __skb_queue_tail(head, skb);
7786 spin_unlock_irq(&head->lock);
7787 }
7788#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007789 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007790#endif
7791}
7792
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007793static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007794{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007795 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7796 struct io_ring_ctx *ctx = rsrc_data->ctx;
7797 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007798
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007799 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7800 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007801 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007802 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007803 }
7804
Xiaoguang Wang05589552020-03-31 14:05:18 +08007805 percpu_ref_exit(&ref_node->refs);
7806 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007807 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007808}
7809
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007810static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007811{
7812 struct io_ring_ctx *ctx;
7813 struct llist_node *node;
7814
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007815 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7816 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007817
7818 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007819 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007820 struct llist_node *next = node->next;
7821
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007822 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7823 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007824 node = next;
7825 }
7826}
7827
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007828static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7829 unsigned i)
7830{
7831 struct fixed_rsrc_table *table;
7832
7833 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7834 return &table->files[i & IORING_FILE_TABLE_MASK];
7835}
7836
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007837static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007838{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007839 struct fixed_rsrc_ref_node *ref_node;
7840 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007841 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007842 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007843 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007844
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007845 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7846 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007847 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007848
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007849 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007850 ref_node->done = true;
7851
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007852 while (!list_empty(&ctx->rsrc_ref_list)) {
7853 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007854 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007855 /* recycle ref nodes in order */
7856 if (!ref_node->done)
7857 break;
7858 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007859 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007860 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007861 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007862
7863 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007864 delay = 0;
7865
Jens Axboe4a38aed22020-05-14 17:21:15 -06007866 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007867 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007868 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007869 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007870}
7871
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007872static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007873 struct io_ring_ctx *ctx)
7874{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007875 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007876
7877 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7878 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007879 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007880
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007881 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007882 0, GFP_KERNEL)) {
7883 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007884 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007885 }
7886 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007887 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007888 ref_node->done = false;
7889 return ref_node;
7890}
7891
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007892static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7893 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007894{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007895 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007896 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007897}
7898
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007899static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007900{
7901 percpu_ref_exit(&ref_node->refs);
7902 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007903}
7904
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007905
Jens Axboe05f3fb32019-12-09 11:22:50 -07007906static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7907 unsigned nr_args)
7908{
7909 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007910 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007911 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007912 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007913 struct fixed_rsrc_ref_node *ref_node;
7914 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007915
7916 if (ctx->file_data)
7917 return -EBUSY;
7918 if (!nr_args)
7919 return -EINVAL;
7920 if (nr_args > IORING_MAX_FIXED_FILES)
7921 return -EMFILE;
7922
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007923 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007924 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007925 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007926 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007927
7928 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007929 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007930 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007931 if (!file_data->table)
7932 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007933
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007934 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007935 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007936
7937 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007938 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7939 ret = -EFAULT;
7940 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007941 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007942 /* allow sparse sets */
7943 if (fd == -1)
7944 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007945
Jens Axboe05f3fb32019-12-09 11:22:50 -07007946 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007947 ret = -EBADF;
7948 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007949 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007950
7951 /*
7952 * Don't allow io_uring instances to be registered. If UNIX
7953 * isn't enabled, then this causes a reference cycle and this
7954 * instance can never get freed. If UNIX is enabled we'll
7955 * handle it just fine, but there's still no point in allowing
7956 * a ring fd as it doesn't support regular read/write anyway.
7957 */
7958 if (file->f_op == &io_uring_fops) {
7959 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007960 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007961 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007962 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007963 }
7964
Jens Axboe05f3fb32019-12-09 11:22:50 -07007965 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007966 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007967 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007968 return ret;
7969 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007970
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007971 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007972 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007973 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007974 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007975 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007976 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007977
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007978 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007979 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007980out_fput:
7981 for (i = 0; i < ctx->nr_user_files; i++) {
7982 file = io_file_from_index(ctx, i);
7983 if (file)
7984 fput(file);
7985 }
7986 for (i = 0; i < nr_tables; i++)
7987 kfree(file_data->table[i].files);
7988 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007989out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007990 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007991 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007992 return ret;
7993}
7994
Jens Axboec3a31e62019-10-03 13:59:56 -06007995static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7996 int index)
7997{
7998#if defined(CONFIG_UNIX)
7999 struct sock *sock = ctx->ring_sock->sk;
8000 struct sk_buff_head *head = &sock->sk_receive_queue;
8001 struct sk_buff *skb;
8002
8003 /*
8004 * See if we can merge this file into an existing skb SCM_RIGHTS
8005 * file set. If there's no room, fall back to allocating a new skb
8006 * and filling it in.
8007 */
8008 spin_lock_irq(&head->lock);
8009 skb = skb_peek(head);
8010 if (skb) {
8011 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8012
8013 if (fpl->count < SCM_MAX_FD) {
8014 __skb_unlink(skb, head);
8015 spin_unlock_irq(&head->lock);
8016 fpl->fp[fpl->count] = get_file(file);
8017 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8018 fpl->count++;
8019 spin_lock_irq(&head->lock);
8020 __skb_queue_head(head, skb);
8021 } else {
8022 skb = NULL;
8023 }
8024 }
8025 spin_unlock_irq(&head->lock);
8026
8027 if (skb) {
8028 fput(file);
8029 return 0;
8030 }
8031
8032 return __io_sqe_files_scm(ctx, 1, index);
8033#else
8034 return 0;
8035#endif
8036}
8037
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008038static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008039{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008040 struct io_rsrc_put *prsrc;
8041 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008042
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008043 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8044 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08008045 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008046
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008047 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008048 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008049
Hillf Dantona5318d32020-03-23 17:47:15 +08008050 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008051}
8052
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008053static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
8054 struct file *file)
8055{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008056 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008057}
8058
Jens Axboe05f3fb32019-12-09 11:22:50 -07008059static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008060 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008061 unsigned nr_args)
8062{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008063 struct fixed_rsrc_data *data = ctx->file_data;
8064 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008065 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06008066 __s32 __user *fds;
8067 int fd, i, err;
8068 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008069 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008070
Jens Axboe05f3fb32019-12-09 11:22:50 -07008071 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008072 return -EOVERFLOW;
8073 if (done > ctx->nr_user_files)
8074 return -EINVAL;
8075
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008076 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008077 if (!ref_node)
8078 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008079 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008080
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008081 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00008082 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008083 err = 0;
8084 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8085 err = -EFAULT;
8086 break;
8087 }
noah4e0377a2021-01-26 15:23:28 -05008088 if (fd == IORING_REGISTER_FILES_SKIP)
8089 continue;
8090
Pavel Begunkov67973b92021-01-26 13:51:09 +00008091 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008092 file_slot = io_fixed_file_slot(ctx->file_data, i);
8093
8094 if (*file_slot) {
8095 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008096 if (err)
8097 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008098 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008099 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008100 }
8101 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008102 file = fget(fd);
8103 if (!file) {
8104 err = -EBADF;
8105 break;
8106 }
8107 /*
8108 * Don't allow io_uring instances to be registered. If
8109 * UNIX isn't enabled, then this causes a reference
8110 * cycle and this instance can never get freed. If UNIX
8111 * is enabled we'll handle it just fine, but there's
8112 * still no point in allowing a ring fd as it doesn't
8113 * support regular read/write anyway.
8114 */
8115 if (file->f_op == &io_uring_fops) {
8116 fput(file);
8117 err = -EBADF;
8118 break;
8119 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008120 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008121 if (err) {
8122 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008123 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008124 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008125 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008126 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008127 }
8128
Xiaoguang Wang05589552020-03-31 14:05:18 +08008129 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008130 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008131 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008132 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008133 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008134
8135 return done ? done : err;
8136}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008137
Jens Axboe05f3fb32019-12-09 11:22:50 -07008138static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8139 unsigned nr_args)
8140{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008141 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008142
8143 if (!ctx->file_data)
8144 return -ENXIO;
8145 if (!nr_args)
8146 return -EINVAL;
8147 if (copy_from_user(&up, arg, sizeof(up)))
8148 return -EFAULT;
8149 if (up.resv)
8150 return -EINVAL;
8151
8152 return __io_sqe_files_update(ctx, &up, nr_args);
8153}
Jens Axboec3a31e62019-10-03 13:59:56 -06008154
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008155static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008156{
8157 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8158
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008159 req = io_put_req_find_next(req);
8160 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008161}
8162
Pavel Begunkov24369c22020-01-28 03:15:48 +03008163static int io_init_wq_offload(struct io_ring_ctx *ctx,
8164 struct io_uring_params *p)
8165{
8166 struct io_wq_data data;
8167 struct fd f;
8168 struct io_ring_ctx *ctx_attach;
8169 unsigned int concurrency;
8170 int ret = 0;
8171
8172 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008173 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008174 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008175
8176 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8177 /* Do QD, or 4 * CPUS, whatever is smallest */
8178 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8179
8180 ctx->io_wq = io_wq_create(concurrency, &data);
8181 if (IS_ERR(ctx->io_wq)) {
8182 ret = PTR_ERR(ctx->io_wq);
8183 ctx->io_wq = NULL;
8184 }
8185 return ret;
8186 }
8187
8188 f = fdget(p->wq_fd);
8189 if (!f.file)
8190 return -EBADF;
8191
8192 if (f.file->f_op != &io_uring_fops) {
8193 ret = -EINVAL;
8194 goto out_fput;
8195 }
8196
8197 ctx_attach = f.file->private_data;
8198 /* @io_wq is protected by holding the fd */
8199 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8200 ret = -EINVAL;
8201 goto out_fput;
8202 }
8203
8204 ctx->io_wq = ctx_attach->io_wq;
8205out_fput:
8206 fdput(f);
8207 return ret;
8208}
8209
Jens Axboe0f212202020-09-13 13:09:39 -06008210static int io_uring_alloc_task_context(struct task_struct *task)
8211{
8212 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008213 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008214
8215 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8216 if (unlikely(!tctx))
8217 return -ENOMEM;
8218
Jens Axboed8a6df12020-10-15 16:24:45 -06008219 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8220 if (unlikely(ret)) {
8221 kfree(tctx);
8222 return ret;
8223 }
8224
Jens Axboe0f212202020-09-13 13:09:39 -06008225 xa_init(&tctx->xa);
8226 init_waitqueue_head(&tctx->wait);
8227 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008228 atomic_set(&tctx->in_idle, 0);
8229 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008230 io_init_identity(&tctx->__identity);
8231 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008232 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008233 spin_lock_init(&tctx->task_lock);
8234 INIT_WQ_LIST(&tctx->task_list);
8235 tctx->task_state = 0;
8236 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008237 return 0;
8238}
8239
8240void __io_uring_free(struct task_struct *tsk)
8241{
8242 struct io_uring_task *tctx = tsk->io_uring;
8243
8244 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008245 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8246 if (tctx->identity != &tctx->__identity)
8247 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008248 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008249 kfree(tctx);
8250 tsk->io_uring = NULL;
8251}
8252
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008253static int io_sq_offload_create(struct io_ring_ctx *ctx,
8254 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008255{
8256 int ret;
8257
Jens Axboe6c271ce2019-01-10 11:22:30 -07008258 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008259 struct io_sq_data *sqd;
8260
Jens Axboe3ec482d2019-04-08 10:51:01 -06008261 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008262 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008263 goto err;
8264
Jens Axboe534ca6d2020-09-02 13:52:19 -06008265 sqd = io_get_sq_data(p);
8266 if (IS_ERR(sqd)) {
8267 ret = PTR_ERR(sqd);
8268 goto err;
8269 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008270
Jens Axboe534ca6d2020-09-02 13:52:19 -06008271 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008272 io_sq_thread_park(sqd);
8273 mutex_lock(&sqd->ctx_lock);
8274 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8275 mutex_unlock(&sqd->ctx_lock);
8276 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008277
Jens Axboe917257d2019-04-13 09:28:55 -06008278 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8279 if (!ctx->sq_thread_idle)
8280 ctx->sq_thread_idle = HZ;
8281
Jens Axboeaa061652020-09-02 14:50:27 -06008282 if (sqd->thread)
8283 goto done;
8284
Jens Axboe6c271ce2019-01-10 11:22:30 -07008285 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008286 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008287
Jens Axboe917257d2019-04-13 09:28:55 -06008288 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008289 if (cpu >= nr_cpu_ids)
8290 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008291 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008292 goto err;
8293
Jens Axboe69fb2132020-09-14 11:16:23 -06008294 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008295 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008296 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008297 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008298 "io_uring-sq");
8299 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008300 if (IS_ERR(sqd->thread)) {
8301 ret = PTR_ERR(sqd->thread);
8302 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008303 goto err;
8304 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008305 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008306 if (ret)
8307 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008308 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8309 /* Can't have SQ_AFF without SQPOLL */
8310 ret = -EINVAL;
8311 goto err;
8312 }
8313
Jens Axboeaa061652020-09-02 14:50:27 -06008314done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008315 ret = io_init_wq_offload(ctx, p);
8316 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008318
8319 return 0;
8320err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008321 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008322 return ret;
8323}
8324
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008325static void io_sq_offload_start(struct io_ring_ctx *ctx)
8326{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008327 struct io_sq_data *sqd = ctx->sq_data;
8328
8329 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8330 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008331}
8332
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008333static inline void __io_unaccount_mem(struct user_struct *user,
8334 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008335{
8336 atomic_long_sub(nr_pages, &user->locked_vm);
8337}
8338
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008339static inline int __io_account_mem(struct user_struct *user,
8340 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008341{
8342 unsigned long page_limit, cur_pages, new_pages;
8343
8344 /* Don't allow more pages than we can safely lock */
8345 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8346
8347 do {
8348 cur_pages = atomic_long_read(&user->locked_vm);
8349 new_pages = cur_pages + nr_pages;
8350 if (new_pages > page_limit)
8351 return -ENOMEM;
8352 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8353 new_pages) != cur_pages);
8354
8355 return 0;
8356}
8357
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008358static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8359 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008360{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008361 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008362 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008363
Jens Axboe2aede0e2020-09-14 10:45:53 -06008364 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008365 if (acct == ACCT_LOCKED) {
8366 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008367 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008368 mmap_write_unlock(ctx->mm_account);
8369 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008370 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008371 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008372 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008373}
8374
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008375static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8376 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008377{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008378 int ret;
8379
8380 if (ctx->limit_mem) {
8381 ret = __io_account_mem(ctx->user, nr_pages);
8382 if (ret)
8383 return ret;
8384 }
8385
Jens Axboe2aede0e2020-09-14 10:45:53 -06008386 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008387 if (acct == ACCT_LOCKED) {
8388 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008389 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008390 mmap_write_unlock(ctx->mm_account);
8391 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008392 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008393 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008394 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008395
8396 return 0;
8397}
8398
Jens Axboe2b188cc2019-01-07 10:46:33 -07008399static void io_mem_free(void *ptr)
8400{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008401 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008402
Mark Rutland52e04ef2019-04-30 17:30:21 +01008403 if (!ptr)
8404 return;
8405
8406 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008407 if (put_page_testzero(page))
8408 free_compound_page(page);
8409}
8410
8411static void *io_mem_alloc(size_t size)
8412{
8413 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8414 __GFP_NORETRY;
8415
8416 return (void *) __get_free_pages(gfp_flags, get_order(size));
8417}
8418
Hristo Venev75b28af2019-08-26 17:23:46 +00008419static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8420 size_t *sq_offset)
8421{
8422 struct io_rings *rings;
8423 size_t off, sq_array_size;
8424
8425 off = struct_size(rings, cqes, cq_entries);
8426 if (off == SIZE_MAX)
8427 return SIZE_MAX;
8428
8429#ifdef CONFIG_SMP
8430 off = ALIGN(off, SMP_CACHE_BYTES);
8431 if (off == 0)
8432 return SIZE_MAX;
8433#endif
8434
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008435 if (sq_offset)
8436 *sq_offset = off;
8437
Hristo Venev75b28af2019-08-26 17:23:46 +00008438 sq_array_size = array_size(sizeof(u32), sq_entries);
8439 if (sq_array_size == SIZE_MAX)
8440 return SIZE_MAX;
8441
8442 if (check_add_overflow(off, sq_array_size, &off))
8443 return SIZE_MAX;
8444
Hristo Venev75b28af2019-08-26 17:23:46 +00008445 return off;
8446}
8447
Jens Axboe2b188cc2019-01-07 10:46:33 -07008448static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8449{
Hristo Venev75b28af2019-08-26 17:23:46 +00008450 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008451
Hristo Venev75b28af2019-08-26 17:23:46 +00008452 pages = (size_t)1 << get_order(
8453 rings_size(sq_entries, cq_entries, NULL));
8454 pages += (size_t)1 << get_order(
8455 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008456
Hristo Venev75b28af2019-08-26 17:23:46 +00008457 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008458}
8459
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008460static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008461{
8462 int i, j;
8463
8464 if (!ctx->user_bufs)
8465 return -ENXIO;
8466
8467 for (i = 0; i < ctx->nr_user_bufs; i++) {
8468 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8469
8470 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008471 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008472
Jens Axboede293932020-09-17 16:19:16 -06008473 if (imu->acct_pages)
8474 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008475 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008476 imu->nr_bvecs = 0;
8477 }
8478
8479 kfree(ctx->user_bufs);
8480 ctx->user_bufs = NULL;
8481 ctx->nr_user_bufs = 0;
8482 return 0;
8483}
8484
8485static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8486 void __user *arg, unsigned index)
8487{
8488 struct iovec __user *src;
8489
8490#ifdef CONFIG_COMPAT
8491 if (ctx->compat) {
8492 struct compat_iovec __user *ciovs;
8493 struct compat_iovec ciov;
8494
8495 ciovs = (struct compat_iovec __user *) arg;
8496 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8497 return -EFAULT;
8498
Jens Axboed55e5f52019-12-11 16:12:15 -07008499 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008500 dst->iov_len = ciov.iov_len;
8501 return 0;
8502 }
8503#endif
8504 src = (struct iovec __user *) arg;
8505 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8506 return -EFAULT;
8507 return 0;
8508}
8509
Jens Axboede293932020-09-17 16:19:16 -06008510/*
8511 * Not super efficient, but this is just a registration time. And we do cache
8512 * the last compound head, so generally we'll only do a full search if we don't
8513 * match that one.
8514 *
8515 * We check if the given compound head page has already been accounted, to
8516 * avoid double accounting it. This allows us to account the full size of the
8517 * page, not just the constituent pages of a huge page.
8518 */
8519static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8520 int nr_pages, struct page *hpage)
8521{
8522 int i, j;
8523
8524 /* check current page array */
8525 for (i = 0; i < nr_pages; i++) {
8526 if (!PageCompound(pages[i]))
8527 continue;
8528 if (compound_head(pages[i]) == hpage)
8529 return true;
8530 }
8531
8532 /* check previously registered pages */
8533 for (i = 0; i < ctx->nr_user_bufs; i++) {
8534 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8535
8536 for (j = 0; j < imu->nr_bvecs; j++) {
8537 if (!PageCompound(imu->bvec[j].bv_page))
8538 continue;
8539 if (compound_head(imu->bvec[j].bv_page) == hpage)
8540 return true;
8541 }
8542 }
8543
8544 return false;
8545}
8546
8547static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8548 int nr_pages, struct io_mapped_ubuf *imu,
8549 struct page **last_hpage)
8550{
8551 int i, ret;
8552
8553 for (i = 0; i < nr_pages; i++) {
8554 if (!PageCompound(pages[i])) {
8555 imu->acct_pages++;
8556 } else {
8557 struct page *hpage;
8558
8559 hpage = compound_head(pages[i]);
8560 if (hpage == *last_hpage)
8561 continue;
8562 *last_hpage = hpage;
8563 if (headpage_already_acct(ctx, pages, i, hpage))
8564 continue;
8565 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8566 }
8567 }
8568
8569 if (!imu->acct_pages)
8570 return 0;
8571
8572 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8573 if (ret)
8574 imu->acct_pages = 0;
8575 return ret;
8576}
8577
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008578static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8579 struct io_mapped_ubuf *imu,
8580 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008581{
8582 struct vm_area_struct **vmas = NULL;
8583 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008584 unsigned long off, start, end, ubuf;
8585 size_t size;
8586 int ret, pret, nr_pages, i;
8587
8588 ubuf = (unsigned long) iov->iov_base;
8589 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8590 start = ubuf >> PAGE_SHIFT;
8591 nr_pages = end - start;
8592
8593 ret = -ENOMEM;
8594
8595 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8596 if (!pages)
8597 goto done;
8598
8599 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8600 GFP_KERNEL);
8601 if (!vmas)
8602 goto done;
8603
8604 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8605 GFP_KERNEL);
8606 if (!imu->bvec)
8607 goto done;
8608
8609 ret = 0;
8610 mmap_read_lock(current->mm);
8611 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8612 pages, vmas);
8613 if (pret == nr_pages) {
8614 /* don't support file backed memory */
8615 for (i = 0; i < nr_pages; i++) {
8616 struct vm_area_struct *vma = vmas[i];
8617
8618 if (vma->vm_file &&
8619 !is_file_hugepages(vma->vm_file)) {
8620 ret = -EOPNOTSUPP;
8621 break;
8622 }
8623 }
8624 } else {
8625 ret = pret < 0 ? pret : -EFAULT;
8626 }
8627 mmap_read_unlock(current->mm);
8628 if (ret) {
8629 /*
8630 * if we did partial map, or found file backed vmas,
8631 * release any pages we did get
8632 */
8633 if (pret > 0)
8634 unpin_user_pages(pages, pret);
8635 kvfree(imu->bvec);
8636 goto done;
8637 }
8638
8639 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8640 if (ret) {
8641 unpin_user_pages(pages, pret);
8642 kvfree(imu->bvec);
8643 goto done;
8644 }
8645
8646 off = ubuf & ~PAGE_MASK;
8647 size = iov->iov_len;
8648 for (i = 0; i < nr_pages; i++) {
8649 size_t vec_len;
8650
8651 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8652 imu->bvec[i].bv_page = pages[i];
8653 imu->bvec[i].bv_len = vec_len;
8654 imu->bvec[i].bv_offset = off;
8655 off = 0;
8656 size -= vec_len;
8657 }
8658 /* store original address for later verification */
8659 imu->ubuf = ubuf;
8660 imu->len = iov->iov_len;
8661 imu->nr_bvecs = nr_pages;
8662 ret = 0;
8663done:
8664 kvfree(pages);
8665 kvfree(vmas);
8666 return ret;
8667}
8668
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008669static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008670{
Jens Axboeedafcce2019-01-09 09:16:05 -07008671 if (ctx->user_bufs)
8672 return -EBUSY;
8673 if (!nr_args || nr_args > UIO_MAXIOV)
8674 return -EINVAL;
8675
8676 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8677 GFP_KERNEL);
8678 if (!ctx->user_bufs)
8679 return -ENOMEM;
8680
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008681 return 0;
8682}
8683
8684static int io_buffer_validate(struct iovec *iov)
8685{
8686 /*
8687 * Don't impose further limits on the size and buffer
8688 * constraints here, we'll -EINVAL later when IO is
8689 * submitted if they are wrong.
8690 */
8691 if (!iov->iov_base || !iov->iov_len)
8692 return -EFAULT;
8693
8694 /* arbitrary limit, but we need something */
8695 if (iov->iov_len > SZ_1G)
8696 return -EFAULT;
8697
8698 return 0;
8699}
8700
8701static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8702 unsigned int nr_args)
8703{
8704 int i, ret;
8705 struct iovec iov;
8706 struct page *last_hpage = NULL;
8707
8708 ret = io_buffers_map_alloc(ctx, nr_args);
8709 if (ret)
8710 return ret;
8711
Jens Axboeedafcce2019-01-09 09:16:05 -07008712 for (i = 0; i < nr_args; i++) {
8713 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008714
8715 ret = io_copy_iov(ctx, &iov, arg, i);
8716 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008717 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008718
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008719 ret = io_buffer_validate(&iov);
8720 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008721 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008722
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008723 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8724 if (ret)
8725 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008726
8727 ctx->nr_user_bufs++;
8728 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008729
8730 if (ret)
8731 io_sqe_buffers_unregister(ctx);
8732
Jens Axboeedafcce2019-01-09 09:16:05 -07008733 return ret;
8734}
8735
Jens Axboe9b402842019-04-11 11:45:41 -06008736static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8737{
8738 __s32 __user *fds = arg;
8739 int fd;
8740
8741 if (ctx->cq_ev_fd)
8742 return -EBUSY;
8743
8744 if (copy_from_user(&fd, fds, sizeof(*fds)))
8745 return -EFAULT;
8746
8747 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8748 if (IS_ERR(ctx->cq_ev_fd)) {
8749 int ret = PTR_ERR(ctx->cq_ev_fd);
8750 ctx->cq_ev_fd = NULL;
8751 return ret;
8752 }
8753
8754 return 0;
8755}
8756
8757static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8758{
8759 if (ctx->cq_ev_fd) {
8760 eventfd_ctx_put(ctx->cq_ev_fd);
8761 ctx->cq_ev_fd = NULL;
8762 return 0;
8763 }
8764
8765 return -ENXIO;
8766}
8767
Jens Axboe5a2e7452020-02-23 16:23:11 -07008768static int __io_destroy_buffers(int id, void *p, void *data)
8769{
8770 struct io_ring_ctx *ctx = data;
8771 struct io_buffer *buf = p;
8772
Jens Axboe067524e2020-03-02 16:32:28 -07008773 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008774 return 0;
8775}
8776
8777static void io_destroy_buffers(struct io_ring_ctx *ctx)
8778{
8779 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8780 idr_destroy(&ctx->io_buffer_idr);
8781}
8782
Jens Axboec7dae4b2021-02-09 19:53:37 -07008783static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008784{
Jens Axboec7dae4b2021-02-09 19:53:37 -07008785 while (!list_empty(list)) {
Jens Axboe1b4c3512021-02-10 00:03:19 +00008786 struct io_kiocb *req;
8787
Jens Axboec7dae4b2021-02-09 19:53:37 -07008788 req = list_first_entry(list, struct io_kiocb, compl.list);
Jens Axboe1b4c3512021-02-10 00:03:19 +00008789 list_del(&req->compl.list);
8790 kmem_cache_free(req_cachep, req);
8791 }
8792}
8793
Jens Axboe2b188cc2019-01-07 10:46:33 -07008794static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8795{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008796 struct io_submit_state *submit_state = &ctx->submit_state;
8797
Jens Axboe6b063142019-01-10 22:13:58 -07008798 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008799 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008800
8801 if (ctx->sqo_task) {
8802 put_task_struct(ctx->sqo_task);
8803 ctx->sqo_task = NULL;
8804 mmdrop(ctx->mm_account);
8805 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008806 }
Jens Axboedef596e2019-01-09 08:59:42 -07008807
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008808 if (submit_state->free_reqs)
8809 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8810 submit_state->reqs);
8811
Dennis Zhou91d8f512020-09-16 13:41:05 -07008812#ifdef CONFIG_BLK_CGROUP
8813 if (ctx->sqo_blkcg_css)
8814 css_put(ctx->sqo_blkcg_css);
8815#endif
8816
Jens Axboe6b063142019-01-10 22:13:58 -07008817 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008818 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008819 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008820 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008821
Jens Axboe2b188cc2019-01-07 10:46:33 -07008822#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008823 if (ctx->ring_sock) {
8824 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008825 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008826 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008827#endif
8828
Hristo Venev75b28af2019-08-26 17:23:46 +00008829 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008830 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008831
8832 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008833 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008834 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008835 kfree(ctx->cancel_hash);
Jens Axboec7dae4b2021-02-09 19:53:37 -07008836 io_req_cache_free(&ctx->submit_state.comp.free_list);
8837 io_req_cache_free(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008838 kfree(ctx);
8839}
8840
8841static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8842{
8843 struct io_ring_ctx *ctx = file->private_data;
8844 __poll_t mask = 0;
8845
8846 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008847 /*
8848 * synchronizes with barrier from wq_has_sleeper call in
8849 * io_commit_cqring
8850 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008851 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008852 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008853 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008854
8855 /*
8856 * Don't flush cqring overflow list here, just do a simple check.
8857 * Otherwise there could possible be ABBA deadlock:
8858 * CPU0 CPU1
8859 * ---- ----
8860 * lock(&ctx->uring_lock);
8861 * lock(&ep->mtx);
8862 * lock(&ctx->uring_lock);
8863 * lock(&ep->mtx);
8864 *
8865 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8866 * pushs them to do the flush.
8867 */
8868 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008869 mask |= EPOLLIN | EPOLLRDNORM;
8870
8871 return mask;
8872}
8873
8874static int io_uring_fasync(int fd, struct file *file, int on)
8875{
8876 struct io_ring_ctx *ctx = file->private_data;
8877
8878 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8879}
8880
Yejune Deng0bead8c2020-12-24 11:02:20 +08008881static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008882{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008883 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008884
Jens Axboe1e6fa522020-10-15 08:46:24 -06008885 iod = idr_remove(&ctx->personality_idr, id);
8886 if (iod) {
8887 put_cred(iod->creds);
8888 if (refcount_dec_and_test(&iod->count))
8889 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008890 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008891 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008892
8893 return -EINVAL;
8894}
8895
8896static int io_remove_personalities(int id, void *p, void *data)
8897{
8898 struct io_ring_ctx *ctx = data;
8899
8900 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008901 return 0;
8902}
8903
Jens Axboe85faa7b2020-04-09 18:14:00 -06008904static void io_ring_exit_work(struct work_struct *work)
8905{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008906 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8907 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008908
Jens Axboe56952e92020-06-17 15:00:04 -06008909 /*
8910 * If we're doing polled IO and end up having requests being
8911 * submitted async (out-of-line), then completions can come in while
8912 * we're waiting for refs to drop. We need to reap these manually,
8913 * as nobody else will be looking for them.
8914 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008915 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008916 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008917 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008918 io_ring_ctx_free(ctx);
8919}
8920
Jens Axboe00c18642020-12-20 10:45:02 -07008921static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8922{
8923 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8924
8925 return req->ctx == data;
8926}
8927
Jens Axboe2b188cc2019-01-07 10:46:33 -07008928static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8929{
8930 mutex_lock(&ctx->uring_lock);
8931 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008932
8933 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8934 ctx->sqo_dead = 1;
8935
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008936 /* if force is set, the ring is going away. always drop after that */
8937 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008938 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008939 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008940 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008941 mutex_unlock(&ctx->uring_lock);
8942
Pavel Begunkov6b819282020-11-06 13:00:25 +00008943 io_kill_timeouts(ctx, NULL, NULL);
8944 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008945
8946 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008947 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008948
Jens Axboe15dff282019-11-13 09:09:23 -07008949 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008950 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008951
8952 /*
8953 * Do this upfront, so we won't have a grace period where the ring
8954 * is closed but resources aren't reaped yet. This can cause
8955 * spurious failure in setting up a new ring.
8956 */
Jens Axboe760618f2020-07-24 12:53:31 -06008957 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8958 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008959
Jens Axboe85faa7b2020-04-09 18:14:00 -06008960 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008961 /*
8962 * Use system_unbound_wq to avoid spawning tons of event kworkers
8963 * if we're exiting a ton of rings at the same time. It just adds
8964 * noise and overhead, there's no discernable change in runtime
8965 * over using system_wq.
8966 */
8967 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008968}
8969
8970static int io_uring_release(struct inode *inode, struct file *file)
8971{
8972 struct io_ring_ctx *ctx = file->private_data;
8973
8974 file->private_data = NULL;
8975 io_ring_ctx_wait_and_kill(ctx);
8976 return 0;
8977}
8978
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008979struct io_task_cancel {
8980 struct task_struct *task;
8981 struct files_struct *files;
8982};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008983
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008984static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008985{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008986 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008987 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008988 bool ret;
8989
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008990 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008991 unsigned long flags;
8992 struct io_ring_ctx *ctx = req->ctx;
8993
8994 /* protect against races with linked timeouts */
8995 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008996 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008997 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8998 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008999 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009000 }
9001 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009002}
9003
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009004static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00009005 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009006 struct files_struct *files)
9007{
9008 struct io_defer_entry *de = NULL;
9009 LIST_HEAD(list);
9010
9011 spin_lock_irq(&ctx->completion_lock);
9012 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00009013 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009014 list_cut_position(&list, &ctx->defer_list, &de->list);
9015 break;
9016 }
9017 }
9018 spin_unlock_irq(&ctx->completion_lock);
9019
9020 while (!list_empty(&list)) {
9021 de = list_first_entry(&list, struct io_defer_entry, list);
9022 list_del_init(&de->list);
9023 req_set_fail_links(de->req);
9024 io_put_req(de->req);
9025 io_req_complete(de->req, -ECANCELED);
9026 kfree(de);
9027 }
9028}
9029
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009030static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9031 struct task_struct *task,
9032 struct files_struct *files)
9033{
9034 struct io_task_cancel cancel = { .task = task, .files = files, };
9035
9036 while (1) {
9037 enum io_wq_cancel cret;
9038 bool ret = false;
9039
9040 if (ctx->io_wq) {
9041 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
9042 &cancel, true);
9043 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9044 }
9045
9046 /* SQPOLL thread does its own polling */
9047 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
9048 while (!list_empty_careful(&ctx->iopoll_list)) {
9049 io_iopoll_try_reap_events(ctx);
9050 ret = true;
9051 }
9052 }
9053
9054 ret |= io_poll_remove_all(ctx, task, files);
9055 ret |= io_kill_timeouts(ctx, task, files);
9056 ret |= io_run_task_work();
9057 io_cqring_overflow_flush(ctx, true, task, files);
9058 if (!ret)
9059 break;
9060 cond_resched();
9061 }
9062}
9063
Pavel Begunkovca70f002021-01-26 15:28:27 +00009064static int io_uring_count_inflight(struct io_ring_ctx *ctx,
9065 struct task_struct *task,
9066 struct files_struct *files)
9067{
9068 struct io_kiocb *req;
9069 int cnt = 0;
9070
9071 spin_lock_irq(&ctx->inflight_lock);
9072 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
9073 cnt += io_match_task(req, task, files);
9074 spin_unlock_irq(&ctx->inflight_lock);
9075 return cnt;
9076}
9077
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009078static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009079 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06009080 struct files_struct *files)
9081{
Jens Axboefcb323c2019-10-24 12:39:47 -06009082 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08009083 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009084 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06009085
Pavel Begunkovca70f002021-01-26 15:28:27 +00009086 inflight = io_uring_count_inflight(ctx, task, files);
9087 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06009088 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009089
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009090 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009091 prepare_to_wait(&task->io_uring->wait, &wait,
9092 TASK_UNINTERRUPTIBLE);
9093 if (inflight == io_uring_count_inflight(ctx, task, files))
9094 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00009095 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06009096 }
9097}
9098
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009099static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9100{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009101 mutex_lock(&ctx->uring_lock);
9102 ctx->sqo_dead = 1;
9103 mutex_unlock(&ctx->uring_lock);
9104
9105 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009106 if (ctx->rings)
9107 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009108}
9109
Jens Axboe0f212202020-09-13 13:09:39 -06009110/*
9111 * We need to iteratively cancel requests, in case a request has dependent
9112 * hard links. These persist even for failure of cancelations, hence keep
9113 * looping until none are found.
9114 */
9115static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9116 struct files_struct *files)
9117{
9118 struct task_struct *task = current;
9119
Jens Axboefdaf0832020-10-30 09:37:30 -06009120 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009121 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009122 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009123 atomic_inc(&task->io_uring->in_idle);
9124 io_sq_thread_park(ctx->sq_data);
9125 }
Jens Axboe0f212202020-09-13 13:09:39 -06009126
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009127 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009128
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009129 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009130 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009131 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009132
9133 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9134 atomic_dec(&task->io_uring->in_idle);
9135 /*
9136 * If the files that are going away are the ones in the thread
9137 * identity, clear them out.
9138 */
9139 if (task->io_uring->identity->files == files)
9140 task->io_uring->identity->files = NULL;
9141 io_sq_thread_unpark(ctx->sq_data);
9142 }
Jens Axboe0f212202020-09-13 13:09:39 -06009143}
9144
9145/*
9146 * Note that this task has used io_uring. We use it for cancelation purposes.
9147 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009148static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009149{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009150 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009151 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009152
9153 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009154 ret = io_uring_alloc_task_context(current);
9155 if (unlikely(ret))
9156 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009157 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009158 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009159 if (tctx->last != file) {
9160 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009161
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009162 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009163 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009164 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9165 file, GFP_KERNEL));
9166 if (ret) {
9167 fput(file);
9168 return ret;
9169 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009170
9171 /* one and only SQPOLL file note, held by sqo_task */
9172 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9173 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009174 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009175 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009176 }
9177
Jens Axboefdaf0832020-10-30 09:37:30 -06009178 /*
9179 * This is race safe in that the task itself is doing this, hence it
9180 * cannot be going through the exit/cancel paths at the same time.
9181 * This cannot be modified while exit/cancel is running.
9182 */
9183 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9184 tctx->sqpoll = true;
9185
Jens Axboe0f212202020-09-13 13:09:39 -06009186 return 0;
9187}
9188
9189/*
9190 * Remove this io_uring_file -> task mapping.
9191 */
9192static void io_uring_del_task_file(struct file *file)
9193{
9194 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009195
9196 if (tctx->last == file)
9197 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009198 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009199 if (file)
9200 fput(file);
9201}
9202
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009203static void io_uring_remove_task_files(struct io_uring_task *tctx)
9204{
9205 struct file *file;
9206 unsigned long index;
9207
9208 xa_for_each(&tctx->xa, index, file)
9209 io_uring_del_task_file(file);
9210}
9211
Jens Axboe0f212202020-09-13 13:09:39 -06009212void __io_uring_files_cancel(struct files_struct *files)
9213{
9214 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009215 struct file *file;
9216 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009217
9218 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009219 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009220 xa_for_each(&tctx->xa, index, file)
9221 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009222 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009223
9224 if (files)
9225 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009226}
9227
9228static s64 tctx_inflight(struct io_uring_task *tctx)
9229{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009230 return percpu_counter_sum(&tctx->inflight);
9231}
9232
9233static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9234{
9235 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009236 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009237 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009238
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009239 if (!ctx->sq_data)
9240 return;
9241 tctx = ctx->sq_data->thread->io_uring;
9242 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009243
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009244 atomic_inc(&tctx->in_idle);
9245 do {
9246 /* read completions before cancelations */
9247 inflight = tctx_inflight(tctx);
9248 if (!inflight)
9249 break;
9250 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009251
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009252 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9253 /*
9254 * If we've seen completions, retry without waiting. This
9255 * avoids a race where a completion comes in before we did
9256 * prepare_to_wait().
9257 */
9258 if (inflight == tctx_inflight(tctx))
9259 schedule();
9260 finish_wait(&tctx->wait, &wait);
9261 } while (1);
9262 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009263}
9264
Jens Axboe0f212202020-09-13 13:09:39 -06009265/*
9266 * Find any io_uring fd that this task has registered or done IO on, and cancel
9267 * requests.
9268 */
9269void __io_uring_task_cancel(void)
9270{
9271 struct io_uring_task *tctx = current->io_uring;
9272 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009273 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009274
9275 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009276 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009277
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009278 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009279 if (tctx->sqpoll) {
9280 struct file *file;
9281 unsigned long index;
9282
9283 xa_for_each(&tctx->xa, index, file)
9284 io_uring_cancel_sqpoll(file->private_data);
9285 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009286
Jens Axboed8a6df12020-10-15 16:24:45 -06009287 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009288 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009289 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009290 if (!inflight)
9291 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009292 __io_uring_files_cancel(NULL);
9293
9294 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9295
9296 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009297 * If we've seen completions, retry without waiting. This
9298 * avoids a race where a completion comes in before we did
9299 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009300 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009301 if (inflight == tctx_inflight(tctx))
9302 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009303 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009304 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009305
Jens Axboefdaf0832020-10-30 09:37:30 -06009306 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009307
9308 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009309}
9310
Jens Axboefcb323c2019-10-24 12:39:47 -06009311static int io_uring_flush(struct file *file, void *data)
9312{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009313 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009314 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009315
Jens Axboe84965ff2021-01-23 15:51:11 -07009316 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9317 io_uring_cancel_task_requests(ctx, NULL);
9318
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009319 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009320 return 0;
9321
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009322 /* we should have cancelled and erased it before PF_EXITING */
9323 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9324 xa_load(&tctx->xa, (unsigned long)file));
9325
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009326 /*
9327 * fput() is pending, will be 2 if the only other ref is our potential
9328 * task file note. If the task is exiting, drop regardless of count.
9329 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009330 if (atomic_long_read(&file->f_count) != 2)
9331 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009332
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009333 if (ctx->flags & IORING_SETUP_SQPOLL) {
9334 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009335 WARN_ON_ONCE(ctx->sqo_task != current &&
9336 xa_load(&tctx->xa, (unsigned long)file));
9337 /* sqo_dead check is for when this happens after cancellation */
9338 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009339 !xa_load(&tctx->xa, (unsigned long)file));
9340
9341 io_disable_sqo_submit(ctx);
9342 }
9343
9344 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9345 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009346 return 0;
9347}
9348
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009349static void *io_uring_validate_mmap_request(struct file *file,
9350 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009351{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009353 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009354 struct page *page;
9355 void *ptr;
9356
9357 switch (offset) {
9358 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009359 case IORING_OFF_CQ_RING:
9360 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009361 break;
9362 case IORING_OFF_SQES:
9363 ptr = ctx->sq_sqes;
9364 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009365 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009366 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009367 }
9368
9369 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009370 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009371 return ERR_PTR(-EINVAL);
9372
9373 return ptr;
9374}
9375
9376#ifdef CONFIG_MMU
9377
9378static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9379{
9380 size_t sz = vma->vm_end - vma->vm_start;
9381 unsigned long pfn;
9382 void *ptr;
9383
9384 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9385 if (IS_ERR(ptr))
9386 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009387
9388 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9389 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9390}
9391
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009392#else /* !CONFIG_MMU */
9393
9394static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9395{
9396 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9397}
9398
9399static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9400{
9401 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9402}
9403
9404static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9405 unsigned long addr, unsigned long len,
9406 unsigned long pgoff, unsigned long flags)
9407{
9408 void *ptr;
9409
9410 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9411 if (IS_ERR(ptr))
9412 return PTR_ERR(ptr);
9413
9414 return (unsigned long) ptr;
9415}
9416
9417#endif /* !CONFIG_MMU */
9418
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009419static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009420{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009421 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009422 DEFINE_WAIT(wait);
9423
9424 do {
9425 if (!io_sqring_full(ctx))
9426 break;
9427
9428 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9429
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009430 if (unlikely(ctx->sqo_dead)) {
9431 ret = -EOWNERDEAD;
9432 goto out;
9433 }
9434
Jens Axboe90554202020-09-03 12:12:41 -06009435 if (!io_sqring_full(ctx))
9436 break;
9437
9438 schedule();
9439 } while (!signal_pending(current));
9440
9441 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009442out:
9443 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009444}
9445
Hao Xuc73ebb62020-11-03 10:54:37 +08009446static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9447 struct __kernel_timespec __user **ts,
9448 const sigset_t __user **sig)
9449{
9450 struct io_uring_getevents_arg arg;
9451
9452 /*
9453 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9454 * is just a pointer to the sigset_t.
9455 */
9456 if (!(flags & IORING_ENTER_EXT_ARG)) {
9457 *sig = (const sigset_t __user *) argp;
9458 *ts = NULL;
9459 return 0;
9460 }
9461
9462 /*
9463 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9464 * timespec and sigset_t pointers if good.
9465 */
9466 if (*argsz != sizeof(arg))
9467 return -EINVAL;
9468 if (copy_from_user(&arg, argp, sizeof(arg)))
9469 return -EFAULT;
9470 *sig = u64_to_user_ptr(arg.sigmask);
9471 *argsz = arg.sigmask_sz;
9472 *ts = u64_to_user_ptr(arg.ts);
9473 return 0;
9474}
9475
Jens Axboe2b188cc2019-01-07 10:46:33 -07009476SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009477 u32, min_complete, u32, flags, const void __user *, argp,
9478 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009479{
9480 struct io_ring_ctx *ctx;
9481 long ret = -EBADF;
9482 int submitted = 0;
9483 struct fd f;
9484
Jens Axboe4c6e2772020-07-01 11:29:10 -06009485 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009486
Jens Axboe90554202020-09-03 12:12:41 -06009487 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009488 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009489 return -EINVAL;
9490
9491 f = fdget(fd);
9492 if (!f.file)
9493 return -EBADF;
9494
9495 ret = -EOPNOTSUPP;
9496 if (f.file->f_op != &io_uring_fops)
9497 goto out_fput;
9498
9499 ret = -ENXIO;
9500 ctx = f.file->private_data;
9501 if (!percpu_ref_tryget(&ctx->refs))
9502 goto out_fput;
9503
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009504 ret = -EBADFD;
9505 if (ctx->flags & IORING_SETUP_R_DISABLED)
9506 goto out;
9507
Jens Axboe6c271ce2019-01-10 11:22:30 -07009508 /*
9509 * For SQ polling, the thread will do all submissions and completions.
9510 * Just return the requested submit count, and wake the thread if
9511 * we were asked to.
9512 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009513 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009514 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009515 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009516
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009517 ret = -EOWNERDEAD;
9518 if (unlikely(ctx->sqo_dead))
9519 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009520 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009521 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009522 if (flags & IORING_ENTER_SQ_WAIT) {
9523 ret = io_sqpoll_wait_sq(ctx);
9524 if (ret)
9525 goto out;
9526 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009527 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009528 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009529 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009530 if (unlikely(ret))
9531 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009532 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009533 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009534 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009535
9536 if (submitted != to_submit)
9537 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009538 }
9539 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009540 const sigset_t __user *sig;
9541 struct __kernel_timespec __user *ts;
9542
9543 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9544 if (unlikely(ret))
9545 goto out;
9546
Jens Axboe2b188cc2019-01-07 10:46:33 -07009547 min_complete = min(min_complete, ctx->cq_entries);
9548
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009549 /*
9550 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9551 * space applications don't need to do io completion events
9552 * polling again, they can rely on io_sq_thread to do polling
9553 * work, which can reduce cpu usage and uring_lock contention.
9554 */
9555 if (ctx->flags & IORING_SETUP_IOPOLL &&
9556 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009557 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009558 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009559 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009560 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009561 }
9562
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009563out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009564 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009565out_fput:
9566 fdput(f);
9567 return submitted ? submitted : ret;
9568}
9569
Tobias Klauserbebdb652020-02-26 18:38:32 +01009570#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009571static int io_uring_show_cred(int id, void *p, void *data)
9572{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009573 struct io_identity *iod = p;
9574 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009575 struct seq_file *m = data;
9576 struct user_namespace *uns = seq_user_ns(m);
9577 struct group_info *gi;
9578 kernel_cap_t cap;
9579 unsigned __capi;
9580 int g;
9581
9582 seq_printf(m, "%5d\n", id);
9583 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9584 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9585 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9586 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9587 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9588 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9589 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9590 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9591 seq_puts(m, "\n\tGroups:\t");
9592 gi = cred->group_info;
9593 for (g = 0; g < gi->ngroups; g++) {
9594 seq_put_decimal_ull(m, g ? " " : "",
9595 from_kgid_munged(uns, gi->gid[g]));
9596 }
9597 seq_puts(m, "\n\tCapEff:\t");
9598 cap = cred->cap_effective;
9599 CAP_FOR_EACH_U32(__capi)
9600 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9601 seq_putc(m, '\n');
9602 return 0;
9603}
9604
9605static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9606{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009607 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009608 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009609 int i;
9610
Jens Axboefad8e0d2020-09-28 08:57:48 -06009611 /*
9612 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9613 * since fdinfo case grabs it in the opposite direction of normal use
9614 * cases. If we fail to get the lock, we just don't iterate any
9615 * structures that could be going away outside the io_uring mutex.
9616 */
9617 has_lock = mutex_trylock(&ctx->uring_lock);
9618
Joseph Qidbbe9c62020-09-29 09:01:22 -06009619 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9620 sq = ctx->sq_data;
9621
9622 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9623 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009624 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009625 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009626 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009627
Jens Axboe87ce9552020-01-30 08:25:34 -07009628 if (f)
9629 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9630 else
9631 seq_printf(m, "%5u: <none>\n", i);
9632 }
9633 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009634 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009635 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9636
9637 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9638 (unsigned int) buf->len);
9639 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009640 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009641 seq_printf(m, "Personalities:\n");
9642 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9643 }
Jens Axboed7718a92020-02-14 22:23:12 -07009644 seq_printf(m, "PollList:\n");
9645 spin_lock_irq(&ctx->completion_lock);
9646 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9647 struct hlist_head *list = &ctx->cancel_hash[i];
9648 struct io_kiocb *req;
9649
9650 hlist_for_each_entry(req, list, hash_node)
9651 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9652 req->task->task_works != NULL);
9653 }
9654 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009655 if (has_lock)
9656 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009657}
9658
9659static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9660{
9661 struct io_ring_ctx *ctx = f->private_data;
9662
9663 if (percpu_ref_tryget(&ctx->refs)) {
9664 __io_uring_show_fdinfo(ctx, m);
9665 percpu_ref_put(&ctx->refs);
9666 }
9667}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009668#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009669
Jens Axboe2b188cc2019-01-07 10:46:33 -07009670static const struct file_operations io_uring_fops = {
9671 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009672 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009673 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009674#ifndef CONFIG_MMU
9675 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9676 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9677#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009678 .poll = io_uring_poll,
9679 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009680#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009681 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009682#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009683};
9684
9685static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9686 struct io_uring_params *p)
9687{
Hristo Venev75b28af2019-08-26 17:23:46 +00009688 struct io_rings *rings;
9689 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009690
Jens Axboebd740482020-08-05 12:58:23 -06009691 /* make sure these are sane, as we already accounted them */
9692 ctx->sq_entries = p->sq_entries;
9693 ctx->cq_entries = p->cq_entries;
9694
Hristo Venev75b28af2019-08-26 17:23:46 +00009695 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9696 if (size == SIZE_MAX)
9697 return -EOVERFLOW;
9698
9699 rings = io_mem_alloc(size);
9700 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009701 return -ENOMEM;
9702
Hristo Venev75b28af2019-08-26 17:23:46 +00009703 ctx->rings = rings;
9704 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9705 rings->sq_ring_mask = p->sq_entries - 1;
9706 rings->cq_ring_mask = p->cq_entries - 1;
9707 rings->sq_ring_entries = p->sq_entries;
9708 rings->cq_ring_entries = p->cq_entries;
9709 ctx->sq_mask = rings->sq_ring_mask;
9710 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009711
9712 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009713 if (size == SIZE_MAX) {
9714 io_mem_free(ctx->rings);
9715 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009716 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009717 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009718
9719 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009720 if (!ctx->sq_sqes) {
9721 io_mem_free(ctx->rings);
9722 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009723 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009724 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009725
Jens Axboe2b188cc2019-01-07 10:46:33 -07009726 return 0;
9727}
9728
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009729static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9730{
9731 int ret, fd;
9732
9733 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9734 if (fd < 0)
9735 return fd;
9736
9737 ret = io_uring_add_task_file(ctx, file);
9738 if (ret) {
9739 put_unused_fd(fd);
9740 return ret;
9741 }
9742 fd_install(fd, file);
9743 return fd;
9744}
9745
Jens Axboe2b188cc2019-01-07 10:46:33 -07009746/*
9747 * Allocate an anonymous fd, this is what constitutes the application
9748 * visible backing of an io_uring instance. The application mmaps this
9749 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9750 * we have to tie this fd to a socket for file garbage collection purposes.
9751 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009752static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009753{
9754 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009755#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009756 int ret;
9757
Jens Axboe2b188cc2019-01-07 10:46:33 -07009758 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9759 &ctx->ring_sock);
9760 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009761 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009762#endif
9763
Jens Axboe2b188cc2019-01-07 10:46:33 -07009764 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9765 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009766#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009767 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009768 sock_release(ctx->ring_sock);
9769 ctx->ring_sock = NULL;
9770 } else {
9771 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009772 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009773#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009774 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009775}
9776
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009777static int io_uring_create(unsigned entries, struct io_uring_params *p,
9778 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009779{
9780 struct user_struct *user = NULL;
9781 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009782 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009783 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009784 int ret;
9785
Jens Axboe8110c1a2019-12-28 15:39:54 -07009786 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009787 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009788 if (entries > IORING_MAX_ENTRIES) {
9789 if (!(p->flags & IORING_SETUP_CLAMP))
9790 return -EINVAL;
9791 entries = IORING_MAX_ENTRIES;
9792 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009793
9794 /*
9795 * Use twice as many entries for the CQ ring. It's possible for the
9796 * application to drive a higher depth than the size of the SQ ring,
9797 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009798 * some flexibility in overcommitting a bit. If the application has
9799 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9800 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009801 */
9802 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009803 if (p->flags & IORING_SETUP_CQSIZE) {
9804 /*
9805 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9806 * to a power-of-two, if it isn't already. We do NOT impose
9807 * any cq vs sq ring sizing.
9808 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009809 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009810 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009811 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9812 if (!(p->flags & IORING_SETUP_CLAMP))
9813 return -EINVAL;
9814 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9815 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009816 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9817 if (p->cq_entries < p->sq_entries)
9818 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009819 } else {
9820 p->cq_entries = 2 * p->sq_entries;
9821 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009822
9823 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009824 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009825
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009826 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009827 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009828 ring_pages(p->sq_entries, p->cq_entries));
9829 if (ret) {
9830 free_uid(user);
9831 return ret;
9832 }
9833 }
9834
9835 ctx = io_ring_ctx_alloc(p);
9836 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009837 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009838 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009839 p->cq_entries));
9840 free_uid(user);
9841 return -ENOMEM;
9842 }
9843 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009844 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009845 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009846#ifdef CONFIG_AUDIT
9847 ctx->loginuid = current->loginuid;
9848 ctx->sessionid = current->sessionid;
9849#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009850 ctx->sqo_task = get_task_struct(current);
9851
9852 /*
9853 * This is just grabbed for accounting purposes. When a process exits,
9854 * the mm is exited and dropped before the files, hence we need to hang
9855 * on to this mm purely for the purposes of being able to unaccount
9856 * memory (locked/pinned vm). It's not used for anything else.
9857 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009858 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009859 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009860
Dennis Zhou91d8f512020-09-16 13:41:05 -07009861#ifdef CONFIG_BLK_CGROUP
9862 /*
9863 * The sq thread will belong to the original cgroup it was inited in.
9864 * If the cgroup goes offline (e.g. disabling the io controller), then
9865 * issued bios will be associated with the closest cgroup later in the
9866 * block layer.
9867 */
9868 rcu_read_lock();
9869 ctx->sqo_blkcg_css = blkcg_css();
9870 ret = css_tryget_online(ctx->sqo_blkcg_css);
9871 rcu_read_unlock();
9872 if (!ret) {
9873 /* don't init against a dying cgroup, have the user try again */
9874 ctx->sqo_blkcg_css = NULL;
9875 ret = -ENODEV;
9876 goto err;
9877 }
9878#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009879
Jens Axboe2b188cc2019-01-07 10:46:33 -07009880 /*
9881 * Account memory _before_ installing the file descriptor. Once
9882 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009883 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009884 * will un-account as well.
9885 */
9886 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9887 ACCT_LOCKED);
9888 ctx->limit_mem = limit_mem;
9889
9890 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009891 if (ret)
9892 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009893
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009894 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009895 if (ret)
9896 goto err;
9897
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009898 if (!(p->flags & IORING_SETUP_R_DISABLED))
9899 io_sq_offload_start(ctx);
9900
Jens Axboe2b188cc2019-01-07 10:46:33 -07009901 memset(&p->sq_off, 0, sizeof(p->sq_off));
9902 p->sq_off.head = offsetof(struct io_rings, sq.head);
9903 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9904 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9905 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9906 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9907 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9908 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9909
9910 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009911 p->cq_off.head = offsetof(struct io_rings, cq.head);
9912 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9913 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9914 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9915 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9916 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009917 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009918
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009919 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9920 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009921 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009922 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9923 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009924
9925 if (copy_to_user(params, p, sizeof(*p))) {
9926 ret = -EFAULT;
9927 goto err;
9928 }
Jens Axboed1719f72020-07-30 13:43:53 -06009929
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009930 file = io_uring_get_file(ctx);
9931 if (IS_ERR(file)) {
9932 ret = PTR_ERR(file);
9933 goto err;
9934 }
9935
Jens Axboed1719f72020-07-30 13:43:53 -06009936 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009937 * Install ring fd as the very last thing, so we don't risk someone
9938 * having closed it before we finish setup
9939 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009940 ret = io_uring_install_fd(ctx, file);
9941 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009942 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009943 /* fput will clean it up */
9944 fput(file);
9945 return ret;
9946 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009947
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009948 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009949 return ret;
9950err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009951 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009952 io_ring_ctx_wait_and_kill(ctx);
9953 return ret;
9954}
9955
9956/*
9957 * Sets up an aio uring context, and returns the fd. Applications asks for a
9958 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9959 * params structure passed in.
9960 */
9961static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9962{
9963 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009964 int i;
9965
9966 if (copy_from_user(&p, params, sizeof(p)))
9967 return -EFAULT;
9968 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9969 if (p.resv[i])
9970 return -EINVAL;
9971 }
9972
Jens Axboe6c271ce2019-01-10 11:22:30 -07009973 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009974 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009975 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9976 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009977 return -EINVAL;
9978
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009979 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009980}
9981
9982SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9983 struct io_uring_params __user *, params)
9984{
9985 return io_uring_setup(entries, params);
9986}
9987
Jens Axboe66f4af92020-01-16 15:36:52 -07009988static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9989{
9990 struct io_uring_probe *p;
9991 size_t size;
9992 int i, ret;
9993
9994 size = struct_size(p, ops, nr_args);
9995 if (size == SIZE_MAX)
9996 return -EOVERFLOW;
9997 p = kzalloc(size, GFP_KERNEL);
9998 if (!p)
9999 return -ENOMEM;
10000
10001 ret = -EFAULT;
10002 if (copy_from_user(p, arg, size))
10003 goto out;
10004 ret = -EINVAL;
10005 if (memchr_inv(p, 0, size))
10006 goto out;
10007
10008 p->last_op = IORING_OP_LAST - 1;
10009 if (nr_args > IORING_OP_LAST)
10010 nr_args = IORING_OP_LAST;
10011
10012 for (i = 0; i < nr_args; i++) {
10013 p->ops[i].op = i;
10014 if (!io_op_defs[i].not_supported)
10015 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10016 }
10017 p->ops_len = i;
10018
10019 ret = 0;
10020 if (copy_to_user(arg, p, size))
10021 ret = -EFAULT;
10022out:
10023 kfree(p);
10024 return ret;
10025}
10026
Jens Axboe071698e2020-01-28 10:04:42 -070010027static int io_register_personality(struct io_ring_ctx *ctx)
10028{
Jens Axboe1e6fa522020-10-15 08:46:24 -060010029 struct io_identity *id;
10030 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010031
Jens Axboe1e6fa522020-10-15 08:46:24 -060010032 id = kmalloc(sizeof(*id), GFP_KERNEL);
10033 if (unlikely(!id))
10034 return -ENOMEM;
10035
10036 io_init_identity(id);
10037 id->creds = get_current_cred();
10038
10039 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
10040 if (ret < 0) {
10041 put_cred(id->creds);
10042 kfree(id);
10043 }
10044 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010045}
10046
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010047static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10048 unsigned int nr_args)
10049{
10050 struct io_uring_restriction *res;
10051 size_t size;
10052 int i, ret;
10053
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010054 /* Restrictions allowed only if rings started disabled */
10055 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10056 return -EBADFD;
10057
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010058 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010059 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010060 return -EBUSY;
10061
10062 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10063 return -EINVAL;
10064
10065 size = array_size(nr_args, sizeof(*res));
10066 if (size == SIZE_MAX)
10067 return -EOVERFLOW;
10068
10069 res = memdup_user(arg, size);
10070 if (IS_ERR(res))
10071 return PTR_ERR(res);
10072
10073 ret = 0;
10074
10075 for (i = 0; i < nr_args; i++) {
10076 switch (res[i].opcode) {
10077 case IORING_RESTRICTION_REGISTER_OP:
10078 if (res[i].register_op >= IORING_REGISTER_LAST) {
10079 ret = -EINVAL;
10080 goto out;
10081 }
10082
10083 __set_bit(res[i].register_op,
10084 ctx->restrictions.register_op);
10085 break;
10086 case IORING_RESTRICTION_SQE_OP:
10087 if (res[i].sqe_op >= IORING_OP_LAST) {
10088 ret = -EINVAL;
10089 goto out;
10090 }
10091
10092 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10093 break;
10094 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10095 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10096 break;
10097 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10098 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10099 break;
10100 default:
10101 ret = -EINVAL;
10102 goto out;
10103 }
10104 }
10105
10106out:
10107 /* Reset all restrictions if an error happened */
10108 if (ret != 0)
10109 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10110 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010111 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010112
10113 kfree(res);
10114 return ret;
10115}
10116
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010117static int io_register_enable_rings(struct io_ring_ctx *ctx)
10118{
10119 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10120 return -EBADFD;
10121
10122 if (ctx->restrictions.registered)
10123 ctx->restricted = 1;
10124
10125 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10126
10127 io_sq_offload_start(ctx);
10128
10129 return 0;
10130}
10131
Jens Axboe071698e2020-01-28 10:04:42 -070010132static bool io_register_op_must_quiesce(int op)
10133{
10134 switch (op) {
10135 case IORING_UNREGISTER_FILES:
10136 case IORING_REGISTER_FILES_UPDATE:
10137 case IORING_REGISTER_PROBE:
10138 case IORING_REGISTER_PERSONALITY:
10139 case IORING_UNREGISTER_PERSONALITY:
10140 return false;
10141 default:
10142 return true;
10143 }
10144}
10145
Jens Axboeedafcce2019-01-09 09:16:05 -070010146static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10147 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010148 __releases(ctx->uring_lock)
10149 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010150{
10151 int ret;
10152
Jens Axboe35fa71a2019-04-22 10:23:23 -060010153 /*
10154 * We're inside the ring mutex, if the ref is already dying, then
10155 * someone else killed the ctx or is already going through
10156 * io_uring_register().
10157 */
10158 if (percpu_ref_is_dying(&ctx->refs))
10159 return -ENXIO;
10160
Jens Axboe071698e2020-01-28 10:04:42 -070010161 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010162 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010163
Jens Axboe05f3fb32019-12-09 11:22:50 -070010164 /*
10165 * Drop uring mutex before waiting for references to exit. If
10166 * another thread is currently inside io_uring_enter() it might
10167 * need to grab the uring_lock to make progress. If we hold it
10168 * here across the drain wait, then we can deadlock. It's safe
10169 * to drop the mutex here, since no new references will come in
10170 * after we've killed the percpu ref.
10171 */
10172 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010173 do {
10174 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10175 if (!ret)
10176 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010177 ret = io_run_task_work_sig();
10178 if (ret < 0)
10179 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010180 } while (1);
10181
Jens Axboe05f3fb32019-12-09 11:22:50 -070010182 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010183
Jens Axboec1503682020-01-08 08:26:07 -070010184 if (ret) {
10185 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010186 goto out_quiesce;
10187 }
10188 }
10189
10190 if (ctx->restricted) {
10191 if (opcode >= IORING_REGISTER_LAST) {
10192 ret = -EINVAL;
10193 goto out;
10194 }
10195
10196 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10197 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010198 goto out;
10199 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010200 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010201
10202 switch (opcode) {
10203 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010204 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010205 break;
10206 case IORING_UNREGISTER_BUFFERS:
10207 ret = -EINVAL;
10208 if (arg || nr_args)
10209 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010210 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010211 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010212 case IORING_REGISTER_FILES:
10213 ret = io_sqe_files_register(ctx, arg, nr_args);
10214 break;
10215 case IORING_UNREGISTER_FILES:
10216 ret = -EINVAL;
10217 if (arg || nr_args)
10218 break;
10219 ret = io_sqe_files_unregister(ctx);
10220 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010221 case IORING_REGISTER_FILES_UPDATE:
10222 ret = io_sqe_files_update(ctx, arg, nr_args);
10223 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010224 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010225 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010226 ret = -EINVAL;
10227 if (nr_args != 1)
10228 break;
10229 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010230 if (ret)
10231 break;
10232 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10233 ctx->eventfd_async = 1;
10234 else
10235 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010236 break;
10237 case IORING_UNREGISTER_EVENTFD:
10238 ret = -EINVAL;
10239 if (arg || nr_args)
10240 break;
10241 ret = io_eventfd_unregister(ctx);
10242 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010243 case IORING_REGISTER_PROBE:
10244 ret = -EINVAL;
10245 if (!arg || nr_args > 256)
10246 break;
10247 ret = io_probe(ctx, arg, nr_args);
10248 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010249 case IORING_REGISTER_PERSONALITY:
10250 ret = -EINVAL;
10251 if (arg || nr_args)
10252 break;
10253 ret = io_register_personality(ctx);
10254 break;
10255 case IORING_UNREGISTER_PERSONALITY:
10256 ret = -EINVAL;
10257 if (arg)
10258 break;
10259 ret = io_unregister_personality(ctx, nr_args);
10260 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010261 case IORING_REGISTER_ENABLE_RINGS:
10262 ret = -EINVAL;
10263 if (arg || nr_args)
10264 break;
10265 ret = io_register_enable_rings(ctx);
10266 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010267 case IORING_REGISTER_RESTRICTIONS:
10268 ret = io_register_restrictions(ctx, arg, nr_args);
10269 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010270 default:
10271 ret = -EINVAL;
10272 break;
10273 }
10274
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010275out:
Jens Axboe071698e2020-01-28 10:04:42 -070010276 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010277 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010278 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010279out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010280 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010281 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010282 return ret;
10283}
10284
10285SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10286 void __user *, arg, unsigned int, nr_args)
10287{
10288 struct io_ring_ctx *ctx;
10289 long ret = -EBADF;
10290 struct fd f;
10291
10292 f = fdget(fd);
10293 if (!f.file)
10294 return -EBADF;
10295
10296 ret = -EOPNOTSUPP;
10297 if (f.file->f_op != &io_uring_fops)
10298 goto out_fput;
10299
10300 ctx = f.file->private_data;
10301
10302 mutex_lock(&ctx->uring_lock);
10303 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10304 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010305 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10306 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010307out_fput:
10308 fdput(f);
10309 return ret;
10310}
10311
Jens Axboe2b188cc2019-01-07 10:46:33 -070010312static int __init io_uring_init(void)
10313{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010314#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10315 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10316 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10317} while (0)
10318
10319#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10320 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10321 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10322 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10323 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10324 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10325 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10326 BUILD_BUG_SQE_ELEM(8, __u64, off);
10327 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10328 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010329 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010330 BUILD_BUG_SQE_ELEM(24, __u32, len);
10331 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10332 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10333 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10334 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010335 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10336 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010337 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10338 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10339 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10340 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10341 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10342 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10343 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10344 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010345 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010346 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10347 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10348 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010349 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010350
Jens Axboed3656342019-12-18 09:50:26 -070010351 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010352 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010353 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10354 return 0;
10355};
10356__initcall(io_uring_init);