blob: 6bce9094280cd78610f9b97cc66f530abcf6353f [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
268
269struct io_comp_state {
270 unsigned int nr;
271 struct list_head list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000272};
273
274struct io_submit_state {
275 struct blk_plug plug;
276
277 /*
278 * io_kiocb alloc cache
279 */
280 void *reqs[IO_IOPOLL_BATCH];
281 unsigned int free_reqs;
282
283 bool plug_started;
284
285 /*
286 * Batch completion logic
287 */
288 struct io_comp_state comp;
289
290 /*
291 * File reference cache
292 */
293 struct file *file;
294 unsigned int fd;
295 unsigned int file_refs;
296 unsigned int ios_left;
297};
298
Jens Axboe2b188cc2019-01-07 10:46:33 -0700299struct io_ring_ctx {
300 struct {
301 struct percpu_ref refs;
302 } ____cacheline_aligned_in_smp;
303
304 struct {
305 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800306 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700307 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800308 unsigned int cq_overflow_flushed: 1;
309 unsigned int drain_next: 1;
310 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200311 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000312 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
Hristo Venev75b28af2019-08-26 17:23:46 +0000314 /*
315 * Ring buffer of indices into array of io_uring_sqe, which is
316 * mmapped by the application using the IORING_OFF_SQES offset.
317 *
318 * This indirection could e.g. be used to assign fixed
319 * io_uring_sqe entries to operations and only submit them to
320 * the queue when needed.
321 *
322 * The kernel modifies neither the indices array nor the entries
323 * array.
324 */
325 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326 unsigned cached_sq_head;
327 unsigned sq_entries;
328 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700329 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600330 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100331 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700332 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600333
334 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600335 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700336 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700337
Jens Axboead3eb2c2019-12-18 17:12:20 -0700338 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339 } ____cacheline_aligned_in_smp;
340
Hristo Venev75b28af2019-08-26 17:23:46 +0000341 struct io_rings *rings;
342
Jens Axboe2b188cc2019-01-07 10:46:33 -0700343 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600344 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600345
346 /*
347 * For SQPOLL usage - we hold a reference to the parent task, so we
348 * have access to the ->files
349 */
350 struct task_struct *sqo_task;
351
352 /* Only used for accounting purposes */
353 struct mm_struct *mm_account;
354
Dennis Zhou91d8f512020-09-16 13:41:05 -0700355#ifdef CONFIG_BLK_CGROUP
356 struct cgroup_subsys_state *sqo_blkcg_css;
357#endif
358
Jens Axboe534ca6d2020-09-02 13:52:19 -0600359 struct io_sq_data *sq_data; /* if using sq thread polling */
360
Jens Axboe90554202020-09-03 12:12:41 -0600361 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600362 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363
Jens Axboe6b063142019-01-10 22:13:58 -0700364 /*
365 * If used, fixed file set. Writers must ensure that ->refs is dead,
366 * readers must ensure that ->refs is alive as long as the file* is
367 * used. Only updated through io_uring_register(2).
368 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000369 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700370 unsigned nr_user_files;
371
Jens Axboeedafcce2019-01-09 09:16:05 -0700372 /* if used, fixed mapped user buffers */
373 unsigned nr_user_bufs;
374 struct io_mapped_ubuf *user_bufs;
375
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 struct user_struct *user;
377
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700378 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700379
Jens Axboe4ea33a92020-10-15 13:46:44 -0600380#ifdef CONFIG_AUDIT
381 kuid_t loginuid;
382 unsigned int sessionid;
383#endif
384
Jens Axboe0f158b42020-05-14 17:18:39 -0600385 struct completion ref_comp;
386 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700387
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700388 /* if all else fails... */
389 struct io_kiocb *fallback_req;
390
Jens Axboe206aefd2019-11-07 18:27:42 -0700391#if defined(CONFIG_UNIX)
392 struct socket *ring_sock;
393#endif
394
Jens Axboe5a2e7452020-02-23 16:23:11 -0700395 struct idr io_buffer_idr;
396
Jens Axboe071698e2020-01-28 10:04:42 -0700397 struct idr personality_idr;
398
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct {
400 unsigned cached_cq_tail;
401 unsigned cq_entries;
402 unsigned cq_mask;
403 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500404 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700405 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700406 struct wait_queue_head cq_wait;
407 struct fasync_struct *cq_fasync;
408 struct eventfd_ctx *cq_ev_fd;
409 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410
411 struct {
412 struct mutex uring_lock;
413 wait_queue_head_t wait;
414 } ____cacheline_aligned_in_smp;
415
416 struct {
417 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700418
Jens Axboedef596e2019-01-09 08:59:42 -0700419 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300420 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700421 * io_uring instances that don't use IORING_SETUP_SQPOLL.
422 * For SQPOLL, only the single threaded io_sq_thread() will
423 * manipulate the list, hence no extra locking is needed there.
424 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300425 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700426 struct hlist_head *cancel_hash;
427 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700428 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600429
430 spinlock_t inflight_lock;
431 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600433
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000434 struct delayed_work rsrc_put_work;
435 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000436 struct list_head rsrc_ref_list;
437 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600438
Jens Axboe85faa7b2020-04-09 18:14:00 -0600439 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200440 struct io_restriction restrictions;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000441 struct io_submit_state submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442};
443
Jens Axboe09bb8392019-03-13 12:39:28 -0600444/*
445 * First field must be the file pointer in all the
446 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
447 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700448struct io_poll_iocb {
449 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000450 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700451 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600452 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700453 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700454 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700455};
456
Pavel Begunkov018043b2020-10-27 23:17:18 +0000457struct io_poll_remove {
458 struct file *file;
459 u64 addr;
460};
461
Jens Axboeb5dba592019-12-11 14:02:38 -0700462struct io_close {
463 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700464 int fd;
465};
466
Jens Axboead8a48a2019-11-15 08:49:11 -0700467struct io_timeout_data {
468 struct io_kiocb *req;
469 struct hrtimer timer;
470 struct timespec64 ts;
471 enum hrtimer_mode mode;
472};
473
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700474struct io_accept {
475 struct file *file;
476 struct sockaddr __user *addr;
477 int __user *addr_len;
478 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600479 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700480};
481
482struct io_sync {
483 struct file *file;
484 loff_t len;
485 loff_t off;
486 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700487 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700488};
489
Jens Axboefbf23842019-12-17 18:45:56 -0700490struct io_cancel {
491 struct file *file;
492 u64 addr;
493};
494
Jens Axboeb29472e2019-12-17 18:50:29 -0700495struct io_timeout {
496 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300497 u32 off;
498 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300499 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000500 /* head of the link, used by linked timeouts only */
501 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700502};
503
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100504struct io_timeout_rem {
505 struct file *file;
506 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000507
508 /* timeout update */
509 struct timespec64 ts;
510 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100511};
512
Jens Axboe9adbd452019-12-20 08:45:55 -0700513struct io_rw {
514 /* NOTE: kiocb has the file as the first member, so don't do it here */
515 struct kiocb kiocb;
516 u64 addr;
517 u64 len;
518};
519
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700520struct io_connect {
521 struct file *file;
522 struct sockaddr __user *addr;
523 int addr_len;
524};
525
Jens Axboee47293f2019-12-20 08:58:21 -0700526struct io_sr_msg {
527 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700528 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300529 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700530 void __user *buf;
531 };
Jens Axboee47293f2019-12-20 08:58:21 -0700532 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700533 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700534 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700535 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700536};
537
Jens Axboe15b71ab2019-12-11 11:20:36 -0700538struct io_open {
539 struct file *file;
540 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700541 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700542 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600543 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700544};
545
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000546struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700547 struct file *file;
548 u64 arg;
549 u32 nr_args;
550 u32 offset;
551};
552
Jens Axboe4840e412019-12-25 22:03:45 -0700553struct io_fadvise {
554 struct file *file;
555 u64 offset;
556 u32 len;
557 u32 advice;
558};
559
Jens Axboec1ca7572019-12-25 22:18:28 -0700560struct io_madvise {
561 struct file *file;
562 u64 addr;
563 u32 len;
564 u32 advice;
565};
566
Jens Axboe3e4827b2020-01-08 15:18:09 -0700567struct io_epoll {
568 struct file *file;
569 int epfd;
570 int op;
571 int fd;
572 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573};
574
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300575struct io_splice {
576 struct file *file_out;
577 struct file *file_in;
578 loff_t off_out;
579 loff_t off_in;
580 u64 len;
581 unsigned int flags;
582};
583
Jens Axboeddf0322d2020-02-23 16:41:33 -0700584struct io_provide_buf {
585 struct file *file;
586 __u64 addr;
587 __s32 len;
588 __u32 bgid;
589 __u16 nbufs;
590 __u16 bid;
591};
592
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700593struct io_statx {
594 struct file *file;
595 int dfd;
596 unsigned int mask;
597 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700598 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700599 struct statx __user *buffer;
600};
601
Jens Axboe36f4fa62020-09-05 11:14:22 -0600602struct io_shutdown {
603 struct file *file;
604 int how;
605};
606
Jens Axboe80a261f2020-09-28 14:23:58 -0600607struct io_rename {
608 struct file *file;
609 int old_dfd;
610 int new_dfd;
611 struct filename *oldpath;
612 struct filename *newpath;
613 int flags;
614};
615
Jens Axboe14a11432020-09-28 14:27:37 -0600616struct io_unlink {
617 struct file *file;
618 int dfd;
619 int flags;
620 struct filename *filename;
621};
622
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300623struct io_completion {
624 struct file *file;
625 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300626 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300627};
628
Jens Axboef499a022019-12-02 16:28:46 -0700629struct io_async_connect {
630 struct sockaddr_storage address;
631};
632
Jens Axboe03b12302019-12-02 18:50:25 -0700633struct io_async_msghdr {
634 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000635 /* points to an allocated iov, if NULL we use fast_iov instead */
636 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700637 struct sockaddr __user *uaddr;
638 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700639 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700640};
641
Jens Axboef67676d2019-12-02 11:03:47 -0700642struct io_async_rw {
643 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600644 const struct iovec *free_iovec;
645 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600646 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600647 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700648};
649
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300650enum {
651 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
652 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
653 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
654 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
655 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700656 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300657
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300658 REQ_F_FAIL_LINK_BIT,
659 REQ_F_INFLIGHT_BIT,
660 REQ_F_CUR_POS_BIT,
661 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300662 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300663 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300664 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700665 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700666 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600667 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800668 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100669 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000670 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700671
672 /* not a real bit, just to check we're not overflowing the space */
673 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300674};
675
676enum {
677 /* ctx owns file */
678 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
679 /* drain existing IO first */
680 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
681 /* linked sqes */
682 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
683 /* doesn't sever on completion < 0 */
684 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
685 /* IOSQE_ASYNC */
686 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700687 /* IOSQE_BUFFER_SELECT */
688 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300689
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300690 /* fail rest of links */
691 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
692 /* on inflight list */
693 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
694 /* read/write uses file position */
695 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
696 /* must not punt to workers */
697 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100698 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300699 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300700 /* regular file */
701 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300702 /* needs cleanup */
703 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700704 /* already went through poll handler */
705 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700706 /* buffer already selected */
707 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600708 /* doesn't need file table for this request */
709 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800710 /* io_wq_work is initialized */
711 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100712 /* linked timeout is active, i.e. prepared by link's head */
713 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000714 /* completion is deferred through io_comp_state */
715 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700716};
717
718struct async_poll {
719 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600720 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721};
722
Jens Axboe09bb8392019-03-13 12:39:28 -0600723/*
724 * NOTE! Each of the iocb union members has the file pointer
725 * as the first entry in their struct definition. So you can
726 * access the file pointer through any of the sub-structs,
727 * or directly as just 'ki_filp' in this struct.
728 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700730 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600731 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700732 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700733 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000734 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700735 struct io_accept accept;
736 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700737 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700738 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100739 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700740 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700741 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700742 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700743 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000744 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700745 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700746 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700747 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300748 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700749 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700750 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600751 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600752 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600753 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300754 /* use only after cleaning per-op data, see io_clean_op() */
755 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700756 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* opcode allocated if it needs to store data for async defer */
759 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700760 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800761 /* polled IO has completed */
762 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700763
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700764 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300765 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700766
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300767 struct io_ring_ctx *ctx;
768 unsigned int flags;
769 refcount_t refs;
770 struct task_struct *task;
771 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000773 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000774 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700775
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300776 /*
777 * 1. used with ctx->iopoll_list with reads/writes
778 * 2. to track reqs with ->files (see io_op_def::file_table)
779 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300780 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300781 struct callback_head task_work;
782 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
783 struct hlist_node hash_node;
784 struct async_poll *apoll;
785 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786};
787
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300788struct io_defer_entry {
789 struct list_head list;
790 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300791 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300792};
793
Jens Axboed3656342019-12-18 09:50:26 -0700794struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700795 /* needs req->file assigned */
796 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700797 /* hash wq insertion if file is a regular file */
798 unsigned hash_reg_file : 1;
799 /* unbound wq insertion if file is a non-regular file */
800 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700801 /* opcode is not supported by this kernel */
802 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700803 /* set if opcode supports polled "wait" */
804 unsigned pollin : 1;
805 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700806 /* op supports buffer selection */
807 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700808 /* must always have async data allocated */
809 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600810 /* should block plug */
811 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700812 /* size of async data needed, if any */
813 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600814 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700815};
816
Jens Axboe09186822020-10-13 15:01:40 -0600817static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_NOP] = {},
819 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700820 .needs_file = 1,
821 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700822 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700823 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600825 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700826 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600827 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700830 .needs_file = 1,
831 .hash_reg_file = 1,
832 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700834 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600835 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700836 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600837 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
838 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600842 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700845 .needs_file = 1,
846 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700847 .pollin = 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 Axboe4017eb92020-10-22 14:14:12 -0600850 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700853 .needs_file = 1,
854 .hash_reg_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600857 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700858 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600859 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
860 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
864 .unbound_nonreg_file = 1,
865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_POLL_REMOVE] = {},
867 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600869 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700875 .needs_async_data = 1,
876 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000877 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700880 .needs_file = 1,
881 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700882 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700883 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700884 .needs_async_data = 1,
885 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000886 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700889 .needs_async_data = 1,
890 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600891 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700892 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000893 [IORING_OP_TIMEOUT_REMOVE] = {
894 /* used by timeout updates' prep() */
895 .work_flags = IO_WQ_WORK_MM,
896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700898 .needs_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700900 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600901 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_ASYNC_CANCEL] = {},
904 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600907 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700908 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300909 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700910 .needs_file = 1,
911 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700912 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700913 .needs_async_data = 1,
914 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600915 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700918 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600919 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600922 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600923 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700924 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600926 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600929 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700930 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300931 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600932 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
933 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700939 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600942 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700945 .needs_file = 1,
946 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700947 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600948 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700949 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600950 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
951 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700954 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600955 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600958 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700961 .needs_file = 1,
962 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700963 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600964 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700967 .needs_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700969 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700970 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600971 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600974 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600975 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700976 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700977 [IORING_OP_EPOLL_CTL] = {
978 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600979 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700980 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300981 [IORING_OP_SPLICE] = {
982 .needs_file = 1,
983 .hash_reg_file = 1,
984 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600985 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700986 },
987 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700988 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300989 [IORING_OP_TEE] = {
990 .needs_file = 1,
991 .hash_reg_file = 1,
992 .unbound_nonreg_file = 1,
993 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600994 [IORING_OP_SHUTDOWN] = {
995 .needs_file = 1,
996 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600997 [IORING_OP_RENAMEAT] = {
998 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
999 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1000 },
Jens Axboe14a11432020-09-28 14:27:37 -06001001 [IORING_OP_UNLINKAT] = {
1002 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1003 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1004 },
Jens Axboed3656342019-12-18 09:50:26 -07001005};
1006
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001007enum io_mem_account {
1008 ACCT_LOCKED,
1009 ACCT_PINNED,
1010};
1011
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001012static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1013 struct task_struct *task,
1014 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001015static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001016static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001017 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001018static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1019 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001020
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001021static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001022 unsigned int issue_flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001023static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001024static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001025static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001026static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001027static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001028static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001029static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001030static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001031 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001032 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001033static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001034static struct file *io_file_get(struct io_submit_state *state,
1035 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001036static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001037static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001038
Pavel Begunkov847595d2021-02-04 13:52:06 +00001039static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1040 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001041static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1042 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001043 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001044static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001045
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046static struct kmem_cache *req_cachep;
1047
Jens Axboe09186822020-10-13 15:01:40 -06001048static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049
1050struct sock *io_uring_get_socket(struct file *file)
1051{
1052#if defined(CONFIG_UNIX)
1053 if (file->f_op == &io_uring_fops) {
1054 struct io_ring_ctx *ctx = file->private_data;
1055
1056 return ctx->ring_sock->sk;
1057 }
1058#endif
1059 return NULL;
1060}
1061EXPORT_SYMBOL(io_uring_get_socket);
1062
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001063#define io_for_each_link(pos, head) \
1064 for (pos = (head); pos; pos = pos->link)
1065
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001066static inline void io_clean_op(struct io_kiocb *req)
1067{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001068 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001069 __io_clean_op(req);
1070}
1071
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001072static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001073{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001074 struct io_ring_ctx *ctx = req->ctx;
1075
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001076 if (!req->fixed_rsrc_refs) {
1077 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1078 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001079 }
1080}
1081
Pavel Begunkov08d23632020-11-06 13:00:22 +00001082static bool io_match_task(struct io_kiocb *head,
1083 struct task_struct *task,
1084 struct files_struct *files)
1085{
1086 struct io_kiocb *req;
1087
Jens Axboe84965ff2021-01-23 15:51:11 -07001088 if (task && head->task != task) {
1089 /* in terms of cancelation, always match if req task is dead */
1090 if (head->task->flags & PF_EXITING)
1091 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001092 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001093 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001094 if (!files)
1095 return true;
1096
1097 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001098 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1099 continue;
1100 if (req->file && req->file->f_op == &io_uring_fops)
1101 return true;
1102 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001103 req->work.identity->files == files)
1104 return true;
1105 }
1106 return false;
1107}
1108
Jens Axboe28cea78a2020-09-14 10:51:17 -06001109static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001110{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001111 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001112 struct mm_struct *mm = current->mm;
1113
1114 if (mm) {
1115 kthread_unuse_mm(mm);
1116 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001117 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001118 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001119 if (files) {
1120 struct nsproxy *nsproxy = current->nsproxy;
1121
1122 task_lock(current);
1123 current->files = NULL;
1124 current->nsproxy = NULL;
1125 task_unlock(current);
1126 put_files_struct(files);
1127 put_nsproxy(nsproxy);
1128 }
1129}
1130
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001131static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001132{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001133 if (current->flags & PF_EXITING)
1134 return -EFAULT;
1135
Jens Axboe28cea78a2020-09-14 10:51:17 -06001136 if (!current->files) {
1137 struct files_struct *files;
1138 struct nsproxy *nsproxy;
1139
1140 task_lock(ctx->sqo_task);
1141 files = ctx->sqo_task->files;
1142 if (!files) {
1143 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001144 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001145 }
1146 atomic_inc(&files->count);
1147 get_nsproxy(ctx->sqo_task->nsproxy);
1148 nsproxy = ctx->sqo_task->nsproxy;
1149 task_unlock(ctx->sqo_task);
1150
1151 task_lock(current);
1152 current->files = files;
1153 current->nsproxy = nsproxy;
1154 task_unlock(current);
1155 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001156 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001157}
1158
1159static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1160{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001161 struct mm_struct *mm;
1162
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001163 if (current->flags & PF_EXITING)
1164 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001165 if (current->mm)
1166 return 0;
1167
1168 /* Should never happen */
1169 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1170 return -EFAULT;
1171
1172 task_lock(ctx->sqo_task);
1173 mm = ctx->sqo_task->mm;
1174 if (unlikely(!mm || !mmget_not_zero(mm)))
1175 mm = NULL;
1176 task_unlock(ctx->sqo_task);
1177
1178 if (mm) {
1179 kthread_use_mm(mm);
1180 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001181 }
1182
Jens Axboe4b70cf92020-11-02 10:39:05 -07001183 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001184}
1185
Jens Axboe28cea78a2020-09-14 10:51:17 -06001186static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1187 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001188{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001189 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001190 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001191
1192 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001193 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001194 if (unlikely(ret))
1195 return ret;
1196 }
1197
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001198 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1199 ret = __io_sq_thread_acquire_files(ctx);
1200 if (unlikely(ret))
1201 return ret;
1202 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001203
1204 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001205}
1206
Dennis Zhou91d8f512020-09-16 13:41:05 -07001207static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1208 struct cgroup_subsys_state **cur_css)
1209
1210{
1211#ifdef CONFIG_BLK_CGROUP
1212 /* puts the old one when swapping */
1213 if (*cur_css != ctx->sqo_blkcg_css) {
1214 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1215 *cur_css = ctx->sqo_blkcg_css;
1216 }
1217#endif
1218}
1219
1220static void io_sq_thread_unassociate_blkcg(void)
1221{
1222#ifdef CONFIG_BLK_CGROUP
1223 kthread_associate_blkcg(NULL);
1224#endif
1225}
1226
Jens Axboec40f6372020-06-25 15:39:59 -06001227static inline void req_set_fail_links(struct io_kiocb *req)
1228{
1229 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1230 req->flags |= REQ_F_FAIL_LINK;
1231}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001232
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001233/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001234 * None of these are dereferenced, they are simply used to check if any of
1235 * them have changed. If we're under current and check they are still the
1236 * same, we're fine to grab references to them for actual out-of-line use.
1237 */
1238static void io_init_identity(struct io_identity *id)
1239{
1240 id->files = current->files;
1241 id->mm = current->mm;
1242#ifdef CONFIG_BLK_CGROUP
1243 rcu_read_lock();
1244 id->blkcg_css = blkcg_css();
1245 rcu_read_unlock();
1246#endif
1247 id->creds = current_cred();
1248 id->nsproxy = current->nsproxy;
1249 id->fs = current->fs;
1250 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001251#ifdef CONFIG_AUDIT
1252 id->loginuid = current->loginuid;
1253 id->sessionid = current->sessionid;
1254#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001255 refcount_set(&id->count, 1);
1256}
1257
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001258static inline void __io_req_init_async(struct io_kiocb *req)
1259{
1260 memset(&req->work, 0, sizeof(req->work));
1261 req->flags |= REQ_F_WORK_INITIALIZED;
1262}
1263
Jens Axboe1e6fa522020-10-15 08:46:24 -06001264/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001265 * Note: must call io_req_init_async() for the first time you
1266 * touch any members of io_wq_work.
1267 */
1268static inline void io_req_init_async(struct io_kiocb *req)
1269{
Jens Axboe500a3732020-10-15 17:38:03 -06001270 struct io_uring_task *tctx = current->io_uring;
1271
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001272 if (req->flags & REQ_F_WORK_INITIALIZED)
1273 return;
1274
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001275 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001276
1277 /* Grab a ref if this isn't our static identity */
1278 req->work.identity = tctx->identity;
1279 if (tctx->identity != &tctx->__identity)
1280 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001281}
1282
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1284{
1285 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1286
Jens Axboe0f158b42020-05-14 17:18:39 -06001287 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288}
1289
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001290static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1291{
1292 return !req->timeout.off;
1293}
1294
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1296{
1297 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001298 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299
1300 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1301 if (!ctx)
1302 return NULL;
1303
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001304 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1305 if (!ctx->fallback_req)
1306 goto err;
1307
Jens Axboe78076bb2019-12-04 19:56:40 -07001308 /*
1309 * Use 5 bits less than the max cq entries, that should give us around
1310 * 32 entries per hash list if totally full and uniformly spread.
1311 */
1312 hash_bits = ilog2(p->cq_entries);
1313 hash_bits -= 5;
1314 if (hash_bits <= 0)
1315 hash_bits = 1;
1316 ctx->cancel_hash_bits = hash_bits;
1317 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1318 GFP_KERNEL);
1319 if (!ctx->cancel_hash)
1320 goto err;
1321 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1322
Roman Gushchin21482892019-05-07 10:01:48 -07001323 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001324 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1325 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001326
1327 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001328 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001329 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001331 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001332 init_completion(&ctx->ref_comp);
1333 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001334 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001335 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336 mutex_init(&ctx->uring_lock);
1337 init_waitqueue_head(&ctx->wait);
1338 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001339 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001340 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001341 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001342 spin_lock_init(&ctx->inflight_lock);
1343 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001344 spin_lock_init(&ctx->rsrc_ref_lock);
1345 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001346 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1347 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov50872752021-02-10 00:03:12 +00001348 INIT_LIST_HEAD(&submit_state->comp.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001350err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001351 if (ctx->fallback_req)
1352 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001353 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001354 kfree(ctx);
1355 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356}
1357
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001358static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001359{
Jens Axboe2bc99302020-07-09 09:43:27 -06001360 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1361 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001362
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001363 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001364 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001365 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001366
Bob Liu9d858b22019-11-13 18:06:25 +08001367 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001368}
1369
Jens Axboe5c3462c2020-10-15 09:02:33 -06001370static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001371{
Jens Axboe500a3732020-10-15 17:38:03 -06001372 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001373 return;
1374 if (refcount_dec_and_test(&req->work.identity->count))
1375 kfree(req->work.identity);
1376}
1377
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001378static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001379{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001380 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001381 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001382
Pavel Begunkove86d0042021-02-01 18:59:54 +00001383 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001384 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001385#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001386 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001387 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001388#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001389 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001390 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001391 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001392 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001393
Jens Axboe98447d62020-10-14 10:48:51 -06001394 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001395 if (--fs->users)
1396 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001397 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001398 if (fs)
1399 free_fs_struct(fs);
1400 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001401 if (req->work.flags & IO_WQ_WORK_FILES) {
1402 put_files_struct(req->work.identity->files);
1403 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001404 }
1405 if (req->flags & REQ_F_INFLIGHT) {
1406 struct io_ring_ctx *ctx = req->ctx;
1407 struct io_uring_task *tctx = req->task->io_uring;
1408 unsigned long flags;
1409
1410 spin_lock_irqsave(&ctx->inflight_lock, flags);
1411 list_del(&req->inflight_entry);
1412 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1413 req->flags &= ~REQ_F_INFLIGHT;
1414 if (atomic_read(&tctx->in_idle))
1415 wake_up(&tctx->wait);
1416 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001417
Pavel Begunkove86d0042021-02-01 18:59:54 +00001418 req->flags &= ~REQ_F_WORK_INITIALIZED;
1419 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1420 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001421 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001422}
1423
1424/*
1425 * Create a private copy of io_identity, since some fields don't match
1426 * the current context.
1427 */
1428static bool io_identity_cow(struct io_kiocb *req)
1429{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001430 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001431 const struct cred *creds = NULL;
1432 struct io_identity *id;
1433
1434 if (req->work.flags & IO_WQ_WORK_CREDS)
1435 creds = req->work.identity->creds;
1436
1437 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1438 if (unlikely(!id)) {
1439 req->work.flags |= IO_WQ_WORK_CANCEL;
1440 return false;
1441 }
1442
1443 /*
1444 * We can safely just re-init the creds we copied Either the field
1445 * matches the current one, or we haven't grabbed it yet. The only
1446 * exception is ->creds, through registered personalities, so handle
1447 * that one separately.
1448 */
1449 io_init_identity(id);
1450 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001451 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001452
1453 /* add one for this request */
1454 refcount_inc(&id->count);
1455
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001456 /* drop tctx and req identity references, if needed */
1457 if (tctx->identity != &tctx->__identity &&
1458 refcount_dec_and_test(&tctx->identity->count))
1459 kfree(tctx->identity);
1460 if (req->work.identity != &tctx->__identity &&
1461 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001462 kfree(req->work.identity);
1463
1464 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001465 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001466 return true;
1467}
1468
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001469static void io_req_track_inflight(struct io_kiocb *req)
1470{
1471 struct io_ring_ctx *ctx = req->ctx;
1472
1473 if (!(req->flags & REQ_F_INFLIGHT)) {
1474 io_req_init_async(req);
1475 req->flags |= REQ_F_INFLIGHT;
1476
1477 spin_lock_irq(&ctx->inflight_lock);
1478 list_add(&req->inflight_entry, &ctx->inflight_list);
1479 spin_unlock_irq(&ctx->inflight_lock);
1480 }
1481}
1482
Jens Axboe1e6fa522020-10-15 08:46:24 -06001483static bool io_grab_identity(struct io_kiocb *req)
1484{
1485 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001486 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001487
Jens Axboe69228332020-10-20 14:28:41 -06001488 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1489 if (id->fsize != rlimit(RLIMIT_FSIZE))
1490 return false;
1491 req->work.flags |= IO_WQ_WORK_FSIZE;
1492 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001493#ifdef CONFIG_BLK_CGROUP
1494 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1495 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1496 rcu_read_lock();
1497 if (id->blkcg_css != blkcg_css()) {
1498 rcu_read_unlock();
1499 return false;
1500 }
1501 /*
1502 * This should be rare, either the cgroup is dying or the task
1503 * is moving cgroups. Just punt to root for the handful of ios.
1504 */
1505 if (css_tryget_online(id->blkcg_css))
1506 req->work.flags |= IO_WQ_WORK_BLKCG;
1507 rcu_read_unlock();
1508 }
1509#endif
1510 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1511 if (id->creds != current_cred())
1512 return false;
1513 get_cred(id->creds);
1514 req->work.flags |= IO_WQ_WORK_CREDS;
1515 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001516#ifdef CONFIG_AUDIT
1517 if (!uid_eq(current->loginuid, id->loginuid) ||
1518 current->sessionid != id->sessionid)
1519 return false;
1520#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001521 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1522 (def->work_flags & IO_WQ_WORK_FS)) {
1523 if (current->fs != id->fs)
1524 return false;
1525 spin_lock(&id->fs->lock);
1526 if (!id->fs->in_exec) {
1527 id->fs->users++;
1528 req->work.flags |= IO_WQ_WORK_FS;
1529 } else {
1530 req->work.flags |= IO_WQ_WORK_CANCEL;
1531 }
1532 spin_unlock(&current->fs->lock);
1533 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001534 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1535 (def->work_flags & IO_WQ_WORK_FILES) &&
1536 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1537 if (id->files != current->files ||
1538 id->nsproxy != current->nsproxy)
1539 return false;
1540 atomic_inc(&id->files->count);
1541 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001542 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001543 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001544 }
Jens Axboe77788772020-12-29 10:50:46 -07001545 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1546 (def->work_flags & IO_WQ_WORK_MM)) {
1547 if (id->mm != current->mm)
1548 return false;
1549 mmgrab(id->mm);
1550 req->work.flags |= IO_WQ_WORK_MM;
1551 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001552
1553 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001554}
1555
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001556static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001557{
Jens Axboed3656342019-12-18 09:50:26 -07001558 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001559 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001560
Pavel Begunkov16d59802020-07-12 16:16:47 +03001561 io_req_init_async(req);
1562
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001563 if (req->flags & REQ_F_FORCE_ASYNC)
1564 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1565
Jens Axboed3656342019-12-18 09:50:26 -07001566 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001567 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001568 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001569 } else {
1570 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001571 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001572 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001573
Jens Axboe1e6fa522020-10-15 08:46:24 -06001574 /* if we fail grabbing identity, we must COW, regrab, and retry */
1575 if (io_grab_identity(req))
1576 return;
1577
1578 if (!io_identity_cow(req))
1579 return;
1580
1581 /* can't fail at this point */
1582 if (!io_grab_identity(req))
1583 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001584}
1585
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001586static void io_prep_async_link(struct io_kiocb *req)
1587{
1588 struct io_kiocb *cur;
1589
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001590 io_for_each_link(cur, req)
1591 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001592}
1593
Jens Axboe7271ef32020-08-10 09:55:22 -06001594static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001595{
Jackie Liua197f662019-11-08 08:09:12 -07001596 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001597 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001598
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001599 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1600 &req->work, req->flags);
1601 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001602 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001603}
1604
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001605static void io_queue_async_work(struct io_kiocb *req)
1606{
Jens Axboe7271ef32020-08-10 09:55:22 -06001607 struct io_kiocb *link;
1608
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001609 /* init ->work of the whole link before punting */
1610 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001611 link = __io_queue_async_work(req);
1612
1613 if (link)
1614 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001615}
1616
Jens Axboe5262f562019-09-17 12:26:57 -06001617static void io_kill_timeout(struct io_kiocb *req)
1618{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001619 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001620 int ret;
1621
Jens Axboee8c2bc12020-08-15 18:44:09 -07001622 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001623 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001624 atomic_set(&req->ctx->cq_timeouts,
1625 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001626 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001627 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001628 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001629 }
1630}
1631
Jens Axboe76e1b642020-09-26 15:05:03 -06001632/*
1633 * Returns true if we found and killed one or more timeouts
1634 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001635static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1636 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001637{
1638 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001639 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001640
1641 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001642 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001643 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001644 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001645 canceled++;
1646 }
Jens Axboef3606e32020-09-22 08:18:24 -06001647 }
Jens Axboe5262f562019-09-17 12:26:57 -06001648 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001649 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001650}
1651
Pavel Begunkov04518942020-05-26 20:34:05 +03001652static void __io_queue_deferred(struct io_ring_ctx *ctx)
1653{
1654 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001655 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1656 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001657
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001658 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001659 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001660 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001661 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001662 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001663 } while (!list_empty(&ctx->defer_list));
1664}
1665
Pavel Begunkov360428f2020-05-30 14:54:17 +03001666static void io_flush_timeouts(struct io_ring_ctx *ctx)
1667{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001668 u32 seq;
1669
1670 if (list_empty(&ctx->timeout_list))
1671 return;
1672
1673 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1674
1675 do {
1676 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001677 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001678 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001679
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001680 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001681 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001682
1683 /*
1684 * Since seq can easily wrap around over time, subtract
1685 * the last seq at which timeouts were flushed before comparing.
1686 * Assuming not more than 2^31-1 events have happened since,
1687 * these subtractions won't have wrapped, so we can check if
1688 * target is in [last_seq, current_seq] by comparing the two.
1689 */
1690 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1691 events_got = seq - ctx->cq_last_tm_flush;
1692 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001693 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001694
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001695 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001696 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001697 } while (!list_empty(&ctx->timeout_list));
1698
1699 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001700}
1701
Jens Axboede0617e2019-04-06 21:51:27 -06001702static void io_commit_cqring(struct io_ring_ctx *ctx)
1703{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001704 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001705
1706 /* order cqe stores with ring update */
1707 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001708
Pavel Begunkov04518942020-05-26 20:34:05 +03001709 if (unlikely(!list_empty(&ctx->defer_list)))
1710 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001711}
1712
Jens Axboe90554202020-09-03 12:12:41 -06001713static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1714{
1715 struct io_rings *r = ctx->rings;
1716
1717 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1718}
1719
Pavel Begunkov888aae22021-01-19 13:32:39 +00001720static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1721{
1722 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1723}
1724
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1726{
Hristo Venev75b28af2019-08-26 17:23:46 +00001727 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728 unsigned tail;
1729
Stefan Bühler115e12e2019-04-24 23:54:18 +02001730 /*
1731 * writes to the cq entry need to come after reading head; the
1732 * control dependency is enough as we're using WRITE_ONCE to
1733 * fill the cq entry
1734 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001735 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736 return NULL;
1737
Pavel Begunkov888aae22021-01-19 13:32:39 +00001738 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001739 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740}
1741
Jens Axboef2842ab2020-01-08 11:04:00 -07001742static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1743{
Jens Axboef0b493e2020-02-01 21:30:11 -07001744 if (!ctx->cq_ev_fd)
1745 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001746 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1747 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001748 if (!ctx->eventfd_async)
1749 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001750 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001751}
1752
Jens Axboeb41e9852020-02-17 09:52:41 -07001753static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001754{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001755 /* see waitqueue_active() comment */
1756 smp_mb();
1757
Jens Axboe8c838782019-03-12 15:48:16 -06001758 if (waitqueue_active(&ctx->wait))
1759 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001760 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1761 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001762 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001763 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001764 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001765 wake_up_interruptible(&ctx->cq_wait);
1766 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1767 }
Jens Axboe8c838782019-03-12 15:48:16 -06001768}
1769
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001770static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1771{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001772 /* see waitqueue_active() comment */
1773 smp_mb();
1774
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001775 if (ctx->flags & IORING_SETUP_SQPOLL) {
1776 if (waitqueue_active(&ctx->wait))
1777 wake_up(&ctx->wait);
1778 }
1779 if (io_should_trigger_evfd(ctx))
1780 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001781 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001782 wake_up_interruptible(&ctx->cq_wait);
1783 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1784 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001785}
1786
Jens Axboec4a2ed72019-11-21 21:01:26 -07001787/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001788static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1789 struct task_struct *tsk,
1790 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001792 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001793 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001794 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001795 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001796 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001797 LIST_HEAD(list);
1798
Pavel Begunkove23de152020-12-17 00:24:37 +00001799 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1800 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001801
Jens Axboeb18032b2021-01-24 16:58:56 -07001802 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001803 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001804 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001805 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001806 continue;
1807
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001808 cqe = io_get_cqring(ctx);
1809 if (!cqe && !force)
1810 break;
1811
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001812 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001813 if (cqe) {
1814 WRITE_ONCE(cqe->user_data, req->user_data);
1815 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001816 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001817 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001818 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001819 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001820 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001821 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001822 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001823 }
1824
Pavel Begunkov09e88402020-12-17 00:24:38 +00001825 all_flushed = list_empty(&ctx->cq_overflow_list);
1826 if (all_flushed) {
1827 clear_bit(0, &ctx->sq_check_overflow);
1828 clear_bit(0, &ctx->cq_check_overflow);
1829 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1830 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001831
Jens Axboeb18032b2021-01-24 16:58:56 -07001832 if (posted)
1833 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001834 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001835 if (posted)
1836 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001837
1838 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001839 req = list_first_entry(&list, struct io_kiocb, compl.list);
1840 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001841 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001842 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001843
Pavel Begunkov09e88402020-12-17 00:24:38 +00001844 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001845}
1846
Pavel Begunkov6c503152021-01-04 20:36:36 +00001847static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1848 struct task_struct *tsk,
1849 struct files_struct *files)
1850{
1851 if (test_bit(0, &ctx->cq_check_overflow)) {
1852 /* iopoll syncs against uring_lock, not completion_lock */
1853 if (ctx->flags & IORING_SETUP_IOPOLL)
1854 mutex_lock(&ctx->uring_lock);
1855 __io_cqring_overflow_flush(ctx, force, tsk, files);
1856 if (ctx->flags & IORING_SETUP_IOPOLL)
1857 mutex_unlock(&ctx->uring_lock);
1858 }
1859}
1860
Jens Axboebcda7ba2020-02-23 16:42:51 -07001861static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001863 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001864 struct io_uring_cqe *cqe;
1865
Jens Axboe78e19bb2019-11-06 15:21:34 -07001866 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001867
Jens Axboe2b188cc2019-01-07 10:46:33 -07001868 /*
1869 * If we can't get a cq entry, userspace overflowed the
1870 * submission (by quite a lot). Increment the overflow count in
1871 * the ring.
1872 */
1873 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001874 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001875 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001877 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001878 } else if (ctx->cq_overflow_flushed ||
1879 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001880 /*
1881 * If we're in ring overflow flush mode, or in task cancel mode,
1882 * then we cannot store the request for later flushing, we need
1883 * to drop it on the floor.
1884 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001885 ctx->cached_cq_overflow++;
1886 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001887 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001888 if (list_empty(&ctx->cq_overflow_list)) {
1889 set_bit(0, &ctx->sq_check_overflow);
1890 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001891 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001892 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001893 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001894 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001895 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001896 refcount_inc(&req->refs);
1897 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898 }
1899}
1900
Jens Axboebcda7ba2020-02-23 16:42:51 -07001901static void io_cqring_fill_event(struct io_kiocb *req, long res)
1902{
1903 __io_cqring_fill_event(req, res, 0);
1904}
1905
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001906static void io_req_complete_post(struct io_kiocb *req, long res,
1907 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001909 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001910 unsigned long flags;
1911
1912 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001913 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914 io_commit_cqring(ctx);
1915 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1916
Jens Axboe8c838782019-03-12 15:48:16 -06001917 io_cqring_ev_posted(ctx);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001918}
1919
1920static inline void io_req_complete_nostate(struct io_kiocb *req, long res,
1921 unsigned int cflags)
1922{
1923 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001924 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925}
1926
Pavel Begunkovba88ff12021-02-10 00:03:11 +00001927static void io_submit_flush_completions(struct io_comp_state *cs,
1928 struct io_ring_ctx *ctx)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001929{
Jens Axboe229a7b62020-06-22 10:13:11 -06001930 spin_lock_irq(&ctx->completion_lock);
1931 while (!list_empty(&cs->list)) {
1932 struct io_kiocb *req;
1933
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001934 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1935 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001936 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001937
1938 /*
1939 * io_free_req() doesn't care about completion_lock unless one
1940 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1941 * because of a potential deadlock with req->work.fs->lock
Pavel Begunkove342c802021-01-19 13:32:47 +00001942 * We defer both, completion and submission refs.
Pavel Begunkov216578e2020-10-13 09:44:00 +01001943 */
1944 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1945 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001946 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkove342c802021-01-19 13:32:47 +00001947 io_double_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001948 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001949 } else {
Pavel Begunkove342c802021-01-19 13:32:47 +00001950 io_double_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001951 }
1952 }
1953 io_commit_cqring(ctx);
1954 spin_unlock_irq(&ctx->completion_lock);
1955
1956 io_cqring_ev_posted(ctx);
1957 cs->nr = 0;
1958}
1959
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001960static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001961 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001962{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001963 io_clean_op(req);
1964 req->result = res;
1965 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001966 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001967}
1968
Pavel Begunkov889fca72021-02-10 00:03:09 +00001969static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1970 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001971{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001972 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1973 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001974 else
Pavel Begunkov889fca72021-02-10 00:03:09 +00001975 io_req_complete_nostate(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001976}
1977
1978static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001979{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001980 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001981}
1982
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001983static inline bool io_is_fallback_req(struct io_kiocb *req)
1984{
1985 return req == (struct io_kiocb *)
1986 ((unsigned long) req->ctx->fallback_req & ~1UL);
1987}
1988
1989static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1990{
1991 struct io_kiocb *req;
1992
1993 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001994 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001995 return req;
1996
1997 return NULL;
1998}
1999
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002000static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002001{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002002 struct io_submit_state *state = &ctx->submit_state;
2003
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002004 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002005 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002006 size_t sz;
2007 int ret;
2008
2009 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06002010 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
2011
2012 /*
2013 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2014 * retry single alloc to be on the safe side.
2015 */
2016 if (unlikely(ret <= 0)) {
2017 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2018 if (!state->reqs[0])
Pavel Begunkov85bcb6c2021-01-19 13:32:40 +00002019 return io_get_fallback_req(ctx);
Jens Axboefd6fab22019-03-14 16:30:06 -06002020 ret = 1;
2021 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002022 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002023 }
2024
Pavel Begunkov291b2822020-09-30 22:57:01 +03002025 state->free_reqs--;
2026 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002027}
2028
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002029static inline void io_put_file(struct io_kiocb *req, struct file *file,
2030 bool fixed)
2031{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002032 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002033 fput(file);
2034}
2035
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002036static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002038 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002039
Jens Axboee8c2bc12020-08-15 18:44:09 -07002040 if (req->async_data)
2041 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002042 if (req->file)
2043 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002044 if (req->fixed_rsrc_refs)
2045 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002046 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002047}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002048
Pavel Begunkov7c660732021-01-25 11:42:21 +00002049static inline void io_put_task(struct task_struct *task, int nr)
2050{
2051 struct io_uring_task *tctx = task->io_uring;
2052
2053 percpu_counter_sub(&tctx->inflight, nr);
2054 if (unlikely(atomic_read(&tctx->in_idle)))
2055 wake_up(&tctx->wait);
2056 put_task_struct_many(task, nr);
2057}
2058
Pavel Begunkov216578e2020-10-13 09:44:00 +01002059static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002060{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002061 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002062
Pavel Begunkov216578e2020-10-13 09:44:00 +01002063 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002064 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002065
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03002066 if (likely(!io_is_fallback_req(req)))
2067 kmem_cache_free(req_cachep, req);
2068 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002069 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
2070 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002071}
2072
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002073static inline void io_remove_next_linked(struct io_kiocb *req)
2074{
2075 struct io_kiocb *nxt = req->link;
2076
2077 req->link = nxt->link;
2078 nxt->link = NULL;
2079}
2080
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002081static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002082{
Jackie Liua197f662019-11-08 08:09:12 -07002083 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002084 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002085 bool cancelled = false;
2086 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002087
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002088 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002089 link = req->link;
2090
Pavel Begunkov900fad42020-10-19 16:39:16 +01002091 /*
2092 * Can happen if a linked timeout fired and link had been like
2093 * req -> link t-out -> link t-out [-> ...]
2094 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002095 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2096 struct io_timeout_data *io = link->async_data;
2097 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002098
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002099 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002100 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002101 ret = hrtimer_try_to_cancel(&io->timer);
2102 if (ret != -1) {
2103 io_cqring_fill_event(link, -ECANCELED);
2104 io_commit_cqring(ctx);
2105 cancelled = true;
2106 }
2107 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002108 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002109 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002110
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002111 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002112 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002113 io_put_req(link);
2114 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002115}
2116
Jens Axboe4d7dd462019-11-20 13:03:52 -07002117
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002118static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002119{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002120 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002121 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002122 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002123
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002124 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002125 link = req->link;
2126 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002127
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002128 while (link) {
2129 nxt = link->link;
2130 link->link = NULL;
2131
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002132 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002133 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002134
2135 /*
2136 * It's ok to free under spinlock as they're not linked anymore,
2137 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2138 * work.fs->lock.
2139 */
2140 if (link->flags & REQ_F_WORK_INITIALIZED)
2141 io_put_req_deferred(link, 2);
2142 else
2143 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002144 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002145 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002146 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002147 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002148
Jens Axboe2665abf2019-11-05 12:40:47 -07002149 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002150}
2151
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002152static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002153{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002154 if (req->flags & REQ_F_LINK_TIMEOUT)
2155 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002156
Jens Axboe9e645e112019-05-10 16:07:28 -06002157 /*
2158 * If LINK is set, we have dependent requests in this chain. If we
2159 * didn't fail this request, queue the first one up, moving any other
2160 * dependencies to the next request. In case of failure, fail the rest
2161 * of the chain.
2162 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002163 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2164 struct io_kiocb *nxt = req->link;
2165
2166 req->link = NULL;
2167 return nxt;
2168 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002169 io_fail_links(req);
2170 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002171}
Jens Axboe2665abf2019-11-05 12:40:47 -07002172
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002173static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002174{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002175 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002176 return NULL;
2177 return __io_req_find_next(req);
2178}
2179
Jens Axboe355fb9e2020-10-22 20:19:35 -06002180static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002181{
2182 struct task_struct *tsk = req->task;
2183 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002184 enum task_work_notify_mode notify;
2185 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002186
Jens Axboe6200b0a2020-09-13 14:38:30 -06002187 if (tsk->flags & PF_EXITING)
2188 return -ESRCH;
2189
Jens Axboec2c4c832020-07-01 15:37:11 -06002190 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002191 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2192 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2193 * processing task_work. There's no reliable way to tell if TWA_RESUME
2194 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002195 */
Jens Axboe91989c72020-10-16 09:02:26 -06002196 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002197 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002198 notify = TWA_SIGNAL;
2199
Jens Axboe87c43112020-09-30 21:00:14 -06002200 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002201 if (!ret)
2202 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002203
Jens Axboec2c4c832020-07-01 15:37:11 -06002204 return ret;
2205}
2206
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002207static void io_req_task_work_add_fallback(struct io_kiocb *req,
2208 void (*cb)(struct callback_head *))
2209{
2210 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2211
2212 init_task_work(&req->task_work, cb);
2213 task_work_add(tsk, &req->task_work, TWA_NONE);
2214 wake_up_process(tsk);
2215}
2216
Jens Axboec40f6372020-06-25 15:39:59 -06002217static void __io_req_task_cancel(struct io_kiocb *req, int error)
2218{
2219 struct io_ring_ctx *ctx = req->ctx;
2220
2221 spin_lock_irq(&ctx->completion_lock);
2222 io_cqring_fill_event(req, error);
2223 io_commit_cqring(ctx);
2224 spin_unlock_irq(&ctx->completion_lock);
2225
2226 io_cqring_ev_posted(ctx);
2227 req_set_fail_links(req);
2228 io_double_put_req(req);
2229}
2230
2231static void io_req_task_cancel(struct callback_head *cb)
2232{
2233 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002234 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002235
2236 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002237 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002238}
2239
2240static void __io_req_task_submit(struct io_kiocb *req)
2241{
2242 struct io_ring_ctx *ctx = req->ctx;
2243
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002244 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002245 if (!ctx->sqo_dead &&
2246 !__io_sq_thread_acquire_mm(ctx) &&
2247 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002248 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002249 else
Jens Axboec40f6372020-06-25 15:39:59 -06002250 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002251 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002252}
2253
Jens Axboec40f6372020-06-25 15:39:59 -06002254static void io_req_task_submit(struct callback_head *cb)
2255{
2256 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002258
2259 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002260 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002261}
2262
2263static void io_req_task_queue(struct io_kiocb *req)
2264{
Jens Axboec40f6372020-06-25 15:39:59 -06002265 int ret;
2266
2267 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002268 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002269
Jens Axboe355fb9e2020-10-22 20:19:35 -06002270 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002271 if (unlikely(ret))
2272 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002273}
2274
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002275static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002276{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002277 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002278
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002279 if (nxt)
2280 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002281}
2282
Jens Axboe9e645e112019-05-10 16:07:28 -06002283static void io_free_req(struct io_kiocb *req)
2284{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002285 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002286 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002287}
2288
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002289struct req_batch {
2290 void *reqs[IO_IOPOLL_BATCH];
2291 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002292
2293 struct task_struct *task;
2294 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002295};
2296
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002297static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002298{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002299 rb->to_free = 0;
2300 rb->task_refs = 0;
2301 rb->task = NULL;
2302}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002303
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002304static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2305 struct req_batch *rb)
2306{
2307 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2308 percpu_ref_put_many(&ctx->refs, rb->to_free);
2309 rb->to_free = 0;
2310}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002311
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002312static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2313 struct req_batch *rb)
2314{
2315 if (rb->to_free)
2316 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002317 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002318 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002319 rb->task = NULL;
2320 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002321}
2322
2323static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2324{
2325 if (unlikely(io_is_fallback_req(req))) {
2326 io_free_req(req);
2327 return;
2328 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002329 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002330
Jens Axboee3bc8e92020-09-24 08:45:57 -06002331 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002332 if (rb->task)
2333 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002334 rb->task = req->task;
2335 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002336 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002337 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002338
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002339 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002340 rb->reqs[rb->to_free++] = req;
2341 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2342 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002343}
2344
Jens Axboeba816ad2019-09-28 11:36:45 -06002345/*
2346 * Drop reference to request, return next in chain (if there is one) if this
2347 * was the last reference to this request.
2348 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002349static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002350{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002351 struct io_kiocb *nxt = NULL;
2352
Jens Axboe2a44f462020-02-25 13:25:41 -07002353 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002354 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002355 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002356 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002357 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002358}
2359
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360static void io_put_req(struct io_kiocb *req)
2361{
Jens Axboedef596e2019-01-09 08:59:42 -07002362 if (refcount_dec_and_test(&req->refs))
2363 io_free_req(req);
2364}
2365
Pavel Begunkov216578e2020-10-13 09:44:00 +01002366static void io_put_req_deferred_cb(struct callback_head *cb)
2367{
2368 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2369
2370 io_free_req(req);
2371}
2372
2373static void io_free_req_deferred(struct io_kiocb *req)
2374{
2375 int ret;
2376
2377 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002378 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002379 if (unlikely(ret))
2380 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002381}
2382
2383static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2384{
2385 if (refcount_sub_and_test(refs, &req->refs))
2386 io_free_req_deferred(req);
2387}
2388
Jens Axboe978db572019-11-14 22:39:04 -07002389static void io_double_put_req(struct io_kiocb *req)
2390{
2391 /* drop both submit and complete references */
2392 if (refcount_sub_and_test(2, &req->refs))
2393 io_free_req(req);
2394}
2395
Pavel Begunkov6c503152021-01-04 20:36:36 +00002396static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002397{
2398 /* See comment at the top of this file */
2399 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002400 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002401}
2402
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002403static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2404{
2405 struct io_rings *rings = ctx->rings;
2406
2407 /* make sure SQ entry isn't read before tail */
2408 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2409}
2410
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002411static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002412{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002413 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002414
Jens Axboebcda7ba2020-02-23 16:42:51 -07002415 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2416 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002417 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002418 kfree(kbuf);
2419 return cflags;
2420}
2421
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002422static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2423{
2424 struct io_buffer *kbuf;
2425
2426 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2427 return io_put_kbuf(req, kbuf);
2428}
2429
Jens Axboe4c6e2772020-07-01 11:29:10 -06002430static inline bool io_run_task_work(void)
2431{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002432 /*
2433 * Not safe to run on exiting task, and the task_work handling will
2434 * not add work to such a task.
2435 */
2436 if (unlikely(current->flags & PF_EXITING))
2437 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002438 if (current->task_works) {
2439 __set_current_state(TASK_RUNNING);
2440 task_work_run();
2441 return true;
2442 }
2443
2444 return false;
2445}
2446
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002447static void io_iopoll_queue(struct list_head *again)
2448{
2449 struct io_kiocb *req;
2450
2451 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002452 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2453 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002454 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002455 } while (!list_empty(again));
2456}
2457
Jens Axboedef596e2019-01-09 08:59:42 -07002458/*
2459 * Find and free completed poll iocbs
2460 */
2461static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2462 struct list_head *done)
2463{
Jens Axboe8237e042019-12-28 10:48:22 -07002464 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002465 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002466 LIST_HEAD(again);
2467
2468 /* order with ->result store in io_complete_rw_iopoll() */
2469 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002470
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002471 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002472 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002473 int cflags = 0;
2474
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002475 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002476 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002477 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002478 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002479 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002480 continue;
2481 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002482 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002483
Jens Axboebcda7ba2020-02-23 16:42:51 -07002484 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002485 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002486
2487 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002488 (*nr_events)++;
2489
Pavel Begunkovc3524382020-06-28 12:52:32 +03002490 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002491 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002492 }
Jens Axboedef596e2019-01-09 08:59:42 -07002493
Jens Axboe09bb8392019-03-13 12:39:28 -06002494 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002495 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002496 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002497
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002498 if (!list_empty(&again))
2499 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002500}
2501
Jens Axboedef596e2019-01-09 08:59:42 -07002502static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2503 long min)
2504{
2505 struct io_kiocb *req, *tmp;
2506 LIST_HEAD(done);
2507 bool spin;
2508 int ret;
2509
2510 /*
2511 * Only spin for completions if we don't have multiple devices hanging
2512 * off our complete list, and we're under the requested amount.
2513 */
2514 spin = !ctx->poll_multi_file && *nr_events < min;
2515
2516 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002517 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002518 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002519
2520 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002521 * Move completed and retryable entries to our local lists.
2522 * If we find a request that requires polling, break out
2523 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002524 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002525 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002526 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002527 continue;
2528 }
2529 if (!list_empty(&done))
2530 break;
2531
2532 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2533 if (ret < 0)
2534 break;
2535
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002536 /* iopoll may have completed current req */
2537 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002538 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002539
Jens Axboedef596e2019-01-09 08:59:42 -07002540 if (ret && spin)
2541 spin = false;
2542 ret = 0;
2543 }
2544
2545 if (!list_empty(&done))
2546 io_iopoll_complete(ctx, nr_events, &done);
2547
2548 return ret;
2549}
2550
2551/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002552 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002553 * non-spinning poll check - we'll still enter the driver poll loop, but only
2554 * as a non-spinning completion check.
2555 */
2556static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2557 long min)
2558{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002559 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002560 int ret;
2561
2562 ret = io_do_iopoll(ctx, nr_events, min);
2563 if (ret < 0)
2564 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002565 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002566 return 0;
2567 }
2568
2569 return 1;
2570}
2571
2572/*
2573 * We can't just wait for polled events to come to us, we have to actively
2574 * find and complete them.
2575 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002576static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002577{
2578 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2579 return;
2580
2581 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002582 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002583 unsigned int nr_events = 0;
2584
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002585 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002586
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002587 /* let it sleep and repeat later if can't complete a request */
2588 if (nr_events == 0)
2589 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002590 /*
2591 * Ensure we allow local-to-the-cpu processing to take place,
2592 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002593 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002594 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002595 if (need_resched()) {
2596 mutex_unlock(&ctx->uring_lock);
2597 cond_resched();
2598 mutex_lock(&ctx->uring_lock);
2599 }
Jens Axboedef596e2019-01-09 08:59:42 -07002600 }
2601 mutex_unlock(&ctx->uring_lock);
2602}
2603
Pavel Begunkov7668b922020-07-07 16:36:21 +03002604static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002605{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002606 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002607 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002608
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002609 /*
2610 * We disallow the app entering submit/complete with polling, but we
2611 * still need to lock the ring to prevent racing with polled issue
2612 * that got punted to a workqueue.
2613 */
2614 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002615 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002616 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002617 * Don't enter poll loop if we already have events pending.
2618 * If we do, we can potentially be spinning for commands that
2619 * already triggered a CQE (eg in error).
2620 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002621 if (test_bit(0, &ctx->cq_check_overflow))
2622 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2623 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002624 break;
2625
2626 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002627 * If a submit got punted to a workqueue, we can have the
2628 * application entering polling for a command before it gets
2629 * issued. That app will hold the uring_lock for the duration
2630 * of the poll right here, so we need to take a breather every
2631 * now and then to ensure that the issue has a chance to add
2632 * the poll to the issued list. Otherwise we can spin here
2633 * forever, while the workqueue is stuck trying to acquire the
2634 * very same mutex.
2635 */
2636 if (!(++iters & 7)) {
2637 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002638 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002639 mutex_lock(&ctx->uring_lock);
2640 }
2641
Pavel Begunkov7668b922020-07-07 16:36:21 +03002642 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002643 if (ret <= 0)
2644 break;
2645 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002646 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002647
Jens Axboe500f9fb2019-08-19 12:15:59 -06002648 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002649 return ret;
2650}
2651
Jens Axboe491381ce2019-10-17 09:20:46 -06002652static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002653{
Jens Axboe491381ce2019-10-17 09:20:46 -06002654 /*
2655 * Tell lockdep we inherited freeze protection from submission
2656 * thread.
2657 */
2658 if (req->flags & REQ_F_ISREG) {
2659 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660
Jens Axboe491381ce2019-10-17 09:20:46 -06002661 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002662 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002663 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664}
2665
Jens Axboea1d7c392020-06-22 11:09:46 -06002666static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002667 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002668{
Jens Axboe9adbd452019-12-20 08:45:55 -07002669 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002670 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002671
Jens Axboe491381ce2019-10-17 09:20:46 -06002672 if (kiocb->ki_flags & IOCB_WRITE)
2673 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002674
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002675 if (res != req->result)
2676 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002677 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002678 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002679 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002680}
2681
Jens Axboeb63534c2020-06-04 11:28:00 -06002682#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002683static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002684{
2685 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002686 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002687 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002688
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002689 /* already prepared */
2690 if (req->async_data)
2691 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002692
2693 switch (req->opcode) {
2694 case IORING_OP_READV:
2695 case IORING_OP_READ_FIXED:
2696 case IORING_OP_READ:
2697 rw = READ;
2698 break;
2699 case IORING_OP_WRITEV:
2700 case IORING_OP_WRITE_FIXED:
2701 case IORING_OP_WRITE:
2702 rw = WRITE;
2703 break;
2704 default:
2705 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2706 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002707 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002708 }
2709
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002710 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2711 if (ret < 0)
2712 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002713 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002714}
Jens Axboeb63534c2020-06-04 11:28:00 -06002715#endif
2716
2717static bool io_rw_reissue(struct io_kiocb *req, long res)
2718{
2719#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002720 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002721 int ret;
2722
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002723 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002724 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002725 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002726 if (!S_ISBLK(mode) && !S_ISREG(mode))
2727 return false;
2728 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002729 return false;
2730
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002731 lockdep_assert_held(&req->ctx->uring_lock);
2732
Jens Axboe28cea78a2020-09-14 10:51:17 -06002733 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002734
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002735 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002736 refcount_inc(&req->refs);
2737 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002738 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002739 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002740 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002741#endif
2742 return false;
2743}
2744
Jens Axboea1d7c392020-06-22 11:09:46 -06002745static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002746 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002747{
2748 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002749 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002750}
2751
2752static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2753{
Jens Axboe9adbd452019-12-20 08:45:55 -07002754 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002755
Pavel Begunkov889fca72021-02-10 00:03:09 +00002756 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002757}
2758
Jens Axboedef596e2019-01-09 08:59:42 -07002759static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2760{
Jens Axboe9adbd452019-12-20 08:45:55 -07002761 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002762
Jens Axboe491381ce2019-10-17 09:20:46 -06002763 if (kiocb->ki_flags & IOCB_WRITE)
2764 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002765
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002766 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002767 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002768
2769 WRITE_ONCE(req->result, res);
2770 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002771 smp_wmb();
2772 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002773}
2774
2775/*
2776 * After the iocb has been issued, it's safe to be found on the poll list.
2777 * Adding the kiocb to the list AFTER submission ensures that we don't
2778 * find it from a io_iopoll_getevents() thread before the issuer is done
2779 * accessing the kiocb cookie.
2780 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002781static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002782{
2783 struct io_ring_ctx *ctx = req->ctx;
2784
2785 /*
2786 * Track whether we have multiple files in our lists. This will impact
2787 * how we do polling eventually, not spinning if we're on potentially
2788 * different devices.
2789 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002790 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002791 ctx->poll_multi_file = false;
2792 } else if (!ctx->poll_multi_file) {
2793 struct io_kiocb *list_req;
2794
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002795 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002796 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002797 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002798 ctx->poll_multi_file = true;
2799 }
2800
2801 /*
2802 * For fast devices, IO may have already completed. If it has, add
2803 * it to the front so we find it first.
2804 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002805 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002806 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002807 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002808 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002809
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002810 /*
2811 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2812 * task context or in io worker task context. If current task context is
2813 * sq thread, we don't need to check whether should wake up sq thread.
2814 */
2815 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002816 wq_has_sleeper(&ctx->sq_data->wait))
2817 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002818}
2819
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002820static inline void io_state_file_put(struct io_submit_state *state)
2821{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002822 if (state->file_refs) {
2823 fput_many(state->file, state->file_refs);
2824 state->file_refs = 0;
2825 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002826}
2827
2828/*
2829 * Get as many references to a file as we have IOs left in this submission,
2830 * assuming most submissions are for one file, or at least that each file
2831 * has more than one submission.
2832 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002833static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002834{
2835 if (!state)
2836 return fget(fd);
2837
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002838 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002839 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002840 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002841 return state->file;
2842 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002843 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002844 }
2845 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002846 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002847 return NULL;
2848
2849 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002850 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002851 return state->file;
2852}
2853
Jens Axboe4503b762020-06-01 10:00:27 -06002854static bool io_bdev_nowait(struct block_device *bdev)
2855{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002856 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002857}
2858
Jens Axboe2b188cc2019-01-07 10:46:33 -07002859/*
2860 * If we tracked the file through the SCM inflight mechanism, we could support
2861 * any file. For now, just ensure that anything potentially problematic is done
2862 * inline.
2863 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002864static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002865{
2866 umode_t mode = file_inode(file)->i_mode;
2867
Jens Axboe4503b762020-06-01 10:00:27 -06002868 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002869 if (IS_ENABLED(CONFIG_BLOCK) &&
2870 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002871 return true;
2872 return false;
2873 }
2874 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002875 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002876 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002877 if (IS_ENABLED(CONFIG_BLOCK) &&
2878 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002879 file->f_op != &io_uring_fops)
2880 return true;
2881 return false;
2882 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883
Jens Axboec5b85622020-06-09 19:23:05 -06002884 /* any ->read/write should understand O_NONBLOCK */
2885 if (file->f_flags & O_NONBLOCK)
2886 return true;
2887
Jens Axboeaf197f52020-04-28 13:15:06 -06002888 if (!(file->f_mode & FMODE_NOWAIT))
2889 return false;
2890
2891 if (rw == READ)
2892 return file->f_op->read_iter != NULL;
2893
2894 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895}
2896
Pavel Begunkova88fc402020-09-30 22:57:53 +03002897static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898{
Jens Axboedef596e2019-01-09 08:59:42 -07002899 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002900 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002901 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002902 unsigned ioprio;
2903 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002904
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002905 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002906 req->flags |= REQ_F_ISREG;
2907
Jens Axboe2b188cc2019-01-07 10:46:33 -07002908 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002909 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002910 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002911 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002912 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002913 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002914 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2915 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2916 if (unlikely(ret))
2917 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002919 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2920 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2921 req->flags |= REQ_F_NOWAIT;
2922
Jens Axboe2b188cc2019-01-07 10:46:33 -07002923 ioprio = READ_ONCE(sqe->ioprio);
2924 if (ioprio) {
2925 ret = ioprio_check_cap(ioprio);
2926 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002927 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002928
2929 kiocb->ki_ioprio = ioprio;
2930 } else
2931 kiocb->ki_ioprio = get_current_ioprio();
2932
Jens Axboedef596e2019-01-09 08:59:42 -07002933 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002934 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2935 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002936 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002937
Jens Axboedef596e2019-01-09 08:59:42 -07002938 kiocb->ki_flags |= IOCB_HIPRI;
2939 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002940 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002941 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002942 if (kiocb->ki_flags & IOCB_HIPRI)
2943 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002944 kiocb->ki_complete = io_complete_rw;
2945 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002946
Jens Axboe3529d8c2019-12-19 18:24:38 -07002947 req->rw.addr = READ_ONCE(sqe->addr);
2948 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002949 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002951}
2952
2953static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2954{
2955 switch (ret) {
2956 case -EIOCBQUEUED:
2957 break;
2958 case -ERESTARTSYS:
2959 case -ERESTARTNOINTR:
2960 case -ERESTARTNOHAND:
2961 case -ERESTART_RESTARTBLOCK:
2962 /*
2963 * We can't just restart the syscall, since previously
2964 * submitted sqes may already be in progress. Just fail this
2965 * IO with EINTR.
2966 */
2967 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002968 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002969 default:
2970 kiocb->ki_complete(kiocb, ret, 0);
2971 }
2972}
2973
Jens Axboea1d7c392020-06-22 11:09:46 -06002974static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002975 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002976{
Jens Axboeba042912019-12-25 16:33:42 -07002977 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002978 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002979
Jens Axboe227c0c92020-08-13 11:51:40 -06002980 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002981 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002982 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002983 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002984 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002985 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002986 }
2987
Jens Axboeba042912019-12-25 16:33:42 -07002988 if (req->flags & REQ_F_CUR_POS)
2989 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002990 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002991 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002992 else
2993 io_rw_done(kiocb, ret);
2994}
2995
Pavel Begunkov847595d2021-02-04 13:52:06 +00002996static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002997{
Jens Axboe9adbd452019-12-20 08:45:55 -07002998 struct io_ring_ctx *ctx = req->ctx;
2999 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003000 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003001 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003002 size_t offset;
3003 u64 buf_addr;
3004
Jens Axboeedafcce2019-01-09 09:16:05 -07003005 if (unlikely(buf_index >= ctx->nr_user_bufs))
3006 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003007 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3008 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003009 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003010
3011 /* overflow */
3012 if (buf_addr + len < buf_addr)
3013 return -EFAULT;
3014 /* not inside the mapped region */
3015 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3016 return -EFAULT;
3017
3018 /*
3019 * May not be a start of buffer, set size appropriately
3020 * and advance us to the beginning.
3021 */
3022 offset = buf_addr - imu->ubuf;
3023 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003024
3025 if (offset) {
3026 /*
3027 * Don't use iov_iter_advance() here, as it's really slow for
3028 * using the latter parts of a big fixed buffer - it iterates
3029 * over each segment manually. We can cheat a bit here, because
3030 * we know that:
3031 *
3032 * 1) it's a BVEC iter, we set it up
3033 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3034 * first and last bvec
3035 *
3036 * So just find our index, and adjust the iterator afterwards.
3037 * If the offset is within the first bvec (or the whole first
3038 * bvec, just use iov_iter_advance(). This makes it easier
3039 * since we can just skip the first segment, which may not
3040 * be PAGE_SIZE aligned.
3041 */
3042 const struct bio_vec *bvec = imu->bvec;
3043
3044 if (offset <= bvec->bv_len) {
3045 iov_iter_advance(iter, offset);
3046 } else {
3047 unsigned long seg_skip;
3048
3049 /* skip first vec */
3050 offset -= bvec->bv_len;
3051 seg_skip = 1 + (offset >> PAGE_SHIFT);
3052
3053 iter->bvec = bvec + seg_skip;
3054 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003055 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003056 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003057 }
3058 }
3059
Pavel Begunkov847595d2021-02-04 13:52:06 +00003060 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003061}
3062
Jens Axboebcda7ba2020-02-23 16:42:51 -07003063static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3064{
3065 if (needs_lock)
3066 mutex_unlock(&ctx->uring_lock);
3067}
3068
3069static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3070{
3071 /*
3072 * "Normal" inline submissions always hold the uring_lock, since we
3073 * grab it from the system call. Same is true for the SQPOLL offload.
3074 * The only exception is when we've detached the request and issue it
3075 * from an async worker thread, grab the lock for that case.
3076 */
3077 if (needs_lock)
3078 mutex_lock(&ctx->uring_lock);
3079}
3080
3081static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3082 int bgid, struct io_buffer *kbuf,
3083 bool needs_lock)
3084{
3085 struct io_buffer *head;
3086
3087 if (req->flags & REQ_F_BUFFER_SELECTED)
3088 return kbuf;
3089
3090 io_ring_submit_lock(req->ctx, needs_lock);
3091
3092 lockdep_assert_held(&req->ctx->uring_lock);
3093
3094 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3095 if (head) {
3096 if (!list_empty(&head->list)) {
3097 kbuf = list_last_entry(&head->list, struct io_buffer,
3098 list);
3099 list_del(&kbuf->list);
3100 } else {
3101 kbuf = head;
3102 idr_remove(&req->ctx->io_buffer_idr, bgid);
3103 }
3104 if (*len > kbuf->len)
3105 *len = kbuf->len;
3106 } else {
3107 kbuf = ERR_PTR(-ENOBUFS);
3108 }
3109
3110 io_ring_submit_unlock(req->ctx, needs_lock);
3111
3112 return kbuf;
3113}
3114
Jens Axboe4d954c22020-02-27 07:31:19 -07003115static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3116 bool needs_lock)
3117{
3118 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003119 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003120
3121 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003122 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003123 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3124 if (IS_ERR(kbuf))
3125 return kbuf;
3126 req->rw.addr = (u64) (unsigned long) kbuf;
3127 req->flags |= REQ_F_BUFFER_SELECTED;
3128 return u64_to_user_ptr(kbuf->addr);
3129}
3130
3131#ifdef CONFIG_COMPAT
3132static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3133 bool needs_lock)
3134{
3135 struct compat_iovec __user *uiov;
3136 compat_ssize_t clen;
3137 void __user *buf;
3138 ssize_t len;
3139
3140 uiov = u64_to_user_ptr(req->rw.addr);
3141 if (!access_ok(uiov, sizeof(*uiov)))
3142 return -EFAULT;
3143 if (__get_user(clen, &uiov->iov_len))
3144 return -EFAULT;
3145 if (clen < 0)
3146 return -EINVAL;
3147
3148 len = clen;
3149 buf = io_rw_buffer_select(req, &len, needs_lock);
3150 if (IS_ERR(buf))
3151 return PTR_ERR(buf);
3152 iov[0].iov_base = buf;
3153 iov[0].iov_len = (compat_size_t) len;
3154 return 0;
3155}
3156#endif
3157
3158static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3159 bool needs_lock)
3160{
3161 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3162 void __user *buf;
3163 ssize_t len;
3164
3165 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3166 return -EFAULT;
3167
3168 len = iov[0].iov_len;
3169 if (len < 0)
3170 return -EINVAL;
3171 buf = io_rw_buffer_select(req, &len, needs_lock);
3172 if (IS_ERR(buf))
3173 return PTR_ERR(buf);
3174 iov[0].iov_base = buf;
3175 iov[0].iov_len = len;
3176 return 0;
3177}
3178
3179static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3180 bool needs_lock)
3181{
Jens Axboedddb3e22020-06-04 11:27:01 -06003182 if (req->flags & REQ_F_BUFFER_SELECTED) {
3183 struct io_buffer *kbuf;
3184
3185 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3186 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3187 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003188 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003189 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003190 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003191 return -EINVAL;
3192
3193#ifdef CONFIG_COMPAT
3194 if (req->ctx->compat)
3195 return io_compat_import(req, iov, needs_lock);
3196#endif
3197
3198 return __io_iov_buffer_select(req, iov, needs_lock);
3199}
3200
Pavel Begunkov847595d2021-02-04 13:52:06 +00003201static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3202 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003203{
Jens Axboe9adbd452019-12-20 08:45:55 -07003204 void __user *buf = u64_to_user_ptr(req->rw.addr);
3205 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003206 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003207 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003208
Pavel Begunkov7d009162019-11-25 23:14:40 +03003209 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003210 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003211 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003212 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003213
Jens Axboebcda7ba2020-02-23 16:42:51 -07003214 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003215 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003216 return -EINVAL;
3217
Jens Axboe3a6820f2019-12-22 15:19:35 -07003218 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003219 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003220 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003221 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003222 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003223 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003224 }
3225
Jens Axboe3a6820f2019-12-22 15:19:35 -07003226 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3227 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003228 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003229 }
3230
Jens Axboe4d954c22020-02-27 07:31:19 -07003231 if (req->flags & REQ_F_BUFFER_SELECT) {
3232 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003233 if (!ret)
3234 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003235 *iovec = NULL;
3236 return ret;
3237 }
3238
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003239 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3240 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003241}
3242
Jens Axboe0fef9482020-08-26 10:36:20 -06003243static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3244{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003245 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003246}
3247
Jens Axboe32960612019-09-23 11:05:34 -06003248/*
3249 * For files that don't have ->read_iter() and ->write_iter(), handle them
3250 * by looping over ->read() or ->write() manually.
3251 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003252static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003253{
Jens Axboe4017eb92020-10-22 14:14:12 -06003254 struct kiocb *kiocb = &req->rw.kiocb;
3255 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003256 ssize_t ret = 0;
3257
3258 /*
3259 * Don't support polled IO through this interface, and we can't
3260 * support non-blocking either. For the latter, this just causes
3261 * the kiocb to be handled from an async context.
3262 */
3263 if (kiocb->ki_flags & IOCB_HIPRI)
3264 return -EOPNOTSUPP;
3265 if (kiocb->ki_flags & IOCB_NOWAIT)
3266 return -EAGAIN;
3267
3268 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003269 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003270 ssize_t nr;
3271
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003272 if (!iov_iter_is_bvec(iter)) {
3273 iovec = iov_iter_iovec(iter);
3274 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003275 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3276 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003277 }
3278
Jens Axboe32960612019-09-23 11:05:34 -06003279 if (rw == READ) {
3280 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003281 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003282 } else {
3283 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003284 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003285 }
3286
3287 if (nr < 0) {
3288 if (!ret)
3289 ret = nr;
3290 break;
3291 }
3292 ret += nr;
3293 if (nr != iovec.iov_len)
3294 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003295 req->rw.len -= nr;
3296 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003297 iov_iter_advance(iter, nr);
3298 }
3299
3300 return ret;
3301}
3302
Jens Axboeff6165b2020-08-13 09:47:43 -06003303static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3304 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003305{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003306 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003307
Jens Axboeff6165b2020-08-13 09:47:43 -06003308 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003309 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003310 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003311 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003312 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003313 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003314 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003315 unsigned iov_off = 0;
3316
3317 rw->iter.iov = rw->fast_iov;
3318 if (iter->iov != fast_iov) {
3319 iov_off = iter->iov - fast_iov;
3320 rw->iter.iov += iov_off;
3321 }
3322 if (rw->fast_iov != fast_iov)
3323 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003324 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003325 } else {
3326 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003327 }
3328}
3329
Jens Axboee8c2bc12020-08-15 18:44:09 -07003330static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003331{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003332 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3333 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3334 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003335}
3336
Jens Axboee8c2bc12020-08-15 18:44:09 -07003337static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003338{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003340 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003341
Jens Axboee8c2bc12020-08-15 18:44:09 -07003342 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003343}
3344
Jens Axboeff6165b2020-08-13 09:47:43 -06003345static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3346 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003347 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003348{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003349 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003350 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003351 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003352 if (__io_alloc_async_data(req)) {
3353 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003354 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003355 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003356
Jens Axboeff6165b2020-08-13 09:47:43 -06003357 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003358 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003359 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003360}
3361
Pavel Begunkov73debe62020-09-30 22:57:54 +03003362static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003363{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003364 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003365 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003366 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003367
Pavel Begunkov2846c482020-11-07 13:16:27 +00003368 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003369 if (unlikely(ret < 0))
3370 return ret;
3371
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003372 iorw->bytes_done = 0;
3373 iorw->free_iovec = iov;
3374 if (iov)
3375 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003376 return 0;
3377}
3378
Pavel Begunkov73debe62020-09-30 22:57:54 +03003379static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003380{
3381 ssize_t ret;
3382
Pavel Begunkova88fc402020-09-30 22:57:53 +03003383 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003384 if (ret)
3385 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003386
Jens Axboe3529d8c2019-12-19 18:24:38 -07003387 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3388 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003389
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003390 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003391 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003392 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003393 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003394}
3395
Jens Axboec1dd91d2020-08-03 16:43:59 -06003396/*
3397 * This is our waitqueue callback handler, registered through lock_page_async()
3398 * when we initially tried to do the IO with the iocb armed our waitqueue.
3399 * This gets called when the page is unlocked, and we generally expect that to
3400 * happen when the page IO is completed and the page is now uptodate. This will
3401 * queue a task_work based retry of the operation, attempting to copy the data
3402 * again. If the latter fails because the page was NOT uptodate, then we will
3403 * do a thread based blocking retry of the operation. That's the unexpected
3404 * slow path.
3405 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003406static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3407 int sync, void *arg)
3408{
3409 struct wait_page_queue *wpq;
3410 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003411 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003412 int ret;
3413
3414 wpq = container_of(wait, struct wait_page_queue, wait);
3415
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003416 if (!wake_page_match(wpq, key))
3417 return 0;
3418
Hao Xuc8d317a2020-09-29 20:00:45 +08003419 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003420 list_del_init(&wait->entry);
3421
Pavel Begunkove7375122020-07-12 20:42:04 +03003422 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003423 percpu_ref_get(&req->ctx->refs);
3424
Jens Axboebcf5a062020-05-22 09:24:42 -06003425 /* submit ref gets dropped, acquire a new one */
3426 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003427 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003428 if (unlikely(ret))
3429 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003430 return 1;
3431}
3432
Jens Axboec1dd91d2020-08-03 16:43:59 -06003433/*
3434 * This controls whether a given IO request should be armed for async page
3435 * based retry. If we return false here, the request is handed to the async
3436 * worker threads for retry. If we're doing buffered reads on a regular file,
3437 * we prepare a private wait_page_queue entry and retry the operation. This
3438 * will either succeed because the page is now uptodate and unlocked, or it
3439 * will register a callback when the page is unlocked at IO completion. Through
3440 * that callback, io_uring uses task_work to setup a retry of the operation.
3441 * That retry will attempt the buffered read again. The retry will generally
3442 * succeed, or in rare cases where it fails, we then fall back to using the
3443 * async worker threads for a blocking retry.
3444 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003445static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003446{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003447 struct io_async_rw *rw = req->async_data;
3448 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003449 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003450
3451 /* never retry for NOWAIT, we just complete with -EAGAIN */
3452 if (req->flags & REQ_F_NOWAIT)
3453 return false;
3454
Jens Axboe227c0c92020-08-13 11:51:40 -06003455 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003456 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003457 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003458
Jens Axboebcf5a062020-05-22 09:24:42 -06003459 /*
3460 * just use poll if we can, and don't attempt if the fs doesn't
3461 * support callback based unlocks
3462 */
3463 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3464 return false;
3465
Jens Axboe3b2a4432020-08-16 10:58:43 -07003466 wait->wait.func = io_async_buf_func;
3467 wait->wait.private = req;
3468 wait->wait.flags = 0;
3469 INIT_LIST_HEAD(&wait->wait.entry);
3470 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003471 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003472 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003473 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003474}
3475
3476static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3477{
3478 if (req->file->f_op->read_iter)
3479 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003480 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003481 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003482 else
3483 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003484}
3485
Pavel Begunkov889fca72021-02-10 00:03:09 +00003486static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003487{
3488 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003489 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003490 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003491 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003492 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003493 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003494
Pavel Begunkov2846c482020-11-07 13:16:27 +00003495 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003496 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003497 iovec = NULL;
3498 } else {
3499 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3500 if (ret < 0)
3501 return ret;
3502 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003503 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003504 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003505
Jens Axboefd6c2e42019-12-18 12:19:41 -07003506 /* Ensure we clear previously set non-block flag */
3507 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003508 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003509 else
3510 kiocb->ki_flags |= IOCB_NOWAIT;
3511
Pavel Begunkov24c74672020-06-21 13:09:51 +03003512 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003513 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3514 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003515 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003516 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003517
Pavel Begunkov632546c2020-11-07 13:16:26 +00003518 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003519 if (unlikely(ret)) {
3520 kfree(iovec);
3521 return ret;
3522 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003523
Jens Axboe227c0c92020-08-13 11:51:40 -06003524 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003525
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003526 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003527 /* it's faster to check here then delegate to kfree */
3528 if (iovec)
3529 kfree(iovec);
3530 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003531 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003532 /* IOPOLL retry should happen for io-wq threads */
3533 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003534 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003535 /* no retry on NONBLOCK nor RWF_NOWAIT */
3536 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003537 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003538 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003539 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003540 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003541 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003542 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003543 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003544 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003545 }
3546
Jens Axboe227c0c92020-08-13 11:51:40 -06003547 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003548 if (ret2)
3549 return ret2;
3550
Jens Axboee8c2bc12020-08-15 18:44:09 -07003551 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003552 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003553 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003554
Pavel Begunkovb23df912021-02-04 13:52:04 +00003555 do {
3556 io_size -= ret;
3557 rw->bytes_done += ret;
3558 /* if we can retry, do so with the callbacks armed */
3559 if (!io_rw_should_retry(req)) {
3560 kiocb->ki_flags &= ~IOCB_WAITQ;
3561 return -EAGAIN;
3562 }
3563
3564 /*
3565 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3566 * we get -EIOCBQUEUED, then we'll get a notification when the
3567 * desired page gets unlocked. We can also get a partial read
3568 * here, and if we do, then just retry at the new offset.
3569 */
3570 ret = io_iter_do_read(req, iter);
3571 if (ret == -EIOCBQUEUED)
3572 return 0;
3573 /* we got some bytes, but not all. retry. */
3574 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003575done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003576 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003577 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003578}
3579
Pavel Begunkov73debe62020-09-30 22:57:54 +03003580static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003581{
3582 ssize_t ret;
3583
Pavel Begunkova88fc402020-09-30 22:57:53 +03003584 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003585 if (ret)
3586 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003587
Jens Axboe3529d8c2019-12-19 18:24:38 -07003588 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3589 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003590
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003591 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003592 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003593 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003594 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003595}
3596
Pavel Begunkov889fca72021-02-10 00:03:09 +00003597static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003598{
3599 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003600 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003601 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003602 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003603 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003604 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003605
Pavel Begunkov2846c482020-11-07 13:16:27 +00003606 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003607 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003608 iovec = NULL;
3609 } else {
3610 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3611 if (ret < 0)
3612 return ret;
3613 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003614 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003615 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003616
Jens Axboefd6c2e42019-12-18 12:19:41 -07003617 /* Ensure we clear previously set non-block flag */
3618 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003619 kiocb->ki_flags &= ~IOCB_NOWAIT;
3620 else
3621 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003622
Pavel Begunkov24c74672020-06-21 13:09:51 +03003623 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003624 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003625 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003626
Jens Axboe10d59342019-12-09 20:16:22 -07003627 /* file path doesn't support NOWAIT for non-direct_IO */
3628 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3629 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003630 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003631
Pavel Begunkov632546c2020-11-07 13:16:26 +00003632 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003633 if (unlikely(ret))
3634 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003635
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003636 /*
3637 * Open-code file_start_write here to grab freeze protection,
3638 * which will be released by another thread in
3639 * io_complete_rw(). Fool lockdep by telling it the lock got
3640 * released so that it doesn't complain about the held lock when
3641 * we return to userspace.
3642 */
3643 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003644 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003645 __sb_writers_release(file_inode(req->file)->i_sb,
3646 SB_FREEZE_WRITE);
3647 }
3648 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003649
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003650 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003651 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003652 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003653 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003654 else
3655 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003656
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003657 /*
3658 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3659 * retry them without IOCB_NOWAIT.
3660 */
3661 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3662 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003663 /* no retry on NONBLOCK nor RWF_NOWAIT */
3664 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003665 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003666 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003667 /* IOPOLL retry should happen for io-wq threads */
3668 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3669 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003670done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003671 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003672 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003673copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003674 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003675 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003676 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003677 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003678 }
Jens Axboe31b51512019-01-18 22:56:34 -07003679out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003680 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003681 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003682 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003683 return ret;
3684}
3685
Jens Axboe80a261f2020-09-28 14:23:58 -06003686static int io_renameat_prep(struct io_kiocb *req,
3687 const struct io_uring_sqe *sqe)
3688{
3689 struct io_rename *ren = &req->rename;
3690 const char __user *oldf, *newf;
3691
3692 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3693 return -EBADF;
3694
3695 ren->old_dfd = READ_ONCE(sqe->fd);
3696 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3697 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3698 ren->new_dfd = READ_ONCE(sqe->len);
3699 ren->flags = READ_ONCE(sqe->rename_flags);
3700
3701 ren->oldpath = getname(oldf);
3702 if (IS_ERR(ren->oldpath))
3703 return PTR_ERR(ren->oldpath);
3704
3705 ren->newpath = getname(newf);
3706 if (IS_ERR(ren->newpath)) {
3707 putname(ren->oldpath);
3708 return PTR_ERR(ren->newpath);
3709 }
3710
3711 req->flags |= REQ_F_NEED_CLEANUP;
3712 return 0;
3713}
3714
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003715static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003716{
3717 struct io_rename *ren = &req->rename;
3718 int ret;
3719
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003720 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003721 return -EAGAIN;
3722
3723 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3724 ren->newpath, ren->flags);
3725
3726 req->flags &= ~REQ_F_NEED_CLEANUP;
3727 if (ret < 0)
3728 req_set_fail_links(req);
3729 io_req_complete(req, ret);
3730 return 0;
3731}
3732
Jens Axboe14a11432020-09-28 14:27:37 -06003733static int io_unlinkat_prep(struct io_kiocb *req,
3734 const struct io_uring_sqe *sqe)
3735{
3736 struct io_unlink *un = &req->unlink;
3737 const char __user *fname;
3738
3739 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3740 return -EBADF;
3741
3742 un->dfd = READ_ONCE(sqe->fd);
3743
3744 un->flags = READ_ONCE(sqe->unlink_flags);
3745 if (un->flags & ~AT_REMOVEDIR)
3746 return -EINVAL;
3747
3748 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3749 un->filename = getname(fname);
3750 if (IS_ERR(un->filename))
3751 return PTR_ERR(un->filename);
3752
3753 req->flags |= REQ_F_NEED_CLEANUP;
3754 return 0;
3755}
3756
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003757static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003758{
3759 struct io_unlink *un = &req->unlink;
3760 int ret;
3761
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003762 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003763 return -EAGAIN;
3764
3765 if (un->flags & AT_REMOVEDIR)
3766 ret = do_rmdir(un->dfd, un->filename);
3767 else
3768 ret = do_unlinkat(un->dfd, un->filename);
3769
3770 req->flags &= ~REQ_F_NEED_CLEANUP;
3771 if (ret < 0)
3772 req_set_fail_links(req);
3773 io_req_complete(req, ret);
3774 return 0;
3775}
3776
Jens Axboe36f4fa62020-09-05 11:14:22 -06003777static int io_shutdown_prep(struct io_kiocb *req,
3778 const struct io_uring_sqe *sqe)
3779{
3780#if defined(CONFIG_NET)
3781 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3782 return -EINVAL;
3783 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3784 sqe->buf_index)
3785 return -EINVAL;
3786
3787 req->shutdown.how = READ_ONCE(sqe->len);
3788 return 0;
3789#else
3790 return -EOPNOTSUPP;
3791#endif
3792}
3793
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003794static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003795{
3796#if defined(CONFIG_NET)
3797 struct socket *sock;
3798 int ret;
3799
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003800 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003801 return -EAGAIN;
3802
Linus Torvalds48aba792020-12-16 12:44:05 -08003803 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003804 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003805 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003806
3807 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003808 if (ret < 0)
3809 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003810 io_req_complete(req, ret);
3811 return 0;
3812#else
3813 return -EOPNOTSUPP;
3814#endif
3815}
3816
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003817static int __io_splice_prep(struct io_kiocb *req,
3818 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003819{
3820 struct io_splice* sp = &req->splice;
3821 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003822
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003823 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3824 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003825
3826 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003827 sp->len = READ_ONCE(sqe->len);
3828 sp->flags = READ_ONCE(sqe->splice_flags);
3829
3830 if (unlikely(sp->flags & ~valid_flags))
3831 return -EINVAL;
3832
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003833 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3834 (sp->flags & SPLICE_F_FD_IN_FIXED));
3835 if (!sp->file_in)
3836 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003837 req->flags |= REQ_F_NEED_CLEANUP;
3838
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003839 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3840 /*
3841 * Splice operation will be punted aync, and here need to
3842 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3843 */
3844 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003845 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003846 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003847
3848 return 0;
3849}
3850
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003851static int io_tee_prep(struct io_kiocb *req,
3852 const struct io_uring_sqe *sqe)
3853{
3854 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3855 return -EINVAL;
3856 return __io_splice_prep(req, sqe);
3857}
3858
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003859static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003860{
3861 struct io_splice *sp = &req->splice;
3862 struct file *in = sp->file_in;
3863 struct file *out = sp->file_out;
3864 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3865 long ret = 0;
3866
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003867 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003868 return -EAGAIN;
3869 if (sp->len)
3870 ret = do_tee(in, out, sp->len, flags);
3871
3872 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3873 req->flags &= ~REQ_F_NEED_CLEANUP;
3874
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003875 if (ret != sp->len)
3876 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003877 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003878 return 0;
3879}
3880
3881static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3882{
3883 struct io_splice* sp = &req->splice;
3884
3885 sp->off_in = READ_ONCE(sqe->splice_off_in);
3886 sp->off_out = READ_ONCE(sqe->off);
3887 return __io_splice_prep(req, sqe);
3888}
3889
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003890static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003891{
3892 struct io_splice *sp = &req->splice;
3893 struct file *in = sp->file_in;
3894 struct file *out = sp->file_out;
3895 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3896 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003897 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003898
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003899 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003900 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003901
3902 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3903 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003904
Jens Axboe948a7742020-05-17 14:21:38 -06003905 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003906 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003907
3908 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3909 req->flags &= ~REQ_F_NEED_CLEANUP;
3910
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003911 if (ret != sp->len)
3912 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003913 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003914 return 0;
3915}
3916
Jens Axboe2b188cc2019-01-07 10:46:33 -07003917/*
3918 * IORING_OP_NOP just posts a completion event, nothing else.
3919 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003920static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003921{
3922 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003923
Jens Axboedef596e2019-01-09 08:59:42 -07003924 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3925 return -EINVAL;
3926
Pavel Begunkov889fca72021-02-10 00:03:09 +00003927 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003928 return 0;
3929}
3930
Jens Axboe3529d8c2019-12-19 18:24:38 -07003931static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003932{
Jens Axboe6b063142019-01-10 22:13:58 -07003933 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003934
Jens Axboe09bb8392019-03-13 12:39:28 -06003935 if (!req->file)
3936 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003937
Jens Axboe6b063142019-01-10 22:13:58 -07003938 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003939 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003940 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003941 return -EINVAL;
3942
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003943 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3944 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3945 return -EINVAL;
3946
3947 req->sync.off = READ_ONCE(sqe->off);
3948 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003949 return 0;
3950}
3951
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003952static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003953{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003954 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003955 int ret;
3956
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003957 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003958 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003959 return -EAGAIN;
3960
Jens Axboe9adbd452019-12-20 08:45:55 -07003961 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962 end > 0 ? end : LLONG_MAX,
3963 req->sync.flags & IORING_FSYNC_DATASYNC);
3964 if (ret < 0)
3965 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003966 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003967 return 0;
3968}
3969
Jens Axboed63d1b52019-12-10 10:38:56 -07003970static int io_fallocate_prep(struct io_kiocb *req,
3971 const struct io_uring_sqe *sqe)
3972{
3973 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3974 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003975 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3976 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003977
3978 req->sync.off = READ_ONCE(sqe->off);
3979 req->sync.len = READ_ONCE(sqe->addr);
3980 req->sync.mode = READ_ONCE(sqe->len);
3981 return 0;
3982}
3983
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003984static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003985{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003986 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003987
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003988 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003989 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003990 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003991 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3992 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003993 if (ret < 0)
3994 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003995 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003996 return 0;
3997}
3998
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003999static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004000{
Jens Axboef8748882020-01-08 17:47:02 -07004001 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004002 int ret;
4003
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004004 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004005 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004006 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004007 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004008
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004009 /* open.how should be already initialised */
4010 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004011 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004012
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004013 req->open.dfd = READ_ONCE(sqe->fd);
4014 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004015 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004016 if (IS_ERR(req->open.filename)) {
4017 ret = PTR_ERR(req->open.filename);
4018 req->open.filename = NULL;
4019 return ret;
4020 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004021 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004022 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004023 return 0;
4024}
4025
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004026static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4027{
4028 u64 flags, mode;
4029
Jens Axboe14587a462020-09-05 11:36:08 -06004030 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004031 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004032 mode = READ_ONCE(sqe->len);
4033 flags = READ_ONCE(sqe->open_flags);
4034 req->open.how = build_open_how(flags, mode);
4035 return __io_openat_prep(req, sqe);
4036}
4037
Jens Axboecebdb982020-01-08 17:59:24 -07004038static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4039{
4040 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004041 size_t len;
4042 int ret;
4043
Jens Axboe14587a462020-09-05 11:36:08 -06004044 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004045 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004046 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4047 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004048 if (len < OPEN_HOW_SIZE_VER0)
4049 return -EINVAL;
4050
4051 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4052 len);
4053 if (ret)
4054 return ret;
4055
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004056 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004057}
4058
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004059static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004060{
4061 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004062 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004063 bool nonblock_set;
4064 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004065 int ret;
4066
Jens Axboecebdb982020-01-08 17:59:24 -07004067 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004068 if (ret)
4069 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004070 nonblock_set = op.open_flag & O_NONBLOCK;
4071 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004072 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004073 /*
4074 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4075 * it'll always -EAGAIN
4076 */
4077 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4078 return -EAGAIN;
4079 op.lookup_flags |= LOOKUP_CACHED;
4080 op.open_flag |= O_NONBLOCK;
4081 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004082
Jens Axboe4022e7a2020-03-19 19:23:18 -06004083 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004084 if (ret < 0)
4085 goto err;
4086
4087 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004088 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004089 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4090 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004091 /*
4092 * We could hang on to this 'fd', but seems like marginal
4093 * gain for something that is now known to be a slower path.
4094 * So just put it, and we'll get a new one when we retry.
4095 */
4096 put_unused_fd(ret);
4097 return -EAGAIN;
4098 }
4099
Jens Axboe15b71ab2019-12-11 11:20:36 -07004100 if (IS_ERR(file)) {
4101 put_unused_fd(ret);
4102 ret = PTR_ERR(file);
4103 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004104 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004105 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004106 fsnotify_open(file);
4107 fd_install(ret, file);
4108 }
4109err:
4110 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004111 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004112 if (ret < 0)
4113 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004114 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004115 return 0;
4116}
4117
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004118static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004119{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004120 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004121}
4122
Jens Axboe067524e2020-03-02 16:32:28 -07004123static int io_remove_buffers_prep(struct io_kiocb *req,
4124 const struct io_uring_sqe *sqe)
4125{
4126 struct io_provide_buf *p = &req->pbuf;
4127 u64 tmp;
4128
4129 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4130 return -EINVAL;
4131
4132 tmp = READ_ONCE(sqe->fd);
4133 if (!tmp || tmp > USHRT_MAX)
4134 return -EINVAL;
4135
4136 memset(p, 0, sizeof(*p));
4137 p->nbufs = tmp;
4138 p->bgid = READ_ONCE(sqe->buf_group);
4139 return 0;
4140}
4141
4142static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4143 int bgid, unsigned nbufs)
4144{
4145 unsigned i = 0;
4146
4147 /* shouldn't happen */
4148 if (!nbufs)
4149 return 0;
4150
4151 /* the head kbuf is the list itself */
4152 while (!list_empty(&buf->list)) {
4153 struct io_buffer *nxt;
4154
4155 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4156 list_del(&nxt->list);
4157 kfree(nxt);
4158 if (++i == nbufs)
4159 return i;
4160 }
4161 i++;
4162 kfree(buf);
4163 idr_remove(&ctx->io_buffer_idr, bgid);
4164
4165 return i;
4166}
4167
Pavel Begunkov889fca72021-02-10 00:03:09 +00004168static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004169{
4170 struct io_provide_buf *p = &req->pbuf;
4171 struct io_ring_ctx *ctx = req->ctx;
4172 struct io_buffer *head;
4173 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004174 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004175
4176 io_ring_submit_lock(ctx, !force_nonblock);
4177
4178 lockdep_assert_held(&ctx->uring_lock);
4179
4180 ret = -ENOENT;
4181 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4182 if (head)
4183 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004184 if (ret < 0)
4185 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004186
4187 /* need to hold the lock to complete IOPOLL requests */
4188 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004189 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004190 io_ring_submit_unlock(ctx, !force_nonblock);
4191 } else {
4192 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004193 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004194 }
Jens Axboe067524e2020-03-02 16:32:28 -07004195 return 0;
4196}
4197
Jens Axboeddf0322d2020-02-23 16:41:33 -07004198static int io_provide_buffers_prep(struct io_kiocb *req,
4199 const struct io_uring_sqe *sqe)
4200{
4201 struct io_provide_buf *p = &req->pbuf;
4202 u64 tmp;
4203
4204 if (sqe->ioprio || sqe->rw_flags)
4205 return -EINVAL;
4206
4207 tmp = READ_ONCE(sqe->fd);
4208 if (!tmp || tmp > USHRT_MAX)
4209 return -E2BIG;
4210 p->nbufs = tmp;
4211 p->addr = READ_ONCE(sqe->addr);
4212 p->len = READ_ONCE(sqe->len);
4213
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004214 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004215 return -EFAULT;
4216
4217 p->bgid = READ_ONCE(sqe->buf_group);
4218 tmp = READ_ONCE(sqe->off);
4219 if (tmp > USHRT_MAX)
4220 return -E2BIG;
4221 p->bid = tmp;
4222 return 0;
4223}
4224
4225static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4226{
4227 struct io_buffer *buf;
4228 u64 addr = pbuf->addr;
4229 int i, bid = pbuf->bid;
4230
4231 for (i = 0; i < pbuf->nbufs; i++) {
4232 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4233 if (!buf)
4234 break;
4235
4236 buf->addr = addr;
4237 buf->len = pbuf->len;
4238 buf->bid = bid;
4239 addr += pbuf->len;
4240 bid++;
4241 if (!*head) {
4242 INIT_LIST_HEAD(&buf->list);
4243 *head = buf;
4244 } else {
4245 list_add_tail(&buf->list, &(*head)->list);
4246 }
4247 }
4248
4249 return i ? i : -ENOMEM;
4250}
4251
Pavel Begunkov889fca72021-02-10 00:03:09 +00004252static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004253{
4254 struct io_provide_buf *p = &req->pbuf;
4255 struct io_ring_ctx *ctx = req->ctx;
4256 struct io_buffer *head, *list;
4257 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004258 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004259
4260 io_ring_submit_lock(ctx, !force_nonblock);
4261
4262 lockdep_assert_held(&ctx->uring_lock);
4263
4264 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4265
4266 ret = io_add_buffers(p, &head);
4267 if (ret < 0)
4268 goto out;
4269
4270 if (!list) {
4271 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4272 GFP_KERNEL);
4273 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004274 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004275 goto out;
4276 }
4277 }
4278out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004279 if (ret < 0)
4280 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004281
4282 /* need to hold the lock to complete IOPOLL requests */
4283 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004284 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004285 io_ring_submit_unlock(ctx, !force_nonblock);
4286 } else {
4287 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004288 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004289 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004290 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004291}
4292
Jens Axboe3e4827b2020-01-08 15:18:09 -07004293static int io_epoll_ctl_prep(struct io_kiocb *req,
4294 const struct io_uring_sqe *sqe)
4295{
4296#if defined(CONFIG_EPOLL)
4297 if (sqe->ioprio || sqe->buf_index)
4298 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004299 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004300 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004301
4302 req->epoll.epfd = READ_ONCE(sqe->fd);
4303 req->epoll.op = READ_ONCE(sqe->len);
4304 req->epoll.fd = READ_ONCE(sqe->off);
4305
4306 if (ep_op_has_event(req->epoll.op)) {
4307 struct epoll_event __user *ev;
4308
4309 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4310 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4311 return -EFAULT;
4312 }
4313
4314 return 0;
4315#else
4316 return -EOPNOTSUPP;
4317#endif
4318}
4319
Pavel Begunkov889fca72021-02-10 00:03:09 +00004320static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004321{
4322#if defined(CONFIG_EPOLL)
4323 struct io_epoll *ie = &req->epoll;
4324 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004325 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004326
4327 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4328 if (force_nonblock && ret == -EAGAIN)
4329 return -EAGAIN;
4330
4331 if (ret < 0)
4332 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004333 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004334 return 0;
4335#else
4336 return -EOPNOTSUPP;
4337#endif
4338}
4339
Jens Axboec1ca7572019-12-25 22:18:28 -07004340static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4341{
4342#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4343 if (sqe->ioprio || sqe->buf_index || sqe->off)
4344 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004345 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4346 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004347
4348 req->madvise.addr = READ_ONCE(sqe->addr);
4349 req->madvise.len = READ_ONCE(sqe->len);
4350 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4351 return 0;
4352#else
4353 return -EOPNOTSUPP;
4354#endif
4355}
4356
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004357static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004358{
4359#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4360 struct io_madvise *ma = &req->madvise;
4361 int ret;
4362
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004363 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004364 return -EAGAIN;
4365
Minchan Kim0726b012020-10-17 16:14:50 -07004366 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004367 if (ret < 0)
4368 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004369 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004370 return 0;
4371#else
4372 return -EOPNOTSUPP;
4373#endif
4374}
4375
Jens Axboe4840e412019-12-25 22:03:45 -07004376static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4377{
4378 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4379 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004380 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4381 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004382
4383 req->fadvise.offset = READ_ONCE(sqe->off);
4384 req->fadvise.len = READ_ONCE(sqe->len);
4385 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4386 return 0;
4387}
4388
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004389static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004390{
4391 struct io_fadvise *fa = &req->fadvise;
4392 int ret;
4393
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004394 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004395 switch (fa->advice) {
4396 case POSIX_FADV_NORMAL:
4397 case POSIX_FADV_RANDOM:
4398 case POSIX_FADV_SEQUENTIAL:
4399 break;
4400 default:
4401 return -EAGAIN;
4402 }
4403 }
Jens Axboe4840e412019-12-25 22:03:45 -07004404
4405 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4406 if (ret < 0)
4407 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004408 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004409 return 0;
4410}
4411
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004412static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4413{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004414 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004415 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004416 if (sqe->ioprio || sqe->buf_index)
4417 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004418 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004419 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004420
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004421 req->statx.dfd = READ_ONCE(sqe->fd);
4422 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004423 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004424 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4425 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004426
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004427 return 0;
4428}
4429
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004430static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004431{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004432 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004433 int ret;
4434
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004435 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004436 /* only need file table for an actual valid fd */
4437 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4438 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004439 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004440 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004441
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004442 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4443 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004444
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004445 if (ret < 0)
4446 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004447 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004448 return 0;
4449}
4450
Jens Axboeb5dba592019-12-11 14:02:38 -07004451static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4452{
Jens Axboe14587a462020-09-05 11:36:08 -06004453 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004454 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004455 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4456 sqe->rw_flags || sqe->buf_index)
4457 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004458 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004459 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004460
4461 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004462 return 0;
4463}
4464
Pavel Begunkov889fca72021-02-10 00:03:09 +00004465static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004466{
Jens Axboe9eac1902021-01-19 15:50:37 -07004467 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004468 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004469 struct fdtable *fdt;
4470 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004471 int ret;
4472
Jens Axboe9eac1902021-01-19 15:50:37 -07004473 file = NULL;
4474 ret = -EBADF;
4475 spin_lock(&files->file_lock);
4476 fdt = files_fdtable(files);
4477 if (close->fd >= fdt->max_fds) {
4478 spin_unlock(&files->file_lock);
4479 goto err;
4480 }
4481 file = fdt->fd[close->fd];
4482 if (!file) {
4483 spin_unlock(&files->file_lock);
4484 goto err;
4485 }
4486
4487 if (file->f_op == &io_uring_fops) {
4488 spin_unlock(&files->file_lock);
4489 file = NULL;
4490 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004491 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004492
4493 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004494 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004495 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004496 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004497 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004498
Jens Axboe9eac1902021-01-19 15:50:37 -07004499 ret = __close_fd_get_file(close->fd, &file);
4500 spin_unlock(&files->file_lock);
4501 if (ret < 0) {
4502 if (ret == -ENOENT)
4503 ret = -EBADF;
4504 goto err;
4505 }
4506
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004507 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004508 ret = filp_close(file, current->files);
4509err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004510 if (ret < 0)
4511 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004512 if (file)
4513 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004514 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004515 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004516}
4517
Jens Axboe3529d8c2019-12-19 18:24:38 -07004518static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004519{
4520 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004521
4522 if (!req->file)
4523 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004524
4525 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4526 return -EINVAL;
4527 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4528 return -EINVAL;
4529
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004530 req->sync.off = READ_ONCE(sqe->off);
4531 req->sync.len = READ_ONCE(sqe->len);
4532 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004533 return 0;
4534}
4535
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004536static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004537{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004538 int ret;
4539
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004540 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004541 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004542 return -EAGAIN;
4543
Jens Axboe9adbd452019-12-20 08:45:55 -07004544 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004545 req->sync.flags);
4546 if (ret < 0)
4547 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004548 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004549 return 0;
4550}
4551
YueHaibing469956e2020-03-04 15:53:52 +08004552#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004553static int io_setup_async_msg(struct io_kiocb *req,
4554 struct io_async_msghdr *kmsg)
4555{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004556 struct io_async_msghdr *async_msg = req->async_data;
4557
4558 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004559 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004560 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004561 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004562 return -ENOMEM;
4563 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004564 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004565 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004566 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004567 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004568 /* if were using fast_iov, set it to the new one */
4569 if (!async_msg->free_iov)
4570 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4571
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004572 return -EAGAIN;
4573}
4574
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004575static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4576 struct io_async_msghdr *iomsg)
4577{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004578 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004579 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004580 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004581 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004582}
4583
Jens Axboe3529d8c2019-12-19 18:24:38 -07004584static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004585{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004586 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004587 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004588 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004589
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004590 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4591 return -EINVAL;
4592
Jens Axboee47293f2019-12-20 08:58:21 -07004593 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004594 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004595 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004596
Jens Axboed8768362020-02-27 14:17:49 -07004597#ifdef CONFIG_COMPAT
4598 if (req->ctx->compat)
4599 sr->msg_flags |= MSG_CMSG_COMPAT;
4600#endif
4601
Jens Axboee8c2bc12020-08-15 18:44:09 -07004602 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004603 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004604 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004605 if (!ret)
4606 req->flags |= REQ_F_NEED_CLEANUP;
4607 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004608}
4609
Pavel Begunkov889fca72021-02-10 00:03:09 +00004610static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004611{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004612 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004613 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004614 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004615 int ret;
4616
Florent Revestdba4a922020-12-04 12:36:04 +01004617 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004618 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004619 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004620
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004621 kmsg = req->async_data;
4622 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004623 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004624 if (ret)
4625 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004626 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004627 }
4628
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004629 flags = req->sr_msg.msg_flags;
4630 if (flags & MSG_DONTWAIT)
4631 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004632 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004633 flags |= MSG_DONTWAIT;
4634
4635 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004636 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004637 return io_setup_async_msg(req, kmsg);
4638 if (ret == -ERESTARTSYS)
4639 ret = -EINTR;
4640
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004641 /* fast path, check for non-NULL to avoid function call */
4642 if (kmsg->free_iov)
4643 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004644 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004645 if (ret < 0)
4646 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004647 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004648 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004649}
4650
Pavel Begunkov889fca72021-02-10 00:03:09 +00004651static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004652{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004653 struct io_sr_msg *sr = &req->sr_msg;
4654 struct msghdr msg;
4655 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004656 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004657 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004658 int ret;
4659
Florent Revestdba4a922020-12-04 12:36:04 +01004660 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004661 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004662 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004663
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004664 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4665 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004666 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004667
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004668 msg.msg_name = NULL;
4669 msg.msg_control = NULL;
4670 msg.msg_controllen = 0;
4671 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004672
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004673 flags = req->sr_msg.msg_flags;
4674 if (flags & MSG_DONTWAIT)
4675 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004676 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004677 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004678
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004679 msg.msg_flags = flags;
4680 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004681 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004682 return -EAGAIN;
4683 if (ret == -ERESTARTSYS)
4684 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004685
Jens Axboe03b12302019-12-02 18:50:25 -07004686 if (ret < 0)
4687 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004688 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004689 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004690}
4691
Pavel Begunkov1400e692020-07-12 20:41:05 +03004692static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4693 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004694{
4695 struct io_sr_msg *sr = &req->sr_msg;
4696 struct iovec __user *uiov;
4697 size_t iov_len;
4698 int ret;
4699
Pavel Begunkov1400e692020-07-12 20:41:05 +03004700 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4701 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004702 if (ret)
4703 return ret;
4704
4705 if (req->flags & REQ_F_BUFFER_SELECT) {
4706 if (iov_len > 1)
4707 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004708 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004709 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004710 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004711 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004712 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004713 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004714 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004715 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004716 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004717 if (ret > 0)
4718 ret = 0;
4719 }
4720
4721 return ret;
4722}
4723
4724#ifdef CONFIG_COMPAT
4725static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004726 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004727{
4728 struct compat_msghdr __user *msg_compat;
4729 struct io_sr_msg *sr = &req->sr_msg;
4730 struct compat_iovec __user *uiov;
4731 compat_uptr_t ptr;
4732 compat_size_t len;
4733 int ret;
4734
Pavel Begunkov270a5942020-07-12 20:41:04 +03004735 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004736 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004737 &ptr, &len);
4738 if (ret)
4739 return ret;
4740
4741 uiov = compat_ptr(ptr);
4742 if (req->flags & REQ_F_BUFFER_SELECT) {
4743 compat_ssize_t clen;
4744
4745 if (len > 1)
4746 return -EINVAL;
4747 if (!access_ok(uiov, sizeof(*uiov)))
4748 return -EFAULT;
4749 if (__get_user(clen, &uiov->iov_len))
4750 return -EFAULT;
4751 if (clen < 0)
4752 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004753 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004754 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004755 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004756 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004757 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004758 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004759 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004760 if (ret < 0)
4761 return ret;
4762 }
4763
4764 return 0;
4765}
Jens Axboe03b12302019-12-02 18:50:25 -07004766#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004767
Pavel Begunkov1400e692020-07-12 20:41:05 +03004768static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4769 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004770{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004771 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004772
4773#ifdef CONFIG_COMPAT
4774 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004775 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004776#endif
4777
Pavel Begunkov1400e692020-07-12 20:41:05 +03004778 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004779}
4780
Jens Axboebcda7ba2020-02-23 16:42:51 -07004781static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004782 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004783{
4784 struct io_sr_msg *sr = &req->sr_msg;
4785 struct io_buffer *kbuf;
4786
Jens Axboebcda7ba2020-02-23 16:42:51 -07004787 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4788 if (IS_ERR(kbuf))
4789 return kbuf;
4790
4791 sr->kbuf = kbuf;
4792 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004793 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004794}
4795
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004796static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4797{
4798 return io_put_kbuf(req, req->sr_msg.kbuf);
4799}
4800
Jens Axboe3529d8c2019-12-19 18:24:38 -07004801static int io_recvmsg_prep(struct io_kiocb *req,
4802 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004803{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004804 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004805 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004806 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004807
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004808 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4809 return -EINVAL;
4810
Jens Axboe3529d8c2019-12-19 18:24:38 -07004811 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004812 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004813 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004814 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004815
Jens Axboed8768362020-02-27 14:17:49 -07004816#ifdef CONFIG_COMPAT
4817 if (req->ctx->compat)
4818 sr->msg_flags |= MSG_CMSG_COMPAT;
4819#endif
4820
Jens Axboee8c2bc12020-08-15 18:44:09 -07004821 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004822 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004823 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004824 if (!ret)
4825 req->flags |= REQ_F_NEED_CLEANUP;
4826 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004827}
4828
Pavel Begunkov889fca72021-02-10 00:03:09 +00004829static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004830{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004831 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004832 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004833 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004834 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004835 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004836 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004837
Florent Revestdba4a922020-12-04 12:36:04 +01004838 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004839 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004840 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004841
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004842 kmsg = req->async_data;
4843 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004844 ret = io_recvmsg_copy_hdr(req, &iomsg);
4845 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004846 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004847 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004848 }
4849
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004850 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004851 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004852 if (IS_ERR(kbuf))
4853 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004854 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004855 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4856 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004857 1, req->sr_msg.len);
4858 }
4859
4860 flags = req->sr_msg.msg_flags;
4861 if (flags & MSG_DONTWAIT)
4862 req->flags |= REQ_F_NOWAIT;
4863 else if (force_nonblock)
4864 flags |= MSG_DONTWAIT;
4865
4866 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4867 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004868 if (force_nonblock && ret == -EAGAIN)
4869 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004870 if (ret == -ERESTARTSYS)
4871 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004872
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004873 if (req->flags & REQ_F_BUFFER_SELECTED)
4874 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004875 /* fast path, check for non-NULL to avoid function call */
4876 if (kmsg->free_iov)
4877 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004878 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004879 if (ret < 0)
4880 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004881 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004882 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004883}
4884
Pavel Begunkov889fca72021-02-10 00:03:09 +00004885static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004886{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004887 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004888 struct io_sr_msg *sr = &req->sr_msg;
4889 struct msghdr msg;
4890 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004891 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004892 struct iovec iov;
4893 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004894 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004895 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004896
Florent Revestdba4a922020-12-04 12:36:04 +01004897 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004898 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004899 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004900
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004901 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004902 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004903 if (IS_ERR(kbuf))
4904 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004905 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004906 }
4907
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004908 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004909 if (unlikely(ret))
4910 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004911
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004912 msg.msg_name = NULL;
4913 msg.msg_control = NULL;
4914 msg.msg_controllen = 0;
4915 msg.msg_namelen = 0;
4916 msg.msg_iocb = NULL;
4917 msg.msg_flags = 0;
4918
4919 flags = req->sr_msg.msg_flags;
4920 if (flags & MSG_DONTWAIT)
4921 req->flags |= REQ_F_NOWAIT;
4922 else if (force_nonblock)
4923 flags |= MSG_DONTWAIT;
4924
4925 ret = sock_recvmsg(sock, &msg, flags);
4926 if (force_nonblock && ret == -EAGAIN)
4927 return -EAGAIN;
4928 if (ret == -ERESTARTSYS)
4929 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004930out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004931 if (req->flags & REQ_F_BUFFER_SELECTED)
4932 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004933 if (ret < 0)
4934 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004935 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004936 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004937}
4938
Jens Axboe3529d8c2019-12-19 18:24:38 -07004939static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004940{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004941 struct io_accept *accept = &req->accept;
4942
Jens Axboe14587a462020-09-05 11:36:08 -06004943 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004944 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004945 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004946 return -EINVAL;
4947
Jens Axboed55e5f52019-12-11 16:12:15 -07004948 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4949 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004950 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004951 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004952 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004953}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004954
Pavel Begunkov889fca72021-02-10 00:03:09 +00004955static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004956{
4957 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004958 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004959 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004960 int ret;
4961
Jiufei Xuee697dee2020-06-10 13:41:59 +08004962 if (req->file->f_flags & O_NONBLOCK)
4963 req->flags |= REQ_F_NOWAIT;
4964
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004965 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004966 accept->addr_len, accept->flags,
4967 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004968 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004969 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004970 if (ret < 0) {
4971 if (ret == -ERESTARTSYS)
4972 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004973 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004974 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004975 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004976 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004977}
4978
Jens Axboe3529d8c2019-12-19 18:24:38 -07004979static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004980{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004981 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004982 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004983
Jens Axboe14587a462020-09-05 11:36:08 -06004984 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004985 return -EINVAL;
4986 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4987 return -EINVAL;
4988
Jens Axboe3529d8c2019-12-19 18:24:38 -07004989 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4990 conn->addr_len = READ_ONCE(sqe->addr2);
4991
4992 if (!io)
4993 return 0;
4994
4995 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004996 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004997}
4998
Pavel Begunkov889fca72021-02-10 00:03:09 +00004999static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005000{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005001 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005002 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005003 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005004 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005005
Jens Axboee8c2bc12020-08-15 18:44:09 -07005006 if (req->async_data) {
5007 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005008 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005009 ret = move_addr_to_kernel(req->connect.addr,
5010 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005011 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005012 if (ret)
5013 goto out;
5014 io = &__io;
5015 }
5016
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005017 file_flags = force_nonblock ? O_NONBLOCK : 0;
5018
Jens Axboee8c2bc12020-08-15 18:44:09 -07005019 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005020 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005021 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005022 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005023 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005024 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005025 ret = -ENOMEM;
5026 goto out;
5027 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005028 io = req->async_data;
5029 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005030 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005031 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005032 if (ret == -ERESTARTSYS)
5033 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005034out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005035 if (ret < 0)
5036 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005037 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005038 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005039}
YueHaibing469956e2020-03-04 15:53:52 +08005040#else /* !CONFIG_NET */
5041static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5042{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005043 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005044}
5045
Pavel Begunkov889fca72021-02-10 00:03:09 +00005046static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005047{
YueHaibing469956e2020-03-04 15:53:52 +08005048 return -EOPNOTSUPP;
5049}
5050
Pavel Begunkov889fca72021-02-10 00:03:09 +00005051static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005052{
5053 return -EOPNOTSUPP;
5054}
5055
5056static int io_recvmsg_prep(struct io_kiocb *req,
5057 const struct io_uring_sqe *sqe)
5058{
5059 return -EOPNOTSUPP;
5060}
5061
Pavel Begunkov889fca72021-02-10 00:03:09 +00005062static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005063{
5064 return -EOPNOTSUPP;
5065}
5066
Pavel Begunkov889fca72021-02-10 00:03:09 +00005067static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005068{
5069 return -EOPNOTSUPP;
5070}
5071
5072static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5073{
5074 return -EOPNOTSUPP;
5075}
5076
Pavel Begunkov889fca72021-02-10 00:03:09 +00005077static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005078{
5079 return -EOPNOTSUPP;
5080}
5081
5082static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5083{
5084 return -EOPNOTSUPP;
5085}
5086
Pavel Begunkov889fca72021-02-10 00:03:09 +00005087static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005088{
5089 return -EOPNOTSUPP;
5090}
5091#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005092
Jens Axboed7718a92020-02-14 22:23:12 -07005093struct io_poll_table {
5094 struct poll_table_struct pt;
5095 struct io_kiocb *req;
5096 int error;
5097};
5098
Jens Axboed7718a92020-02-14 22:23:12 -07005099static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5100 __poll_t mask, task_work_func_t func)
5101{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005102 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005103
5104 /* for instances that support it check for an event match first: */
5105 if (mask && !(mask & poll->events))
5106 return 0;
5107
5108 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5109
5110 list_del_init(&poll->wait.entry);
5111
Jens Axboed7718a92020-02-14 22:23:12 -07005112 req->result = mask;
5113 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005114 percpu_ref_get(&req->ctx->refs);
5115
Jens Axboed7718a92020-02-14 22:23:12 -07005116 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005117 * If this fails, then the task is exiting. When a task exits, the
5118 * work gets canceled, so just cancel this request as well instead
5119 * of executing it. We can't safely execute it anyway, as we may not
5120 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005121 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005122 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005123 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005124 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005125 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005126 }
Jens Axboed7718a92020-02-14 22:23:12 -07005127 return 1;
5128}
5129
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005130static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5131 __acquires(&req->ctx->completion_lock)
5132{
5133 struct io_ring_ctx *ctx = req->ctx;
5134
5135 if (!req->result && !READ_ONCE(poll->canceled)) {
5136 struct poll_table_struct pt = { ._key = poll->events };
5137
5138 req->result = vfs_poll(req->file, &pt) & poll->events;
5139 }
5140
5141 spin_lock_irq(&ctx->completion_lock);
5142 if (!req->result && !READ_ONCE(poll->canceled)) {
5143 add_wait_queue(poll->head, &poll->wait);
5144 return true;
5145 }
5146
5147 return false;
5148}
5149
Jens Axboed4e7cd32020-08-15 11:44:50 -07005150static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005151{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005152 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005153 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005154 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005155 return req->apoll->double_poll;
5156}
5157
5158static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5159{
5160 if (req->opcode == IORING_OP_POLL_ADD)
5161 return &req->poll;
5162 return &req->apoll->poll;
5163}
5164
5165static void io_poll_remove_double(struct io_kiocb *req)
5166{
5167 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005168
5169 lockdep_assert_held(&req->ctx->completion_lock);
5170
5171 if (poll && poll->head) {
5172 struct wait_queue_head *head = poll->head;
5173
5174 spin_lock(&head->lock);
5175 list_del_init(&poll->wait.entry);
5176 if (poll->wait.private)
5177 refcount_dec(&req->refs);
5178 poll->head = NULL;
5179 spin_unlock(&head->lock);
5180 }
5181}
5182
5183static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5184{
5185 struct io_ring_ctx *ctx = req->ctx;
5186
Jens Axboed4e7cd32020-08-15 11:44:50 -07005187 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005188 req->poll.done = true;
5189 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5190 io_commit_cqring(ctx);
5191}
5192
Jens Axboe18bceab2020-05-15 11:56:54 -06005193static void io_poll_task_func(struct callback_head *cb)
5194{
5195 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005196 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005197 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005198
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005199 if (io_poll_rewait(req, &req->poll)) {
5200 spin_unlock_irq(&ctx->completion_lock);
5201 } else {
5202 hash_del(&req->hash_node);
5203 io_poll_complete(req, req->result, 0);
5204 spin_unlock_irq(&ctx->completion_lock);
5205
5206 nxt = io_put_req_find_next(req);
5207 io_cqring_ev_posted(ctx);
5208 if (nxt)
5209 __io_req_task_submit(nxt);
5210 }
5211
Jens Axboe6d816e02020-08-11 08:04:14 -06005212 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005213}
5214
5215static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5216 int sync, void *key)
5217{
5218 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005219 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005220 __poll_t mask = key_to_poll(key);
5221
5222 /* for instances that support it check for an event match first: */
5223 if (mask && !(mask & poll->events))
5224 return 0;
5225
Jens Axboe8706e042020-09-28 08:38:54 -06005226 list_del_init(&wait->entry);
5227
Jens Axboe807abcb2020-07-17 17:09:27 -06005228 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005229 bool done;
5230
Jens Axboe807abcb2020-07-17 17:09:27 -06005231 spin_lock(&poll->head->lock);
5232 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005233 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005234 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005235 /* make sure double remove sees this as being gone */
5236 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005237 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005238 if (!done) {
5239 /* use wait func handler, so it matches the rq type */
5240 poll->wait.func(&poll->wait, mode, sync, key);
5241 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005242 }
5243 refcount_dec(&req->refs);
5244 return 1;
5245}
5246
5247static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5248 wait_queue_func_t wake_func)
5249{
5250 poll->head = NULL;
5251 poll->done = false;
5252 poll->canceled = false;
5253 poll->events = events;
5254 INIT_LIST_HEAD(&poll->wait.entry);
5255 init_waitqueue_func_entry(&poll->wait, wake_func);
5256}
5257
5258static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005259 struct wait_queue_head *head,
5260 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005261{
5262 struct io_kiocb *req = pt->req;
5263
5264 /*
5265 * If poll->head is already set, it's because the file being polled
5266 * uses multiple waitqueues for poll handling (eg one for read, one
5267 * for write). Setup a separate io_poll_iocb if this happens.
5268 */
5269 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005270 struct io_poll_iocb *poll_one = poll;
5271
Jens Axboe18bceab2020-05-15 11:56:54 -06005272 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005273 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005274 pt->error = -EINVAL;
5275 return;
5276 }
5277 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5278 if (!poll) {
5279 pt->error = -ENOMEM;
5280 return;
5281 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005282 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005283 refcount_inc(&req->refs);
5284 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005285 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005286 }
5287
5288 pt->error = 0;
5289 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005290
5291 if (poll->events & EPOLLEXCLUSIVE)
5292 add_wait_queue_exclusive(head, &poll->wait);
5293 else
5294 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005295}
5296
5297static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5298 struct poll_table_struct *p)
5299{
5300 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005301 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005302
Jens Axboe807abcb2020-07-17 17:09:27 -06005303 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005304}
5305
Jens Axboed7718a92020-02-14 22:23:12 -07005306static void io_async_task_func(struct callback_head *cb)
5307{
5308 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5309 struct async_poll *apoll = req->apoll;
5310 struct io_ring_ctx *ctx = req->ctx;
5311
5312 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5313
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005314 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005315 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005316 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005317 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005318 }
5319
Jens Axboe31067252020-05-17 17:43:31 -06005320 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005321 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005322 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005323
Jens Axboed4e7cd32020-08-15 11:44:50 -07005324 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005325 spin_unlock_irq(&ctx->completion_lock);
5326
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005327 if (!READ_ONCE(apoll->poll.canceled))
5328 __io_req_task_submit(req);
5329 else
5330 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005331
Jens Axboe6d816e02020-08-11 08:04:14 -06005332 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005333 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005334 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005335}
5336
5337static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5338 void *key)
5339{
5340 struct io_kiocb *req = wait->private;
5341 struct io_poll_iocb *poll = &req->apoll->poll;
5342
5343 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5344 key_to_poll(key));
5345
5346 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5347}
5348
5349static void io_poll_req_insert(struct io_kiocb *req)
5350{
5351 struct io_ring_ctx *ctx = req->ctx;
5352 struct hlist_head *list;
5353
5354 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5355 hlist_add_head(&req->hash_node, list);
5356}
5357
5358static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5359 struct io_poll_iocb *poll,
5360 struct io_poll_table *ipt, __poll_t mask,
5361 wait_queue_func_t wake_func)
5362 __acquires(&ctx->completion_lock)
5363{
5364 struct io_ring_ctx *ctx = req->ctx;
5365 bool cancel = false;
5366
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005367 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005368 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005369 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005370 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005371
5372 ipt->pt._key = mask;
5373 ipt->req = req;
5374 ipt->error = -EINVAL;
5375
Jens Axboed7718a92020-02-14 22:23:12 -07005376 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5377
5378 spin_lock_irq(&ctx->completion_lock);
5379 if (likely(poll->head)) {
5380 spin_lock(&poll->head->lock);
5381 if (unlikely(list_empty(&poll->wait.entry))) {
5382 if (ipt->error)
5383 cancel = true;
5384 ipt->error = 0;
5385 mask = 0;
5386 }
5387 if (mask || ipt->error)
5388 list_del_init(&poll->wait.entry);
5389 else if (cancel)
5390 WRITE_ONCE(poll->canceled, true);
5391 else if (!poll->done) /* actually waiting for an event */
5392 io_poll_req_insert(req);
5393 spin_unlock(&poll->head->lock);
5394 }
5395
5396 return mask;
5397}
5398
5399static bool io_arm_poll_handler(struct io_kiocb *req)
5400{
5401 const struct io_op_def *def = &io_op_defs[req->opcode];
5402 struct io_ring_ctx *ctx = req->ctx;
5403 struct async_poll *apoll;
5404 struct io_poll_table ipt;
5405 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005406 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005407
5408 if (!req->file || !file_can_poll(req->file))
5409 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005410 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005411 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005412 if (def->pollin)
5413 rw = READ;
5414 else if (def->pollout)
5415 rw = WRITE;
5416 else
5417 return false;
5418 /* if we can't nonblock try, then no point in arming a poll handler */
5419 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005420 return false;
5421
5422 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5423 if (unlikely(!apoll))
5424 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005425 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005426
5427 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005428 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005429
Nathan Chancellor8755d972020-03-02 16:01:19 -07005430 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005431 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005432 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005433 if (def->pollout)
5434 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005435
5436 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5437 if ((req->opcode == IORING_OP_RECVMSG) &&
5438 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5439 mask &= ~POLLIN;
5440
Jens Axboed7718a92020-02-14 22:23:12 -07005441 mask |= POLLERR | POLLPRI;
5442
5443 ipt.pt._qproc = io_async_queue_proc;
5444
5445 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5446 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005447 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005448 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005449 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005450 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005451 kfree(apoll);
5452 return false;
5453 }
5454 spin_unlock_irq(&ctx->completion_lock);
5455 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5456 apoll->poll.events);
5457 return true;
5458}
5459
5460static bool __io_poll_remove_one(struct io_kiocb *req,
5461 struct io_poll_iocb *poll)
5462{
Jens Axboeb41e9852020-02-17 09:52:41 -07005463 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005464
5465 spin_lock(&poll->head->lock);
5466 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005467 if (!list_empty(&poll->wait.entry)) {
5468 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005469 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005470 }
5471 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005472 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005473 return do_complete;
5474}
5475
5476static bool io_poll_remove_one(struct io_kiocb *req)
5477{
5478 bool do_complete;
5479
Jens Axboed4e7cd32020-08-15 11:44:50 -07005480 io_poll_remove_double(req);
5481
Jens Axboed7718a92020-02-14 22:23:12 -07005482 if (req->opcode == IORING_OP_POLL_ADD) {
5483 do_complete = __io_poll_remove_one(req, &req->poll);
5484 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005485 struct async_poll *apoll = req->apoll;
5486
Jens Axboed7718a92020-02-14 22:23:12 -07005487 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005488 do_complete = __io_poll_remove_one(req, &apoll->poll);
5489 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005490 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005491 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005492 kfree(apoll);
5493 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005494 }
5495
Jens Axboeb41e9852020-02-17 09:52:41 -07005496 if (do_complete) {
5497 io_cqring_fill_event(req, -ECANCELED);
5498 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005499 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005500 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005501 }
5502
5503 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005504}
5505
Jens Axboe76e1b642020-09-26 15:05:03 -06005506/*
5507 * Returns true if we found and killed one or more poll requests
5508 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005509static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5510 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005511{
Jens Axboe78076bb2019-12-04 19:56:40 -07005512 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005513 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005514 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005515
5516 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005517 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5518 struct hlist_head *list;
5519
5520 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005521 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005522 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005523 posted += io_poll_remove_one(req);
5524 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005525 }
5526 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005527
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005528 if (posted)
5529 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005530
5531 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005532}
5533
Jens Axboe47f46762019-11-09 17:43:02 -07005534static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5535{
Jens Axboe78076bb2019-12-04 19:56:40 -07005536 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005537 struct io_kiocb *req;
5538
Jens Axboe78076bb2019-12-04 19:56:40 -07005539 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5540 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005541 if (sqe_addr != req->user_data)
5542 continue;
5543 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005544 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005545 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005546 }
5547
5548 return -ENOENT;
5549}
5550
Jens Axboe3529d8c2019-12-19 18:24:38 -07005551static int io_poll_remove_prep(struct io_kiocb *req,
5552 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005553{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005554 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5555 return -EINVAL;
5556 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5557 sqe->poll_events)
5558 return -EINVAL;
5559
Pavel Begunkov018043b2020-10-27 23:17:18 +00005560 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005561 return 0;
5562}
5563
5564/*
5565 * Find a running poll command that matches one specified in sqe->addr,
5566 * and remove it if found.
5567 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005568static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005569{
5570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005571 int ret;
5572
Jens Axboe221c5eb2019-01-17 09:41:58 -07005573 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005574 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005575 spin_unlock_irq(&ctx->completion_lock);
5576
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005577 if (ret < 0)
5578 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005579 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005580 return 0;
5581}
5582
Jens Axboe221c5eb2019-01-17 09:41:58 -07005583static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5584 void *key)
5585{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005586 struct io_kiocb *req = wait->private;
5587 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005588
Jens Axboed7718a92020-02-14 22:23:12 -07005589 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005590}
5591
Jens Axboe221c5eb2019-01-17 09:41:58 -07005592static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5593 struct poll_table_struct *p)
5594{
5595 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5596
Jens Axboee8c2bc12020-08-15 18:44:09 -07005597 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005598}
5599
Jens Axboe3529d8c2019-12-19 18:24:38 -07005600static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005601{
5602 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005603 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005604
5605 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5606 return -EINVAL;
5607 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5608 return -EINVAL;
5609
Jiufei Xue5769a352020-06-17 17:53:55 +08005610 events = READ_ONCE(sqe->poll32_events);
5611#ifdef __BIG_ENDIAN
5612 events = swahw32(events);
5613#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005614 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5615 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005616 return 0;
5617}
5618
Pavel Begunkov61e98202021-02-10 00:03:08 +00005619static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005620{
5621 struct io_poll_iocb *poll = &req->poll;
5622 struct io_ring_ctx *ctx = req->ctx;
5623 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005624 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005625
Jens Axboed7718a92020-02-14 22:23:12 -07005626 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005627
Jens Axboed7718a92020-02-14 22:23:12 -07005628 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5629 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005630
Jens Axboe8c838782019-03-12 15:48:16 -06005631 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005632 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005633 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005634 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005635 spin_unlock_irq(&ctx->completion_lock);
5636
Jens Axboe8c838782019-03-12 15:48:16 -06005637 if (mask) {
5638 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005639 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005640 }
Jens Axboe8c838782019-03-12 15:48:16 -06005641 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005642}
5643
Jens Axboe5262f562019-09-17 12:26:57 -06005644static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5645{
Jens Axboead8a48a2019-11-15 08:49:11 -07005646 struct io_timeout_data *data = container_of(timer,
5647 struct io_timeout_data, timer);
5648 struct io_kiocb *req = data->req;
5649 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005650 unsigned long flags;
5651
Jens Axboe5262f562019-09-17 12:26:57 -06005652 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005653 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005654 atomic_set(&req->ctx->cq_timeouts,
5655 atomic_read(&req->ctx->cq_timeouts) + 1);
5656
Jens Axboe78e19bb2019-11-06 15:21:34 -07005657 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005658 io_commit_cqring(ctx);
5659 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5660
5661 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005662 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005663 io_put_req(req);
5664 return HRTIMER_NORESTART;
5665}
5666
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005667static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5668 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005669{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005670 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005671 struct io_kiocb *req;
5672 int ret = -ENOENT;
5673
5674 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5675 if (user_data == req->user_data) {
5676 ret = 0;
5677 break;
5678 }
5679 }
5680
5681 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005682 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005683
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005684 io = req->async_data;
5685 ret = hrtimer_try_to_cancel(&io->timer);
5686 if (ret == -1)
5687 return ERR_PTR(-EALREADY);
5688 list_del_init(&req->timeout.list);
5689 return req;
5690}
5691
5692static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5693{
5694 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5695
5696 if (IS_ERR(req))
5697 return PTR_ERR(req);
5698
5699 req_set_fail_links(req);
5700 io_cqring_fill_event(req, -ECANCELED);
5701 io_put_req_deferred(req, 1);
5702 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005703}
5704
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005705static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5706 struct timespec64 *ts, enum hrtimer_mode mode)
5707{
5708 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5709 struct io_timeout_data *data;
5710
5711 if (IS_ERR(req))
5712 return PTR_ERR(req);
5713
5714 req->timeout.off = 0; /* noseq */
5715 data = req->async_data;
5716 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5717 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5718 data->timer.function = io_timeout_fn;
5719 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5720 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005721}
5722
Jens Axboe3529d8c2019-12-19 18:24:38 -07005723static int io_timeout_remove_prep(struct io_kiocb *req,
5724 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005725{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005726 struct io_timeout_rem *tr = &req->timeout_rem;
5727
Jens Axboeb29472e2019-12-17 18:50:29 -07005728 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5729 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005730 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5731 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005732 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005733 return -EINVAL;
5734
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005735 tr->addr = READ_ONCE(sqe->addr);
5736 tr->flags = READ_ONCE(sqe->timeout_flags);
5737 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5738 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5739 return -EINVAL;
5740 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5741 return -EFAULT;
5742 } else if (tr->flags) {
5743 /* timeout removal doesn't support flags */
5744 return -EINVAL;
5745 }
5746
Jens Axboeb29472e2019-12-17 18:50:29 -07005747 return 0;
5748}
5749
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005750static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5751{
5752 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5753 : HRTIMER_MODE_REL;
5754}
5755
Jens Axboe11365042019-10-16 09:08:32 -06005756/*
5757 * Remove or update an existing timeout command
5758 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005759static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005760{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005761 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005762 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005763 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005764
Jens Axboe11365042019-10-16 09:08:32 -06005765 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005766 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005767 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005768 else
5769 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5770 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005771
Jens Axboe47f46762019-11-09 17:43:02 -07005772 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005773 io_commit_cqring(ctx);
5774 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005775 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005776 if (ret < 0)
5777 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005778 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005779 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005780}
5781
Jens Axboe3529d8c2019-12-19 18:24:38 -07005782static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005783 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005784{
Jens Axboead8a48a2019-11-15 08:49:11 -07005785 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005786 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005787 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005788
Jens Axboead8a48a2019-11-15 08:49:11 -07005789 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005790 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005791 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005792 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005793 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005794 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005795 flags = READ_ONCE(sqe->timeout_flags);
5796 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005797 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005798
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005799 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005800
Jens Axboee8c2bc12020-08-15 18:44:09 -07005801 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005802 return -ENOMEM;
5803
Jens Axboee8c2bc12020-08-15 18:44:09 -07005804 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005805 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005806
5807 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005808 return -EFAULT;
5809
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005810 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005811 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5812 return 0;
5813}
5814
Pavel Begunkov61e98202021-02-10 00:03:08 +00005815static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005816{
Jens Axboead8a48a2019-11-15 08:49:11 -07005817 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005818 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005819 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005820 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005821
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005822 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005823
Jens Axboe5262f562019-09-17 12:26:57 -06005824 /*
5825 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005826 * timeout event to be satisfied. If it isn't set, then this is
5827 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005828 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005829 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005830 entry = ctx->timeout_list.prev;
5831 goto add;
5832 }
Jens Axboe5262f562019-09-17 12:26:57 -06005833
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005834 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5835 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005836
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005837 /* Update the last seq here in case io_flush_timeouts() hasn't.
5838 * This is safe because ->completion_lock is held, and submissions
5839 * and completions are never mixed in the same ->completion_lock section.
5840 */
5841 ctx->cq_last_tm_flush = tail;
5842
Jens Axboe5262f562019-09-17 12:26:57 -06005843 /*
5844 * Insertion sort, ensuring the first entry in the list is always
5845 * the one we need first.
5846 */
Jens Axboe5262f562019-09-17 12:26:57 -06005847 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005848 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5849 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005850
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005851 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005852 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005853 /* nxt.seq is behind @tail, otherwise would've been completed */
5854 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005855 break;
5856 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005857add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005858 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005859 data->timer.function = io_timeout_fn;
5860 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005861 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005862 return 0;
5863}
5864
Jens Axboe62755e32019-10-28 21:49:21 -06005865static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005866{
Jens Axboe62755e32019-10-28 21:49:21 -06005867 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005868
Jens Axboe62755e32019-10-28 21:49:21 -06005869 return req->user_data == (unsigned long) data;
5870}
5871
Jens Axboee977d6d2019-11-05 12:39:45 -07005872static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005873{
Jens Axboe62755e32019-10-28 21:49:21 -06005874 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005875 int ret = 0;
5876
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005877 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005878 switch (cancel_ret) {
5879 case IO_WQ_CANCEL_OK:
5880 ret = 0;
5881 break;
5882 case IO_WQ_CANCEL_RUNNING:
5883 ret = -EALREADY;
5884 break;
5885 case IO_WQ_CANCEL_NOTFOUND:
5886 ret = -ENOENT;
5887 break;
5888 }
5889
Jens Axboee977d6d2019-11-05 12:39:45 -07005890 return ret;
5891}
5892
Jens Axboe47f46762019-11-09 17:43:02 -07005893static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5894 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005895 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005896{
5897 unsigned long flags;
5898 int ret;
5899
5900 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5901 if (ret != -ENOENT) {
5902 spin_lock_irqsave(&ctx->completion_lock, flags);
5903 goto done;
5904 }
5905
5906 spin_lock_irqsave(&ctx->completion_lock, flags);
5907 ret = io_timeout_cancel(ctx, sqe_addr);
5908 if (ret != -ENOENT)
5909 goto done;
5910 ret = io_poll_cancel(ctx, sqe_addr);
5911done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005912 if (!ret)
5913 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005914 io_cqring_fill_event(req, ret);
5915 io_commit_cqring(ctx);
5916 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5917 io_cqring_ev_posted(ctx);
5918
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005919 if (ret < 0)
5920 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005921 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005922}
5923
Jens Axboe3529d8c2019-12-19 18:24:38 -07005924static int io_async_cancel_prep(struct io_kiocb *req,
5925 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005926{
Jens Axboefbf23842019-12-17 18:45:56 -07005927 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005928 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005929 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5930 return -EINVAL;
5931 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005932 return -EINVAL;
5933
Jens Axboefbf23842019-12-17 18:45:56 -07005934 req->cancel.addr = READ_ONCE(sqe->addr);
5935 return 0;
5936}
5937
Pavel Begunkov61e98202021-02-10 00:03:08 +00005938static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005939{
5940 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005941
Pavel Begunkov014db002020-03-03 21:33:12 +03005942 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005943 return 0;
5944}
5945
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005946static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005947 const struct io_uring_sqe *sqe)
5948{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005949 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5950 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005951 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5952 return -EINVAL;
5953 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005954 return -EINVAL;
5955
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005956 req->rsrc_update.offset = READ_ONCE(sqe->off);
5957 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5958 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005959 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005960 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005961 return 0;
5962}
5963
Pavel Begunkov889fca72021-02-10 00:03:09 +00005964static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005965{
5966 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005967 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005968 int ret;
5969
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005970 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005971 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005972
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005973 up.offset = req->rsrc_update.offset;
5974 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005975
5976 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005977 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005978 mutex_unlock(&ctx->uring_lock);
5979
5980 if (ret < 0)
5981 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005982 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005983 return 0;
5984}
5985
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005986static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005987{
Jens Axboed625c6e2019-12-17 19:53:05 -07005988 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005989 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005990 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005991 case IORING_OP_READV:
5992 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005993 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005994 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005995 case IORING_OP_WRITEV:
5996 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005997 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005998 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005999 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006000 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006001 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006002 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006003 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006004 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006005 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006006 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006007 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006008 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006009 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006010 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006011 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006012 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006013 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006014 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006015 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006016 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006017 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006018 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006019 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006020 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006021 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006022 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006023 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006024 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006025 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006026 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006027 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006028 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006029 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006030 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006031 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006032 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006033 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006035 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006036 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006037 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006038 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006039 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006040 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006041 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006042 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006043 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006044 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006045 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006047 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006048 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006049 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006050 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006051 case IORING_OP_SHUTDOWN:
6052 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006053 case IORING_OP_RENAMEAT:
6054 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006055 case IORING_OP_UNLINKAT:
6056 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006057 }
6058
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006059 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6060 req->opcode);
6061 return-EINVAL;
6062}
6063
Jens Axboedef596e2019-01-09 08:59:42 -07006064static int io_req_defer_prep(struct io_kiocb *req,
6065 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006066{
Jens Axboedef596e2019-01-09 08:59:42 -07006067 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006069 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006070 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006071 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006072}
6073
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006074static u32 io_get_sequence(struct io_kiocb *req)
6075{
6076 struct io_kiocb *pos;
6077 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006078 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006079
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006080 io_for_each_link(pos, req)
6081 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006082
6083 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6084 return total_submitted - nr_reqs;
6085}
6086
Jens Axboe3529d8c2019-12-19 18:24:38 -07006087static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006088{
6089 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006090 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006092 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006093
6094 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006095 if (likely(list_empty_careful(&ctx->defer_list) &&
6096 !(req->flags & REQ_F_IO_DRAIN)))
6097 return 0;
6098
6099 seq = io_get_sequence(req);
6100 /* Still a chance to pass the sequence check */
6101 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006102 return 0;
6103
Jens Axboee8c2bc12020-08-15 18:44:09 -07006104 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006105 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006106 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006107 return ret;
6108 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006109 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006110 de = kmalloc(sizeof(*de), GFP_KERNEL);
6111 if (!de)
6112 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006113
6114 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006115 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006116 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006117 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006118 io_queue_async_work(req);
6119 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006120 }
6121
6122 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006123 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006124 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006125 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006126 spin_unlock_irq(&ctx->completion_lock);
6127 return -EIOCBQUEUED;
6128}
Jens Axboeedafcce2019-01-09 09:16:05 -07006129
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006130static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006131{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006132 if (req->flags & REQ_F_BUFFER_SELECTED) {
6133 switch (req->opcode) {
6134 case IORING_OP_READV:
6135 case IORING_OP_READ_FIXED:
6136 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006137 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006138 break;
6139 case IORING_OP_RECVMSG:
6140 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006141 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006142 break;
6143 }
6144 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006145 }
6146
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006147 if (req->flags & REQ_F_NEED_CLEANUP) {
6148 switch (req->opcode) {
6149 case IORING_OP_READV:
6150 case IORING_OP_READ_FIXED:
6151 case IORING_OP_READ:
6152 case IORING_OP_WRITEV:
6153 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006154 case IORING_OP_WRITE: {
6155 struct io_async_rw *io = req->async_data;
6156 if (io->free_iovec)
6157 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006158 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006159 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006160 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006161 case IORING_OP_SENDMSG: {
6162 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006163
6164 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006165 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006166 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006167 case IORING_OP_SPLICE:
6168 case IORING_OP_TEE:
6169 io_put_file(req, req->splice.file_in,
6170 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6171 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006172 case IORING_OP_OPENAT:
6173 case IORING_OP_OPENAT2:
6174 if (req->open.filename)
6175 putname(req->open.filename);
6176 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006177 case IORING_OP_RENAMEAT:
6178 putname(req->rename.oldpath);
6179 putname(req->rename.newpath);
6180 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006181 case IORING_OP_UNLINKAT:
6182 putname(req->unlink.filename);
6183 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006184 }
6185 req->flags &= ~REQ_F_NEED_CLEANUP;
6186 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006187}
6188
Pavel Begunkov889fca72021-02-10 00:03:09 +00006189static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006190{
Jens Axboeedafcce2019-01-09 09:16:05 -07006191 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006192 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006193
Jens Axboed625c6e2019-12-17 19:53:05 -07006194 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006195 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006196 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006197 break;
6198 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006199 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006200 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006201 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006202 break;
6203 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006205 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006206 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006207 break;
6208 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006209 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006210 break;
6211 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006212 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006213 break;
6214 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006215 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006216 break;
6217 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006218 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006219 break;
6220 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006221 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006222 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006223 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006224 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006225 break;
6226 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006227 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006228 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006229 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006230 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006231 break;
6232 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006233 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006234 break;
6235 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006236 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006237 break;
6238 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006239 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006240 break;
6241 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006242 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006243 break;
6244 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006245 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006247 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006248 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006249 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006250 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006251 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006252 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006253 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006254 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006255 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006256 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006257 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006258 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006259 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006260 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006261 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006262 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006263 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006264 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006265 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006266 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006267 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006268 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006269 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006270 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006271 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006272 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006273 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006274 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006275 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006276 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006277 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006278 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006279 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006280 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006281 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006282 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006283 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006284 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006285 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006286 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006287 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006288 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006289 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006290 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006291 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006292 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006293 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006294 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295 default:
6296 ret = -EINVAL;
6297 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006298 }
6299
6300 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006301 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302
Jens Axboeb5325762020-05-19 21:20:27 -06006303 /* If the op doesn't have a file, we're not polling for it */
6304 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006305 const bool in_async = io_wq_current_is_worker();
6306
Jens Axboe11ba8202020-01-15 21:51:17 -07006307 /* workqueue context doesn't hold uring_lock, grab it now */
6308 if (in_async)
6309 mutex_lock(&ctx->uring_lock);
6310
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006311 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006312
6313 if (in_async)
6314 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315 }
6316
6317 return 0;
6318}
6319
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006320static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006321{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006322 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006323 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006324 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006325
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006326 timeout = io_prep_linked_timeout(req);
6327 if (timeout)
6328 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006329
Jens Axboe4014d942021-01-19 15:53:54 -07006330 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006331 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006332
Jens Axboe561fb042019-10-24 07:25:42 -06006333 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006334 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006335 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006336 /*
6337 * We can get EAGAIN for polled IO even though we're
6338 * forcing a sync submission from here, since we can't
6339 * wait for request slots on the block side.
6340 */
6341 if (ret != -EAGAIN)
6342 break;
6343 cond_resched();
6344 } while (1);
6345 }
Jens Axboe31b51512019-01-18 22:56:34 -07006346
Jens Axboe561fb042019-10-24 07:25:42 -06006347 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006348 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006349
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006350 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6351 lock_ctx = req->ctx;
6352
6353 /*
6354 * io_iopoll_complete() does not hold completion_lock to
6355 * complete polled io, so here for polled io, we can not call
6356 * io_req_complete() directly, otherwise there maybe concurrent
6357 * access to cqring, defer_list, etc, which is not safe. Given
6358 * that io_iopoll_complete() is always called under uring_lock,
6359 * so here for polled io, we also get uring_lock to complete
6360 * it.
6361 */
6362 if (lock_ctx)
6363 mutex_lock(&lock_ctx->uring_lock);
6364
6365 req_set_fail_links(req);
6366 io_req_complete(req, ret);
6367
6368 if (lock_ctx)
6369 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006370 }
Jens Axboe31b51512019-01-18 22:56:34 -07006371}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006372
Jens Axboe65e19f52019-10-26 07:20:21 -06006373static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6374 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006375{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006376 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006377
Jens Axboe05f3fb32019-12-09 11:22:50 -07006378 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006379 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006380}
6381
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006382static struct file *io_file_get(struct io_submit_state *state,
6383 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006384{
6385 struct io_ring_ctx *ctx = req->ctx;
6386 struct file *file;
6387
6388 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006389 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006390 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006391 fd = array_index_nospec(fd, ctx->nr_user_files);
6392 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006393 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006394 } else {
6395 trace_io_uring_file_get(ctx, fd);
6396 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006397 }
6398
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006399 if (file && unlikely(file->f_op == &io_uring_fops))
6400 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006401 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006402}
6403
Jens Axboe2665abf2019-11-05 12:40:47 -07006404static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6405{
Jens Axboead8a48a2019-11-15 08:49:11 -07006406 struct io_timeout_data *data = container_of(timer,
6407 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006408 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006409 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006410 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006411
6412 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006413 prev = req->timeout.head;
6414 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006415
6416 /*
6417 * We don't expect the list to be empty, that will only happen if we
6418 * race with the completion of the linked work.
6419 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006420 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006421 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006422 else
6423 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006424 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6425
6426 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006427 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006428 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006429 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006430 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006431 io_req_complete_post(req, -ETIME, 0);
6432 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006433 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006434 return HRTIMER_NORESTART;
6435}
6436
Jens Axboe7271ef32020-08-10 09:55:22 -06006437static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006438{
Jens Axboe76a46e02019-11-10 23:34:16 -07006439 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006440 * If the back reference is NULL, then our linked request finished
6441 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006442 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006443 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006444 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006445
Jens Axboead8a48a2019-11-15 08:49:11 -07006446 data->timer.function = io_link_timeout_fn;
6447 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6448 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006449 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006450}
6451
6452static void io_queue_linked_timeout(struct io_kiocb *req)
6453{
6454 struct io_ring_ctx *ctx = req->ctx;
6455
6456 spin_lock_irq(&ctx->completion_lock);
6457 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006458 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006459
Jens Axboe2665abf2019-11-05 12:40:47 -07006460 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006461 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006462}
6463
Jens Axboead8a48a2019-11-15 08:49:11 -07006464static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006465{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006466 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006467
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006468 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6469 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006470 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006471
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006472 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006473 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006474 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006475 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006476}
6477
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006478static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006479{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006480 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006481 const struct cred *old_creds = NULL;
Pavel Begunkov889fca72021-02-10 00:03:09 +00006482 int ret, issue_flags = IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006483
Pavel Begunkov889fca72021-02-10 00:03:09 +00006484 if (cs)
6485 issue_flags |= IO_URING_F_COMPLETE_DEFER;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006486again:
6487 linked_timeout = io_prep_linked_timeout(req);
6488
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006489 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6490 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006491 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006492 if (old_creds)
6493 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006494 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006495 old_creds = NULL; /* restored original creds */
6496 else
Jens Axboe98447d62020-10-14 10:48:51 -06006497 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006498 }
6499
Pavel Begunkov889fca72021-02-10 00:03:09 +00006500 ret = io_issue_sqe(req, issue_flags);
Jens Axboe491381ce2019-10-17 09:20:46 -06006501
6502 /*
6503 * We async punt it if the file wasn't marked NOWAIT, or if the file
6504 * doesn't support non-blocking read/write attempts
6505 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006506 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006507 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006508 /*
6509 * Queued up for async execution, worker will release
6510 * submit reference when the iocb is actually submitted.
6511 */
6512 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006513 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006514
Pavel Begunkovf063c542020-07-25 14:41:59 +03006515 if (linked_timeout)
6516 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006517 } else if (likely(!ret)) {
6518 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006519 if (req->flags & REQ_F_COMPLETE_INLINE) {
6520 list_add_tail(&req->compl.list, &cs->list);
6521 if (++cs->nr >= 32)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006522 io_submit_flush_completions(cs, req->ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006523 req = NULL;
6524 } else {
6525 req = io_put_req_find_next(req);
6526 }
6527
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006528 if (linked_timeout)
6529 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006530
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006531 if (req) {
6532 if (!(req->flags & REQ_F_FORCE_ASYNC))
6533 goto again;
6534 io_queue_async_work(req);
6535 }
6536 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006537 /* un-prep timeout, so it'll be killed as any other linked */
6538 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006539 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006540 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006541 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006542 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006543
Jens Axboe193155c2020-02-22 23:22:19 -07006544 if (old_creds)
6545 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006546}
6547
Jens Axboef13fad72020-06-22 09:34:30 -06006548static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6549 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006550{
6551 int ret;
6552
Jens Axboe3529d8c2019-12-19 18:24:38 -07006553 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006554 if (ret) {
6555 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006556fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006557 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006558 io_put_req(req);
6559 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006560 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006561 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006562 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006563 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006564 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006565 goto fail_req;
6566 }
Jens Axboece35a472019-12-17 08:04:44 -07006567 io_queue_async_work(req);
6568 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006569 if (sqe) {
6570 ret = io_req_prep(req, sqe);
6571 if (unlikely(ret))
6572 goto fail_req;
6573 }
6574 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006575 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006576}
6577
Jens Axboef13fad72020-06-22 09:34:30 -06006578static inline void io_queue_link_head(struct io_kiocb *req,
6579 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006580{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006581 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006582 io_put_req(req);
6583 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006584 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006585 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006586}
6587
Pavel Begunkov863e0562020-10-27 23:25:35 +00006588struct io_submit_link {
6589 struct io_kiocb *head;
6590 struct io_kiocb *last;
6591};
6592
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006593static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006594 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006595{
Jackie Liua197f662019-11-08 08:09:12 -07006596 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006597 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006598
Jens Axboe9e645e112019-05-10 16:07:28 -06006599 /*
6600 * If we already have a head request, queue this one for async
6601 * submittal once the head completes. If we don't have a head but
6602 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6603 * submitted sync once the chain is complete. If none of those
6604 * conditions are true (normal request), then just queue it.
6605 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006606 if (link->head) {
6607 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006608
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006609 /*
6610 * Taking sequential execution of a link, draining both sides
6611 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6612 * requests in the link. So, it drains the head and the
6613 * next after the link request. The last one is done via
6614 * drain_next flag to persist the effect across calls.
6615 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006616 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006617 head->flags |= REQ_F_IO_DRAIN;
6618 ctx->drain_next = 1;
6619 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006620 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006621 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006622 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006623 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006624 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006625 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006626 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006627 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006628 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006629
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006630 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006631 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006632 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006633 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006634 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006635 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006636 if (unlikely(ctx->drain_next)) {
6637 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006638 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006639 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006640 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006641 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006642 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006643 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006644 link->head = req;
6645 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006646 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006647 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006648 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006649 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006650
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006651 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006652}
6653
Jens Axboe9a56a232019-01-09 09:06:50 -07006654/*
6655 * Batched submission is done, ensure local IO is flushed out.
6656 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006657static void io_submit_state_end(struct io_submit_state *state,
6658 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006659{
Jens Axboef13fad72020-06-22 09:34:30 -06006660 if (!list_empty(&state->comp.list))
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006661 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006662 if (state->plug_started)
6663 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006664 io_state_file_put(state);
Pavel Begunkov50872752021-02-10 00:03:12 +00006665 if (state->free_reqs) {
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006666 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Pavel Begunkov50872752021-02-10 00:03:12 +00006667 state->free_reqs = 0;
6668 }
Jens Axboe9a56a232019-01-09 09:06:50 -07006669}
6670
6671/*
6672 * Start submission side cache.
6673 */
6674static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006675 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006676{
Jens Axboe27926b62020-10-28 09:33:23 -06006677 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006678 state->ios_left = max_ios;
6679}
6680
Jens Axboe2b188cc2019-01-07 10:46:33 -07006681static void io_commit_sqring(struct io_ring_ctx *ctx)
6682{
Hristo Venev75b28af2019-08-26 17:23:46 +00006683 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006684
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006685 /*
6686 * Ensure any loads from the SQEs are done at this point,
6687 * since once we write the new head, the application could
6688 * write new data to them.
6689 */
6690 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006691}
6692
6693/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006694 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006695 * that is mapped by userspace. This means that care needs to be taken to
6696 * ensure that reads are stable, as we cannot rely on userspace always
6697 * being a good citizen. If members of the sqe are validated and then later
6698 * used, it's important that those reads are done through READ_ONCE() to
6699 * prevent a re-load down the line.
6700 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006701static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006702{
Hristo Venev75b28af2019-08-26 17:23:46 +00006703 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006704 unsigned head;
6705
6706 /*
6707 * The cached sq head (or cq tail) serves two purposes:
6708 *
6709 * 1) allows us to batch the cost of updating the user visible
6710 * head updates.
6711 * 2) allows the kernel side to track the head on its own, even
6712 * though the application is the one updating it.
6713 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006714 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006715 if (likely(head < ctx->sq_entries))
6716 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006717
6718 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006719 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006720 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006721 return NULL;
6722}
6723
6724static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6725{
6726 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006727}
6728
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006729/*
6730 * Check SQE restrictions (opcode and flags).
6731 *
6732 * Returns 'true' if SQE is allowed, 'false' otherwise.
6733 */
6734static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6735 struct io_kiocb *req,
6736 unsigned int sqe_flags)
6737{
6738 if (!ctx->restricted)
6739 return true;
6740
6741 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6742 return false;
6743
6744 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6745 ctx->restrictions.sqe_flags_required)
6746 return false;
6747
6748 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6749 ctx->restrictions.sqe_flags_required))
6750 return false;
6751
6752 return true;
6753}
6754
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006755#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6756 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6757 IOSQE_BUFFER_SELECT)
6758
6759static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006760 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006761{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006762 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006763 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006764 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006765
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006766 req->opcode = READ_ONCE(sqe->opcode);
6767 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006768 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006769 req->file = NULL;
6770 req->ctx = ctx;
6771 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006772 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006773 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006774 /* one is dropped after submission, the other at completion */
6775 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006776 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006777 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006778
6779 if (unlikely(req->opcode >= IORING_OP_LAST))
6780 return -EINVAL;
6781
Jens Axboe28cea78a2020-09-14 10:51:17 -06006782 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006783 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006784
6785 sqe_flags = READ_ONCE(sqe->flags);
6786 /* enforce forwards compatibility on users */
6787 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6788 return -EINVAL;
6789
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006790 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6791 return -EACCES;
6792
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006793 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6794 !io_op_defs[req->opcode].buffer_select)
6795 return -EOPNOTSUPP;
6796
6797 id = READ_ONCE(sqe->personality);
6798 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006799 struct io_identity *iod;
6800
Jens Axboe1e6fa522020-10-15 08:46:24 -06006801 iod = idr_find(&ctx->personality_idr, id);
6802 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006803 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006804 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006805
6806 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006807 get_cred(iod->creds);
6808 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006809 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006810 }
6811
6812 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006813 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006814 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006815
Jens Axboe27926b62020-10-28 09:33:23 -06006816 /*
6817 * Plug now if we have more than 1 IO left after this, and the target
6818 * is potentially a read/write to block based storage.
6819 */
6820 if (!state->plug_started && state->ios_left > 1 &&
6821 io_op_defs[req->opcode].plug) {
6822 blk_start_plug(&state->plug);
6823 state->plug_started = true;
6824 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006825
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006826 ret = 0;
6827 if (io_op_defs[req->opcode].needs_file) {
6828 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006829
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006830 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006831 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006832 ret = -EBADF;
6833 }
6834
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006835 state->ios_left--;
6836 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006837}
6838
Jens Axboe0f212202020-09-13 13:09:39 -06006839static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006840{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006841 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006842 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006843
Jens Axboec4a2ed72019-11-21 21:01:26 -07006844 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006845 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006846 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006847 return -EBUSY;
6848 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006849
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006850 /* make sure SQ entry isn't read before tail */
6851 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006852
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006853 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6854 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006855
Jens Axboed8a6df12020-10-15 16:24:45 -06006856 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006857 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006858
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006859 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006860 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006861
Jens Axboe6c271ce2019-01-10 11:22:30 -07006862 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006863 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006864 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006865 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006866
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006867 sqe = io_get_sqe(ctx);
6868 if (unlikely(!sqe)) {
6869 io_consume_sqe(ctx);
6870 break;
6871 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006872 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006873 if (unlikely(!req)) {
6874 if (!submitted)
6875 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006876 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006877 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006878 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006879 /* will complete beyond this point, count as submitted */
6880 submitted++;
6881
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006882 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006883 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006884fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006885 io_put_req(req);
6886 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006887 break;
6888 }
6889
Jens Axboe354420f2020-01-08 18:55:15 -07006890 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006891 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006892 err = io_submit_sqe(req, sqe, &link, &ctx->submit_state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006893 if (err)
6894 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006895 }
6896
Pavel Begunkov9466f432020-01-25 22:34:01 +03006897 if (unlikely(submitted != nr)) {
6898 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006899 struct io_uring_task *tctx = current->io_uring;
6900 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006901
Jens Axboed8a6df12020-10-15 16:24:45 -06006902 percpu_ref_put_many(&ctx->refs, unused);
6903 percpu_counter_sub(&tctx->inflight, unused);
6904 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006905 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006906 if (link.head)
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006907 io_queue_link_head(link.head, &ctx->submit_state.comp);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006908 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006909
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006910 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6911 io_commit_sqring(ctx);
6912
Jens Axboe6c271ce2019-01-10 11:22:30 -07006913 return submitted;
6914}
6915
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006916static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6917{
6918 /* Tell userspace we may need a wakeup call */
6919 spin_lock_irq(&ctx->completion_lock);
6920 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6921 spin_unlock_irq(&ctx->completion_lock);
6922}
6923
6924static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6925{
6926 spin_lock_irq(&ctx->completion_lock);
6927 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6928 spin_unlock_irq(&ctx->completion_lock);
6929}
6930
Xiaoguang Wang08369242020-11-03 14:15:59 +08006931static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006932{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006933 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006934 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006935
Jens Axboec8d1ba52020-09-14 11:07:26 -06006936 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006937 /* if we're handling multiple rings, cap submit size for fairness */
6938 if (cap_entries && to_submit > 8)
6939 to_submit = 8;
6940
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006941 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6942 unsigned nr_events = 0;
6943
Xiaoguang Wang08369242020-11-03 14:15:59 +08006944 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006945 if (!list_empty(&ctx->iopoll_list))
6946 io_do_iopoll(ctx, &nr_events, 0);
6947
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006948 if (to_submit && !ctx->sqo_dead &&
6949 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006950 ret = io_submit_sqes(ctx, to_submit);
6951 mutex_unlock(&ctx->uring_lock);
6952 }
Jens Axboe90554202020-09-03 12:12:41 -06006953
6954 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6955 wake_up(&ctx->sqo_sq_wait);
6956
Xiaoguang Wang08369242020-11-03 14:15:59 +08006957 return ret;
6958}
6959
6960static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6961{
6962 struct io_ring_ctx *ctx;
6963 unsigned sq_thread_idle = 0;
6964
6965 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6966 if (sq_thread_idle < ctx->sq_thread_idle)
6967 sq_thread_idle = ctx->sq_thread_idle;
6968 }
6969
6970 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006971}
6972
Jens Axboe69fb2132020-09-14 11:16:23 -06006973static void io_sqd_init_new(struct io_sq_data *sqd)
6974{
6975 struct io_ring_ctx *ctx;
6976
6977 while (!list_empty(&sqd->ctx_new_list)) {
6978 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006979 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6980 complete(&ctx->sq_thread_comp);
6981 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006982
6983 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006984}
6985
Jens Axboe6c271ce2019-01-10 11:22:30 -07006986static int io_sq_thread(void *data)
6987{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006988 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006989 struct files_struct *old_files = current->files;
6990 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006991 const struct cred *old_cred = NULL;
6992 struct io_sq_data *sqd = data;
6993 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006994 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006995 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006996
Jens Axboe28cea78a2020-09-14 10:51:17 -06006997 task_lock(current);
6998 current->files = NULL;
6999 current->nsproxy = NULL;
7000 task_unlock(current);
7001
Jens Axboe69fb2132020-09-14 11:16:23 -06007002 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007003 int ret;
7004 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007005
7006 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007007 * Any changes to the sqd lists are synchronized through the
7008 * kthread parking. This synchronizes the thread vs users,
7009 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007010 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007011 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007012 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007013 /*
7014 * When sq thread is unparked, in case the previous park operation
7015 * comes from io_put_sq_data(), which means that sq thread is going
7016 * to be stopped, so here needs to have a check.
7017 */
7018 if (kthread_should_stop())
7019 break;
7020 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007021
Xiaoguang Wang08369242020-11-03 14:15:59 +08007022 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007023 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007024 timeout = jiffies + sqd->sq_thread_idle;
7025 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007026
Xiaoguang Wang08369242020-11-03 14:15:59 +08007027 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007028 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007029 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7030 if (current->cred != ctx->creds) {
7031 if (old_cred)
7032 revert_creds(old_cred);
7033 old_cred = override_creds(ctx->creds);
7034 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007035 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007036#ifdef CONFIG_AUDIT
7037 current->loginuid = ctx->loginuid;
7038 current->sessionid = ctx->sessionid;
7039#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007040
Xiaoguang Wang08369242020-11-03 14:15:59 +08007041 ret = __io_sq_thread(ctx, cap_entries);
7042 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7043 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007044
Jens Axboe28cea78a2020-09-14 10:51:17 -06007045 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007046 }
7047
Xiaoguang Wang08369242020-11-03 14:15:59 +08007048 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007049 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007050 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007051 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007052 if (sqt_spin)
7053 timeout = jiffies + sqd->sq_thread_idle;
7054 continue;
7055 }
7056
Xiaoguang Wang08369242020-11-03 14:15:59 +08007057 needs_sched = true;
7058 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7059 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7060 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7061 !list_empty_careful(&ctx->iopoll_list)) {
7062 needs_sched = false;
7063 break;
7064 }
7065 if (io_sqring_entries(ctx)) {
7066 needs_sched = false;
7067 break;
7068 }
7069 }
7070
Hao Xu8b28fdf2021-01-31 22:39:04 +08007071 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007072 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7073 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007074
Jens Axboe69fb2132020-09-14 11:16:23 -06007075 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007076 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7077 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007078 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007079
7080 finish_wait(&sqd->wait, &wait);
7081 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007082 }
7083
Jens Axboe4c6e2772020-07-01 11:29:10 -06007084 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007085 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007086
Dennis Zhou91d8f512020-09-16 13:41:05 -07007087 if (cur_css)
7088 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007089 if (old_cred)
7090 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007091
Jens Axboe28cea78a2020-09-14 10:51:17 -06007092 task_lock(current);
7093 current->files = old_files;
7094 current->nsproxy = old_nsproxy;
7095 task_unlock(current);
7096
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007097 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007098
Jens Axboe6c271ce2019-01-10 11:22:30 -07007099 return 0;
7100}
7101
Jens Axboebda52162019-09-24 13:47:15 -06007102struct io_wait_queue {
7103 struct wait_queue_entry wq;
7104 struct io_ring_ctx *ctx;
7105 unsigned to_wait;
7106 unsigned nr_timeouts;
7107};
7108
Pavel Begunkov6c503152021-01-04 20:36:36 +00007109static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007110{
7111 struct io_ring_ctx *ctx = iowq->ctx;
7112
7113 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007114 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007115 * started waiting. For timeouts, we always want to return to userspace,
7116 * regardless of event count.
7117 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007118 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007119 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7120}
7121
7122static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7123 int wake_flags, void *key)
7124{
7125 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7126 wq);
7127
Pavel Begunkov6c503152021-01-04 20:36:36 +00007128 /*
7129 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7130 * the task, and the next invocation will do it.
7131 */
7132 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7133 return autoremove_wake_function(curr, mode, wake_flags, key);
7134 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007135}
7136
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007137static int io_run_task_work_sig(void)
7138{
7139 if (io_run_task_work())
7140 return 1;
7141 if (!signal_pending(current))
7142 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007143 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7144 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007145 return -EINTR;
7146}
7147
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007148/* when returns >0, the caller should retry */
7149static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7150 struct io_wait_queue *iowq,
7151 signed long *timeout)
7152{
7153 int ret;
7154
7155 /* make sure we run task_work before checking for signals */
7156 ret = io_run_task_work_sig();
7157 if (ret || io_should_wake(iowq))
7158 return ret;
7159 /* let the caller flush overflows, retry */
7160 if (test_bit(0, &ctx->cq_check_overflow))
7161 return 1;
7162
7163 *timeout = schedule_timeout(*timeout);
7164 return !*timeout ? -ETIME : 1;
7165}
7166
Jens Axboe2b188cc2019-01-07 10:46:33 -07007167/*
7168 * Wait until events become available, if we don't already have some. The
7169 * application must reap them itself, as they reside on the shared cq ring.
7170 */
7171static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007172 const sigset_t __user *sig, size_t sigsz,
7173 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007174{
Jens Axboebda52162019-09-24 13:47:15 -06007175 struct io_wait_queue iowq = {
7176 .wq = {
7177 .private = current,
7178 .func = io_wake_function,
7179 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7180 },
7181 .ctx = ctx,
7182 .to_wait = min_events,
7183 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007184 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007185 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7186 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007187
Jens Axboeb41e9852020-02-17 09:52:41 -07007188 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007189 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7190 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007191 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007192 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007193 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007194 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007195
7196 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007197#ifdef CONFIG_COMPAT
7198 if (in_compat_syscall())
7199 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007200 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007201 else
7202#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007203 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007204
Jens Axboe2b188cc2019-01-07 10:46:33 -07007205 if (ret)
7206 return ret;
7207 }
7208
Hao Xuc73ebb62020-11-03 10:54:37 +08007209 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007210 struct timespec64 ts;
7211
Hao Xuc73ebb62020-11-03 10:54:37 +08007212 if (get_timespec64(&ts, uts))
7213 return -EFAULT;
7214 timeout = timespec64_to_jiffies(&ts);
7215 }
7216
Jens Axboebda52162019-09-24 13:47:15 -06007217 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007218 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007219 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007220 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007221 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7222 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007223 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7224 finish_wait(&ctx->wait, &iowq.wq);
7225 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007226
Jens Axboeb7db41c2020-07-04 08:55:50 -06007227 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007228
Hristo Venev75b28af2019-08-26 17:23:46 +00007229 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007230}
7231
Jens Axboe6b063142019-01-10 22:13:58 -07007232static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7233{
7234#if defined(CONFIG_UNIX)
7235 if (ctx->ring_sock) {
7236 struct sock *sock = ctx->ring_sock->sk;
7237 struct sk_buff *skb;
7238
7239 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7240 kfree_skb(skb);
7241 }
7242#else
7243 int i;
7244
Jens Axboe65e19f52019-10-26 07:20:21 -06007245 for (i = 0; i < ctx->nr_user_files; i++) {
7246 struct file *file;
7247
7248 file = io_file_from_index(ctx, i);
7249 if (file)
7250 fput(file);
7251 }
Jens Axboe6b063142019-01-10 22:13:58 -07007252#endif
7253}
7254
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007255static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007256{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007257 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007258
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007259 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007260 complete(&data->done);
7261}
7262
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007263static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7264{
7265 spin_lock_bh(&ctx->rsrc_ref_lock);
7266}
7267
7268static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7269{
7270 spin_unlock_bh(&ctx->rsrc_ref_lock);
7271}
7272
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007273static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7274 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007275 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007276{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007277 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007278 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007279 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007280 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007281 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007282}
7283
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007284static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7285 struct io_ring_ctx *ctx,
7286 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007287{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007288 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007289 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007290
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007291 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007292 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007293 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007294 if (ref_node)
7295 percpu_ref_kill(&ref_node->refs);
7296
7297 percpu_ref_kill(&data->refs);
7298
7299 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007300 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007301 do {
7302 ret = wait_for_completion_interruptible(&data->done);
7303 if (!ret)
7304 break;
7305 ret = io_run_task_work_sig();
7306 if (ret < 0) {
7307 percpu_ref_resurrect(&data->refs);
7308 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007309 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007310 return ret;
7311 }
7312 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007313
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007314 destroy_fixed_rsrc_ref_node(backup_node);
7315 return 0;
7316}
7317
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007318static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7319{
7320 struct fixed_rsrc_data *data;
7321
7322 data = kzalloc(sizeof(*data), GFP_KERNEL);
7323 if (!data)
7324 return NULL;
7325
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007326 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007327 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7328 kfree(data);
7329 return NULL;
7330 }
7331 data->ctx = ctx;
7332 init_completion(&data->done);
7333 return data;
7334}
7335
7336static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7337{
7338 percpu_ref_exit(&data->refs);
7339 kfree(data->table);
7340 kfree(data);
7341}
7342
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007343static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7344{
7345 struct fixed_rsrc_data *data = ctx->file_data;
7346 struct fixed_rsrc_ref_node *backup_node;
7347 unsigned nr_tables, i;
7348 int ret;
7349
7350 if (!data)
7351 return -ENXIO;
7352 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7353 if (!backup_node)
7354 return -ENOMEM;
7355 init_fixed_file_ref_node(ctx, backup_node);
7356
7357 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7358 if (ret)
7359 return ret;
7360
Jens Axboe6b063142019-01-10 22:13:58 -07007361 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007362 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7363 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007364 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007365 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007366 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007367 ctx->nr_user_files = 0;
7368 return 0;
7369}
7370
Jens Axboe534ca6d2020-09-02 13:52:19 -06007371static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007372{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007373 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007374 /*
7375 * The park is a bit of a work-around, without it we get
7376 * warning spews on shutdown with SQPOLL set and affinity
7377 * set to a single CPU.
7378 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007379 if (sqd->thread) {
7380 kthread_park(sqd->thread);
7381 kthread_stop(sqd->thread);
7382 }
7383
7384 kfree(sqd);
7385 }
7386}
7387
Jens Axboeaa061652020-09-02 14:50:27 -06007388static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7389{
7390 struct io_ring_ctx *ctx_attach;
7391 struct io_sq_data *sqd;
7392 struct fd f;
7393
7394 f = fdget(p->wq_fd);
7395 if (!f.file)
7396 return ERR_PTR(-ENXIO);
7397 if (f.file->f_op != &io_uring_fops) {
7398 fdput(f);
7399 return ERR_PTR(-EINVAL);
7400 }
7401
7402 ctx_attach = f.file->private_data;
7403 sqd = ctx_attach->sq_data;
7404 if (!sqd) {
7405 fdput(f);
7406 return ERR_PTR(-EINVAL);
7407 }
7408
7409 refcount_inc(&sqd->refs);
7410 fdput(f);
7411 return sqd;
7412}
7413
Jens Axboe534ca6d2020-09-02 13:52:19 -06007414static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7415{
7416 struct io_sq_data *sqd;
7417
Jens Axboeaa061652020-09-02 14:50:27 -06007418 if (p->flags & IORING_SETUP_ATTACH_WQ)
7419 return io_attach_sq_data(p);
7420
Jens Axboe534ca6d2020-09-02 13:52:19 -06007421 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7422 if (!sqd)
7423 return ERR_PTR(-ENOMEM);
7424
7425 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007426 INIT_LIST_HEAD(&sqd->ctx_list);
7427 INIT_LIST_HEAD(&sqd->ctx_new_list);
7428 mutex_init(&sqd->ctx_lock);
7429 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007430 init_waitqueue_head(&sqd->wait);
7431 return sqd;
7432}
7433
Jens Axboe69fb2132020-09-14 11:16:23 -06007434static void io_sq_thread_unpark(struct io_sq_data *sqd)
7435 __releases(&sqd->lock)
7436{
7437 if (!sqd->thread)
7438 return;
7439 kthread_unpark(sqd->thread);
7440 mutex_unlock(&sqd->lock);
7441}
7442
7443static void io_sq_thread_park(struct io_sq_data *sqd)
7444 __acquires(&sqd->lock)
7445{
7446 if (!sqd->thread)
7447 return;
7448 mutex_lock(&sqd->lock);
7449 kthread_park(sqd->thread);
7450}
7451
Jens Axboe534ca6d2020-09-02 13:52:19 -06007452static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7453{
7454 struct io_sq_data *sqd = ctx->sq_data;
7455
7456 if (sqd) {
7457 if (sqd->thread) {
7458 /*
7459 * We may arrive here from the error branch in
7460 * io_sq_offload_create() where the kthread is created
7461 * without being waked up, thus wake it up now to make
7462 * sure the wait will complete.
7463 */
7464 wake_up_process(sqd->thread);
7465 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007466
7467 io_sq_thread_park(sqd);
7468 }
7469
7470 mutex_lock(&sqd->ctx_lock);
7471 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007472 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007473 mutex_unlock(&sqd->ctx_lock);
7474
Xiaoguang Wang08369242020-11-03 14:15:59 +08007475 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007476 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007477
7478 io_put_sq_data(sqd);
7479 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007480 }
7481}
7482
Jens Axboe6b063142019-01-10 22:13:58 -07007483static void io_finish_async(struct io_ring_ctx *ctx)
7484{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007485 io_sq_thread_stop(ctx);
7486
Jens Axboe561fb042019-10-24 07:25:42 -06007487 if (ctx->io_wq) {
7488 io_wq_destroy(ctx->io_wq);
7489 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007490 }
7491}
7492
7493#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007494/*
7495 * Ensure the UNIX gc is aware of our file set, so we are certain that
7496 * the io_uring can be safely unregistered on process exit, even if we have
7497 * loops in the file referencing.
7498 */
7499static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7500{
7501 struct sock *sk = ctx->ring_sock->sk;
7502 struct scm_fp_list *fpl;
7503 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007504 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007505
Jens Axboe6b063142019-01-10 22:13:58 -07007506 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7507 if (!fpl)
7508 return -ENOMEM;
7509
7510 skb = alloc_skb(0, GFP_KERNEL);
7511 if (!skb) {
7512 kfree(fpl);
7513 return -ENOMEM;
7514 }
7515
7516 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007517
Jens Axboe08a45172019-10-03 08:11:03 -06007518 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007519 fpl->user = get_uid(ctx->user);
7520 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007521 struct file *file = io_file_from_index(ctx, i + offset);
7522
7523 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007524 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007525 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007526 unix_inflight(fpl->user, fpl->fp[nr_files]);
7527 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007528 }
7529
Jens Axboe08a45172019-10-03 08:11:03 -06007530 if (nr_files) {
7531 fpl->max = SCM_MAX_FD;
7532 fpl->count = nr_files;
7533 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007534 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007535 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7536 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007537
Jens Axboe08a45172019-10-03 08:11:03 -06007538 for (i = 0; i < nr_files; i++)
7539 fput(fpl->fp[i]);
7540 } else {
7541 kfree_skb(skb);
7542 kfree(fpl);
7543 }
Jens Axboe6b063142019-01-10 22:13:58 -07007544
7545 return 0;
7546}
7547
7548/*
7549 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7550 * causes regular reference counting to break down. We rely on the UNIX
7551 * garbage collection to take care of this problem for us.
7552 */
7553static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7554{
7555 unsigned left, total;
7556 int ret = 0;
7557
7558 total = 0;
7559 left = ctx->nr_user_files;
7560 while (left) {
7561 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007562
7563 ret = __io_sqe_files_scm(ctx, this_files, total);
7564 if (ret)
7565 break;
7566 left -= this_files;
7567 total += this_files;
7568 }
7569
7570 if (!ret)
7571 return 0;
7572
7573 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007574 struct file *file = io_file_from_index(ctx, total);
7575
7576 if (file)
7577 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007578 total++;
7579 }
7580
7581 return ret;
7582}
7583#else
7584static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7585{
7586 return 0;
7587}
7588#endif
7589
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007590static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007591 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007592{
7593 int i;
7594
7595 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007596 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007597 unsigned this_files;
7598
7599 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7600 table->files = kcalloc(this_files, sizeof(struct file *),
7601 GFP_KERNEL);
7602 if (!table->files)
7603 break;
7604 nr_files -= this_files;
7605 }
7606
7607 if (i == nr_tables)
7608 return 0;
7609
7610 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007611 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007612 kfree(table->files);
7613 }
7614 return 1;
7615}
7616
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007617static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007618{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007619 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007620#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007621 struct sock *sock = ctx->ring_sock->sk;
7622 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7623 struct sk_buff *skb;
7624 int i;
7625
7626 __skb_queue_head_init(&list);
7627
7628 /*
7629 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7630 * remove this entry and rearrange the file array.
7631 */
7632 skb = skb_dequeue(head);
7633 while (skb) {
7634 struct scm_fp_list *fp;
7635
7636 fp = UNIXCB(skb).fp;
7637 for (i = 0; i < fp->count; i++) {
7638 int left;
7639
7640 if (fp->fp[i] != file)
7641 continue;
7642
7643 unix_notinflight(fp->user, fp->fp[i]);
7644 left = fp->count - 1 - i;
7645 if (left) {
7646 memmove(&fp->fp[i], &fp->fp[i + 1],
7647 left * sizeof(struct file *));
7648 }
7649 fp->count--;
7650 if (!fp->count) {
7651 kfree_skb(skb);
7652 skb = NULL;
7653 } else {
7654 __skb_queue_tail(&list, skb);
7655 }
7656 fput(file);
7657 file = NULL;
7658 break;
7659 }
7660
7661 if (!file)
7662 break;
7663
7664 __skb_queue_tail(&list, skb);
7665
7666 skb = skb_dequeue(head);
7667 }
7668
7669 if (skb_peek(&list)) {
7670 spin_lock_irq(&head->lock);
7671 while ((skb = __skb_dequeue(&list)) != NULL)
7672 __skb_queue_tail(head, skb);
7673 spin_unlock_irq(&head->lock);
7674 }
7675#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007676 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007677#endif
7678}
7679
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007680static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007681{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007682 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7683 struct io_ring_ctx *ctx = rsrc_data->ctx;
7684 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007685
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007686 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7687 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007688 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007689 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007690 }
7691
Xiaoguang Wang05589552020-03-31 14:05:18 +08007692 percpu_ref_exit(&ref_node->refs);
7693 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007694 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007695}
7696
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007697static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007698{
7699 struct io_ring_ctx *ctx;
7700 struct llist_node *node;
7701
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007702 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7703 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007704
7705 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007706 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007707 struct llist_node *next = node->next;
7708
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007709 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7710 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007711 node = next;
7712 }
7713}
7714
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007715static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7716 unsigned i)
7717{
7718 struct fixed_rsrc_table *table;
7719
7720 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7721 return &table->files[i & IORING_FILE_TABLE_MASK];
7722}
7723
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007724static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007725{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007726 struct fixed_rsrc_ref_node *ref_node;
7727 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007728 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007729 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007730 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007731
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007732 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7733 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007734 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007735
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007736 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007737 ref_node->done = true;
7738
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007739 while (!list_empty(&ctx->rsrc_ref_list)) {
7740 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007741 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007742 /* recycle ref nodes in order */
7743 if (!ref_node->done)
7744 break;
7745 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007746 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007747 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007748 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007749
7750 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007751 delay = 0;
7752
Jens Axboe4a38aed22020-05-14 17:21:15 -06007753 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007754 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007755 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007756 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007757}
7758
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007759static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007760 struct io_ring_ctx *ctx)
7761{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007762 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007763
7764 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7765 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007766 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007767
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007768 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007769 0, GFP_KERNEL)) {
7770 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007771 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007772 }
7773 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007774 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007775 ref_node->done = false;
7776 return ref_node;
7777}
7778
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007779static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7780 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007781{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007782 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007783 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007784}
7785
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007786static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007787{
7788 percpu_ref_exit(&ref_node->refs);
7789 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007790}
7791
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007792
Jens Axboe05f3fb32019-12-09 11:22:50 -07007793static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7794 unsigned nr_args)
7795{
7796 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007797 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007799 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007800 struct fixed_rsrc_ref_node *ref_node;
7801 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007802
7803 if (ctx->file_data)
7804 return -EBUSY;
7805 if (!nr_args)
7806 return -EINVAL;
7807 if (nr_args > IORING_MAX_FIXED_FILES)
7808 return -EMFILE;
7809
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007810 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007811 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007812 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007813 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007814
7815 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007816 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007817 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007818 if (!file_data->table)
7819 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007820
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007821 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007822 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007823
7824 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007825 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7826 ret = -EFAULT;
7827 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007828 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007829 /* allow sparse sets */
7830 if (fd == -1)
7831 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007832
Jens Axboe05f3fb32019-12-09 11:22:50 -07007833 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007834 ret = -EBADF;
7835 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007836 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007837
7838 /*
7839 * Don't allow io_uring instances to be registered. If UNIX
7840 * isn't enabled, then this causes a reference cycle and this
7841 * instance can never get freed. If UNIX is enabled we'll
7842 * handle it just fine, but there's still no point in allowing
7843 * a ring fd as it doesn't support regular read/write anyway.
7844 */
7845 if (file->f_op == &io_uring_fops) {
7846 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007847 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007848 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007849 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850 }
7851
Jens Axboe05f3fb32019-12-09 11:22:50 -07007852 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007853 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007854 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007855 return ret;
7856 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007857
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007858 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007859 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007860 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007861 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007862 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007863 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007864
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007865 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007866 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007867out_fput:
7868 for (i = 0; i < ctx->nr_user_files; i++) {
7869 file = io_file_from_index(ctx, i);
7870 if (file)
7871 fput(file);
7872 }
7873 for (i = 0; i < nr_tables; i++)
7874 kfree(file_data->table[i].files);
7875 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007876out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007877 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007878 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007879 return ret;
7880}
7881
Jens Axboec3a31e62019-10-03 13:59:56 -06007882static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7883 int index)
7884{
7885#if defined(CONFIG_UNIX)
7886 struct sock *sock = ctx->ring_sock->sk;
7887 struct sk_buff_head *head = &sock->sk_receive_queue;
7888 struct sk_buff *skb;
7889
7890 /*
7891 * See if we can merge this file into an existing skb SCM_RIGHTS
7892 * file set. If there's no room, fall back to allocating a new skb
7893 * and filling it in.
7894 */
7895 spin_lock_irq(&head->lock);
7896 skb = skb_peek(head);
7897 if (skb) {
7898 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7899
7900 if (fpl->count < SCM_MAX_FD) {
7901 __skb_unlink(skb, head);
7902 spin_unlock_irq(&head->lock);
7903 fpl->fp[fpl->count] = get_file(file);
7904 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7905 fpl->count++;
7906 spin_lock_irq(&head->lock);
7907 __skb_queue_head(head, skb);
7908 } else {
7909 skb = NULL;
7910 }
7911 }
7912 spin_unlock_irq(&head->lock);
7913
7914 if (skb) {
7915 fput(file);
7916 return 0;
7917 }
7918
7919 return __io_sqe_files_scm(ctx, 1, index);
7920#else
7921 return 0;
7922#endif
7923}
7924
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007925static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007926{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007927 struct io_rsrc_put *prsrc;
7928 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007929
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007930 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7931 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007932 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007933
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007934 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007935 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007936
Hillf Dantona5318d32020-03-23 17:47:15 +08007937 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007938}
7939
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007940static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7941 struct file *file)
7942{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007943 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007944}
7945
Jens Axboe05f3fb32019-12-09 11:22:50 -07007946static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007947 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007948 unsigned nr_args)
7949{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007950 struct fixed_rsrc_data *data = ctx->file_data;
7951 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007952 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007953 __s32 __user *fds;
7954 int fd, i, err;
7955 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007956 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007957
Jens Axboe05f3fb32019-12-09 11:22:50 -07007958 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007959 return -EOVERFLOW;
7960 if (done > ctx->nr_user_files)
7961 return -EINVAL;
7962
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007963 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007964 if (!ref_node)
7965 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007966 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007967
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007968 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007969 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007970 err = 0;
7971 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7972 err = -EFAULT;
7973 break;
7974 }
noah4e0377a2021-01-26 15:23:28 -05007975 if (fd == IORING_REGISTER_FILES_SKIP)
7976 continue;
7977
Pavel Begunkov67973b92021-01-26 13:51:09 +00007978 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007979 file_slot = io_fixed_file_slot(ctx->file_data, i);
7980
7981 if (*file_slot) {
7982 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007983 if (err)
7984 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007985 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007986 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007987 }
7988 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007989 file = fget(fd);
7990 if (!file) {
7991 err = -EBADF;
7992 break;
7993 }
7994 /*
7995 * Don't allow io_uring instances to be registered. If
7996 * UNIX isn't enabled, then this causes a reference
7997 * cycle and this instance can never get freed. If UNIX
7998 * is enabled we'll handle it just fine, but there's
7999 * still no point in allowing a ring fd as it doesn't
8000 * support regular read/write anyway.
8001 */
8002 if (file->f_op == &io_uring_fops) {
8003 fput(file);
8004 err = -EBADF;
8005 break;
8006 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008007 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008008 if (err) {
8009 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008010 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008011 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008012 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008013 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008014 }
8015
Xiaoguang Wang05589552020-03-31 14:05:18 +08008016 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008017 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008018 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008019 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008020 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008021
8022 return done ? done : err;
8023}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008024
Jens Axboe05f3fb32019-12-09 11:22:50 -07008025static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8026 unsigned nr_args)
8027{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008028 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008029
8030 if (!ctx->file_data)
8031 return -ENXIO;
8032 if (!nr_args)
8033 return -EINVAL;
8034 if (copy_from_user(&up, arg, sizeof(up)))
8035 return -EFAULT;
8036 if (up.resv)
8037 return -EINVAL;
8038
8039 return __io_sqe_files_update(ctx, &up, nr_args);
8040}
Jens Axboec3a31e62019-10-03 13:59:56 -06008041
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008042static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008043{
8044 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8045
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008046 req = io_put_req_find_next(req);
8047 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008048}
8049
Pavel Begunkov24369c22020-01-28 03:15:48 +03008050static int io_init_wq_offload(struct io_ring_ctx *ctx,
8051 struct io_uring_params *p)
8052{
8053 struct io_wq_data data;
8054 struct fd f;
8055 struct io_ring_ctx *ctx_attach;
8056 unsigned int concurrency;
8057 int ret = 0;
8058
8059 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008060 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008061 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008062
8063 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8064 /* Do QD, or 4 * CPUS, whatever is smallest */
8065 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8066
8067 ctx->io_wq = io_wq_create(concurrency, &data);
8068 if (IS_ERR(ctx->io_wq)) {
8069 ret = PTR_ERR(ctx->io_wq);
8070 ctx->io_wq = NULL;
8071 }
8072 return ret;
8073 }
8074
8075 f = fdget(p->wq_fd);
8076 if (!f.file)
8077 return -EBADF;
8078
8079 if (f.file->f_op != &io_uring_fops) {
8080 ret = -EINVAL;
8081 goto out_fput;
8082 }
8083
8084 ctx_attach = f.file->private_data;
8085 /* @io_wq is protected by holding the fd */
8086 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8087 ret = -EINVAL;
8088 goto out_fput;
8089 }
8090
8091 ctx->io_wq = ctx_attach->io_wq;
8092out_fput:
8093 fdput(f);
8094 return ret;
8095}
8096
Jens Axboe0f212202020-09-13 13:09:39 -06008097static int io_uring_alloc_task_context(struct task_struct *task)
8098{
8099 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008100 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008101
8102 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8103 if (unlikely(!tctx))
8104 return -ENOMEM;
8105
Jens Axboed8a6df12020-10-15 16:24:45 -06008106 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8107 if (unlikely(ret)) {
8108 kfree(tctx);
8109 return ret;
8110 }
8111
Jens Axboe0f212202020-09-13 13:09:39 -06008112 xa_init(&tctx->xa);
8113 init_waitqueue_head(&tctx->wait);
8114 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008115 atomic_set(&tctx->in_idle, 0);
8116 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008117 io_init_identity(&tctx->__identity);
8118 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008119 task->io_uring = tctx;
8120 return 0;
8121}
8122
8123void __io_uring_free(struct task_struct *tsk)
8124{
8125 struct io_uring_task *tctx = tsk->io_uring;
8126
8127 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008128 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8129 if (tctx->identity != &tctx->__identity)
8130 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008131 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008132 kfree(tctx);
8133 tsk->io_uring = NULL;
8134}
8135
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008136static int io_sq_offload_create(struct io_ring_ctx *ctx,
8137 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008138{
8139 int ret;
8140
Jens Axboe6c271ce2019-01-10 11:22:30 -07008141 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008142 struct io_sq_data *sqd;
8143
Jens Axboe3ec482d2019-04-08 10:51:01 -06008144 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008145 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008146 goto err;
8147
Jens Axboe534ca6d2020-09-02 13:52:19 -06008148 sqd = io_get_sq_data(p);
8149 if (IS_ERR(sqd)) {
8150 ret = PTR_ERR(sqd);
8151 goto err;
8152 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008153
Jens Axboe534ca6d2020-09-02 13:52:19 -06008154 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008155 io_sq_thread_park(sqd);
8156 mutex_lock(&sqd->ctx_lock);
8157 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8158 mutex_unlock(&sqd->ctx_lock);
8159 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008160
Jens Axboe917257d2019-04-13 09:28:55 -06008161 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8162 if (!ctx->sq_thread_idle)
8163 ctx->sq_thread_idle = HZ;
8164
Jens Axboeaa061652020-09-02 14:50:27 -06008165 if (sqd->thread)
8166 goto done;
8167
Jens Axboe6c271ce2019-01-10 11:22:30 -07008168 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008169 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008170
Jens Axboe917257d2019-04-13 09:28:55 -06008171 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008172 if (cpu >= nr_cpu_ids)
8173 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008174 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008175 goto err;
8176
Jens Axboe69fb2132020-09-14 11:16:23 -06008177 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008178 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008179 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008180 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008181 "io_uring-sq");
8182 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008183 if (IS_ERR(sqd->thread)) {
8184 ret = PTR_ERR(sqd->thread);
8185 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008186 goto err;
8187 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008188 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008189 if (ret)
8190 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008191 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8192 /* Can't have SQ_AFF without SQPOLL */
8193 ret = -EINVAL;
8194 goto err;
8195 }
8196
Jens Axboeaa061652020-09-02 14:50:27 -06008197done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008198 ret = io_init_wq_offload(ctx, p);
8199 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008200 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008201
8202 return 0;
8203err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008204 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008205 return ret;
8206}
8207
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008208static void io_sq_offload_start(struct io_ring_ctx *ctx)
8209{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008210 struct io_sq_data *sqd = ctx->sq_data;
8211
8212 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8213 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008214}
8215
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008216static inline void __io_unaccount_mem(struct user_struct *user,
8217 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008218{
8219 atomic_long_sub(nr_pages, &user->locked_vm);
8220}
8221
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008222static inline int __io_account_mem(struct user_struct *user,
8223 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008224{
8225 unsigned long page_limit, cur_pages, new_pages;
8226
8227 /* Don't allow more pages than we can safely lock */
8228 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8229
8230 do {
8231 cur_pages = atomic_long_read(&user->locked_vm);
8232 new_pages = cur_pages + nr_pages;
8233 if (new_pages > page_limit)
8234 return -ENOMEM;
8235 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8236 new_pages) != cur_pages);
8237
8238 return 0;
8239}
8240
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008241static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8242 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008243{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008244 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008245 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008246
Jens Axboe2aede0e2020-09-14 10:45:53 -06008247 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008248 if (acct == ACCT_LOCKED) {
8249 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008250 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008251 mmap_write_unlock(ctx->mm_account);
8252 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008253 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008254 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008255 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008256}
8257
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008258static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8259 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008260{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008261 int ret;
8262
8263 if (ctx->limit_mem) {
8264 ret = __io_account_mem(ctx->user, nr_pages);
8265 if (ret)
8266 return ret;
8267 }
8268
Jens Axboe2aede0e2020-09-14 10:45:53 -06008269 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008270 if (acct == ACCT_LOCKED) {
8271 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008272 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008273 mmap_write_unlock(ctx->mm_account);
8274 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008275 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008276 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008277 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008278
8279 return 0;
8280}
8281
Jens Axboe2b188cc2019-01-07 10:46:33 -07008282static void io_mem_free(void *ptr)
8283{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008284 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008285
Mark Rutland52e04ef2019-04-30 17:30:21 +01008286 if (!ptr)
8287 return;
8288
8289 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008290 if (put_page_testzero(page))
8291 free_compound_page(page);
8292}
8293
8294static void *io_mem_alloc(size_t size)
8295{
8296 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8297 __GFP_NORETRY;
8298
8299 return (void *) __get_free_pages(gfp_flags, get_order(size));
8300}
8301
Hristo Venev75b28af2019-08-26 17:23:46 +00008302static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8303 size_t *sq_offset)
8304{
8305 struct io_rings *rings;
8306 size_t off, sq_array_size;
8307
8308 off = struct_size(rings, cqes, cq_entries);
8309 if (off == SIZE_MAX)
8310 return SIZE_MAX;
8311
8312#ifdef CONFIG_SMP
8313 off = ALIGN(off, SMP_CACHE_BYTES);
8314 if (off == 0)
8315 return SIZE_MAX;
8316#endif
8317
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008318 if (sq_offset)
8319 *sq_offset = off;
8320
Hristo Venev75b28af2019-08-26 17:23:46 +00008321 sq_array_size = array_size(sizeof(u32), sq_entries);
8322 if (sq_array_size == SIZE_MAX)
8323 return SIZE_MAX;
8324
8325 if (check_add_overflow(off, sq_array_size, &off))
8326 return SIZE_MAX;
8327
Hristo Venev75b28af2019-08-26 17:23:46 +00008328 return off;
8329}
8330
Jens Axboe2b188cc2019-01-07 10:46:33 -07008331static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8332{
Hristo Venev75b28af2019-08-26 17:23:46 +00008333 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008334
Hristo Venev75b28af2019-08-26 17:23:46 +00008335 pages = (size_t)1 << get_order(
8336 rings_size(sq_entries, cq_entries, NULL));
8337 pages += (size_t)1 << get_order(
8338 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008339
Hristo Venev75b28af2019-08-26 17:23:46 +00008340 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008341}
8342
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008343static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008344{
8345 int i, j;
8346
8347 if (!ctx->user_bufs)
8348 return -ENXIO;
8349
8350 for (i = 0; i < ctx->nr_user_bufs; i++) {
8351 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8352
8353 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008354 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008355
Jens Axboede293932020-09-17 16:19:16 -06008356 if (imu->acct_pages)
8357 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008358 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008359 imu->nr_bvecs = 0;
8360 }
8361
8362 kfree(ctx->user_bufs);
8363 ctx->user_bufs = NULL;
8364 ctx->nr_user_bufs = 0;
8365 return 0;
8366}
8367
8368static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8369 void __user *arg, unsigned index)
8370{
8371 struct iovec __user *src;
8372
8373#ifdef CONFIG_COMPAT
8374 if (ctx->compat) {
8375 struct compat_iovec __user *ciovs;
8376 struct compat_iovec ciov;
8377
8378 ciovs = (struct compat_iovec __user *) arg;
8379 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8380 return -EFAULT;
8381
Jens Axboed55e5f52019-12-11 16:12:15 -07008382 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008383 dst->iov_len = ciov.iov_len;
8384 return 0;
8385 }
8386#endif
8387 src = (struct iovec __user *) arg;
8388 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8389 return -EFAULT;
8390 return 0;
8391}
8392
Jens Axboede293932020-09-17 16:19:16 -06008393/*
8394 * Not super efficient, but this is just a registration time. And we do cache
8395 * the last compound head, so generally we'll only do a full search if we don't
8396 * match that one.
8397 *
8398 * We check if the given compound head page has already been accounted, to
8399 * avoid double accounting it. This allows us to account the full size of the
8400 * page, not just the constituent pages of a huge page.
8401 */
8402static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8403 int nr_pages, struct page *hpage)
8404{
8405 int i, j;
8406
8407 /* check current page array */
8408 for (i = 0; i < nr_pages; i++) {
8409 if (!PageCompound(pages[i]))
8410 continue;
8411 if (compound_head(pages[i]) == hpage)
8412 return true;
8413 }
8414
8415 /* check previously registered pages */
8416 for (i = 0; i < ctx->nr_user_bufs; i++) {
8417 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8418
8419 for (j = 0; j < imu->nr_bvecs; j++) {
8420 if (!PageCompound(imu->bvec[j].bv_page))
8421 continue;
8422 if (compound_head(imu->bvec[j].bv_page) == hpage)
8423 return true;
8424 }
8425 }
8426
8427 return false;
8428}
8429
8430static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8431 int nr_pages, struct io_mapped_ubuf *imu,
8432 struct page **last_hpage)
8433{
8434 int i, ret;
8435
8436 for (i = 0; i < nr_pages; i++) {
8437 if (!PageCompound(pages[i])) {
8438 imu->acct_pages++;
8439 } else {
8440 struct page *hpage;
8441
8442 hpage = compound_head(pages[i]);
8443 if (hpage == *last_hpage)
8444 continue;
8445 *last_hpage = hpage;
8446 if (headpage_already_acct(ctx, pages, i, hpage))
8447 continue;
8448 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8449 }
8450 }
8451
8452 if (!imu->acct_pages)
8453 return 0;
8454
8455 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8456 if (ret)
8457 imu->acct_pages = 0;
8458 return ret;
8459}
8460
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008461static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8462 struct io_mapped_ubuf *imu,
8463 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008464{
8465 struct vm_area_struct **vmas = NULL;
8466 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008467 unsigned long off, start, end, ubuf;
8468 size_t size;
8469 int ret, pret, nr_pages, i;
8470
8471 ubuf = (unsigned long) iov->iov_base;
8472 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8473 start = ubuf >> PAGE_SHIFT;
8474 nr_pages = end - start;
8475
8476 ret = -ENOMEM;
8477
8478 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8479 if (!pages)
8480 goto done;
8481
8482 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8483 GFP_KERNEL);
8484 if (!vmas)
8485 goto done;
8486
8487 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8488 GFP_KERNEL);
8489 if (!imu->bvec)
8490 goto done;
8491
8492 ret = 0;
8493 mmap_read_lock(current->mm);
8494 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8495 pages, vmas);
8496 if (pret == nr_pages) {
8497 /* don't support file backed memory */
8498 for (i = 0; i < nr_pages; i++) {
8499 struct vm_area_struct *vma = vmas[i];
8500
8501 if (vma->vm_file &&
8502 !is_file_hugepages(vma->vm_file)) {
8503 ret = -EOPNOTSUPP;
8504 break;
8505 }
8506 }
8507 } else {
8508 ret = pret < 0 ? pret : -EFAULT;
8509 }
8510 mmap_read_unlock(current->mm);
8511 if (ret) {
8512 /*
8513 * if we did partial map, or found file backed vmas,
8514 * release any pages we did get
8515 */
8516 if (pret > 0)
8517 unpin_user_pages(pages, pret);
8518 kvfree(imu->bvec);
8519 goto done;
8520 }
8521
8522 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8523 if (ret) {
8524 unpin_user_pages(pages, pret);
8525 kvfree(imu->bvec);
8526 goto done;
8527 }
8528
8529 off = ubuf & ~PAGE_MASK;
8530 size = iov->iov_len;
8531 for (i = 0; i < nr_pages; i++) {
8532 size_t vec_len;
8533
8534 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8535 imu->bvec[i].bv_page = pages[i];
8536 imu->bvec[i].bv_len = vec_len;
8537 imu->bvec[i].bv_offset = off;
8538 off = 0;
8539 size -= vec_len;
8540 }
8541 /* store original address for later verification */
8542 imu->ubuf = ubuf;
8543 imu->len = iov->iov_len;
8544 imu->nr_bvecs = nr_pages;
8545 ret = 0;
8546done:
8547 kvfree(pages);
8548 kvfree(vmas);
8549 return ret;
8550}
8551
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008552static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008553{
Jens Axboeedafcce2019-01-09 09:16:05 -07008554 if (ctx->user_bufs)
8555 return -EBUSY;
8556 if (!nr_args || nr_args > UIO_MAXIOV)
8557 return -EINVAL;
8558
8559 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8560 GFP_KERNEL);
8561 if (!ctx->user_bufs)
8562 return -ENOMEM;
8563
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008564 return 0;
8565}
8566
8567static int io_buffer_validate(struct iovec *iov)
8568{
8569 /*
8570 * Don't impose further limits on the size and buffer
8571 * constraints here, we'll -EINVAL later when IO is
8572 * submitted if they are wrong.
8573 */
8574 if (!iov->iov_base || !iov->iov_len)
8575 return -EFAULT;
8576
8577 /* arbitrary limit, but we need something */
8578 if (iov->iov_len > SZ_1G)
8579 return -EFAULT;
8580
8581 return 0;
8582}
8583
8584static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8585 unsigned int nr_args)
8586{
8587 int i, ret;
8588 struct iovec iov;
8589 struct page *last_hpage = NULL;
8590
8591 ret = io_buffers_map_alloc(ctx, nr_args);
8592 if (ret)
8593 return ret;
8594
Jens Axboeedafcce2019-01-09 09:16:05 -07008595 for (i = 0; i < nr_args; i++) {
8596 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008597
8598 ret = io_copy_iov(ctx, &iov, arg, i);
8599 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008600 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008601
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008602 ret = io_buffer_validate(&iov);
8603 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008604 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008605
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008606 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8607 if (ret)
8608 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008609
8610 ctx->nr_user_bufs++;
8611 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008612
8613 if (ret)
8614 io_sqe_buffers_unregister(ctx);
8615
Jens Axboeedafcce2019-01-09 09:16:05 -07008616 return ret;
8617}
8618
Jens Axboe9b402842019-04-11 11:45:41 -06008619static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8620{
8621 __s32 __user *fds = arg;
8622 int fd;
8623
8624 if (ctx->cq_ev_fd)
8625 return -EBUSY;
8626
8627 if (copy_from_user(&fd, fds, sizeof(*fds)))
8628 return -EFAULT;
8629
8630 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8631 if (IS_ERR(ctx->cq_ev_fd)) {
8632 int ret = PTR_ERR(ctx->cq_ev_fd);
8633 ctx->cq_ev_fd = NULL;
8634 return ret;
8635 }
8636
8637 return 0;
8638}
8639
8640static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8641{
8642 if (ctx->cq_ev_fd) {
8643 eventfd_ctx_put(ctx->cq_ev_fd);
8644 ctx->cq_ev_fd = NULL;
8645 return 0;
8646 }
8647
8648 return -ENXIO;
8649}
8650
Jens Axboe5a2e7452020-02-23 16:23:11 -07008651static int __io_destroy_buffers(int id, void *p, void *data)
8652{
8653 struct io_ring_ctx *ctx = data;
8654 struct io_buffer *buf = p;
8655
Jens Axboe067524e2020-03-02 16:32:28 -07008656 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008657 return 0;
8658}
8659
8660static void io_destroy_buffers(struct io_ring_ctx *ctx)
8661{
8662 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8663 idr_destroy(&ctx->io_buffer_idr);
8664}
8665
Jens Axboe2b188cc2019-01-07 10:46:33 -07008666static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8667{
Jens Axboe6b063142019-01-10 22:13:58 -07008668 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008669 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008670
8671 if (ctx->sqo_task) {
8672 put_task_struct(ctx->sqo_task);
8673 ctx->sqo_task = NULL;
8674 mmdrop(ctx->mm_account);
8675 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008676 }
Jens Axboedef596e2019-01-09 08:59:42 -07008677
Dennis Zhou91d8f512020-09-16 13:41:05 -07008678#ifdef CONFIG_BLK_CGROUP
8679 if (ctx->sqo_blkcg_css)
8680 css_put(ctx->sqo_blkcg_css);
8681#endif
8682
Jens Axboe6b063142019-01-10 22:13:58 -07008683 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008684 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008685 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008686 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008687
Jens Axboe2b188cc2019-01-07 10:46:33 -07008688#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008689 if (ctx->ring_sock) {
8690 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008691 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008692 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008693#endif
8694
Hristo Venev75b28af2019-08-26 17:23:46 +00008695 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008696 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008697
8698 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008699 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008700 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008701 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008702 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008703 kfree(ctx);
8704}
8705
8706static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8707{
8708 struct io_ring_ctx *ctx = file->private_data;
8709 __poll_t mask = 0;
8710
8711 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008712 /*
8713 * synchronizes with barrier from wq_has_sleeper call in
8714 * io_commit_cqring
8715 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008716 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008717 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008718 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008719 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8720 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008721 mask |= EPOLLIN | EPOLLRDNORM;
8722
8723 return mask;
8724}
8725
8726static int io_uring_fasync(int fd, struct file *file, int on)
8727{
8728 struct io_ring_ctx *ctx = file->private_data;
8729
8730 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8731}
8732
Yejune Deng0bead8c2020-12-24 11:02:20 +08008733static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008734{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008735 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008736
Jens Axboe1e6fa522020-10-15 08:46:24 -06008737 iod = idr_remove(&ctx->personality_idr, id);
8738 if (iod) {
8739 put_cred(iod->creds);
8740 if (refcount_dec_and_test(&iod->count))
8741 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008742 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008743 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008744
8745 return -EINVAL;
8746}
8747
8748static int io_remove_personalities(int id, void *p, void *data)
8749{
8750 struct io_ring_ctx *ctx = data;
8751
8752 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008753 return 0;
8754}
8755
Jens Axboe85faa7b2020-04-09 18:14:00 -06008756static void io_ring_exit_work(struct work_struct *work)
8757{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008758 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8759 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008760
Jens Axboe56952e92020-06-17 15:00:04 -06008761 /*
8762 * If we're doing polled IO and end up having requests being
8763 * submitted async (out-of-line), then completions can come in while
8764 * we're waiting for refs to drop. We need to reap these manually,
8765 * as nobody else will be looking for them.
8766 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008767 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008768 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008769 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008770 io_ring_ctx_free(ctx);
8771}
8772
Jens Axboe00c18642020-12-20 10:45:02 -07008773static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8774{
8775 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8776
8777 return req->ctx == data;
8778}
8779
Jens Axboe2b188cc2019-01-07 10:46:33 -07008780static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8781{
8782 mutex_lock(&ctx->uring_lock);
8783 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008784
8785 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8786 ctx->sqo_dead = 1;
8787
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008788 /* if force is set, the ring is going away. always drop after that */
8789 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008790 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008791 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008792 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008793 mutex_unlock(&ctx->uring_lock);
8794
Pavel Begunkov6b819282020-11-06 13:00:25 +00008795 io_kill_timeouts(ctx, NULL, NULL);
8796 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008797
8798 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008799 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008800
Jens Axboe15dff282019-11-13 09:09:23 -07008801 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008802 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008803
8804 /*
8805 * Do this upfront, so we won't have a grace period where the ring
8806 * is closed but resources aren't reaped yet. This can cause
8807 * spurious failure in setting up a new ring.
8808 */
Jens Axboe760618f2020-07-24 12:53:31 -06008809 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8810 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008811
Jens Axboe85faa7b2020-04-09 18:14:00 -06008812 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008813 /*
8814 * Use system_unbound_wq to avoid spawning tons of event kworkers
8815 * if we're exiting a ton of rings at the same time. It just adds
8816 * noise and overhead, there's no discernable change in runtime
8817 * over using system_wq.
8818 */
8819 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008820}
8821
8822static int io_uring_release(struct inode *inode, struct file *file)
8823{
8824 struct io_ring_ctx *ctx = file->private_data;
8825
8826 file->private_data = NULL;
8827 io_ring_ctx_wait_and_kill(ctx);
8828 return 0;
8829}
8830
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008831struct io_task_cancel {
8832 struct task_struct *task;
8833 struct files_struct *files;
8834};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008835
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008836static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008837{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008838 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008839 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008840 bool ret;
8841
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008842 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008843 unsigned long flags;
8844 struct io_ring_ctx *ctx = req->ctx;
8845
8846 /* protect against races with linked timeouts */
8847 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008848 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008849 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8850 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008851 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008852 }
8853 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008854}
8855
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008856static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008857 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008858 struct files_struct *files)
8859{
8860 struct io_defer_entry *de = NULL;
8861 LIST_HEAD(list);
8862
8863 spin_lock_irq(&ctx->completion_lock);
8864 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008865 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008866 list_cut_position(&list, &ctx->defer_list, &de->list);
8867 break;
8868 }
8869 }
8870 spin_unlock_irq(&ctx->completion_lock);
8871
8872 while (!list_empty(&list)) {
8873 de = list_first_entry(&list, struct io_defer_entry, list);
8874 list_del_init(&de->list);
8875 req_set_fail_links(de->req);
8876 io_put_req(de->req);
8877 io_req_complete(de->req, -ECANCELED);
8878 kfree(de);
8879 }
8880}
8881
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008882static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8883 struct task_struct *task,
8884 struct files_struct *files)
8885{
8886 struct io_task_cancel cancel = { .task = task, .files = files, };
8887
8888 while (1) {
8889 enum io_wq_cancel cret;
8890 bool ret = false;
8891
8892 if (ctx->io_wq) {
8893 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8894 &cancel, true);
8895 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8896 }
8897
8898 /* SQPOLL thread does its own polling */
8899 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8900 while (!list_empty_careful(&ctx->iopoll_list)) {
8901 io_iopoll_try_reap_events(ctx);
8902 ret = true;
8903 }
8904 }
8905
8906 ret |= io_poll_remove_all(ctx, task, files);
8907 ret |= io_kill_timeouts(ctx, task, files);
8908 ret |= io_run_task_work();
8909 io_cqring_overflow_flush(ctx, true, task, files);
8910 if (!ret)
8911 break;
8912 cond_resched();
8913 }
8914}
8915
Pavel Begunkovca70f002021-01-26 15:28:27 +00008916static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8917 struct task_struct *task,
8918 struct files_struct *files)
8919{
8920 struct io_kiocb *req;
8921 int cnt = 0;
8922
8923 spin_lock_irq(&ctx->inflight_lock);
8924 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8925 cnt += io_match_task(req, task, files);
8926 spin_unlock_irq(&ctx->inflight_lock);
8927 return cnt;
8928}
8929
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008930static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008931 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008932 struct files_struct *files)
8933{
Jens Axboefcb323c2019-10-24 12:39:47 -06008934 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008935 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008936 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008937
Pavel Begunkovca70f002021-01-26 15:28:27 +00008938 inflight = io_uring_count_inflight(ctx, task, files);
8939 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008940 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008941
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008942 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008943 prepare_to_wait(&task->io_uring->wait, &wait,
8944 TASK_UNINTERRUPTIBLE);
8945 if (inflight == io_uring_count_inflight(ctx, task, files))
8946 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008947 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008948 }
8949}
8950
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008951static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8952{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008953 mutex_lock(&ctx->uring_lock);
8954 ctx->sqo_dead = 1;
8955 mutex_unlock(&ctx->uring_lock);
8956
8957 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008958 if (ctx->rings)
8959 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008960}
8961
Jens Axboe0f212202020-09-13 13:09:39 -06008962/*
8963 * We need to iteratively cancel requests, in case a request has dependent
8964 * hard links. These persist even for failure of cancelations, hence keep
8965 * looping until none are found.
8966 */
8967static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8968 struct files_struct *files)
8969{
8970 struct task_struct *task = current;
8971
Jens Axboefdaf0832020-10-30 09:37:30 -06008972 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008973 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008974 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008975 atomic_inc(&task->io_uring->in_idle);
8976 io_sq_thread_park(ctx->sq_data);
8977 }
Jens Axboe0f212202020-09-13 13:09:39 -06008978
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008979 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008980
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008981 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008982 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008983 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008984
8985 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8986 atomic_dec(&task->io_uring->in_idle);
8987 /*
8988 * If the files that are going away are the ones in the thread
8989 * identity, clear them out.
8990 */
8991 if (task->io_uring->identity->files == files)
8992 task->io_uring->identity->files = NULL;
8993 io_sq_thread_unpark(ctx->sq_data);
8994 }
Jens Axboe0f212202020-09-13 13:09:39 -06008995}
8996
8997/*
8998 * Note that this task has used io_uring. We use it for cancelation purposes.
8999 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009000static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009001{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009002 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009003 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009004
9005 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009006 ret = io_uring_alloc_task_context(current);
9007 if (unlikely(ret))
9008 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009009 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009010 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009011 if (tctx->last != file) {
9012 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009013
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009014 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009015 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009016 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9017 file, GFP_KERNEL));
9018 if (ret) {
9019 fput(file);
9020 return ret;
9021 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009022
9023 /* one and only SQPOLL file note, held by sqo_task */
9024 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9025 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009026 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009027 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009028 }
9029
Jens Axboefdaf0832020-10-30 09:37:30 -06009030 /*
9031 * This is race safe in that the task itself is doing this, hence it
9032 * cannot be going through the exit/cancel paths at the same time.
9033 * This cannot be modified while exit/cancel is running.
9034 */
9035 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9036 tctx->sqpoll = true;
9037
Jens Axboe0f212202020-09-13 13:09:39 -06009038 return 0;
9039}
9040
9041/*
9042 * Remove this io_uring_file -> task mapping.
9043 */
9044static void io_uring_del_task_file(struct file *file)
9045{
9046 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009047
9048 if (tctx->last == file)
9049 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009050 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009051 if (file)
9052 fput(file);
9053}
9054
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009055static void io_uring_remove_task_files(struct io_uring_task *tctx)
9056{
9057 struct file *file;
9058 unsigned long index;
9059
9060 xa_for_each(&tctx->xa, index, file)
9061 io_uring_del_task_file(file);
9062}
9063
Jens Axboe0f212202020-09-13 13:09:39 -06009064void __io_uring_files_cancel(struct files_struct *files)
9065{
9066 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009067 struct file *file;
9068 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009069
9070 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009071 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009072 xa_for_each(&tctx->xa, index, file)
9073 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009074 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009075
9076 if (files)
9077 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009078}
9079
9080static s64 tctx_inflight(struct io_uring_task *tctx)
9081{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009082 return percpu_counter_sum(&tctx->inflight);
9083}
9084
9085static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9086{
9087 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009088 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009089 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009090
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009091 if (!ctx->sq_data)
9092 return;
9093 tctx = ctx->sq_data->thread->io_uring;
9094 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009095
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009096 atomic_inc(&tctx->in_idle);
9097 do {
9098 /* read completions before cancelations */
9099 inflight = tctx_inflight(tctx);
9100 if (!inflight)
9101 break;
9102 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009103
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009104 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9105 /*
9106 * If we've seen completions, retry without waiting. This
9107 * avoids a race where a completion comes in before we did
9108 * prepare_to_wait().
9109 */
9110 if (inflight == tctx_inflight(tctx))
9111 schedule();
9112 finish_wait(&tctx->wait, &wait);
9113 } while (1);
9114 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009115}
9116
Jens Axboe0f212202020-09-13 13:09:39 -06009117/*
9118 * Find any io_uring fd that this task has registered or done IO on, and cancel
9119 * requests.
9120 */
9121void __io_uring_task_cancel(void)
9122{
9123 struct io_uring_task *tctx = current->io_uring;
9124 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009125 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009126
9127 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009128 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009129
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009130 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009131 if (tctx->sqpoll) {
9132 struct file *file;
9133 unsigned long index;
9134
9135 xa_for_each(&tctx->xa, index, file)
9136 io_uring_cancel_sqpoll(file->private_data);
9137 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009138
Jens Axboed8a6df12020-10-15 16:24:45 -06009139 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009140 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009141 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009142 if (!inflight)
9143 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009144 __io_uring_files_cancel(NULL);
9145
9146 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9147
9148 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009149 * If we've seen completions, retry without waiting. This
9150 * avoids a race where a completion comes in before we did
9151 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009152 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009153 if (inflight == tctx_inflight(tctx))
9154 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009155 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009156 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009157
Jens Axboefdaf0832020-10-30 09:37:30 -06009158 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009159
9160 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009161}
9162
Jens Axboefcb323c2019-10-24 12:39:47 -06009163static int io_uring_flush(struct file *file, void *data)
9164{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009165 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009166 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009167
Jens Axboe84965ff2021-01-23 15:51:11 -07009168 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9169 io_uring_cancel_task_requests(ctx, NULL);
9170
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009171 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009172 return 0;
9173
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009174 /* we should have cancelled and erased it before PF_EXITING */
9175 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9176 xa_load(&tctx->xa, (unsigned long)file));
9177
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009178 /*
9179 * fput() is pending, will be 2 if the only other ref is our potential
9180 * task file note. If the task is exiting, drop regardless of count.
9181 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009182 if (atomic_long_read(&file->f_count) != 2)
9183 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009184
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009185 if (ctx->flags & IORING_SETUP_SQPOLL) {
9186 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009187 WARN_ON_ONCE(ctx->sqo_task != current &&
9188 xa_load(&tctx->xa, (unsigned long)file));
9189 /* sqo_dead check is for when this happens after cancellation */
9190 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009191 !xa_load(&tctx->xa, (unsigned long)file));
9192
9193 io_disable_sqo_submit(ctx);
9194 }
9195
9196 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9197 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009198 return 0;
9199}
9200
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009201static void *io_uring_validate_mmap_request(struct file *file,
9202 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009203{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009204 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009205 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009206 struct page *page;
9207 void *ptr;
9208
9209 switch (offset) {
9210 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009211 case IORING_OFF_CQ_RING:
9212 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009213 break;
9214 case IORING_OFF_SQES:
9215 ptr = ctx->sq_sqes;
9216 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009217 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009218 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009219 }
9220
9221 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009222 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009223 return ERR_PTR(-EINVAL);
9224
9225 return ptr;
9226}
9227
9228#ifdef CONFIG_MMU
9229
9230static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9231{
9232 size_t sz = vma->vm_end - vma->vm_start;
9233 unsigned long pfn;
9234 void *ptr;
9235
9236 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9237 if (IS_ERR(ptr))
9238 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009239
9240 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9241 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9242}
9243
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009244#else /* !CONFIG_MMU */
9245
9246static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9247{
9248 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9249}
9250
9251static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9252{
9253 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9254}
9255
9256static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9257 unsigned long addr, unsigned long len,
9258 unsigned long pgoff, unsigned long flags)
9259{
9260 void *ptr;
9261
9262 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9263 if (IS_ERR(ptr))
9264 return PTR_ERR(ptr);
9265
9266 return (unsigned long) ptr;
9267}
9268
9269#endif /* !CONFIG_MMU */
9270
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009271static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009272{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009273 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009274 DEFINE_WAIT(wait);
9275
9276 do {
9277 if (!io_sqring_full(ctx))
9278 break;
9279
9280 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9281
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009282 if (unlikely(ctx->sqo_dead)) {
9283 ret = -EOWNERDEAD;
9284 goto out;
9285 }
9286
Jens Axboe90554202020-09-03 12:12:41 -06009287 if (!io_sqring_full(ctx))
9288 break;
9289
9290 schedule();
9291 } while (!signal_pending(current));
9292
9293 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009294out:
9295 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009296}
9297
Hao Xuc73ebb62020-11-03 10:54:37 +08009298static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9299 struct __kernel_timespec __user **ts,
9300 const sigset_t __user **sig)
9301{
9302 struct io_uring_getevents_arg arg;
9303
9304 /*
9305 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9306 * is just a pointer to the sigset_t.
9307 */
9308 if (!(flags & IORING_ENTER_EXT_ARG)) {
9309 *sig = (const sigset_t __user *) argp;
9310 *ts = NULL;
9311 return 0;
9312 }
9313
9314 /*
9315 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9316 * timespec and sigset_t pointers if good.
9317 */
9318 if (*argsz != sizeof(arg))
9319 return -EINVAL;
9320 if (copy_from_user(&arg, argp, sizeof(arg)))
9321 return -EFAULT;
9322 *sig = u64_to_user_ptr(arg.sigmask);
9323 *argsz = arg.sigmask_sz;
9324 *ts = u64_to_user_ptr(arg.ts);
9325 return 0;
9326}
9327
Jens Axboe2b188cc2019-01-07 10:46:33 -07009328SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009329 u32, min_complete, u32, flags, const void __user *, argp,
9330 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331{
9332 struct io_ring_ctx *ctx;
9333 long ret = -EBADF;
9334 int submitted = 0;
9335 struct fd f;
9336
Jens Axboe4c6e2772020-07-01 11:29:10 -06009337 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009338
Jens Axboe90554202020-09-03 12:12:41 -06009339 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009340 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009341 return -EINVAL;
9342
9343 f = fdget(fd);
9344 if (!f.file)
9345 return -EBADF;
9346
9347 ret = -EOPNOTSUPP;
9348 if (f.file->f_op != &io_uring_fops)
9349 goto out_fput;
9350
9351 ret = -ENXIO;
9352 ctx = f.file->private_data;
9353 if (!percpu_ref_tryget(&ctx->refs))
9354 goto out_fput;
9355
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009356 ret = -EBADFD;
9357 if (ctx->flags & IORING_SETUP_R_DISABLED)
9358 goto out;
9359
Jens Axboe6c271ce2019-01-10 11:22:30 -07009360 /*
9361 * For SQ polling, the thread will do all submissions and completions.
9362 * Just return the requested submit count, and wake the thread if
9363 * we were asked to.
9364 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009365 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009366 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009367 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009368
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009369 ret = -EOWNERDEAD;
9370 if (unlikely(ctx->sqo_dead))
9371 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009372 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009373 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009374 if (flags & IORING_ENTER_SQ_WAIT) {
9375 ret = io_sqpoll_wait_sq(ctx);
9376 if (ret)
9377 goto out;
9378 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009379 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009380 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009381 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009382 if (unlikely(ret))
9383 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009384 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009385 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009387
9388 if (submitted != to_submit)
9389 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009390 }
9391 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009392 const sigset_t __user *sig;
9393 struct __kernel_timespec __user *ts;
9394
9395 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9396 if (unlikely(ret))
9397 goto out;
9398
Jens Axboe2b188cc2019-01-07 10:46:33 -07009399 min_complete = min(min_complete, ctx->cq_entries);
9400
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009401 /*
9402 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9403 * space applications don't need to do io completion events
9404 * polling again, they can rely on io_sq_thread to do polling
9405 * work, which can reduce cpu usage and uring_lock contention.
9406 */
9407 if (ctx->flags & IORING_SETUP_IOPOLL &&
9408 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009409 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009410 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009411 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009412 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009413 }
9414
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009415out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009416 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417out_fput:
9418 fdput(f);
9419 return submitted ? submitted : ret;
9420}
9421
Tobias Klauserbebdb652020-02-26 18:38:32 +01009422#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009423static int io_uring_show_cred(int id, void *p, void *data)
9424{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009425 struct io_identity *iod = p;
9426 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009427 struct seq_file *m = data;
9428 struct user_namespace *uns = seq_user_ns(m);
9429 struct group_info *gi;
9430 kernel_cap_t cap;
9431 unsigned __capi;
9432 int g;
9433
9434 seq_printf(m, "%5d\n", id);
9435 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9436 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9437 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9438 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9439 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9440 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9441 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9442 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9443 seq_puts(m, "\n\tGroups:\t");
9444 gi = cred->group_info;
9445 for (g = 0; g < gi->ngroups; g++) {
9446 seq_put_decimal_ull(m, g ? " " : "",
9447 from_kgid_munged(uns, gi->gid[g]));
9448 }
9449 seq_puts(m, "\n\tCapEff:\t");
9450 cap = cred->cap_effective;
9451 CAP_FOR_EACH_U32(__capi)
9452 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9453 seq_putc(m, '\n');
9454 return 0;
9455}
9456
9457static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9458{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009459 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009460 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009461 int i;
9462
Jens Axboefad8e0d2020-09-28 08:57:48 -06009463 /*
9464 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9465 * since fdinfo case grabs it in the opposite direction of normal use
9466 * cases. If we fail to get the lock, we just don't iterate any
9467 * structures that could be going away outside the io_uring mutex.
9468 */
9469 has_lock = mutex_trylock(&ctx->uring_lock);
9470
Joseph Qidbbe9c62020-09-29 09:01:22 -06009471 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9472 sq = ctx->sq_data;
9473
9474 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9475 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009476 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009477 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009478 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009479
Jens Axboe87ce9552020-01-30 08:25:34 -07009480 if (f)
9481 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9482 else
9483 seq_printf(m, "%5u: <none>\n", i);
9484 }
9485 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009486 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009487 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9488
9489 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9490 (unsigned int) buf->len);
9491 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009492 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009493 seq_printf(m, "Personalities:\n");
9494 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9495 }
Jens Axboed7718a92020-02-14 22:23:12 -07009496 seq_printf(m, "PollList:\n");
9497 spin_lock_irq(&ctx->completion_lock);
9498 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9499 struct hlist_head *list = &ctx->cancel_hash[i];
9500 struct io_kiocb *req;
9501
9502 hlist_for_each_entry(req, list, hash_node)
9503 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9504 req->task->task_works != NULL);
9505 }
9506 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009507 if (has_lock)
9508 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009509}
9510
9511static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9512{
9513 struct io_ring_ctx *ctx = f->private_data;
9514
9515 if (percpu_ref_tryget(&ctx->refs)) {
9516 __io_uring_show_fdinfo(ctx, m);
9517 percpu_ref_put(&ctx->refs);
9518 }
9519}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009520#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009521
Jens Axboe2b188cc2019-01-07 10:46:33 -07009522static const struct file_operations io_uring_fops = {
9523 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009524 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009525 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009526#ifndef CONFIG_MMU
9527 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9528 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9529#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009530 .poll = io_uring_poll,
9531 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009532#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009533 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009534#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009535};
9536
9537static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9538 struct io_uring_params *p)
9539{
Hristo Venev75b28af2019-08-26 17:23:46 +00009540 struct io_rings *rings;
9541 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009542
Jens Axboebd740482020-08-05 12:58:23 -06009543 /* make sure these are sane, as we already accounted them */
9544 ctx->sq_entries = p->sq_entries;
9545 ctx->cq_entries = p->cq_entries;
9546
Hristo Venev75b28af2019-08-26 17:23:46 +00009547 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9548 if (size == SIZE_MAX)
9549 return -EOVERFLOW;
9550
9551 rings = io_mem_alloc(size);
9552 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553 return -ENOMEM;
9554
Hristo Venev75b28af2019-08-26 17:23:46 +00009555 ctx->rings = rings;
9556 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9557 rings->sq_ring_mask = p->sq_entries - 1;
9558 rings->cq_ring_mask = p->cq_entries - 1;
9559 rings->sq_ring_entries = p->sq_entries;
9560 rings->cq_ring_entries = p->cq_entries;
9561 ctx->sq_mask = rings->sq_ring_mask;
9562 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009563
9564 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009565 if (size == SIZE_MAX) {
9566 io_mem_free(ctx->rings);
9567 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009568 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009569 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009570
9571 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009572 if (!ctx->sq_sqes) {
9573 io_mem_free(ctx->rings);
9574 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009575 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009576 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009577
Jens Axboe2b188cc2019-01-07 10:46:33 -07009578 return 0;
9579}
9580
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009581static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9582{
9583 int ret, fd;
9584
9585 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9586 if (fd < 0)
9587 return fd;
9588
9589 ret = io_uring_add_task_file(ctx, file);
9590 if (ret) {
9591 put_unused_fd(fd);
9592 return ret;
9593 }
9594 fd_install(fd, file);
9595 return fd;
9596}
9597
Jens Axboe2b188cc2019-01-07 10:46:33 -07009598/*
9599 * Allocate an anonymous fd, this is what constitutes the application
9600 * visible backing of an io_uring instance. The application mmaps this
9601 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9602 * we have to tie this fd to a socket for file garbage collection purposes.
9603 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009604static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009605{
9606 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009607#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009608 int ret;
9609
Jens Axboe2b188cc2019-01-07 10:46:33 -07009610 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9611 &ctx->ring_sock);
9612 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009613 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009614#endif
9615
Jens Axboe2b188cc2019-01-07 10:46:33 -07009616 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9617 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009618#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009619 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009620 sock_release(ctx->ring_sock);
9621 ctx->ring_sock = NULL;
9622 } else {
9623 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009624 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009625#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009626 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009627}
9628
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009629static int io_uring_create(unsigned entries, struct io_uring_params *p,
9630 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009631{
9632 struct user_struct *user = NULL;
9633 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009634 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009635 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009636 int ret;
9637
Jens Axboe8110c1a2019-12-28 15:39:54 -07009638 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009639 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009640 if (entries > IORING_MAX_ENTRIES) {
9641 if (!(p->flags & IORING_SETUP_CLAMP))
9642 return -EINVAL;
9643 entries = IORING_MAX_ENTRIES;
9644 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009645
9646 /*
9647 * Use twice as many entries for the CQ ring. It's possible for the
9648 * application to drive a higher depth than the size of the SQ ring,
9649 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009650 * some flexibility in overcommitting a bit. If the application has
9651 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9652 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009653 */
9654 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009655 if (p->flags & IORING_SETUP_CQSIZE) {
9656 /*
9657 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9658 * to a power-of-two, if it isn't already. We do NOT impose
9659 * any cq vs sq ring sizing.
9660 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009661 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009662 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009663 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9664 if (!(p->flags & IORING_SETUP_CLAMP))
9665 return -EINVAL;
9666 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9667 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009668 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9669 if (p->cq_entries < p->sq_entries)
9670 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009671 } else {
9672 p->cq_entries = 2 * p->sq_entries;
9673 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009674
9675 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009676 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009677
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009678 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009679 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009680 ring_pages(p->sq_entries, p->cq_entries));
9681 if (ret) {
9682 free_uid(user);
9683 return ret;
9684 }
9685 }
9686
9687 ctx = io_ring_ctx_alloc(p);
9688 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009689 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009690 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009691 p->cq_entries));
9692 free_uid(user);
9693 return -ENOMEM;
9694 }
9695 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009696 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009697 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009698#ifdef CONFIG_AUDIT
9699 ctx->loginuid = current->loginuid;
9700 ctx->sessionid = current->sessionid;
9701#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009702 ctx->sqo_task = get_task_struct(current);
9703
9704 /*
9705 * This is just grabbed for accounting purposes. When a process exits,
9706 * the mm is exited and dropped before the files, hence we need to hang
9707 * on to this mm purely for the purposes of being able to unaccount
9708 * memory (locked/pinned vm). It's not used for anything else.
9709 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009710 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009711 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009712
Dennis Zhou91d8f512020-09-16 13:41:05 -07009713#ifdef CONFIG_BLK_CGROUP
9714 /*
9715 * The sq thread will belong to the original cgroup it was inited in.
9716 * If the cgroup goes offline (e.g. disabling the io controller), then
9717 * issued bios will be associated with the closest cgroup later in the
9718 * block layer.
9719 */
9720 rcu_read_lock();
9721 ctx->sqo_blkcg_css = blkcg_css();
9722 ret = css_tryget_online(ctx->sqo_blkcg_css);
9723 rcu_read_unlock();
9724 if (!ret) {
9725 /* don't init against a dying cgroup, have the user try again */
9726 ctx->sqo_blkcg_css = NULL;
9727 ret = -ENODEV;
9728 goto err;
9729 }
9730#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009731
Jens Axboe2b188cc2019-01-07 10:46:33 -07009732 /*
9733 * Account memory _before_ installing the file descriptor. Once
9734 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009735 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009736 * will un-account as well.
9737 */
9738 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9739 ACCT_LOCKED);
9740 ctx->limit_mem = limit_mem;
9741
9742 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009743 if (ret)
9744 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009745
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009746 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009747 if (ret)
9748 goto err;
9749
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009750 if (!(p->flags & IORING_SETUP_R_DISABLED))
9751 io_sq_offload_start(ctx);
9752
Jens Axboe2b188cc2019-01-07 10:46:33 -07009753 memset(&p->sq_off, 0, sizeof(p->sq_off));
9754 p->sq_off.head = offsetof(struct io_rings, sq.head);
9755 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9756 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9757 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9758 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9759 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9760 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9761
9762 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009763 p->cq_off.head = offsetof(struct io_rings, cq.head);
9764 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9765 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9766 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9767 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9768 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009769 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009770
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009771 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9772 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009773 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009774 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9775 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009776
9777 if (copy_to_user(params, p, sizeof(*p))) {
9778 ret = -EFAULT;
9779 goto err;
9780 }
Jens Axboed1719f72020-07-30 13:43:53 -06009781
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009782 file = io_uring_get_file(ctx);
9783 if (IS_ERR(file)) {
9784 ret = PTR_ERR(file);
9785 goto err;
9786 }
9787
Jens Axboed1719f72020-07-30 13:43:53 -06009788 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009789 * Install ring fd as the very last thing, so we don't risk someone
9790 * having closed it before we finish setup
9791 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009792 ret = io_uring_install_fd(ctx, file);
9793 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009794 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009795 /* fput will clean it up */
9796 fput(file);
9797 return ret;
9798 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009799
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009800 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009801 return ret;
9802err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009803 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009804 io_ring_ctx_wait_and_kill(ctx);
9805 return ret;
9806}
9807
9808/*
9809 * Sets up an aio uring context, and returns the fd. Applications asks for a
9810 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9811 * params structure passed in.
9812 */
9813static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9814{
9815 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009816 int i;
9817
9818 if (copy_from_user(&p, params, sizeof(p)))
9819 return -EFAULT;
9820 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9821 if (p.resv[i])
9822 return -EINVAL;
9823 }
9824
Jens Axboe6c271ce2019-01-10 11:22:30 -07009825 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009826 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009827 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9828 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009829 return -EINVAL;
9830
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009831 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009832}
9833
9834SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9835 struct io_uring_params __user *, params)
9836{
9837 return io_uring_setup(entries, params);
9838}
9839
Jens Axboe66f4af92020-01-16 15:36:52 -07009840static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9841{
9842 struct io_uring_probe *p;
9843 size_t size;
9844 int i, ret;
9845
9846 size = struct_size(p, ops, nr_args);
9847 if (size == SIZE_MAX)
9848 return -EOVERFLOW;
9849 p = kzalloc(size, GFP_KERNEL);
9850 if (!p)
9851 return -ENOMEM;
9852
9853 ret = -EFAULT;
9854 if (copy_from_user(p, arg, size))
9855 goto out;
9856 ret = -EINVAL;
9857 if (memchr_inv(p, 0, size))
9858 goto out;
9859
9860 p->last_op = IORING_OP_LAST - 1;
9861 if (nr_args > IORING_OP_LAST)
9862 nr_args = IORING_OP_LAST;
9863
9864 for (i = 0; i < nr_args; i++) {
9865 p->ops[i].op = i;
9866 if (!io_op_defs[i].not_supported)
9867 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9868 }
9869 p->ops_len = i;
9870
9871 ret = 0;
9872 if (copy_to_user(arg, p, size))
9873 ret = -EFAULT;
9874out:
9875 kfree(p);
9876 return ret;
9877}
9878
Jens Axboe071698e2020-01-28 10:04:42 -07009879static int io_register_personality(struct io_ring_ctx *ctx)
9880{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009881 struct io_identity *id;
9882 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009883
Jens Axboe1e6fa522020-10-15 08:46:24 -06009884 id = kmalloc(sizeof(*id), GFP_KERNEL);
9885 if (unlikely(!id))
9886 return -ENOMEM;
9887
9888 io_init_identity(id);
9889 id->creds = get_current_cred();
9890
9891 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9892 if (ret < 0) {
9893 put_cred(id->creds);
9894 kfree(id);
9895 }
9896 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009897}
9898
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009899static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9900 unsigned int nr_args)
9901{
9902 struct io_uring_restriction *res;
9903 size_t size;
9904 int i, ret;
9905
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009906 /* Restrictions allowed only if rings started disabled */
9907 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9908 return -EBADFD;
9909
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009910 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009911 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009912 return -EBUSY;
9913
9914 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9915 return -EINVAL;
9916
9917 size = array_size(nr_args, sizeof(*res));
9918 if (size == SIZE_MAX)
9919 return -EOVERFLOW;
9920
9921 res = memdup_user(arg, size);
9922 if (IS_ERR(res))
9923 return PTR_ERR(res);
9924
9925 ret = 0;
9926
9927 for (i = 0; i < nr_args; i++) {
9928 switch (res[i].opcode) {
9929 case IORING_RESTRICTION_REGISTER_OP:
9930 if (res[i].register_op >= IORING_REGISTER_LAST) {
9931 ret = -EINVAL;
9932 goto out;
9933 }
9934
9935 __set_bit(res[i].register_op,
9936 ctx->restrictions.register_op);
9937 break;
9938 case IORING_RESTRICTION_SQE_OP:
9939 if (res[i].sqe_op >= IORING_OP_LAST) {
9940 ret = -EINVAL;
9941 goto out;
9942 }
9943
9944 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9945 break;
9946 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9947 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9948 break;
9949 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9950 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9951 break;
9952 default:
9953 ret = -EINVAL;
9954 goto out;
9955 }
9956 }
9957
9958out:
9959 /* Reset all restrictions if an error happened */
9960 if (ret != 0)
9961 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9962 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009963 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009964
9965 kfree(res);
9966 return ret;
9967}
9968
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009969static int io_register_enable_rings(struct io_ring_ctx *ctx)
9970{
9971 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9972 return -EBADFD;
9973
9974 if (ctx->restrictions.registered)
9975 ctx->restricted = 1;
9976
9977 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9978
9979 io_sq_offload_start(ctx);
9980
9981 return 0;
9982}
9983
Jens Axboe071698e2020-01-28 10:04:42 -07009984static bool io_register_op_must_quiesce(int op)
9985{
9986 switch (op) {
9987 case IORING_UNREGISTER_FILES:
9988 case IORING_REGISTER_FILES_UPDATE:
9989 case IORING_REGISTER_PROBE:
9990 case IORING_REGISTER_PERSONALITY:
9991 case IORING_UNREGISTER_PERSONALITY:
9992 return false;
9993 default:
9994 return true;
9995 }
9996}
9997
Jens Axboeedafcce2019-01-09 09:16:05 -07009998static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9999 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010000 __releases(ctx->uring_lock)
10001 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010002{
10003 int ret;
10004
Jens Axboe35fa71a2019-04-22 10:23:23 -060010005 /*
10006 * We're inside the ring mutex, if the ref is already dying, then
10007 * someone else killed the ctx or is already going through
10008 * io_uring_register().
10009 */
10010 if (percpu_ref_is_dying(&ctx->refs))
10011 return -ENXIO;
10012
Jens Axboe071698e2020-01-28 10:04:42 -070010013 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010014 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010015
Jens Axboe05f3fb32019-12-09 11:22:50 -070010016 /*
10017 * Drop uring mutex before waiting for references to exit. If
10018 * another thread is currently inside io_uring_enter() it might
10019 * need to grab the uring_lock to make progress. If we hold it
10020 * here across the drain wait, then we can deadlock. It's safe
10021 * to drop the mutex here, since no new references will come in
10022 * after we've killed the percpu ref.
10023 */
10024 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010025 do {
10026 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10027 if (!ret)
10028 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010029 ret = io_run_task_work_sig();
10030 if (ret < 0)
10031 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010032 } while (1);
10033
Jens Axboe05f3fb32019-12-09 11:22:50 -070010034 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010035
Jens Axboec1503682020-01-08 08:26:07 -070010036 if (ret) {
10037 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010038 goto out_quiesce;
10039 }
10040 }
10041
10042 if (ctx->restricted) {
10043 if (opcode >= IORING_REGISTER_LAST) {
10044 ret = -EINVAL;
10045 goto out;
10046 }
10047
10048 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10049 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010050 goto out;
10051 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010052 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010053
10054 switch (opcode) {
10055 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010056 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010057 break;
10058 case IORING_UNREGISTER_BUFFERS:
10059 ret = -EINVAL;
10060 if (arg || nr_args)
10061 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010062 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010063 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010064 case IORING_REGISTER_FILES:
10065 ret = io_sqe_files_register(ctx, arg, nr_args);
10066 break;
10067 case IORING_UNREGISTER_FILES:
10068 ret = -EINVAL;
10069 if (arg || nr_args)
10070 break;
10071 ret = io_sqe_files_unregister(ctx);
10072 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010073 case IORING_REGISTER_FILES_UPDATE:
10074 ret = io_sqe_files_update(ctx, arg, nr_args);
10075 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010076 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010077 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010078 ret = -EINVAL;
10079 if (nr_args != 1)
10080 break;
10081 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010082 if (ret)
10083 break;
10084 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10085 ctx->eventfd_async = 1;
10086 else
10087 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010088 break;
10089 case IORING_UNREGISTER_EVENTFD:
10090 ret = -EINVAL;
10091 if (arg || nr_args)
10092 break;
10093 ret = io_eventfd_unregister(ctx);
10094 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010095 case IORING_REGISTER_PROBE:
10096 ret = -EINVAL;
10097 if (!arg || nr_args > 256)
10098 break;
10099 ret = io_probe(ctx, arg, nr_args);
10100 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010101 case IORING_REGISTER_PERSONALITY:
10102 ret = -EINVAL;
10103 if (arg || nr_args)
10104 break;
10105 ret = io_register_personality(ctx);
10106 break;
10107 case IORING_UNREGISTER_PERSONALITY:
10108 ret = -EINVAL;
10109 if (arg)
10110 break;
10111 ret = io_unregister_personality(ctx, nr_args);
10112 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010113 case IORING_REGISTER_ENABLE_RINGS:
10114 ret = -EINVAL;
10115 if (arg || nr_args)
10116 break;
10117 ret = io_register_enable_rings(ctx);
10118 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010119 case IORING_REGISTER_RESTRICTIONS:
10120 ret = io_register_restrictions(ctx, arg, nr_args);
10121 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010122 default:
10123 ret = -EINVAL;
10124 break;
10125 }
10126
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010127out:
Jens Axboe071698e2020-01-28 10:04:42 -070010128 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010129 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010130 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010131out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010132 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010133 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010134 return ret;
10135}
10136
10137SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10138 void __user *, arg, unsigned int, nr_args)
10139{
10140 struct io_ring_ctx *ctx;
10141 long ret = -EBADF;
10142 struct fd f;
10143
10144 f = fdget(fd);
10145 if (!f.file)
10146 return -EBADF;
10147
10148 ret = -EOPNOTSUPP;
10149 if (f.file->f_op != &io_uring_fops)
10150 goto out_fput;
10151
10152 ctx = f.file->private_data;
10153
10154 mutex_lock(&ctx->uring_lock);
10155 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10156 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010157 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10158 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010159out_fput:
10160 fdput(f);
10161 return ret;
10162}
10163
Jens Axboe2b188cc2019-01-07 10:46:33 -070010164static int __init io_uring_init(void)
10165{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010166#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10167 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10168 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10169} while (0)
10170
10171#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10172 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10173 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10174 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10175 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10176 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10177 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10178 BUILD_BUG_SQE_ELEM(8, __u64, off);
10179 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10180 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010181 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010182 BUILD_BUG_SQE_ELEM(24, __u32, len);
10183 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10184 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10185 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10186 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010187 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10188 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010189 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10190 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10191 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10192 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10193 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10194 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10195 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10196 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010197 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010198 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10199 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10200 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010201 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010202
Jens Axboed3656342019-12-18 09:50:26 -070010203 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010204 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010205 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10206 return 0;
10207};
10208__initcall(io_uring_init);