blob: ac233d04ee712f21888ef0e7f3b19d225f882735 [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,
192};
193
Jens Axboeedafcce2019-01-09 09:16:05 -0700194struct io_mapped_ubuf {
195 u64 ubuf;
196 size_t len;
197 struct bio_vec *bvec;
198 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600199 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700200};
201
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000202struct io_ring_ctx;
203
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000204struct io_rsrc_put {
205 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000206 union {
207 void *rsrc;
208 struct file *file;
209 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000210};
211
212struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600213 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700214};
215
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000216struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800217 struct percpu_ref refs;
218 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000219 struct list_head rsrc_list;
220 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000221 void (*rsrc_put)(struct io_ring_ctx *ctx,
222 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600223 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000224 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800225};
226
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000227struct fixed_rsrc_data {
228 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700229 struct io_ring_ctx *ctx;
230
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000231 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700232 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700233 struct completion done;
234};
235
Jens Axboe5a2e7452020-02-23 16:23:11 -0700236struct io_buffer {
237 struct list_head list;
238 __u64 addr;
239 __s32 len;
240 __u16 bid;
241};
242
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200243struct io_restriction {
244 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
245 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
246 u8 sqe_flags_allowed;
247 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200248 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200249};
250
Jens Axboe534ca6d2020-09-02 13:52:19 -0600251struct io_sq_data {
252 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600253 struct mutex lock;
254
255 /* ctx's that are using this sqd */
256 struct list_head ctx_list;
257 struct list_head ctx_new_list;
258 struct mutex ctx_lock;
259
Jens Axboe534ca6d2020-09-02 13:52:19 -0600260 struct task_struct *thread;
261 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800262
263 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600264};
265
Jens Axboe2b188cc2019-01-07 10:46:33 -0700266struct io_ring_ctx {
267 struct {
268 struct percpu_ref refs;
269 } ____cacheline_aligned_in_smp;
270
271 struct {
272 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800273 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700274 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800275 unsigned int cq_overflow_flushed: 1;
276 unsigned int drain_next: 1;
277 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200278 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000279 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700280
Hristo Venev75b28af2019-08-26 17:23:46 +0000281 /*
282 * Ring buffer of indices into array of io_uring_sqe, which is
283 * mmapped by the application using the IORING_OFF_SQES offset.
284 *
285 * This indirection could e.g. be used to assign fixed
286 * io_uring_sqe entries to operations and only submit them to
287 * the queue when needed.
288 *
289 * The kernel modifies neither the indices array nor the entries
290 * array.
291 */
292 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700293 unsigned cached_sq_head;
294 unsigned sq_entries;
295 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700296 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600297 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100298 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700299 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600300
301 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600302 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700303 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700304
Jens Axboead3eb2c2019-12-18 17:12:20 -0700305 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306 } ____cacheline_aligned_in_smp;
307
Hristo Venev75b28af2019-08-26 17:23:46 +0000308 struct io_rings *rings;
309
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600311 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600312
313 /*
314 * For SQPOLL usage - we hold a reference to the parent task, so we
315 * have access to the ->files
316 */
317 struct task_struct *sqo_task;
318
319 /* Only used for accounting purposes */
320 struct mm_struct *mm_account;
321
Dennis Zhou91d8f512020-09-16 13:41:05 -0700322#ifdef CONFIG_BLK_CGROUP
323 struct cgroup_subsys_state *sqo_blkcg_css;
324#endif
325
Jens Axboe534ca6d2020-09-02 13:52:19 -0600326 struct io_sq_data *sq_data; /* if using sq thread polling */
327
Jens Axboe90554202020-09-03 12:12:41 -0600328 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600329 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330
Jens Axboe6b063142019-01-10 22:13:58 -0700331 /*
332 * If used, fixed file set. Writers must ensure that ->refs is dead,
333 * readers must ensure that ->refs is alive as long as the file* is
334 * used. Only updated through io_uring_register(2).
335 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000336 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700337 unsigned nr_user_files;
338
Jens Axboeedafcce2019-01-09 09:16:05 -0700339 /* if used, fixed mapped user buffers */
340 unsigned nr_user_bufs;
341 struct io_mapped_ubuf *user_bufs;
342
Jens Axboe2b188cc2019-01-07 10:46:33 -0700343 struct user_struct *user;
344
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700345 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700346
Jens Axboe4ea33a92020-10-15 13:46:44 -0600347#ifdef CONFIG_AUDIT
348 kuid_t loginuid;
349 unsigned int sessionid;
350#endif
351
Jens Axboe0f158b42020-05-14 17:18:39 -0600352 struct completion ref_comp;
353 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700354
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700355 /* if all else fails... */
356 struct io_kiocb *fallback_req;
357
Jens Axboe206aefd2019-11-07 18:27:42 -0700358#if defined(CONFIG_UNIX)
359 struct socket *ring_sock;
360#endif
361
Jens Axboe5a2e7452020-02-23 16:23:11 -0700362 struct idr io_buffer_idr;
363
Jens Axboe071698e2020-01-28 10:04:42 -0700364 struct idr personality_idr;
365
Jens Axboe206aefd2019-11-07 18:27:42 -0700366 struct {
367 unsigned cached_cq_tail;
368 unsigned cq_entries;
369 unsigned cq_mask;
370 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500371 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700372 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700373 struct wait_queue_head cq_wait;
374 struct fasync_struct *cq_fasync;
375 struct eventfd_ctx *cq_ev_fd;
376 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377
378 struct {
379 struct mutex uring_lock;
380 wait_queue_head_t wait;
381 } ____cacheline_aligned_in_smp;
382
383 struct {
384 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700385
Jens Axboedef596e2019-01-09 08:59:42 -0700386 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300387 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700388 * io_uring instances that don't use IORING_SETUP_SQPOLL.
389 * For SQPOLL, only the single threaded io_sq_thread() will
390 * manipulate the list, hence no extra locking is needed there.
391 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300392 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700393 struct hlist_head *cancel_hash;
394 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700395 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600396
397 spinlock_t inflight_lock;
398 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600400
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000401 struct delayed_work rsrc_put_work;
402 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000403 struct list_head rsrc_ref_list;
404 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600405
Jens Axboe85faa7b2020-04-09 18:14:00 -0600406 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200407 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408};
409
Jens Axboe09bb8392019-03-13 12:39:28 -0600410/*
411 * First field must be the file pointer in all the
412 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
413 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700414struct io_poll_iocb {
415 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000416 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700417 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600418 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700419 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700420 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700421};
422
Pavel Begunkov018043b2020-10-27 23:17:18 +0000423struct io_poll_remove {
424 struct file *file;
425 u64 addr;
426};
427
Jens Axboeb5dba592019-12-11 14:02:38 -0700428struct io_close {
429 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700430 int fd;
431};
432
Jens Axboead8a48a2019-11-15 08:49:11 -0700433struct io_timeout_data {
434 struct io_kiocb *req;
435 struct hrtimer timer;
436 struct timespec64 ts;
437 enum hrtimer_mode mode;
438};
439
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700440struct io_accept {
441 struct file *file;
442 struct sockaddr __user *addr;
443 int __user *addr_len;
444 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600445 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700446};
447
448struct io_sync {
449 struct file *file;
450 loff_t len;
451 loff_t off;
452 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700453 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700454};
455
Jens Axboefbf23842019-12-17 18:45:56 -0700456struct io_cancel {
457 struct file *file;
458 u64 addr;
459};
460
Jens Axboeb29472e2019-12-17 18:50:29 -0700461struct io_timeout {
462 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300463 u32 off;
464 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300465 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000466 /* head of the link, used by linked timeouts only */
467 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700468};
469
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100470struct io_timeout_rem {
471 struct file *file;
472 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000473
474 /* timeout update */
475 struct timespec64 ts;
476 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100477};
478
Jens Axboe9adbd452019-12-20 08:45:55 -0700479struct io_rw {
480 /* NOTE: kiocb has the file as the first member, so don't do it here */
481 struct kiocb kiocb;
482 u64 addr;
483 u64 len;
484};
485
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700486struct io_connect {
487 struct file *file;
488 struct sockaddr __user *addr;
489 int addr_len;
490};
491
Jens Axboee47293f2019-12-20 08:58:21 -0700492struct io_sr_msg {
493 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700494 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300495 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700496 void __user *buf;
497 };
Jens Axboee47293f2019-12-20 08:58:21 -0700498 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700499 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700500 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700501 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700502};
503
Jens Axboe15b71ab2019-12-11 11:20:36 -0700504struct io_open {
505 struct file *file;
506 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700507 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700508 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600509 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700510};
511
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000512struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700513 struct file *file;
514 u64 arg;
515 u32 nr_args;
516 u32 offset;
517};
518
Jens Axboe4840e412019-12-25 22:03:45 -0700519struct io_fadvise {
520 struct file *file;
521 u64 offset;
522 u32 len;
523 u32 advice;
524};
525
Jens Axboec1ca7572019-12-25 22:18:28 -0700526struct io_madvise {
527 struct file *file;
528 u64 addr;
529 u32 len;
530 u32 advice;
531};
532
Jens Axboe3e4827b2020-01-08 15:18:09 -0700533struct io_epoll {
534 struct file *file;
535 int epfd;
536 int op;
537 int fd;
538 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700539};
540
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300541struct io_splice {
542 struct file *file_out;
543 struct file *file_in;
544 loff_t off_out;
545 loff_t off_in;
546 u64 len;
547 unsigned int flags;
548};
549
Jens Axboeddf0322d2020-02-23 16:41:33 -0700550struct io_provide_buf {
551 struct file *file;
552 __u64 addr;
553 __s32 len;
554 __u32 bgid;
555 __u16 nbufs;
556 __u16 bid;
557};
558
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700559struct io_statx {
560 struct file *file;
561 int dfd;
562 unsigned int mask;
563 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700564 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700565 struct statx __user *buffer;
566};
567
Jens Axboe36f4fa62020-09-05 11:14:22 -0600568struct io_shutdown {
569 struct file *file;
570 int how;
571};
572
Jens Axboe80a261f2020-09-28 14:23:58 -0600573struct io_rename {
574 struct file *file;
575 int old_dfd;
576 int new_dfd;
577 struct filename *oldpath;
578 struct filename *newpath;
579 int flags;
580};
581
Jens Axboe14a11432020-09-28 14:27:37 -0600582struct io_unlink {
583 struct file *file;
584 int dfd;
585 int flags;
586 struct filename *filename;
587};
588
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300589struct io_completion {
590 struct file *file;
591 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300592 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300593};
594
Jens Axboef499a022019-12-02 16:28:46 -0700595struct io_async_connect {
596 struct sockaddr_storage address;
597};
598
Jens Axboe03b12302019-12-02 18:50:25 -0700599struct io_async_msghdr {
600 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000601 /* points to an allocated iov, if NULL we use fast_iov instead */
602 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700603 struct sockaddr __user *uaddr;
604 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700605 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700606};
607
Jens Axboef67676d2019-12-02 11:03:47 -0700608struct io_async_rw {
609 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600610 const struct iovec *free_iovec;
611 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600612 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600613 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700614};
615
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300616enum {
617 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
618 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
619 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
620 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
621 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700622 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300623
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624 REQ_F_FAIL_LINK_BIT,
625 REQ_F_INFLIGHT_BIT,
626 REQ_F_CUR_POS_BIT,
627 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300628 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300629 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300630 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700631 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700632 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600633 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800634 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100635 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000636 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700637
638 /* not a real bit, just to check we're not overflowing the space */
639 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300640};
641
642enum {
643 /* ctx owns file */
644 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
645 /* drain existing IO first */
646 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
647 /* linked sqes */
648 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
649 /* doesn't sever on completion < 0 */
650 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
651 /* IOSQE_ASYNC */
652 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700653 /* IOSQE_BUFFER_SELECT */
654 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300655
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300656 /* fail rest of links */
657 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
658 /* on inflight list */
659 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
660 /* read/write uses file position */
661 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
662 /* must not punt to workers */
663 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100664 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300665 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300666 /* regular file */
667 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300668 /* needs cleanup */
669 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700670 /* already went through poll handler */
671 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700672 /* buffer already selected */
673 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600674 /* doesn't need file table for this request */
675 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800676 /* io_wq_work is initialized */
677 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100678 /* linked timeout is active, i.e. prepared by link's head */
679 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000680 /* completion is deferred through io_comp_state */
681 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700682};
683
684struct async_poll {
685 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600686 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300687};
688
Jens Axboe09bb8392019-03-13 12:39:28 -0600689/*
690 * NOTE! Each of the iocb union members has the file pointer
691 * as the first entry in their struct definition. So you can
692 * access the file pointer through any of the sub-structs,
693 * or directly as just 'ki_filp' in this struct.
694 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700695struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700696 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600697 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700698 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700699 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000700 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700701 struct io_accept accept;
702 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700703 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700704 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100705 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700706 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700707 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700708 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700709 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000710 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700711 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700712 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700713 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300714 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700715 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700716 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600717 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600718 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600719 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300720 /* use only after cleaning per-op data, see io_clean_op() */
721 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700722 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723
Jens Axboee8c2bc12020-08-15 18:44:09 -0700724 /* opcode allocated if it needs to store data for async defer */
725 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700726 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800727 /* polled IO has completed */
728 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700730 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300731 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700732
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300733 struct io_ring_ctx *ctx;
734 unsigned int flags;
735 refcount_t refs;
736 struct task_struct *task;
737 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700738
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000739 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000740 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700741
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300742 /*
743 * 1. used with ctx->iopoll_list with reads/writes
744 * 2. to track reqs with ->files (see io_op_def::file_table)
745 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300746 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300747 struct callback_head task_work;
748 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
749 struct hlist_node hash_node;
750 struct async_poll *apoll;
751 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700752};
753
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300754struct io_defer_entry {
755 struct list_head list;
756 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300757 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300758};
759
Jens Axboedef596e2019-01-09 08:59:42 -0700760#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700761
Jens Axboe013538b2020-06-22 09:29:15 -0600762struct io_comp_state {
763 unsigned int nr;
764 struct list_head list;
765 struct io_ring_ctx *ctx;
766};
767
Jens Axboe9a56a232019-01-09 09:06:50 -0700768struct io_submit_state {
769 struct blk_plug plug;
770
771 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700772 * io_kiocb alloc cache
773 */
774 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300775 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700776
Jens Axboe27926b62020-10-28 09:33:23 -0600777 bool plug_started;
778
Jens Axboe2579f912019-01-09 09:10:43 -0700779 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600780 * Batch completion logic
781 */
782 struct io_comp_state comp;
783
784 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700785 * File reference cache
786 */
787 struct file *file;
788 unsigned int fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +0000789 unsigned int file_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700790 unsigned int ios_left;
791};
792
Jens Axboed3656342019-12-18 09:50:26 -0700793struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700794 /* needs req->file assigned */
795 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700796 /* hash wq insertion if file is a regular file */
797 unsigned hash_reg_file : 1;
798 /* unbound wq insertion if file is a non-regular file */
799 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700800 /* opcode is not supported by this kernel */
801 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700802 /* set if opcode supports polled "wait" */
803 unsigned pollin : 1;
804 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700805 /* op supports buffer selection */
806 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700807 /* must always have async data allocated */
808 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600809 /* should block plug */
810 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700811 /* size of async data needed, if any */
812 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600813 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700814};
815
Jens Axboe09186822020-10-13 15:01:40 -0600816static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_NOP] = {},
818 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .needs_file = 1,
820 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700821 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700822 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700823 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600824 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700825 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600826 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700829 .needs_file = 1,
830 .hash_reg_file = 1,
831 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700832 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700833 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600834 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600836 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
837 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700840 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600841 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700842 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300843 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700844 .needs_file = 1,
845 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600847 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700848 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600849 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700850 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300851 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700852 .needs_file = 1,
853 .hash_reg_file = 1,
854 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700855 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600856 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700857 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600858 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
859 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700862 .needs_file = 1,
863 .unbound_nonreg_file = 1,
864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_POLL_REMOVE] = {},
866 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700867 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600868 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700871 .needs_file = 1,
872 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700873 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700874 .needs_async_data = 1,
875 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000876 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700879 .needs_file = 1,
880 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700881 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700882 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700883 .needs_async_data = 1,
884 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000885 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700886 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300887 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700888 .needs_async_data = 1,
889 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600890 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700891 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000892 [IORING_OP_TIMEOUT_REMOVE] = {
893 /* used by timeout updates' prep() */
894 .work_flags = IO_WQ_WORK_MM,
895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700897 .needs_file = 1,
898 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700899 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600900 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700901 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300902 [IORING_OP_ASYNC_CANCEL] = {},
903 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700904 .needs_async_data = 1,
905 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600906 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700907 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300908 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700909 .needs_file = 1,
910 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700911 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700912 .needs_async_data = 1,
913 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600914 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700915 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300916 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700917 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600918 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700919 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300920 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600921 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600922 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700923 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600925 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700926 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300927 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600928 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700929 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300930 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600931 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
932 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700933 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300934 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700935 .needs_file = 1,
936 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700937 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700938 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600939 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700940 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600941 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700944 .needs_file = 1,
945 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700946 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600947 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700948 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600949 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
950 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700951 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300952 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700953 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600954 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700955 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300956 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600957 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700958 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300959 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700960 .needs_file = 1,
961 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700962 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600963 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700966 .needs_file = 1,
967 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700968 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700969 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600970 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700971 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300972 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600973 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600974 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700975 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700976 [IORING_OP_EPOLL_CTL] = {
977 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600978 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700979 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300980 [IORING_OP_SPLICE] = {
981 .needs_file = 1,
982 .hash_reg_file = 1,
983 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600984 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700985 },
986 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700987 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300988 [IORING_OP_TEE] = {
989 .needs_file = 1,
990 .hash_reg_file = 1,
991 .unbound_nonreg_file = 1,
992 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600993 [IORING_OP_SHUTDOWN] = {
994 .needs_file = 1,
995 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600996 [IORING_OP_RENAMEAT] = {
997 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
998 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
999 },
Jens Axboe14a11432020-09-28 14:27:37 -06001000 [IORING_OP_UNLINKAT] = {
1001 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1002 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1003 },
Jens Axboed3656342019-12-18 09:50:26 -07001004};
1005
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001006enum io_mem_account {
1007 ACCT_LOCKED,
1008 ACCT_PINNED,
1009};
1010
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001011static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1012 struct task_struct *task,
1013 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001014static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001015static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001016 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001017static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1018 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001019
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001020static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
1021 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001022static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001023static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001024static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001025static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001026static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001027static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001028static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001029static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001030 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001031 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001032static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001033static struct file *io_file_get(struct io_submit_state *state,
1034 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001035static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001036static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001037
Pavel Begunkov847595d2021-02-04 13:52:06 +00001038static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1039 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001040static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1041 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001042 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001043static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044
1045static struct kmem_cache *req_cachep;
1046
Jens Axboe09186822020-10-13 15:01:40 -06001047static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048
1049struct sock *io_uring_get_socket(struct file *file)
1050{
1051#if defined(CONFIG_UNIX)
1052 if (file->f_op == &io_uring_fops) {
1053 struct io_ring_ctx *ctx = file->private_data;
1054
1055 return ctx->ring_sock->sk;
1056 }
1057#endif
1058 return NULL;
1059}
1060EXPORT_SYMBOL(io_uring_get_socket);
1061
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001062#define io_for_each_link(pos, head) \
1063 for (pos = (head); pos; pos = pos->link)
1064
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001065static inline void io_clean_op(struct io_kiocb *req)
1066{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001067 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001068 __io_clean_op(req);
1069}
1070
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001071static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001072{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001073 struct io_ring_ctx *ctx = req->ctx;
1074
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001075 if (!req->fixed_rsrc_refs) {
1076 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1077 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001078 }
1079}
1080
Pavel Begunkov08d23632020-11-06 13:00:22 +00001081static bool io_match_task(struct io_kiocb *head,
1082 struct task_struct *task,
1083 struct files_struct *files)
1084{
1085 struct io_kiocb *req;
1086
Jens Axboe84965ff2021-01-23 15:51:11 -07001087 if (task && head->task != task) {
1088 /* in terms of cancelation, always match if req task is dead */
1089 if (head->task->flags & PF_EXITING)
1090 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001091 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001092 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001093 if (!files)
1094 return true;
1095
1096 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001097 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1098 continue;
1099 if (req->file && req->file->f_op == &io_uring_fops)
1100 return true;
1101 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001102 req->work.identity->files == files)
1103 return true;
1104 }
1105 return false;
1106}
1107
Jens Axboe28cea78a2020-09-14 10:51:17 -06001108static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001109{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001110 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001111 struct mm_struct *mm = current->mm;
1112
1113 if (mm) {
1114 kthread_unuse_mm(mm);
1115 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001116 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001117 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001118 if (files) {
1119 struct nsproxy *nsproxy = current->nsproxy;
1120
1121 task_lock(current);
1122 current->files = NULL;
1123 current->nsproxy = NULL;
1124 task_unlock(current);
1125 put_files_struct(files);
1126 put_nsproxy(nsproxy);
1127 }
1128}
1129
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001130static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001131{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001132 if (current->flags & PF_EXITING)
1133 return -EFAULT;
1134
Jens Axboe28cea78a2020-09-14 10:51:17 -06001135 if (!current->files) {
1136 struct files_struct *files;
1137 struct nsproxy *nsproxy;
1138
1139 task_lock(ctx->sqo_task);
1140 files = ctx->sqo_task->files;
1141 if (!files) {
1142 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001143 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001144 }
1145 atomic_inc(&files->count);
1146 get_nsproxy(ctx->sqo_task->nsproxy);
1147 nsproxy = ctx->sqo_task->nsproxy;
1148 task_unlock(ctx->sqo_task);
1149
1150 task_lock(current);
1151 current->files = files;
1152 current->nsproxy = nsproxy;
1153 task_unlock(current);
1154 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001155 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001156}
1157
1158static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1159{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001160 struct mm_struct *mm;
1161
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001162 if (current->flags & PF_EXITING)
1163 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001164 if (current->mm)
1165 return 0;
1166
1167 /* Should never happen */
1168 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1169 return -EFAULT;
1170
1171 task_lock(ctx->sqo_task);
1172 mm = ctx->sqo_task->mm;
1173 if (unlikely(!mm || !mmget_not_zero(mm)))
1174 mm = NULL;
1175 task_unlock(ctx->sqo_task);
1176
1177 if (mm) {
1178 kthread_use_mm(mm);
1179 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001180 }
1181
Jens Axboe4b70cf92020-11-02 10:39:05 -07001182 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001183}
1184
Jens Axboe28cea78a2020-09-14 10:51:17 -06001185static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1186 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001187{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001188 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001189 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001190
1191 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001192 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001193 if (unlikely(ret))
1194 return ret;
1195 }
1196
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001197 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1198 ret = __io_sq_thread_acquire_files(ctx);
1199 if (unlikely(ret))
1200 return ret;
1201 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001202
1203 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001204}
1205
Dennis Zhou91d8f512020-09-16 13:41:05 -07001206static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1207 struct cgroup_subsys_state **cur_css)
1208
1209{
1210#ifdef CONFIG_BLK_CGROUP
1211 /* puts the old one when swapping */
1212 if (*cur_css != ctx->sqo_blkcg_css) {
1213 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1214 *cur_css = ctx->sqo_blkcg_css;
1215 }
1216#endif
1217}
1218
1219static void io_sq_thread_unassociate_blkcg(void)
1220{
1221#ifdef CONFIG_BLK_CGROUP
1222 kthread_associate_blkcg(NULL);
1223#endif
1224}
1225
Jens Axboec40f6372020-06-25 15:39:59 -06001226static inline void req_set_fail_links(struct io_kiocb *req)
1227{
1228 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1229 req->flags |= REQ_F_FAIL_LINK;
1230}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001231
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001232/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001233 * None of these are dereferenced, they are simply used to check if any of
1234 * them have changed. If we're under current and check they are still the
1235 * same, we're fine to grab references to them for actual out-of-line use.
1236 */
1237static void io_init_identity(struct io_identity *id)
1238{
1239 id->files = current->files;
1240 id->mm = current->mm;
1241#ifdef CONFIG_BLK_CGROUP
1242 rcu_read_lock();
1243 id->blkcg_css = blkcg_css();
1244 rcu_read_unlock();
1245#endif
1246 id->creds = current_cred();
1247 id->nsproxy = current->nsproxy;
1248 id->fs = current->fs;
1249 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001250#ifdef CONFIG_AUDIT
1251 id->loginuid = current->loginuid;
1252 id->sessionid = current->sessionid;
1253#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001254 refcount_set(&id->count, 1);
1255}
1256
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001257static inline void __io_req_init_async(struct io_kiocb *req)
1258{
1259 memset(&req->work, 0, sizeof(req->work));
1260 req->flags |= REQ_F_WORK_INITIALIZED;
1261}
1262
Jens Axboe1e6fa522020-10-15 08:46:24 -06001263/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001264 * Note: must call io_req_init_async() for the first time you
1265 * touch any members of io_wq_work.
1266 */
1267static inline void io_req_init_async(struct io_kiocb *req)
1268{
Jens Axboe500a3732020-10-15 17:38:03 -06001269 struct io_uring_task *tctx = current->io_uring;
1270
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001271 if (req->flags & REQ_F_WORK_INITIALIZED)
1272 return;
1273
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001274 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001275
1276 /* Grab a ref if this isn't our static identity */
1277 req->work.identity = tctx->identity;
1278 if (tctx->identity != &tctx->__identity)
1279 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001280}
1281
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1283{
1284 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1285
Jens Axboe0f158b42020-05-14 17:18:39 -06001286 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287}
1288
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001289static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1290{
1291 return !req->timeout.off;
1292}
1293
Jens Axboe2b188cc2019-01-07 10:46:33 -07001294static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1295{
1296 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001297 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001298
1299 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1300 if (!ctx)
1301 return NULL;
1302
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001303 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1304 if (!ctx->fallback_req)
1305 goto err;
1306
Jens Axboe78076bb2019-12-04 19:56:40 -07001307 /*
1308 * Use 5 bits less than the max cq entries, that should give us around
1309 * 32 entries per hash list if totally full and uniformly spread.
1310 */
1311 hash_bits = ilog2(p->cq_entries);
1312 hash_bits -= 5;
1313 if (hash_bits <= 0)
1314 hash_bits = 1;
1315 ctx->cancel_hash_bits = hash_bits;
1316 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1317 GFP_KERNEL);
1318 if (!ctx->cancel_hash)
1319 goto err;
1320 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1321
Roman Gushchin21482892019-05-07 10:01:48 -07001322 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001323 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1324 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325
1326 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001327 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001328 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001330 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001331 init_completion(&ctx->ref_comp);
1332 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001333 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001334 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001335 mutex_init(&ctx->uring_lock);
1336 init_waitqueue_head(&ctx->wait);
1337 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001338 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001339 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001340 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001341 spin_lock_init(&ctx->inflight_lock);
1342 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001343 spin_lock_init(&ctx->rsrc_ref_lock);
1344 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001345 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1346 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001348err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001349 if (ctx->fallback_req)
1350 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001351 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001352 kfree(ctx);
1353 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001354}
1355
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001356static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001357{
Jens Axboe2bc99302020-07-09 09:43:27 -06001358 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1359 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001360
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001361 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001362 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001363 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001364
Bob Liu9d858b22019-11-13 18:06:25 +08001365 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001366}
1367
Jens Axboe5c3462c2020-10-15 09:02:33 -06001368static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001369{
Jens Axboe500a3732020-10-15 17:38:03 -06001370 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001371 return;
1372 if (refcount_dec_and_test(&req->work.identity->count))
1373 kfree(req->work.identity);
1374}
1375
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001376static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001377{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001378 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001379 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001380
Pavel Begunkove86d0042021-02-01 18:59:54 +00001381 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001382 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001383#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001384 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001385 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001386#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001387 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001388 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001389 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001390 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001391
Jens Axboe98447d62020-10-14 10:48:51 -06001392 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001393 if (--fs->users)
1394 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001395 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001396 if (fs)
1397 free_fs_struct(fs);
1398 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001399 if (req->work.flags & IO_WQ_WORK_FILES) {
1400 put_files_struct(req->work.identity->files);
1401 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001402 }
1403 if (req->flags & REQ_F_INFLIGHT) {
1404 struct io_ring_ctx *ctx = req->ctx;
1405 struct io_uring_task *tctx = req->task->io_uring;
1406 unsigned long flags;
1407
1408 spin_lock_irqsave(&ctx->inflight_lock, flags);
1409 list_del(&req->inflight_entry);
1410 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1411 req->flags &= ~REQ_F_INFLIGHT;
1412 if (atomic_read(&tctx->in_idle))
1413 wake_up(&tctx->wait);
1414 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001415
Pavel Begunkove86d0042021-02-01 18:59:54 +00001416 req->flags &= ~REQ_F_WORK_INITIALIZED;
1417 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1418 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001419 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001420}
1421
1422/*
1423 * Create a private copy of io_identity, since some fields don't match
1424 * the current context.
1425 */
1426static bool io_identity_cow(struct io_kiocb *req)
1427{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001428 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001429 const struct cred *creds = NULL;
1430 struct io_identity *id;
1431
1432 if (req->work.flags & IO_WQ_WORK_CREDS)
1433 creds = req->work.identity->creds;
1434
1435 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1436 if (unlikely(!id)) {
1437 req->work.flags |= IO_WQ_WORK_CANCEL;
1438 return false;
1439 }
1440
1441 /*
1442 * We can safely just re-init the creds we copied Either the field
1443 * matches the current one, or we haven't grabbed it yet. The only
1444 * exception is ->creds, through registered personalities, so handle
1445 * that one separately.
1446 */
1447 io_init_identity(id);
1448 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001449 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001450
1451 /* add one for this request */
1452 refcount_inc(&id->count);
1453
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001454 /* drop tctx and req identity references, if needed */
1455 if (tctx->identity != &tctx->__identity &&
1456 refcount_dec_and_test(&tctx->identity->count))
1457 kfree(tctx->identity);
1458 if (req->work.identity != &tctx->__identity &&
1459 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001460 kfree(req->work.identity);
1461
1462 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001463 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001464 return true;
1465}
1466
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001467static void io_req_track_inflight(struct io_kiocb *req)
1468{
1469 struct io_ring_ctx *ctx = req->ctx;
1470
1471 if (!(req->flags & REQ_F_INFLIGHT)) {
1472 io_req_init_async(req);
1473 req->flags |= REQ_F_INFLIGHT;
1474
1475 spin_lock_irq(&ctx->inflight_lock);
1476 list_add(&req->inflight_entry, &ctx->inflight_list);
1477 spin_unlock_irq(&ctx->inflight_lock);
1478 }
1479}
1480
Jens Axboe1e6fa522020-10-15 08:46:24 -06001481static bool io_grab_identity(struct io_kiocb *req)
1482{
1483 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001484 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001485
Jens Axboe69228332020-10-20 14:28:41 -06001486 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1487 if (id->fsize != rlimit(RLIMIT_FSIZE))
1488 return false;
1489 req->work.flags |= IO_WQ_WORK_FSIZE;
1490 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001491#ifdef CONFIG_BLK_CGROUP
1492 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1493 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1494 rcu_read_lock();
1495 if (id->blkcg_css != blkcg_css()) {
1496 rcu_read_unlock();
1497 return false;
1498 }
1499 /*
1500 * This should be rare, either the cgroup is dying or the task
1501 * is moving cgroups. Just punt to root for the handful of ios.
1502 */
1503 if (css_tryget_online(id->blkcg_css))
1504 req->work.flags |= IO_WQ_WORK_BLKCG;
1505 rcu_read_unlock();
1506 }
1507#endif
1508 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1509 if (id->creds != current_cred())
1510 return false;
1511 get_cred(id->creds);
1512 req->work.flags |= IO_WQ_WORK_CREDS;
1513 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001514#ifdef CONFIG_AUDIT
1515 if (!uid_eq(current->loginuid, id->loginuid) ||
1516 current->sessionid != id->sessionid)
1517 return false;
1518#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001519 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1520 (def->work_flags & IO_WQ_WORK_FS)) {
1521 if (current->fs != id->fs)
1522 return false;
1523 spin_lock(&id->fs->lock);
1524 if (!id->fs->in_exec) {
1525 id->fs->users++;
1526 req->work.flags |= IO_WQ_WORK_FS;
1527 } else {
1528 req->work.flags |= IO_WQ_WORK_CANCEL;
1529 }
1530 spin_unlock(&current->fs->lock);
1531 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001532 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1533 (def->work_flags & IO_WQ_WORK_FILES) &&
1534 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1535 if (id->files != current->files ||
1536 id->nsproxy != current->nsproxy)
1537 return false;
1538 atomic_inc(&id->files->count);
1539 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001540 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001541 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001542 }
Jens Axboe77788772020-12-29 10:50:46 -07001543 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1544 (def->work_flags & IO_WQ_WORK_MM)) {
1545 if (id->mm != current->mm)
1546 return false;
1547 mmgrab(id->mm);
1548 req->work.flags |= IO_WQ_WORK_MM;
1549 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001550
1551 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001552}
1553
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001554static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001555{
Jens Axboed3656342019-12-18 09:50:26 -07001556 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001557 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001558
Pavel Begunkov16d59802020-07-12 16:16:47 +03001559 io_req_init_async(req);
1560
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001561 if (req->flags & REQ_F_FORCE_ASYNC)
1562 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1563
Jens Axboed3656342019-12-18 09:50:26 -07001564 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001565 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001566 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001567 } else {
1568 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001569 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001570 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001571
Jens Axboe1e6fa522020-10-15 08:46:24 -06001572 /* if we fail grabbing identity, we must COW, regrab, and retry */
1573 if (io_grab_identity(req))
1574 return;
1575
1576 if (!io_identity_cow(req))
1577 return;
1578
1579 /* can't fail at this point */
1580 if (!io_grab_identity(req))
1581 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001582}
1583
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001584static void io_prep_async_link(struct io_kiocb *req)
1585{
1586 struct io_kiocb *cur;
1587
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001588 io_for_each_link(cur, req)
1589 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001590}
1591
Jens Axboe7271ef32020-08-10 09:55:22 -06001592static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001593{
Jackie Liua197f662019-11-08 08:09:12 -07001594 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001595 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001596
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001597 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1598 &req->work, req->flags);
1599 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001600 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001601}
1602
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001603static void io_queue_async_work(struct io_kiocb *req)
1604{
Jens Axboe7271ef32020-08-10 09:55:22 -06001605 struct io_kiocb *link;
1606
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001607 /* init ->work of the whole link before punting */
1608 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001609 link = __io_queue_async_work(req);
1610
1611 if (link)
1612 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001613}
1614
Jens Axboe5262f562019-09-17 12:26:57 -06001615static void io_kill_timeout(struct io_kiocb *req)
1616{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001617 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001618 int ret;
1619
Jens Axboee8c2bc12020-08-15 18:44:09 -07001620 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001621 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001622 atomic_set(&req->ctx->cq_timeouts,
1623 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001624 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001625 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001626 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001627 }
1628}
1629
Jens Axboe76e1b642020-09-26 15:05:03 -06001630/*
1631 * Returns true if we found and killed one or more timeouts
1632 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001633static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1634 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001635{
1636 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001637 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001638
1639 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001640 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001641 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001642 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001643 canceled++;
1644 }
Jens Axboef3606e32020-09-22 08:18:24 -06001645 }
Jens Axboe5262f562019-09-17 12:26:57 -06001646 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001647 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001648}
1649
Pavel Begunkov04518942020-05-26 20:34:05 +03001650static void __io_queue_deferred(struct io_ring_ctx *ctx)
1651{
1652 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001653 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1654 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001655
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001656 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001657 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001658 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001659 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001660 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001661 } while (!list_empty(&ctx->defer_list));
1662}
1663
Pavel Begunkov360428f2020-05-30 14:54:17 +03001664static void io_flush_timeouts(struct io_ring_ctx *ctx)
1665{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001666 u32 seq;
1667
1668 if (list_empty(&ctx->timeout_list))
1669 return;
1670
1671 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1672
1673 do {
1674 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001675 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001676 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001677
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001678 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001679 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001680
1681 /*
1682 * Since seq can easily wrap around over time, subtract
1683 * the last seq at which timeouts were flushed before comparing.
1684 * Assuming not more than 2^31-1 events have happened since,
1685 * these subtractions won't have wrapped, so we can check if
1686 * target is in [last_seq, current_seq] by comparing the two.
1687 */
1688 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1689 events_got = seq - ctx->cq_last_tm_flush;
1690 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001691 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001692
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001693 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001694 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001695 } while (!list_empty(&ctx->timeout_list));
1696
1697 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001698}
1699
Jens Axboede0617e2019-04-06 21:51:27 -06001700static void io_commit_cqring(struct io_ring_ctx *ctx)
1701{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001702 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001703
1704 /* order cqe stores with ring update */
1705 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001706
Pavel Begunkov04518942020-05-26 20:34:05 +03001707 if (unlikely(!list_empty(&ctx->defer_list)))
1708 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001709}
1710
Jens Axboe90554202020-09-03 12:12:41 -06001711static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1712{
1713 struct io_rings *r = ctx->rings;
1714
1715 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1716}
1717
Pavel Begunkov888aae22021-01-19 13:32:39 +00001718static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1719{
1720 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1721}
1722
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1724{
Hristo Venev75b28af2019-08-26 17:23:46 +00001725 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726 unsigned tail;
1727
Stefan Bühler115e12e2019-04-24 23:54:18 +02001728 /*
1729 * writes to the cq entry need to come after reading head; the
1730 * control dependency is enough as we're using WRITE_ONCE to
1731 * fill the cq entry
1732 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001733 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734 return NULL;
1735
Pavel Begunkov888aae22021-01-19 13:32:39 +00001736 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001737 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738}
1739
Jens Axboef2842ab2020-01-08 11:04:00 -07001740static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1741{
Jens Axboef0b493e2020-02-01 21:30:11 -07001742 if (!ctx->cq_ev_fd)
1743 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001744 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1745 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001746 if (!ctx->eventfd_async)
1747 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001748 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001749}
1750
Jens Axboeb41e9852020-02-17 09:52:41 -07001751static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001752{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001753 /* see waitqueue_active() comment */
1754 smp_mb();
1755
Jens Axboe8c838782019-03-12 15:48:16 -06001756 if (waitqueue_active(&ctx->wait))
1757 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001758 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1759 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001760 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001761 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001762 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001763 wake_up_interruptible(&ctx->cq_wait);
1764 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1765 }
Jens Axboe8c838782019-03-12 15:48:16 -06001766}
1767
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001768static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1769{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001770 /* see waitqueue_active() comment */
1771 smp_mb();
1772
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001773 if (ctx->flags & IORING_SETUP_SQPOLL) {
1774 if (waitqueue_active(&ctx->wait))
1775 wake_up(&ctx->wait);
1776 }
1777 if (io_should_trigger_evfd(ctx))
1778 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001779 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001780 wake_up_interruptible(&ctx->cq_wait);
1781 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1782 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001783}
1784
Jens Axboec4a2ed72019-11-21 21:01:26 -07001785/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001786static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1787 struct task_struct *tsk,
1788 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001790 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001791 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001792 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001793 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001794 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001795 LIST_HEAD(list);
1796
Pavel Begunkove23de152020-12-17 00:24:37 +00001797 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1798 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001799
Jens Axboeb18032b2021-01-24 16:58:56 -07001800 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001801 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001802 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001803 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001804 continue;
1805
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001806 cqe = io_get_cqring(ctx);
1807 if (!cqe && !force)
1808 break;
1809
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001810 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001811 if (cqe) {
1812 WRITE_ONCE(cqe->user_data, req->user_data);
1813 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001814 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001815 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001816 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001817 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001818 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001819 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001820 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001821 }
1822
Pavel Begunkov09e88402020-12-17 00:24:38 +00001823 all_flushed = list_empty(&ctx->cq_overflow_list);
1824 if (all_flushed) {
1825 clear_bit(0, &ctx->sq_check_overflow);
1826 clear_bit(0, &ctx->cq_check_overflow);
1827 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1828 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001829
Jens Axboeb18032b2021-01-24 16:58:56 -07001830 if (posted)
1831 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001832 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001833 if (posted)
1834 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001835
1836 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001837 req = list_first_entry(&list, struct io_kiocb, compl.list);
1838 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001839 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001840 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001841
Pavel Begunkov09e88402020-12-17 00:24:38 +00001842 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001843}
1844
Pavel Begunkov6c503152021-01-04 20:36:36 +00001845static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1846 struct task_struct *tsk,
1847 struct files_struct *files)
1848{
1849 if (test_bit(0, &ctx->cq_check_overflow)) {
1850 /* iopoll syncs against uring_lock, not completion_lock */
1851 if (ctx->flags & IORING_SETUP_IOPOLL)
1852 mutex_lock(&ctx->uring_lock);
1853 __io_cqring_overflow_flush(ctx, force, tsk, files);
1854 if (ctx->flags & IORING_SETUP_IOPOLL)
1855 mutex_unlock(&ctx->uring_lock);
1856 }
1857}
1858
Jens Axboebcda7ba2020-02-23 16:42:51 -07001859static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001860{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862 struct io_uring_cqe *cqe;
1863
Jens Axboe78e19bb2019-11-06 15:21:34 -07001864 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001865
Jens Axboe2b188cc2019-01-07 10:46:33 -07001866 /*
1867 * If we can't get a cq entry, userspace overflowed the
1868 * submission (by quite a lot). Increment the overflow count in
1869 * the ring.
1870 */
1871 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001872 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001873 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001874 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001875 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001876 } else if (ctx->cq_overflow_flushed ||
1877 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001878 /*
1879 * If we're in ring overflow flush mode, or in task cancel mode,
1880 * then we cannot store the request for later flushing, we need
1881 * to drop it on the floor.
1882 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001883 ctx->cached_cq_overflow++;
1884 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001885 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001886 if (list_empty(&ctx->cq_overflow_list)) {
1887 set_bit(0, &ctx->sq_check_overflow);
1888 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001889 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001890 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001891 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001892 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001893 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001894 refcount_inc(&req->refs);
1895 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896 }
1897}
1898
Jens Axboebcda7ba2020-02-23 16:42:51 -07001899static void io_cqring_fill_event(struct io_kiocb *req, long res)
1900{
1901 __io_cqring_fill_event(req, res, 0);
1902}
1903
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001904static void io_req_complete_post(struct io_kiocb *req, long res,
1905 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001906{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001907 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908 unsigned long flags;
1909
1910 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001911 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912 io_commit_cqring(ctx);
1913 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1914
Jens Axboe8c838782019-03-12 15:48:16 -06001915 io_cqring_ev_posted(ctx);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001916}
1917
1918static inline void io_req_complete_nostate(struct io_kiocb *req, long res,
1919 unsigned int cflags)
1920{
1921 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001922 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923}
1924
Jens Axboe229a7b62020-06-22 10:13:11 -06001925static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001926{
Jens Axboe229a7b62020-06-22 10:13:11 -06001927 struct io_ring_ctx *ctx = cs->ctx;
1928
1929 spin_lock_irq(&ctx->completion_lock);
1930 while (!list_empty(&cs->list)) {
1931 struct io_kiocb *req;
1932
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001933 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1934 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001935 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001936
1937 /*
1938 * io_free_req() doesn't care about completion_lock unless one
1939 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1940 * because of a potential deadlock with req->work.fs->lock
Pavel Begunkove342c802021-01-19 13:32:47 +00001941 * We defer both, completion and submission refs.
Pavel Begunkov216578e2020-10-13 09:44:00 +01001942 */
1943 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1944 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001945 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkove342c802021-01-19 13:32:47 +00001946 io_double_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001947 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001948 } else {
Pavel Begunkove342c802021-01-19 13:32:47 +00001949 io_double_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001950 }
1951 }
1952 io_commit_cqring(ctx);
1953 spin_unlock_irq(&ctx->completion_lock);
1954
1955 io_cqring_ev_posted(ctx);
1956 cs->nr = 0;
1957}
1958
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001959static void io_req_complete_state(struct io_kiocb *req, long res,
1960 unsigned int cflags, struct io_comp_state *cs)
Jens Axboe229a7b62020-06-22 10:13:11 -06001961{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001962 io_clean_op(req);
1963 req->result = res;
1964 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001965 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001966}
1967
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001968static inline void __io_req_complete(struct io_kiocb *req, long res,
1969 unsigned cflags, struct io_comp_state *cs)
1970{
1971 if (!cs)
1972 io_req_complete_nostate(req, res, cflags);
1973 else
1974 io_req_complete_state(req, res, cflags, cs);
1975}
1976
1977static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001978{
Jens Axboe229a7b62020-06-22 10:13:11 -06001979 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001980}
1981
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001982static inline bool io_is_fallback_req(struct io_kiocb *req)
1983{
1984 return req == (struct io_kiocb *)
1985 ((unsigned long) req->ctx->fallback_req & ~1UL);
1986}
1987
1988static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1989{
1990 struct io_kiocb *req;
1991
1992 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001993 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001994 return req;
1995
1996 return NULL;
1997}
1998
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001999static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
2000 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002001{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002002 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002003 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002004 size_t sz;
2005 int ret;
2006
2007 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06002008 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
2009
2010 /*
2011 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2012 * retry single alloc to be on the safe side.
2013 */
2014 if (unlikely(ret <= 0)) {
2015 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2016 if (!state->reqs[0])
Pavel Begunkov85bcb6c2021-01-19 13:32:40 +00002017 return io_get_fallback_req(ctx);
Jens Axboefd6fab22019-03-14 16:30:06 -06002018 ret = 1;
2019 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002020 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021 }
2022
Pavel Begunkov291b2822020-09-30 22:57:01 +03002023 state->free_reqs--;
2024 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025}
2026
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002027static inline void io_put_file(struct io_kiocb *req, struct file *file,
2028 bool fixed)
2029{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002030 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002031 fput(file);
2032}
2033
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002034static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002035{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002036 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002037
Jens Axboee8c2bc12020-08-15 18:44:09 -07002038 if (req->async_data)
2039 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002040 if (req->file)
2041 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002042 if (req->fixed_rsrc_refs)
2043 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002044 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002045}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002046
Pavel Begunkov7c660732021-01-25 11:42:21 +00002047static inline void io_put_task(struct task_struct *task, int nr)
2048{
2049 struct io_uring_task *tctx = task->io_uring;
2050
2051 percpu_counter_sub(&tctx->inflight, nr);
2052 if (unlikely(atomic_read(&tctx->in_idle)))
2053 wake_up(&tctx->wait);
2054 put_task_struct_many(task, nr);
2055}
2056
Pavel Begunkov216578e2020-10-13 09:44:00 +01002057static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002058{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002059 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002060
Pavel Begunkov216578e2020-10-13 09:44:00 +01002061 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002062 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002063
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03002064 if (likely(!io_is_fallback_req(req)))
2065 kmem_cache_free(req_cachep, req);
2066 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002067 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
2068 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002069}
2070
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002071static inline void io_remove_next_linked(struct io_kiocb *req)
2072{
2073 struct io_kiocb *nxt = req->link;
2074
2075 req->link = nxt->link;
2076 nxt->link = NULL;
2077}
2078
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002079static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002080{
Jackie Liua197f662019-11-08 08:09:12 -07002081 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002082 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002083 bool cancelled = false;
2084 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002085
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002086 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002087 link = req->link;
2088
Pavel Begunkov900fad42020-10-19 16:39:16 +01002089 /*
2090 * Can happen if a linked timeout fired and link had been like
2091 * req -> link t-out -> link t-out [-> ...]
2092 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002093 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2094 struct io_timeout_data *io = link->async_data;
2095 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002096
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002097 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002098 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002099 ret = hrtimer_try_to_cancel(&io->timer);
2100 if (ret != -1) {
2101 io_cqring_fill_event(link, -ECANCELED);
2102 io_commit_cqring(ctx);
2103 cancelled = true;
2104 }
2105 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002106 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002107 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002108
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002109 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002110 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002111 io_put_req(link);
2112 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002113}
2114
Jens Axboe4d7dd462019-11-20 13:03:52 -07002115
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002116static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002117{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002118 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002119 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002120 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002121
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002122 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002123 link = req->link;
2124 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002125
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002126 while (link) {
2127 nxt = link->link;
2128 link->link = NULL;
2129
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002130 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002131 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002132
2133 /*
2134 * It's ok to free under spinlock as they're not linked anymore,
2135 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2136 * work.fs->lock.
2137 */
2138 if (link->flags & REQ_F_WORK_INITIALIZED)
2139 io_put_req_deferred(link, 2);
2140 else
2141 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002142 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002143 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002144 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002145 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002146
Jens Axboe2665abf2019-11-05 12:40:47 -07002147 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002148}
2149
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002150static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002151{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002152 if (req->flags & REQ_F_LINK_TIMEOUT)
2153 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002154
Jens Axboe9e645e112019-05-10 16:07:28 -06002155 /*
2156 * If LINK is set, we have dependent requests in this chain. If we
2157 * didn't fail this request, queue the first one up, moving any other
2158 * dependencies to the next request. In case of failure, fail the rest
2159 * of the chain.
2160 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002161 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2162 struct io_kiocb *nxt = req->link;
2163
2164 req->link = NULL;
2165 return nxt;
2166 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002167 io_fail_links(req);
2168 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002169}
Jens Axboe2665abf2019-11-05 12:40:47 -07002170
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002171static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002172{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002173 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002174 return NULL;
2175 return __io_req_find_next(req);
2176}
2177
Jens Axboe355fb9e2020-10-22 20:19:35 -06002178static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002179{
2180 struct task_struct *tsk = req->task;
2181 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002182 enum task_work_notify_mode notify;
2183 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002184
Jens Axboe6200b0a2020-09-13 14:38:30 -06002185 if (tsk->flags & PF_EXITING)
2186 return -ESRCH;
2187
Jens Axboec2c4c832020-07-01 15:37:11 -06002188 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002189 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2190 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2191 * processing task_work. There's no reliable way to tell if TWA_RESUME
2192 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002193 */
Jens Axboe91989c72020-10-16 09:02:26 -06002194 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002195 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002196 notify = TWA_SIGNAL;
2197
Jens Axboe87c43112020-09-30 21:00:14 -06002198 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002199 if (!ret)
2200 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002201
Jens Axboec2c4c832020-07-01 15:37:11 -06002202 return ret;
2203}
2204
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002205static void io_req_task_work_add_fallback(struct io_kiocb *req,
2206 void (*cb)(struct callback_head *))
2207{
2208 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2209
2210 init_task_work(&req->task_work, cb);
2211 task_work_add(tsk, &req->task_work, TWA_NONE);
2212 wake_up_process(tsk);
2213}
2214
Jens Axboec40f6372020-06-25 15:39:59 -06002215static void __io_req_task_cancel(struct io_kiocb *req, int error)
2216{
2217 struct io_ring_ctx *ctx = req->ctx;
2218
2219 spin_lock_irq(&ctx->completion_lock);
2220 io_cqring_fill_event(req, error);
2221 io_commit_cqring(ctx);
2222 spin_unlock_irq(&ctx->completion_lock);
2223
2224 io_cqring_ev_posted(ctx);
2225 req_set_fail_links(req);
2226 io_double_put_req(req);
2227}
2228
2229static void io_req_task_cancel(struct callback_head *cb)
2230{
2231 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002232 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002233
2234 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002235 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002236}
2237
2238static void __io_req_task_submit(struct io_kiocb *req)
2239{
2240 struct io_ring_ctx *ctx = req->ctx;
2241
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002242 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002243 if (!ctx->sqo_dead &&
2244 !__io_sq_thread_acquire_mm(ctx) &&
2245 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002246 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002247 else
Jens Axboec40f6372020-06-25 15:39:59 -06002248 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002249 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002250}
2251
Jens Axboec40f6372020-06-25 15:39:59 -06002252static void io_req_task_submit(struct callback_head *cb)
2253{
2254 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002255 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002256
2257 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002258 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002259}
2260
2261static void io_req_task_queue(struct io_kiocb *req)
2262{
Jens Axboec40f6372020-06-25 15:39:59 -06002263 int ret;
2264
2265 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002266 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002267
Jens Axboe355fb9e2020-10-22 20:19:35 -06002268 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002269 if (unlikely(ret))
2270 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002271}
2272
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002273static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002274{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002275 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002276
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002277 if (nxt)
2278 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002279}
2280
Jens Axboe9e645e112019-05-10 16:07:28 -06002281static void io_free_req(struct io_kiocb *req)
2282{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002283 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002284 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002285}
2286
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002287struct req_batch {
2288 void *reqs[IO_IOPOLL_BATCH];
2289 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002290
2291 struct task_struct *task;
2292 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002293};
2294
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002295static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002296{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002297 rb->to_free = 0;
2298 rb->task_refs = 0;
2299 rb->task = NULL;
2300}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002301
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002302static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2303 struct req_batch *rb)
2304{
2305 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2306 percpu_ref_put_many(&ctx->refs, rb->to_free);
2307 rb->to_free = 0;
2308}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002309
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002310static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2311 struct req_batch *rb)
2312{
2313 if (rb->to_free)
2314 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002315 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002316 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002317 rb->task = NULL;
2318 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002319}
2320
2321static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2322{
2323 if (unlikely(io_is_fallback_req(req))) {
2324 io_free_req(req);
2325 return;
2326 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002327 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002328
Jens Axboee3bc8e92020-09-24 08:45:57 -06002329 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002330 if (rb->task)
2331 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002332 rb->task = req->task;
2333 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002334 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002335 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002336
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002337 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002338 rb->reqs[rb->to_free++] = req;
2339 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2340 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002341}
2342
Jens Axboeba816ad2019-09-28 11:36:45 -06002343/*
2344 * Drop reference to request, return next in chain (if there is one) if this
2345 * was the last reference to this request.
2346 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002347static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002348{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002349 struct io_kiocb *nxt = NULL;
2350
Jens Axboe2a44f462020-02-25 13:25:41 -07002351 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002352 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002353 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002354 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002355 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002356}
2357
Jens Axboe2b188cc2019-01-07 10:46:33 -07002358static void io_put_req(struct io_kiocb *req)
2359{
Jens Axboedef596e2019-01-09 08:59:42 -07002360 if (refcount_dec_and_test(&req->refs))
2361 io_free_req(req);
2362}
2363
Pavel Begunkov216578e2020-10-13 09:44:00 +01002364static void io_put_req_deferred_cb(struct callback_head *cb)
2365{
2366 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2367
2368 io_free_req(req);
2369}
2370
2371static void io_free_req_deferred(struct io_kiocb *req)
2372{
2373 int ret;
2374
2375 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002376 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002377 if (unlikely(ret))
2378 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002379}
2380
2381static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2382{
2383 if (refcount_sub_and_test(refs, &req->refs))
2384 io_free_req_deferred(req);
2385}
2386
Jens Axboe978db572019-11-14 22:39:04 -07002387static void io_double_put_req(struct io_kiocb *req)
2388{
2389 /* drop both submit and complete references */
2390 if (refcount_sub_and_test(2, &req->refs))
2391 io_free_req(req);
2392}
2393
Pavel Begunkov6c503152021-01-04 20:36:36 +00002394static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002395{
2396 /* See comment at the top of this file */
2397 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002398 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002399}
2400
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002401static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2402{
2403 struct io_rings *rings = ctx->rings;
2404
2405 /* make sure SQ entry isn't read before tail */
2406 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2407}
2408
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002409static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002410{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002411 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002412
Jens Axboebcda7ba2020-02-23 16:42:51 -07002413 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2414 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002415 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002416 kfree(kbuf);
2417 return cflags;
2418}
2419
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002420static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2421{
2422 struct io_buffer *kbuf;
2423
2424 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2425 return io_put_kbuf(req, kbuf);
2426}
2427
Jens Axboe4c6e2772020-07-01 11:29:10 -06002428static inline bool io_run_task_work(void)
2429{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002430 /*
2431 * Not safe to run on exiting task, and the task_work handling will
2432 * not add work to such a task.
2433 */
2434 if (unlikely(current->flags & PF_EXITING))
2435 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002436 if (current->task_works) {
2437 __set_current_state(TASK_RUNNING);
2438 task_work_run();
2439 return true;
2440 }
2441
2442 return false;
2443}
2444
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002445static void io_iopoll_queue(struct list_head *again)
2446{
2447 struct io_kiocb *req;
2448
2449 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002450 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2451 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002452 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002453 } while (!list_empty(again));
2454}
2455
Jens Axboedef596e2019-01-09 08:59:42 -07002456/*
2457 * Find and free completed poll iocbs
2458 */
2459static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2460 struct list_head *done)
2461{
Jens Axboe8237e042019-12-28 10:48:22 -07002462 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002463 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002464 LIST_HEAD(again);
2465
2466 /* order with ->result store in io_complete_rw_iopoll() */
2467 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002468
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002469 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002470 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002471 int cflags = 0;
2472
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002473 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002474 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002475 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002476 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002477 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002478 continue;
2479 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002480 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002481
Jens Axboebcda7ba2020-02-23 16:42:51 -07002482 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002483 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002484
2485 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002486 (*nr_events)++;
2487
Pavel Begunkovc3524382020-06-28 12:52:32 +03002488 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002489 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002490 }
Jens Axboedef596e2019-01-09 08:59:42 -07002491
Jens Axboe09bb8392019-03-13 12:39:28 -06002492 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002493 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002494 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002495
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002496 if (!list_empty(&again))
2497 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002498}
2499
Jens Axboedef596e2019-01-09 08:59:42 -07002500static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2501 long min)
2502{
2503 struct io_kiocb *req, *tmp;
2504 LIST_HEAD(done);
2505 bool spin;
2506 int ret;
2507
2508 /*
2509 * Only spin for completions if we don't have multiple devices hanging
2510 * off our complete list, and we're under the requested amount.
2511 */
2512 spin = !ctx->poll_multi_file && *nr_events < min;
2513
2514 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002515 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002516 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002517
2518 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002519 * Move completed and retryable entries to our local lists.
2520 * If we find a request that requires polling, break out
2521 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002522 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002523 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002524 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002525 continue;
2526 }
2527 if (!list_empty(&done))
2528 break;
2529
2530 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2531 if (ret < 0)
2532 break;
2533
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002534 /* iopoll may have completed current req */
2535 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002536 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002537
Jens Axboedef596e2019-01-09 08:59:42 -07002538 if (ret && spin)
2539 spin = false;
2540 ret = 0;
2541 }
2542
2543 if (!list_empty(&done))
2544 io_iopoll_complete(ctx, nr_events, &done);
2545
2546 return ret;
2547}
2548
2549/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002550 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002551 * non-spinning poll check - we'll still enter the driver poll loop, but only
2552 * as a non-spinning completion check.
2553 */
2554static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2555 long min)
2556{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002557 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002558 int ret;
2559
2560 ret = io_do_iopoll(ctx, nr_events, min);
2561 if (ret < 0)
2562 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002563 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002564 return 0;
2565 }
2566
2567 return 1;
2568}
2569
2570/*
2571 * We can't just wait for polled events to come to us, we have to actively
2572 * find and complete them.
2573 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002574static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002575{
2576 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2577 return;
2578
2579 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002580 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002581 unsigned int nr_events = 0;
2582
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002583 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002584
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002585 /* let it sleep and repeat later if can't complete a request */
2586 if (nr_events == 0)
2587 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002588 /*
2589 * Ensure we allow local-to-the-cpu processing to take place,
2590 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002591 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002592 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002593 if (need_resched()) {
2594 mutex_unlock(&ctx->uring_lock);
2595 cond_resched();
2596 mutex_lock(&ctx->uring_lock);
2597 }
Jens Axboedef596e2019-01-09 08:59:42 -07002598 }
2599 mutex_unlock(&ctx->uring_lock);
2600}
2601
Pavel Begunkov7668b922020-07-07 16:36:21 +03002602static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002603{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002604 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002605 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002606
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002607 /*
2608 * We disallow the app entering submit/complete with polling, but we
2609 * still need to lock the ring to prevent racing with polled issue
2610 * that got punted to a workqueue.
2611 */
2612 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002613 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002614 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002615 * Don't enter poll loop if we already have events pending.
2616 * If we do, we can potentially be spinning for commands that
2617 * already triggered a CQE (eg in error).
2618 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002619 if (test_bit(0, &ctx->cq_check_overflow))
2620 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2621 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002622 break;
2623
2624 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002625 * If a submit got punted to a workqueue, we can have the
2626 * application entering polling for a command before it gets
2627 * issued. That app will hold the uring_lock for the duration
2628 * of the poll right here, so we need to take a breather every
2629 * now and then to ensure that the issue has a chance to add
2630 * the poll to the issued list. Otherwise we can spin here
2631 * forever, while the workqueue is stuck trying to acquire the
2632 * very same mutex.
2633 */
2634 if (!(++iters & 7)) {
2635 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002636 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002637 mutex_lock(&ctx->uring_lock);
2638 }
2639
Pavel Begunkov7668b922020-07-07 16:36:21 +03002640 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002641 if (ret <= 0)
2642 break;
2643 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002644 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002645
Jens Axboe500f9fb2019-08-19 12:15:59 -06002646 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002647 return ret;
2648}
2649
Jens Axboe491381ce2019-10-17 09:20:46 -06002650static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651{
Jens Axboe491381ce2019-10-17 09:20:46 -06002652 /*
2653 * Tell lockdep we inherited freeze protection from submission
2654 * thread.
2655 */
2656 if (req->flags & REQ_F_ISREG) {
2657 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002658
Jens Axboe491381ce2019-10-17 09:20:46 -06002659 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002661 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002662}
2663
Jens Axboea1d7c392020-06-22 11:09:46 -06002664static void io_complete_rw_common(struct kiocb *kiocb, long res,
2665 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002666{
Jens Axboe9adbd452019-12-20 08:45:55 -07002667 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002668 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669
Jens Axboe491381ce2019-10-17 09:20:46 -06002670 if (kiocb->ki_flags & IOCB_WRITE)
2671 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002673 if (res != req->result)
2674 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002675 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002676 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002677 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002678}
2679
Jens Axboeb63534c2020-06-04 11:28:00 -06002680#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002681static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002682{
2683 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002684 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002685 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002686
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002687 /* already prepared */
2688 if (req->async_data)
2689 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002690
2691 switch (req->opcode) {
2692 case IORING_OP_READV:
2693 case IORING_OP_READ_FIXED:
2694 case IORING_OP_READ:
2695 rw = READ;
2696 break;
2697 case IORING_OP_WRITEV:
2698 case IORING_OP_WRITE_FIXED:
2699 case IORING_OP_WRITE:
2700 rw = WRITE;
2701 break;
2702 default:
2703 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2704 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002705 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002706 }
2707
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002708 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2709 if (ret < 0)
2710 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002711 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002712}
Jens Axboeb63534c2020-06-04 11:28:00 -06002713#endif
2714
2715static bool io_rw_reissue(struct io_kiocb *req, long res)
2716{
2717#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002718 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002719 int ret;
2720
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002721 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002722 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002723 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002724 if (!S_ISBLK(mode) && !S_ISREG(mode))
2725 return false;
2726 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002727 return false;
2728
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002729 lockdep_assert_held(&req->ctx->uring_lock);
2730
Jens Axboe28cea78a2020-09-14 10:51:17 -06002731 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002732
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002733 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002734 refcount_inc(&req->refs);
2735 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002736 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002737 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002738 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002739#endif
2740 return false;
2741}
2742
Jens Axboea1d7c392020-06-22 11:09:46 -06002743static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2744 struct io_comp_state *cs)
2745{
2746 if (!io_rw_reissue(req, res))
2747 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002748}
2749
2750static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2751{
Jens Axboe9adbd452019-12-20 08:45:55 -07002752 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002753
Jens Axboea1d7c392020-06-22 11:09:46 -06002754 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002755}
2756
Jens Axboedef596e2019-01-09 08:59:42 -07002757static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2758{
Jens Axboe9adbd452019-12-20 08:45:55 -07002759 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002760
Jens Axboe491381ce2019-10-17 09:20:46 -06002761 if (kiocb->ki_flags & IOCB_WRITE)
2762 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002763
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002764 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002765 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002766
2767 WRITE_ONCE(req->result, res);
2768 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002769 smp_wmb();
2770 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002771}
2772
2773/*
2774 * After the iocb has been issued, it's safe to be found on the poll list.
2775 * Adding the kiocb to the list AFTER submission ensures that we don't
2776 * find it from a io_iopoll_getevents() thread before the issuer is done
2777 * accessing the kiocb cookie.
2778 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002779static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002780{
2781 struct io_ring_ctx *ctx = req->ctx;
2782
2783 /*
2784 * Track whether we have multiple files in our lists. This will impact
2785 * how we do polling eventually, not spinning if we're on potentially
2786 * different devices.
2787 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002788 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002789 ctx->poll_multi_file = false;
2790 } else if (!ctx->poll_multi_file) {
2791 struct io_kiocb *list_req;
2792
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002793 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002794 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002795 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002796 ctx->poll_multi_file = true;
2797 }
2798
2799 /*
2800 * For fast devices, IO may have already completed. If it has, add
2801 * it to the front so we find it first.
2802 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002803 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002804 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002805 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002806 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002807
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002808 /*
2809 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2810 * task context or in io worker task context. If current task context is
2811 * sq thread, we don't need to check whether should wake up sq thread.
2812 */
2813 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002814 wq_has_sleeper(&ctx->sq_data->wait))
2815 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002816}
2817
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002818static inline void io_state_file_put(struct io_submit_state *state)
2819{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002820 if (state->file_refs) {
2821 fput_many(state->file, state->file_refs);
2822 state->file_refs = 0;
2823 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002824}
2825
2826/*
2827 * Get as many references to a file as we have IOs left in this submission,
2828 * assuming most submissions are for one file, or at least that each file
2829 * has more than one submission.
2830 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002831static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002832{
2833 if (!state)
2834 return fget(fd);
2835
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002836 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002837 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002838 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002839 return state->file;
2840 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002841 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002842 }
2843 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002844 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002845 return NULL;
2846
2847 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002848 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002849 return state->file;
2850}
2851
Jens Axboe4503b762020-06-01 10:00:27 -06002852static bool io_bdev_nowait(struct block_device *bdev)
2853{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002854 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002855}
2856
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857/*
2858 * If we tracked the file through the SCM inflight mechanism, we could support
2859 * any file. For now, just ensure that anything potentially problematic is done
2860 * inline.
2861 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002862static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002863{
2864 umode_t mode = file_inode(file)->i_mode;
2865
Jens Axboe4503b762020-06-01 10:00:27 -06002866 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002867 if (IS_ENABLED(CONFIG_BLOCK) &&
2868 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002869 return true;
2870 return false;
2871 }
2872 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002873 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002874 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002875 if (IS_ENABLED(CONFIG_BLOCK) &&
2876 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002877 file->f_op != &io_uring_fops)
2878 return true;
2879 return false;
2880 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881
Jens Axboec5b85622020-06-09 19:23:05 -06002882 /* any ->read/write should understand O_NONBLOCK */
2883 if (file->f_flags & O_NONBLOCK)
2884 return true;
2885
Jens Axboeaf197f52020-04-28 13:15:06 -06002886 if (!(file->f_mode & FMODE_NOWAIT))
2887 return false;
2888
2889 if (rw == READ)
2890 return file->f_op->read_iter != NULL;
2891
2892 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002893}
2894
Pavel Begunkova88fc402020-09-30 22:57:53 +03002895static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896{
Jens Axboedef596e2019-01-09 08:59:42 -07002897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002898 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002899 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002900 unsigned ioprio;
2901 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002902
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002903 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002904 req->flags |= REQ_F_ISREG;
2905
Jens Axboe2b188cc2019-01-07 10:46:33 -07002906 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002907 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002908 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002909 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002910 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002911 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002912 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2913 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2914 if (unlikely(ret))
2915 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002917 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2918 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2919 req->flags |= REQ_F_NOWAIT;
2920
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921 ioprio = READ_ONCE(sqe->ioprio);
2922 if (ioprio) {
2923 ret = ioprio_check_cap(ioprio);
2924 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002925 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926
2927 kiocb->ki_ioprio = ioprio;
2928 } else
2929 kiocb->ki_ioprio = get_current_ioprio();
2930
Jens Axboedef596e2019-01-09 08:59:42 -07002931 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002932 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2933 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002934 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935
Jens Axboedef596e2019-01-09 08:59:42 -07002936 kiocb->ki_flags |= IOCB_HIPRI;
2937 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002938 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002939 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002940 if (kiocb->ki_flags & IOCB_HIPRI)
2941 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002942 kiocb->ki_complete = io_complete_rw;
2943 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002944
Jens Axboe3529d8c2019-12-19 18:24:38 -07002945 req->rw.addr = READ_ONCE(sqe->addr);
2946 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002947 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002948 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949}
2950
2951static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2952{
2953 switch (ret) {
2954 case -EIOCBQUEUED:
2955 break;
2956 case -ERESTARTSYS:
2957 case -ERESTARTNOINTR:
2958 case -ERESTARTNOHAND:
2959 case -ERESTART_RESTARTBLOCK:
2960 /*
2961 * We can't just restart the syscall, since previously
2962 * submitted sqes may already be in progress. Just fail this
2963 * IO with EINTR.
2964 */
2965 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002966 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002967 default:
2968 kiocb->ki_complete(kiocb, ret, 0);
2969 }
2970}
2971
Jens Axboea1d7c392020-06-22 11:09:46 -06002972static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2973 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002974{
Jens Axboeba042912019-12-25 16:33:42 -07002975 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002976 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002977
Jens Axboe227c0c92020-08-13 11:51:40 -06002978 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002979 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002980 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002981 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002982 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002983 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002984 }
2985
Jens Axboeba042912019-12-25 16:33:42 -07002986 if (req->flags & REQ_F_CUR_POS)
2987 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002988 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002989 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002990 else
2991 io_rw_done(kiocb, ret);
2992}
2993
Pavel Begunkov847595d2021-02-04 13:52:06 +00002994static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002995{
Jens Axboe9adbd452019-12-20 08:45:55 -07002996 struct io_ring_ctx *ctx = req->ctx;
2997 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002998 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002999 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003000 size_t offset;
3001 u64 buf_addr;
3002
Jens Axboeedafcce2019-01-09 09:16:05 -07003003 if (unlikely(buf_index >= ctx->nr_user_bufs))
3004 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003005 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3006 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003007 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003008
3009 /* overflow */
3010 if (buf_addr + len < buf_addr)
3011 return -EFAULT;
3012 /* not inside the mapped region */
3013 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3014 return -EFAULT;
3015
3016 /*
3017 * May not be a start of buffer, set size appropriately
3018 * and advance us to the beginning.
3019 */
3020 offset = buf_addr - imu->ubuf;
3021 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003022
3023 if (offset) {
3024 /*
3025 * Don't use iov_iter_advance() here, as it's really slow for
3026 * using the latter parts of a big fixed buffer - it iterates
3027 * over each segment manually. We can cheat a bit here, because
3028 * we know that:
3029 *
3030 * 1) it's a BVEC iter, we set it up
3031 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3032 * first and last bvec
3033 *
3034 * So just find our index, and adjust the iterator afterwards.
3035 * If the offset is within the first bvec (or the whole first
3036 * bvec, just use iov_iter_advance(). This makes it easier
3037 * since we can just skip the first segment, which may not
3038 * be PAGE_SIZE aligned.
3039 */
3040 const struct bio_vec *bvec = imu->bvec;
3041
3042 if (offset <= bvec->bv_len) {
3043 iov_iter_advance(iter, offset);
3044 } else {
3045 unsigned long seg_skip;
3046
3047 /* skip first vec */
3048 offset -= bvec->bv_len;
3049 seg_skip = 1 + (offset >> PAGE_SHIFT);
3050
3051 iter->bvec = bvec + seg_skip;
3052 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003053 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003054 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003055 }
3056 }
3057
Pavel Begunkov847595d2021-02-04 13:52:06 +00003058 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003059}
3060
Jens Axboebcda7ba2020-02-23 16:42:51 -07003061static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3062{
3063 if (needs_lock)
3064 mutex_unlock(&ctx->uring_lock);
3065}
3066
3067static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3068{
3069 /*
3070 * "Normal" inline submissions always hold the uring_lock, since we
3071 * grab it from the system call. Same is true for the SQPOLL offload.
3072 * The only exception is when we've detached the request and issue it
3073 * from an async worker thread, grab the lock for that case.
3074 */
3075 if (needs_lock)
3076 mutex_lock(&ctx->uring_lock);
3077}
3078
3079static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3080 int bgid, struct io_buffer *kbuf,
3081 bool needs_lock)
3082{
3083 struct io_buffer *head;
3084
3085 if (req->flags & REQ_F_BUFFER_SELECTED)
3086 return kbuf;
3087
3088 io_ring_submit_lock(req->ctx, needs_lock);
3089
3090 lockdep_assert_held(&req->ctx->uring_lock);
3091
3092 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3093 if (head) {
3094 if (!list_empty(&head->list)) {
3095 kbuf = list_last_entry(&head->list, struct io_buffer,
3096 list);
3097 list_del(&kbuf->list);
3098 } else {
3099 kbuf = head;
3100 idr_remove(&req->ctx->io_buffer_idr, bgid);
3101 }
3102 if (*len > kbuf->len)
3103 *len = kbuf->len;
3104 } else {
3105 kbuf = ERR_PTR(-ENOBUFS);
3106 }
3107
3108 io_ring_submit_unlock(req->ctx, needs_lock);
3109
3110 return kbuf;
3111}
3112
Jens Axboe4d954c22020-02-27 07:31:19 -07003113static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3114 bool needs_lock)
3115{
3116 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003117 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003118
3119 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003120 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003121 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3122 if (IS_ERR(kbuf))
3123 return kbuf;
3124 req->rw.addr = (u64) (unsigned long) kbuf;
3125 req->flags |= REQ_F_BUFFER_SELECTED;
3126 return u64_to_user_ptr(kbuf->addr);
3127}
3128
3129#ifdef CONFIG_COMPAT
3130static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3131 bool needs_lock)
3132{
3133 struct compat_iovec __user *uiov;
3134 compat_ssize_t clen;
3135 void __user *buf;
3136 ssize_t len;
3137
3138 uiov = u64_to_user_ptr(req->rw.addr);
3139 if (!access_ok(uiov, sizeof(*uiov)))
3140 return -EFAULT;
3141 if (__get_user(clen, &uiov->iov_len))
3142 return -EFAULT;
3143 if (clen < 0)
3144 return -EINVAL;
3145
3146 len = clen;
3147 buf = io_rw_buffer_select(req, &len, needs_lock);
3148 if (IS_ERR(buf))
3149 return PTR_ERR(buf);
3150 iov[0].iov_base = buf;
3151 iov[0].iov_len = (compat_size_t) len;
3152 return 0;
3153}
3154#endif
3155
3156static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3157 bool needs_lock)
3158{
3159 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3160 void __user *buf;
3161 ssize_t len;
3162
3163 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3164 return -EFAULT;
3165
3166 len = iov[0].iov_len;
3167 if (len < 0)
3168 return -EINVAL;
3169 buf = io_rw_buffer_select(req, &len, needs_lock);
3170 if (IS_ERR(buf))
3171 return PTR_ERR(buf);
3172 iov[0].iov_base = buf;
3173 iov[0].iov_len = len;
3174 return 0;
3175}
3176
3177static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3178 bool needs_lock)
3179{
Jens Axboedddb3e22020-06-04 11:27:01 -06003180 if (req->flags & REQ_F_BUFFER_SELECTED) {
3181 struct io_buffer *kbuf;
3182
3183 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3184 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3185 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003186 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003187 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003188 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003189 return -EINVAL;
3190
3191#ifdef CONFIG_COMPAT
3192 if (req->ctx->compat)
3193 return io_compat_import(req, iov, needs_lock);
3194#endif
3195
3196 return __io_iov_buffer_select(req, iov, needs_lock);
3197}
3198
Pavel Begunkov847595d2021-02-04 13:52:06 +00003199static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3200 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003201{
Jens Axboe9adbd452019-12-20 08:45:55 -07003202 void __user *buf = u64_to_user_ptr(req->rw.addr);
3203 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003204 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003205 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003206
Pavel Begunkov7d009162019-11-25 23:14:40 +03003207 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003208 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003209 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003210 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003211
Jens Axboebcda7ba2020-02-23 16:42:51 -07003212 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003213 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003214 return -EINVAL;
3215
Jens Axboe3a6820f2019-12-22 15:19:35 -07003216 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003217 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003218 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003219 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003220 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003221 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003222 }
3223
Jens Axboe3a6820f2019-12-22 15:19:35 -07003224 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3225 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003226 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003227 }
3228
Jens Axboe4d954c22020-02-27 07:31:19 -07003229 if (req->flags & REQ_F_BUFFER_SELECT) {
3230 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003231 if (!ret)
3232 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003233 *iovec = NULL;
3234 return ret;
3235 }
3236
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003237 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3238 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003239}
3240
Jens Axboe0fef9482020-08-26 10:36:20 -06003241static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3242{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003243 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003244}
3245
Jens Axboe32960612019-09-23 11:05:34 -06003246/*
3247 * For files that don't have ->read_iter() and ->write_iter(), handle them
3248 * by looping over ->read() or ->write() manually.
3249 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003250static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003251{
Jens Axboe4017eb92020-10-22 14:14:12 -06003252 struct kiocb *kiocb = &req->rw.kiocb;
3253 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003254 ssize_t ret = 0;
3255
3256 /*
3257 * Don't support polled IO through this interface, and we can't
3258 * support non-blocking either. For the latter, this just causes
3259 * the kiocb to be handled from an async context.
3260 */
3261 if (kiocb->ki_flags & IOCB_HIPRI)
3262 return -EOPNOTSUPP;
3263 if (kiocb->ki_flags & IOCB_NOWAIT)
3264 return -EAGAIN;
3265
3266 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003267 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003268 ssize_t nr;
3269
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003270 if (!iov_iter_is_bvec(iter)) {
3271 iovec = iov_iter_iovec(iter);
3272 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003273 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3274 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003275 }
3276
Jens Axboe32960612019-09-23 11:05:34 -06003277 if (rw == READ) {
3278 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003279 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003280 } else {
3281 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003282 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003283 }
3284
3285 if (nr < 0) {
3286 if (!ret)
3287 ret = nr;
3288 break;
3289 }
3290 ret += nr;
3291 if (nr != iovec.iov_len)
3292 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003293 req->rw.len -= nr;
3294 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003295 iov_iter_advance(iter, nr);
3296 }
3297
3298 return ret;
3299}
3300
Jens Axboeff6165b2020-08-13 09:47:43 -06003301static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3302 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003303{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003304 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003305
Jens Axboeff6165b2020-08-13 09:47:43 -06003306 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003307 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003308 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003309 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003310 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003311 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003312 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003313 unsigned iov_off = 0;
3314
3315 rw->iter.iov = rw->fast_iov;
3316 if (iter->iov != fast_iov) {
3317 iov_off = iter->iov - fast_iov;
3318 rw->iter.iov += iov_off;
3319 }
3320 if (rw->fast_iov != fast_iov)
3321 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003322 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003323 } else {
3324 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003325 }
3326}
3327
Jens Axboee8c2bc12020-08-15 18:44:09 -07003328static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003329{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003330 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3331 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3332 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003333}
3334
Jens Axboee8c2bc12020-08-15 18:44:09 -07003335static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003336{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003337 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003338 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003339
Jens Axboee8c2bc12020-08-15 18:44:09 -07003340 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003341}
3342
Jens Axboeff6165b2020-08-13 09:47:43 -06003343static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3344 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003345 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003346{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003347 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003348 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003349 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003350 if (__io_alloc_async_data(req)) {
3351 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003352 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003353 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003354
Jens Axboeff6165b2020-08-13 09:47:43 -06003355 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003356 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003357 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003358}
3359
Pavel Begunkov73debe62020-09-30 22:57:54 +03003360static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003361{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003362 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003363 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003364 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003365
Pavel Begunkov2846c482020-11-07 13:16:27 +00003366 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003367 if (unlikely(ret < 0))
3368 return ret;
3369
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003370 iorw->bytes_done = 0;
3371 iorw->free_iovec = iov;
3372 if (iov)
3373 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003374 return 0;
3375}
3376
Pavel Begunkov73debe62020-09-30 22:57:54 +03003377static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003378{
3379 ssize_t ret;
3380
Pavel Begunkova88fc402020-09-30 22:57:53 +03003381 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003382 if (ret)
3383 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003384
Jens Axboe3529d8c2019-12-19 18:24:38 -07003385 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3386 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003387
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003388 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003389 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003390 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003391 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003392}
3393
Jens Axboec1dd91d2020-08-03 16:43:59 -06003394/*
3395 * This is our waitqueue callback handler, registered through lock_page_async()
3396 * when we initially tried to do the IO with the iocb armed our waitqueue.
3397 * This gets called when the page is unlocked, and we generally expect that to
3398 * happen when the page IO is completed and the page is now uptodate. This will
3399 * queue a task_work based retry of the operation, attempting to copy the data
3400 * again. If the latter fails because the page was NOT uptodate, then we will
3401 * do a thread based blocking retry of the operation. That's the unexpected
3402 * slow path.
3403 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003404static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3405 int sync, void *arg)
3406{
3407 struct wait_page_queue *wpq;
3408 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003409 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003410 int ret;
3411
3412 wpq = container_of(wait, struct wait_page_queue, wait);
3413
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003414 if (!wake_page_match(wpq, key))
3415 return 0;
3416
Hao Xuc8d317a2020-09-29 20:00:45 +08003417 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003418 list_del_init(&wait->entry);
3419
Pavel Begunkove7375122020-07-12 20:42:04 +03003420 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003421 percpu_ref_get(&req->ctx->refs);
3422
Jens Axboebcf5a062020-05-22 09:24:42 -06003423 /* submit ref gets dropped, acquire a new one */
3424 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003425 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003426 if (unlikely(ret))
3427 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003428 return 1;
3429}
3430
Jens Axboec1dd91d2020-08-03 16:43:59 -06003431/*
3432 * This controls whether a given IO request should be armed for async page
3433 * based retry. If we return false here, the request is handed to the async
3434 * worker threads for retry. If we're doing buffered reads on a regular file,
3435 * we prepare a private wait_page_queue entry and retry the operation. This
3436 * will either succeed because the page is now uptodate and unlocked, or it
3437 * will register a callback when the page is unlocked at IO completion. Through
3438 * that callback, io_uring uses task_work to setup a retry of the operation.
3439 * That retry will attempt the buffered read again. The retry will generally
3440 * succeed, or in rare cases where it fails, we then fall back to using the
3441 * async worker threads for a blocking retry.
3442 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003443static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003444{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003445 struct io_async_rw *rw = req->async_data;
3446 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003447 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003448
3449 /* never retry for NOWAIT, we just complete with -EAGAIN */
3450 if (req->flags & REQ_F_NOWAIT)
3451 return false;
3452
Jens Axboe227c0c92020-08-13 11:51:40 -06003453 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003454 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003455 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003456
Jens Axboebcf5a062020-05-22 09:24:42 -06003457 /*
3458 * just use poll if we can, and don't attempt if the fs doesn't
3459 * support callback based unlocks
3460 */
3461 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3462 return false;
3463
Jens Axboe3b2a4432020-08-16 10:58:43 -07003464 wait->wait.func = io_async_buf_func;
3465 wait->wait.private = req;
3466 wait->wait.flags = 0;
3467 INIT_LIST_HEAD(&wait->wait.entry);
3468 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003469 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003470 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003471 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003472}
3473
3474static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3475{
3476 if (req->file->f_op->read_iter)
3477 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003478 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003479 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003480 else
3481 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003482}
3483
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003484static int io_read(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboea1d7c392020-06-22 11:09:46 -06003485 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486{
3487 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003488 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003489 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003490 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003491 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003492 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493
Pavel Begunkov2846c482020-11-07 13:16:27 +00003494 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003495 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003496 iovec = NULL;
3497 } else {
3498 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3499 if (ret < 0)
3500 return ret;
3501 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003502 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003503 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003504
Jens Axboefd6c2e42019-12-18 12:19:41 -07003505 /* Ensure we clear previously set non-block flag */
3506 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003507 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003508 else
3509 kiocb->ki_flags |= IOCB_NOWAIT;
3510
Pavel Begunkov24c74672020-06-21 13:09:51 +03003511 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003512 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3513 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003514 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003515 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003516
Pavel Begunkov632546c2020-11-07 13:16:26 +00003517 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003518 if (unlikely(ret)) {
3519 kfree(iovec);
3520 return ret;
3521 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522
Jens Axboe227c0c92020-08-13 11:51:40 -06003523 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003524
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003525 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003526 /* it's faster to check here then delegate to kfree */
3527 if (iovec)
3528 kfree(iovec);
3529 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003530 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003531 /* IOPOLL retry should happen for io-wq threads */
3532 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003533 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003534 /* no retry on NONBLOCK nor RWF_NOWAIT */
3535 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003536 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003537 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003538 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003539 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003540 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003541 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003542 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003543 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003544 }
3545
Jens Axboe227c0c92020-08-13 11:51:40 -06003546 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003547 if (ret2)
3548 return ret2;
3549
Jens Axboee8c2bc12020-08-15 18:44:09 -07003550 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003551 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003552 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003553
Pavel Begunkovb23df912021-02-04 13:52:04 +00003554 do {
3555 io_size -= ret;
3556 rw->bytes_done += ret;
3557 /* if we can retry, do so with the callbacks armed */
3558 if (!io_rw_should_retry(req)) {
3559 kiocb->ki_flags &= ~IOCB_WAITQ;
3560 return -EAGAIN;
3561 }
3562
3563 /*
3564 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3565 * we get -EIOCBQUEUED, then we'll get a notification when the
3566 * desired page gets unlocked. We can also get a partial read
3567 * here, and if we do, then just retry at the new offset.
3568 */
3569 ret = io_iter_do_read(req, iter);
3570 if (ret == -EIOCBQUEUED)
3571 return 0;
3572 /* we got some bytes, but not all. retry. */
3573 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003574done:
3575 kiocb_done(kiocb, ret, cs);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003576 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003577}
3578
Pavel Begunkov73debe62020-09-30 22:57:54 +03003579static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003580{
3581 ssize_t ret;
3582
Pavel Begunkova88fc402020-09-30 22:57:53 +03003583 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003584 if (ret)
3585 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003586
Jens Axboe3529d8c2019-12-19 18:24:38 -07003587 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3588 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003589
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003590 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003591 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003592 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003593 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003594}
3595
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003596static int io_write(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboea1d7c392020-06-22 11:09:46 -06003597 struct io_comp_state *cs)
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 Begunkovfa15baf2020-08-01 13:50:02 +03003671 kiocb_done(kiocb, ret2, cs);
3672 } 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 Begunkov61e98202021-02-10 00:03:08 +00003920static int io_nop(struct io_kiocb *req, unsigned int issue_flags,
3921 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003922{
3923 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003924
Jens Axboedef596e2019-01-09 08:59:42 -07003925 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3926 return -EINVAL;
3927
Jens Axboe229a7b62020-06-22 10:13:11 -06003928 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003929 return 0;
3930}
3931
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003933{
Jens Axboe6b063142019-01-10 22:13:58 -07003934 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003935
Jens Axboe09bb8392019-03-13 12:39:28 -06003936 if (!req->file)
3937 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003938
Jens Axboe6b063142019-01-10 22:13:58 -07003939 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003940 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003941 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003942 return -EINVAL;
3943
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003944 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3945 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3946 return -EINVAL;
3947
3948 req->sync.off = READ_ONCE(sqe->off);
3949 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003950 return 0;
3951}
3952
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003953static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003954{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003955 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003956 int ret;
3957
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003958 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003959 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003960 return -EAGAIN;
3961
Jens Axboe9adbd452019-12-20 08:45:55 -07003962 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003963 end > 0 ? end : LLONG_MAX,
3964 req->sync.flags & IORING_FSYNC_DATASYNC);
3965 if (ret < 0)
3966 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003967 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003968 return 0;
3969}
3970
Jens Axboed63d1b52019-12-10 10:38:56 -07003971static int io_fallocate_prep(struct io_kiocb *req,
3972 const struct io_uring_sqe *sqe)
3973{
3974 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3975 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003976 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3977 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003978
3979 req->sync.off = READ_ONCE(sqe->off);
3980 req->sync.len = READ_ONCE(sqe->addr);
3981 req->sync.mode = READ_ONCE(sqe->len);
3982 return 0;
3983}
3984
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003985static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003986{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003987 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003988
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003989 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003990 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003991 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003992 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3993 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003994 if (ret < 0)
3995 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003996 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003997 return 0;
3998}
3999
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004000static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004001{
Jens Axboef8748882020-01-08 17:47:02 -07004002 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004003 int ret;
4004
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004005 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004006 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004007 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004008 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004009
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004010 /* open.how should be already initialised */
4011 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004012 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004013
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004014 req->open.dfd = READ_ONCE(sqe->fd);
4015 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004016 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004017 if (IS_ERR(req->open.filename)) {
4018 ret = PTR_ERR(req->open.filename);
4019 req->open.filename = NULL;
4020 return ret;
4021 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004022 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004023 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004024 return 0;
4025}
4026
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004027static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4028{
4029 u64 flags, mode;
4030
Jens Axboe14587a462020-09-05 11:36:08 -06004031 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004032 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004033 mode = READ_ONCE(sqe->len);
4034 flags = READ_ONCE(sqe->open_flags);
4035 req->open.how = build_open_how(flags, mode);
4036 return __io_openat_prep(req, sqe);
4037}
4038
Jens Axboecebdb982020-01-08 17:59:24 -07004039static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4040{
4041 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004042 size_t len;
4043 int ret;
4044
Jens Axboe14587a462020-09-05 11:36:08 -06004045 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004046 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004047 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4048 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004049 if (len < OPEN_HOW_SIZE_VER0)
4050 return -EINVAL;
4051
4052 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4053 len);
4054 if (ret)
4055 return ret;
4056
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004057 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004058}
4059
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004060static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004061{
4062 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004063 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004064 bool nonblock_set;
4065 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004066 int ret;
4067
Jens Axboecebdb982020-01-08 17:59:24 -07004068 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004069 if (ret)
4070 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004071 nonblock_set = op.open_flag & O_NONBLOCK;
4072 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004073 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004074 /*
4075 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4076 * it'll always -EAGAIN
4077 */
4078 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4079 return -EAGAIN;
4080 op.lookup_flags |= LOOKUP_CACHED;
4081 op.open_flag |= O_NONBLOCK;
4082 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004083
Jens Axboe4022e7a2020-03-19 19:23:18 -06004084 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004085 if (ret < 0)
4086 goto err;
4087
4088 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004089 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004090 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4091 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004092 /*
4093 * We could hang on to this 'fd', but seems like marginal
4094 * gain for something that is now known to be a slower path.
4095 * So just put it, and we'll get a new one when we retry.
4096 */
4097 put_unused_fd(ret);
4098 return -EAGAIN;
4099 }
4100
Jens Axboe15b71ab2019-12-11 11:20:36 -07004101 if (IS_ERR(file)) {
4102 put_unused_fd(ret);
4103 ret = PTR_ERR(file);
4104 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004105 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004106 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004107 fsnotify_open(file);
4108 fd_install(ret, file);
4109 }
4110err:
4111 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004112 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004113 if (ret < 0)
4114 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004115 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004116 return 0;
4117}
4118
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004119static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004120{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004121 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004122}
4123
Jens Axboe067524e2020-03-02 16:32:28 -07004124static int io_remove_buffers_prep(struct io_kiocb *req,
4125 const struct io_uring_sqe *sqe)
4126{
4127 struct io_provide_buf *p = &req->pbuf;
4128 u64 tmp;
4129
4130 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4131 return -EINVAL;
4132
4133 tmp = READ_ONCE(sqe->fd);
4134 if (!tmp || tmp > USHRT_MAX)
4135 return -EINVAL;
4136
4137 memset(p, 0, sizeof(*p));
4138 p->nbufs = tmp;
4139 p->bgid = READ_ONCE(sqe->buf_group);
4140 return 0;
4141}
4142
4143static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4144 int bgid, unsigned nbufs)
4145{
4146 unsigned i = 0;
4147
4148 /* shouldn't happen */
4149 if (!nbufs)
4150 return 0;
4151
4152 /* the head kbuf is the list itself */
4153 while (!list_empty(&buf->list)) {
4154 struct io_buffer *nxt;
4155
4156 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4157 list_del(&nxt->list);
4158 kfree(nxt);
4159 if (++i == nbufs)
4160 return i;
4161 }
4162 i++;
4163 kfree(buf);
4164 idr_remove(&ctx->io_buffer_idr, bgid);
4165
4166 return i;
4167}
4168
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004169static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004170 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004171{
4172 struct io_provide_buf *p = &req->pbuf;
4173 struct io_ring_ctx *ctx = req->ctx;
4174 struct io_buffer *head;
4175 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004176 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004177
4178 io_ring_submit_lock(ctx, !force_nonblock);
4179
4180 lockdep_assert_held(&ctx->uring_lock);
4181
4182 ret = -ENOENT;
4183 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4184 if (head)
4185 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004186 if (ret < 0)
4187 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004188
4189 /* need to hold the lock to complete IOPOLL requests */
4190 if (ctx->flags & IORING_SETUP_IOPOLL) {
4191 __io_req_complete(req, ret, 0, cs);
4192 io_ring_submit_unlock(ctx, !force_nonblock);
4193 } else {
4194 io_ring_submit_unlock(ctx, !force_nonblock);
4195 __io_req_complete(req, ret, 0, cs);
4196 }
Jens Axboe067524e2020-03-02 16:32:28 -07004197 return 0;
4198}
4199
Jens Axboeddf0322d2020-02-23 16:41:33 -07004200static int io_provide_buffers_prep(struct io_kiocb *req,
4201 const struct io_uring_sqe *sqe)
4202{
4203 struct io_provide_buf *p = &req->pbuf;
4204 u64 tmp;
4205
4206 if (sqe->ioprio || sqe->rw_flags)
4207 return -EINVAL;
4208
4209 tmp = READ_ONCE(sqe->fd);
4210 if (!tmp || tmp > USHRT_MAX)
4211 return -E2BIG;
4212 p->nbufs = tmp;
4213 p->addr = READ_ONCE(sqe->addr);
4214 p->len = READ_ONCE(sqe->len);
4215
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004216 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004217 return -EFAULT;
4218
4219 p->bgid = READ_ONCE(sqe->buf_group);
4220 tmp = READ_ONCE(sqe->off);
4221 if (tmp > USHRT_MAX)
4222 return -E2BIG;
4223 p->bid = tmp;
4224 return 0;
4225}
4226
4227static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4228{
4229 struct io_buffer *buf;
4230 u64 addr = pbuf->addr;
4231 int i, bid = pbuf->bid;
4232
4233 for (i = 0; i < pbuf->nbufs; i++) {
4234 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4235 if (!buf)
4236 break;
4237
4238 buf->addr = addr;
4239 buf->len = pbuf->len;
4240 buf->bid = bid;
4241 addr += pbuf->len;
4242 bid++;
4243 if (!*head) {
4244 INIT_LIST_HEAD(&buf->list);
4245 *head = buf;
4246 } else {
4247 list_add_tail(&buf->list, &(*head)->list);
4248 }
4249 }
4250
4251 return i ? i : -ENOMEM;
4252}
4253
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004254static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004255 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004256{
4257 struct io_provide_buf *p = &req->pbuf;
4258 struct io_ring_ctx *ctx = req->ctx;
4259 struct io_buffer *head, *list;
4260 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004261 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004262
4263 io_ring_submit_lock(ctx, !force_nonblock);
4264
4265 lockdep_assert_held(&ctx->uring_lock);
4266
4267 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4268
4269 ret = io_add_buffers(p, &head);
4270 if (ret < 0)
4271 goto out;
4272
4273 if (!list) {
4274 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4275 GFP_KERNEL);
4276 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004277 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004278 goto out;
4279 }
4280 }
4281out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004282 if (ret < 0)
4283 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004284
4285 /* need to hold the lock to complete IOPOLL requests */
4286 if (ctx->flags & IORING_SETUP_IOPOLL) {
4287 __io_req_complete(req, ret, 0, cs);
4288 io_ring_submit_unlock(ctx, !force_nonblock);
4289 } else {
4290 io_ring_submit_unlock(ctx, !force_nonblock);
4291 __io_req_complete(req, ret, 0, cs);
4292 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004293 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004294}
4295
Jens Axboe3e4827b2020-01-08 15:18:09 -07004296static int io_epoll_ctl_prep(struct io_kiocb *req,
4297 const struct io_uring_sqe *sqe)
4298{
4299#if defined(CONFIG_EPOLL)
4300 if (sqe->ioprio || sqe->buf_index)
4301 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004302 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004303 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004304
4305 req->epoll.epfd = READ_ONCE(sqe->fd);
4306 req->epoll.op = READ_ONCE(sqe->len);
4307 req->epoll.fd = READ_ONCE(sqe->off);
4308
4309 if (ep_op_has_event(req->epoll.op)) {
4310 struct epoll_event __user *ev;
4311
4312 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4313 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4314 return -EFAULT;
4315 }
4316
4317 return 0;
4318#else
4319 return -EOPNOTSUPP;
4320#endif
4321}
4322
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004323static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004324 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004325{
4326#if defined(CONFIG_EPOLL)
4327 struct io_epoll *ie = &req->epoll;
4328 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004329 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004330
4331 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4332 if (force_nonblock && ret == -EAGAIN)
4333 return -EAGAIN;
4334
4335 if (ret < 0)
4336 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004337 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004338 return 0;
4339#else
4340 return -EOPNOTSUPP;
4341#endif
4342}
4343
Jens Axboec1ca7572019-12-25 22:18:28 -07004344static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4345{
4346#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4347 if (sqe->ioprio || sqe->buf_index || sqe->off)
4348 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004349 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4350 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004351
4352 req->madvise.addr = READ_ONCE(sqe->addr);
4353 req->madvise.len = READ_ONCE(sqe->len);
4354 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4355 return 0;
4356#else
4357 return -EOPNOTSUPP;
4358#endif
4359}
4360
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004361static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004362{
4363#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4364 struct io_madvise *ma = &req->madvise;
4365 int ret;
4366
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004367 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004368 return -EAGAIN;
4369
Minchan Kim0726b012020-10-17 16:14:50 -07004370 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004371 if (ret < 0)
4372 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004373 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004374 return 0;
4375#else
4376 return -EOPNOTSUPP;
4377#endif
4378}
4379
Jens Axboe4840e412019-12-25 22:03:45 -07004380static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4381{
4382 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4383 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004384 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4385 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004386
4387 req->fadvise.offset = READ_ONCE(sqe->off);
4388 req->fadvise.len = READ_ONCE(sqe->len);
4389 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4390 return 0;
4391}
4392
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004393static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004394{
4395 struct io_fadvise *fa = &req->fadvise;
4396 int ret;
4397
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004398 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004399 switch (fa->advice) {
4400 case POSIX_FADV_NORMAL:
4401 case POSIX_FADV_RANDOM:
4402 case POSIX_FADV_SEQUENTIAL:
4403 break;
4404 default:
4405 return -EAGAIN;
4406 }
4407 }
Jens Axboe4840e412019-12-25 22:03:45 -07004408
4409 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4410 if (ret < 0)
4411 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004412 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004413 return 0;
4414}
4415
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004416static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4417{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004418 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004419 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004420 if (sqe->ioprio || sqe->buf_index)
4421 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004422 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004423 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004424
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004425 req->statx.dfd = READ_ONCE(sqe->fd);
4426 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004427 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004428 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4429 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004430
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004431 return 0;
4432}
4433
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004434static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004435{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004436 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004437 int ret;
4438
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004439 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004440 /* only need file table for an actual valid fd */
4441 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4442 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004443 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004444 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004445
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004446 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4447 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004448
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004449 if (ret < 0)
4450 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004451 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004452 return 0;
4453}
4454
Jens Axboeb5dba592019-12-11 14:02:38 -07004455static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4456{
Jens Axboe14587a462020-09-05 11:36:08 -06004457 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004458 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004459 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4460 sqe->rw_flags || sqe->buf_index)
4461 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004462 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004463 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004464
4465 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004466 return 0;
4467}
4468
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004469static int io_close(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004470 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004471{
Jens Axboe9eac1902021-01-19 15:50:37 -07004472 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004473 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004474 struct fdtable *fdt;
4475 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004476 int ret;
4477
Jens Axboe9eac1902021-01-19 15:50:37 -07004478 file = NULL;
4479 ret = -EBADF;
4480 spin_lock(&files->file_lock);
4481 fdt = files_fdtable(files);
4482 if (close->fd >= fdt->max_fds) {
4483 spin_unlock(&files->file_lock);
4484 goto err;
4485 }
4486 file = fdt->fd[close->fd];
4487 if (!file) {
4488 spin_unlock(&files->file_lock);
4489 goto err;
4490 }
4491
4492 if (file->f_op == &io_uring_fops) {
4493 spin_unlock(&files->file_lock);
4494 file = NULL;
4495 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004496 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004497
4498 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004499 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004500 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004501 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004502 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004503
Jens Axboe9eac1902021-01-19 15:50:37 -07004504 ret = __close_fd_get_file(close->fd, &file);
4505 spin_unlock(&files->file_lock);
4506 if (ret < 0) {
4507 if (ret == -ENOENT)
4508 ret = -EBADF;
4509 goto err;
4510 }
4511
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004512 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004513 ret = filp_close(file, current->files);
4514err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004515 if (ret < 0)
4516 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004517 if (file)
4518 fput(file);
Jens Axboe229a7b62020-06-22 10:13:11 -06004519 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004520 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004521}
4522
Jens Axboe3529d8c2019-12-19 18:24:38 -07004523static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004524{
4525 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004526
4527 if (!req->file)
4528 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004529
4530 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4531 return -EINVAL;
4532 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4533 return -EINVAL;
4534
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004535 req->sync.off = READ_ONCE(sqe->off);
4536 req->sync.len = READ_ONCE(sqe->len);
4537 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004538 return 0;
4539}
4540
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004541static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004542{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004543 int ret;
4544
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004545 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004546 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004547 return -EAGAIN;
4548
Jens Axboe9adbd452019-12-20 08:45:55 -07004549 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004550 req->sync.flags);
4551 if (ret < 0)
4552 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004553 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004554 return 0;
4555}
4556
YueHaibing469956e2020-03-04 15:53:52 +08004557#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004558static int io_setup_async_msg(struct io_kiocb *req,
4559 struct io_async_msghdr *kmsg)
4560{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004561 struct io_async_msghdr *async_msg = req->async_data;
4562
4563 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004564 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004565 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004566 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004567 return -ENOMEM;
4568 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004569 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004570 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004571 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004572 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004573 /* if were using fast_iov, set it to the new one */
4574 if (!async_msg->free_iov)
4575 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4576
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004577 return -EAGAIN;
4578}
4579
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004580static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4581 struct io_async_msghdr *iomsg)
4582{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004583 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004584 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004585 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004586 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004587}
4588
Jens Axboe3529d8c2019-12-19 18:24:38 -07004589static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004590{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004591 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004592 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004593 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004594
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004595 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4596 return -EINVAL;
4597
Jens Axboee47293f2019-12-20 08:58:21 -07004598 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004599 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004600 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004601
Jens Axboed8768362020-02-27 14:17:49 -07004602#ifdef CONFIG_COMPAT
4603 if (req->ctx->compat)
4604 sr->msg_flags |= MSG_CMSG_COMPAT;
4605#endif
4606
Jens Axboee8c2bc12020-08-15 18:44:09 -07004607 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004608 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004609 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004610 if (!ret)
4611 req->flags |= REQ_F_NEED_CLEANUP;
4612 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004613}
4614
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004615static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004616 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004617{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004618 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004619 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004620 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004621 int ret;
4622
Florent Revestdba4a922020-12-04 12:36:04 +01004623 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004624 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004625 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004626
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004627 kmsg = req->async_data;
4628 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004629 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004630 if (ret)
4631 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004632 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004633 }
4634
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004635 flags = req->sr_msg.msg_flags;
4636 if (flags & MSG_DONTWAIT)
4637 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004638 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004639 flags |= MSG_DONTWAIT;
4640
4641 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004642 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004643 return io_setup_async_msg(req, kmsg);
4644 if (ret == -ERESTARTSYS)
4645 ret = -EINTR;
4646
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004647 /* fast path, check for non-NULL to avoid function call */
4648 if (kmsg->free_iov)
4649 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004650 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004651 if (ret < 0)
4652 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004653 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004654 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004655}
4656
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004657static int io_send(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004658 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004659{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004660 struct io_sr_msg *sr = &req->sr_msg;
4661 struct msghdr msg;
4662 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004663 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004664 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004665 int ret;
4666
Florent Revestdba4a922020-12-04 12:36:04 +01004667 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004668 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004669 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004670
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004671 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4672 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004673 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004674
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004675 msg.msg_name = NULL;
4676 msg.msg_control = NULL;
4677 msg.msg_controllen = 0;
4678 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004679
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004680 flags = req->sr_msg.msg_flags;
4681 if (flags & MSG_DONTWAIT)
4682 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004683 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004684 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004685
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004686 msg.msg_flags = flags;
4687 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004688 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004689 return -EAGAIN;
4690 if (ret == -ERESTARTSYS)
4691 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004692
Jens Axboe03b12302019-12-02 18:50:25 -07004693 if (ret < 0)
4694 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004695 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004696 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004697}
4698
Pavel Begunkov1400e692020-07-12 20:41:05 +03004699static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4700 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004701{
4702 struct io_sr_msg *sr = &req->sr_msg;
4703 struct iovec __user *uiov;
4704 size_t iov_len;
4705 int ret;
4706
Pavel Begunkov1400e692020-07-12 20:41:05 +03004707 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4708 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004709 if (ret)
4710 return ret;
4711
4712 if (req->flags & REQ_F_BUFFER_SELECT) {
4713 if (iov_len > 1)
4714 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004715 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004716 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004717 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004718 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004719 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004720 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004721 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004722 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004723 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004724 if (ret > 0)
4725 ret = 0;
4726 }
4727
4728 return ret;
4729}
4730
4731#ifdef CONFIG_COMPAT
4732static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004733 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004734{
4735 struct compat_msghdr __user *msg_compat;
4736 struct io_sr_msg *sr = &req->sr_msg;
4737 struct compat_iovec __user *uiov;
4738 compat_uptr_t ptr;
4739 compat_size_t len;
4740 int ret;
4741
Pavel Begunkov270a5942020-07-12 20:41:04 +03004742 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004743 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004744 &ptr, &len);
4745 if (ret)
4746 return ret;
4747
4748 uiov = compat_ptr(ptr);
4749 if (req->flags & REQ_F_BUFFER_SELECT) {
4750 compat_ssize_t clen;
4751
4752 if (len > 1)
4753 return -EINVAL;
4754 if (!access_ok(uiov, sizeof(*uiov)))
4755 return -EFAULT;
4756 if (__get_user(clen, &uiov->iov_len))
4757 return -EFAULT;
4758 if (clen < 0)
4759 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004760 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004761 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004762 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004763 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004764 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004765 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004766 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004767 if (ret < 0)
4768 return ret;
4769 }
4770
4771 return 0;
4772}
Jens Axboe03b12302019-12-02 18:50:25 -07004773#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004774
Pavel Begunkov1400e692020-07-12 20:41:05 +03004775static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4776 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004777{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004778 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004779
4780#ifdef CONFIG_COMPAT
4781 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004782 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004783#endif
4784
Pavel Begunkov1400e692020-07-12 20:41:05 +03004785 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004786}
4787
Jens Axboebcda7ba2020-02-23 16:42:51 -07004788static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004789 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004790{
4791 struct io_sr_msg *sr = &req->sr_msg;
4792 struct io_buffer *kbuf;
4793
Jens Axboebcda7ba2020-02-23 16:42:51 -07004794 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4795 if (IS_ERR(kbuf))
4796 return kbuf;
4797
4798 sr->kbuf = kbuf;
4799 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004800 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004801}
4802
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004803static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4804{
4805 return io_put_kbuf(req, req->sr_msg.kbuf);
4806}
4807
Jens Axboe3529d8c2019-12-19 18:24:38 -07004808static int io_recvmsg_prep(struct io_kiocb *req,
4809 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004810{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004811 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004812 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004813 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004814
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004815 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4816 return -EINVAL;
4817
Jens Axboe3529d8c2019-12-19 18:24:38 -07004818 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004819 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004820 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004821 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004822
Jens Axboed8768362020-02-27 14:17:49 -07004823#ifdef CONFIG_COMPAT
4824 if (req->ctx->compat)
4825 sr->msg_flags |= MSG_CMSG_COMPAT;
4826#endif
4827
Jens Axboee8c2bc12020-08-15 18:44:09 -07004828 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004829 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004830 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004831 if (!ret)
4832 req->flags |= REQ_F_NEED_CLEANUP;
4833 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004834}
4835
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004836static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004837 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004838{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004839 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004840 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004841 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004842 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004843 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004844 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004845
Florent Revestdba4a922020-12-04 12:36:04 +01004846 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004847 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004848 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004849
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004850 kmsg = req->async_data;
4851 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004852 ret = io_recvmsg_copy_hdr(req, &iomsg);
4853 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004854 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004855 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004856 }
4857
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004858 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004859 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004860 if (IS_ERR(kbuf))
4861 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004862 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004863 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4864 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004865 1, req->sr_msg.len);
4866 }
4867
4868 flags = req->sr_msg.msg_flags;
4869 if (flags & MSG_DONTWAIT)
4870 req->flags |= REQ_F_NOWAIT;
4871 else if (force_nonblock)
4872 flags |= MSG_DONTWAIT;
4873
4874 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4875 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004876 if (force_nonblock && ret == -EAGAIN)
4877 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004878 if (ret == -ERESTARTSYS)
4879 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004880
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004881 if (req->flags & REQ_F_BUFFER_SELECTED)
4882 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004883 /* fast path, check for non-NULL to avoid function call */
4884 if (kmsg->free_iov)
4885 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004886 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004887 if (ret < 0)
4888 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004889 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004890 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004891}
4892
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004893static int io_recv(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004894 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004895{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004896 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004897 struct io_sr_msg *sr = &req->sr_msg;
4898 struct msghdr msg;
4899 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004900 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004901 struct iovec iov;
4902 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004903 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004904 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004905
Florent Revestdba4a922020-12-04 12:36:04 +01004906 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004907 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004908 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004909
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004910 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004911 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004912 if (IS_ERR(kbuf))
4913 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004914 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004915 }
4916
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004917 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004918 if (unlikely(ret))
4919 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004920
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004921 msg.msg_name = NULL;
4922 msg.msg_control = NULL;
4923 msg.msg_controllen = 0;
4924 msg.msg_namelen = 0;
4925 msg.msg_iocb = NULL;
4926 msg.msg_flags = 0;
4927
4928 flags = req->sr_msg.msg_flags;
4929 if (flags & MSG_DONTWAIT)
4930 req->flags |= REQ_F_NOWAIT;
4931 else if (force_nonblock)
4932 flags |= MSG_DONTWAIT;
4933
4934 ret = sock_recvmsg(sock, &msg, flags);
4935 if (force_nonblock && ret == -EAGAIN)
4936 return -EAGAIN;
4937 if (ret == -ERESTARTSYS)
4938 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004939out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004940 if (req->flags & REQ_F_BUFFER_SELECTED)
4941 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004942 if (ret < 0)
4943 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004944 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004945 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004946}
4947
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004949{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004950 struct io_accept *accept = &req->accept;
4951
Jens Axboe14587a462020-09-05 11:36:08 -06004952 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004953 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004954 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004955 return -EINVAL;
4956
Jens Axboed55e5f52019-12-11 16:12:15 -07004957 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4958 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004959 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004960 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004961 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004962}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004963
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004964static int io_accept(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004965 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004966{
4967 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004968 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004969 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004970 int ret;
4971
Jiufei Xuee697dee2020-06-10 13:41:59 +08004972 if (req->file->f_flags & O_NONBLOCK)
4973 req->flags |= REQ_F_NOWAIT;
4974
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004975 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004976 accept->addr_len, accept->flags,
4977 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004978 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004979 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004980 if (ret < 0) {
4981 if (ret == -ERESTARTSYS)
4982 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004983 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004984 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004985 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004986 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004987}
4988
Jens Axboe3529d8c2019-12-19 18:24:38 -07004989static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004990{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004991 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004992 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004993
Jens Axboe14587a462020-09-05 11:36:08 -06004994 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004995 return -EINVAL;
4996 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4997 return -EINVAL;
4998
Jens Axboe3529d8c2019-12-19 18:24:38 -07004999 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5000 conn->addr_len = READ_ONCE(sqe->addr2);
5001
5002 if (!io)
5003 return 0;
5004
5005 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005006 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005007}
5008
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005009static int io_connect(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06005010 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005011{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005012 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005013 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005014 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005015 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005016
Jens Axboee8c2bc12020-08-15 18:44:09 -07005017 if (req->async_data) {
5018 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005019 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020 ret = move_addr_to_kernel(req->connect.addr,
5021 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005022 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005023 if (ret)
5024 goto out;
5025 io = &__io;
5026 }
5027
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005028 file_flags = force_nonblock ? O_NONBLOCK : 0;
5029
Jens Axboee8c2bc12020-08-15 18:44:09 -07005030 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005031 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005032 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005033 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005034 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005035 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005036 ret = -ENOMEM;
5037 goto out;
5038 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005039 io = req->async_data;
5040 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005041 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005042 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005043 if (ret == -ERESTARTSYS)
5044 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005045out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005046 if (ret < 0)
5047 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005048 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005049 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005050}
YueHaibing469956e2020-03-04 15:53:52 +08005051#else /* !CONFIG_NET */
5052static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5053{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005054 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005055}
5056
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005057static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005058 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005059{
YueHaibing469956e2020-03-04 15:53:52 +08005060 return -EOPNOTSUPP;
5061}
5062
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005063static int io_send(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005064 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005065{
5066 return -EOPNOTSUPP;
5067}
5068
5069static int io_recvmsg_prep(struct io_kiocb *req,
5070 const struct io_uring_sqe *sqe)
5071{
5072 return -EOPNOTSUPP;
5073}
5074
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005075static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005076 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005077{
5078 return -EOPNOTSUPP;
5079}
5080
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005081static int io_recv(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005082 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005083{
5084 return -EOPNOTSUPP;
5085}
5086
5087static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5088{
5089 return -EOPNOTSUPP;
5090}
5091
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005092static int io_accept(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005093 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005094{
5095 return -EOPNOTSUPP;
5096}
5097
5098static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5099{
5100 return -EOPNOTSUPP;
5101}
5102
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005103static int io_connect(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005104 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005105{
5106 return -EOPNOTSUPP;
5107}
5108#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005109
Jens Axboed7718a92020-02-14 22:23:12 -07005110struct io_poll_table {
5111 struct poll_table_struct pt;
5112 struct io_kiocb *req;
5113 int error;
5114};
5115
Jens Axboed7718a92020-02-14 22:23:12 -07005116static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5117 __poll_t mask, task_work_func_t func)
5118{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005119 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005120
5121 /* for instances that support it check for an event match first: */
5122 if (mask && !(mask & poll->events))
5123 return 0;
5124
5125 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5126
5127 list_del_init(&poll->wait.entry);
5128
Jens Axboed7718a92020-02-14 22:23:12 -07005129 req->result = mask;
5130 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005131 percpu_ref_get(&req->ctx->refs);
5132
Jens Axboed7718a92020-02-14 22:23:12 -07005133 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005134 * If this fails, then the task is exiting. When a task exits, the
5135 * work gets canceled, so just cancel this request as well instead
5136 * of executing it. We can't safely execute it anyway, as we may not
5137 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005138 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005139 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005140 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005141 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005142 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005143 }
Jens Axboed7718a92020-02-14 22:23:12 -07005144 return 1;
5145}
5146
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005147static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5148 __acquires(&req->ctx->completion_lock)
5149{
5150 struct io_ring_ctx *ctx = req->ctx;
5151
5152 if (!req->result && !READ_ONCE(poll->canceled)) {
5153 struct poll_table_struct pt = { ._key = poll->events };
5154
5155 req->result = vfs_poll(req->file, &pt) & poll->events;
5156 }
5157
5158 spin_lock_irq(&ctx->completion_lock);
5159 if (!req->result && !READ_ONCE(poll->canceled)) {
5160 add_wait_queue(poll->head, &poll->wait);
5161 return true;
5162 }
5163
5164 return false;
5165}
5166
Jens Axboed4e7cd32020-08-15 11:44:50 -07005167static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005168{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005169 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005170 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005171 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005172 return req->apoll->double_poll;
5173}
5174
5175static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5176{
5177 if (req->opcode == IORING_OP_POLL_ADD)
5178 return &req->poll;
5179 return &req->apoll->poll;
5180}
5181
5182static void io_poll_remove_double(struct io_kiocb *req)
5183{
5184 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005185
5186 lockdep_assert_held(&req->ctx->completion_lock);
5187
5188 if (poll && poll->head) {
5189 struct wait_queue_head *head = poll->head;
5190
5191 spin_lock(&head->lock);
5192 list_del_init(&poll->wait.entry);
5193 if (poll->wait.private)
5194 refcount_dec(&req->refs);
5195 poll->head = NULL;
5196 spin_unlock(&head->lock);
5197 }
5198}
5199
5200static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5201{
5202 struct io_ring_ctx *ctx = req->ctx;
5203
Jens Axboed4e7cd32020-08-15 11:44:50 -07005204 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005205 req->poll.done = true;
5206 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5207 io_commit_cqring(ctx);
5208}
5209
Jens Axboe18bceab2020-05-15 11:56:54 -06005210static void io_poll_task_func(struct callback_head *cb)
5211{
5212 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005213 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005214 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005215
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005216 if (io_poll_rewait(req, &req->poll)) {
5217 spin_unlock_irq(&ctx->completion_lock);
5218 } else {
5219 hash_del(&req->hash_node);
5220 io_poll_complete(req, req->result, 0);
5221 spin_unlock_irq(&ctx->completion_lock);
5222
5223 nxt = io_put_req_find_next(req);
5224 io_cqring_ev_posted(ctx);
5225 if (nxt)
5226 __io_req_task_submit(nxt);
5227 }
5228
Jens Axboe6d816e02020-08-11 08:04:14 -06005229 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005230}
5231
5232static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5233 int sync, void *key)
5234{
5235 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005236 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005237 __poll_t mask = key_to_poll(key);
5238
5239 /* for instances that support it check for an event match first: */
5240 if (mask && !(mask & poll->events))
5241 return 0;
5242
Jens Axboe8706e042020-09-28 08:38:54 -06005243 list_del_init(&wait->entry);
5244
Jens Axboe807abcb2020-07-17 17:09:27 -06005245 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005246 bool done;
5247
Jens Axboe807abcb2020-07-17 17:09:27 -06005248 spin_lock(&poll->head->lock);
5249 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005250 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005251 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005252 /* make sure double remove sees this as being gone */
5253 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005254 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005255 if (!done) {
5256 /* use wait func handler, so it matches the rq type */
5257 poll->wait.func(&poll->wait, mode, sync, key);
5258 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005259 }
5260 refcount_dec(&req->refs);
5261 return 1;
5262}
5263
5264static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5265 wait_queue_func_t wake_func)
5266{
5267 poll->head = NULL;
5268 poll->done = false;
5269 poll->canceled = false;
5270 poll->events = events;
5271 INIT_LIST_HEAD(&poll->wait.entry);
5272 init_waitqueue_func_entry(&poll->wait, wake_func);
5273}
5274
5275static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005276 struct wait_queue_head *head,
5277 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005278{
5279 struct io_kiocb *req = pt->req;
5280
5281 /*
5282 * If poll->head is already set, it's because the file being polled
5283 * uses multiple waitqueues for poll handling (eg one for read, one
5284 * for write). Setup a separate io_poll_iocb if this happens.
5285 */
5286 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005287 struct io_poll_iocb *poll_one = poll;
5288
Jens Axboe18bceab2020-05-15 11:56:54 -06005289 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005290 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005291 pt->error = -EINVAL;
5292 return;
5293 }
5294 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5295 if (!poll) {
5296 pt->error = -ENOMEM;
5297 return;
5298 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005299 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005300 refcount_inc(&req->refs);
5301 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005302 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005303 }
5304
5305 pt->error = 0;
5306 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005307
5308 if (poll->events & EPOLLEXCLUSIVE)
5309 add_wait_queue_exclusive(head, &poll->wait);
5310 else
5311 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005312}
5313
5314static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5315 struct poll_table_struct *p)
5316{
5317 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005318 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005319
Jens Axboe807abcb2020-07-17 17:09:27 -06005320 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005321}
5322
Jens Axboed7718a92020-02-14 22:23:12 -07005323static void io_async_task_func(struct callback_head *cb)
5324{
5325 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5326 struct async_poll *apoll = req->apoll;
5327 struct io_ring_ctx *ctx = req->ctx;
5328
5329 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5330
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005331 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005332 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005333 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005334 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005335 }
5336
Jens Axboe31067252020-05-17 17:43:31 -06005337 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005338 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005339 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005340
Jens Axboed4e7cd32020-08-15 11:44:50 -07005341 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005342 spin_unlock_irq(&ctx->completion_lock);
5343
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005344 if (!READ_ONCE(apoll->poll.canceled))
5345 __io_req_task_submit(req);
5346 else
5347 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005348
Jens Axboe6d816e02020-08-11 08:04:14 -06005349 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005350 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005351 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005352}
5353
5354static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5355 void *key)
5356{
5357 struct io_kiocb *req = wait->private;
5358 struct io_poll_iocb *poll = &req->apoll->poll;
5359
5360 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5361 key_to_poll(key));
5362
5363 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5364}
5365
5366static void io_poll_req_insert(struct io_kiocb *req)
5367{
5368 struct io_ring_ctx *ctx = req->ctx;
5369 struct hlist_head *list;
5370
5371 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5372 hlist_add_head(&req->hash_node, list);
5373}
5374
5375static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5376 struct io_poll_iocb *poll,
5377 struct io_poll_table *ipt, __poll_t mask,
5378 wait_queue_func_t wake_func)
5379 __acquires(&ctx->completion_lock)
5380{
5381 struct io_ring_ctx *ctx = req->ctx;
5382 bool cancel = false;
5383
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005384 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005385 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005386 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005387 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005388
5389 ipt->pt._key = mask;
5390 ipt->req = req;
5391 ipt->error = -EINVAL;
5392
Jens Axboed7718a92020-02-14 22:23:12 -07005393 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5394
5395 spin_lock_irq(&ctx->completion_lock);
5396 if (likely(poll->head)) {
5397 spin_lock(&poll->head->lock);
5398 if (unlikely(list_empty(&poll->wait.entry))) {
5399 if (ipt->error)
5400 cancel = true;
5401 ipt->error = 0;
5402 mask = 0;
5403 }
5404 if (mask || ipt->error)
5405 list_del_init(&poll->wait.entry);
5406 else if (cancel)
5407 WRITE_ONCE(poll->canceled, true);
5408 else if (!poll->done) /* actually waiting for an event */
5409 io_poll_req_insert(req);
5410 spin_unlock(&poll->head->lock);
5411 }
5412
5413 return mask;
5414}
5415
5416static bool io_arm_poll_handler(struct io_kiocb *req)
5417{
5418 const struct io_op_def *def = &io_op_defs[req->opcode];
5419 struct io_ring_ctx *ctx = req->ctx;
5420 struct async_poll *apoll;
5421 struct io_poll_table ipt;
5422 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005423 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005424
5425 if (!req->file || !file_can_poll(req->file))
5426 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005427 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005428 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005429 if (def->pollin)
5430 rw = READ;
5431 else if (def->pollout)
5432 rw = WRITE;
5433 else
5434 return false;
5435 /* if we can't nonblock try, then no point in arming a poll handler */
5436 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005437 return false;
5438
5439 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5440 if (unlikely(!apoll))
5441 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005442 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005443
5444 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005445 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005446
Nathan Chancellor8755d972020-03-02 16:01:19 -07005447 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005448 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005449 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005450 if (def->pollout)
5451 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005452
5453 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5454 if ((req->opcode == IORING_OP_RECVMSG) &&
5455 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5456 mask &= ~POLLIN;
5457
Jens Axboed7718a92020-02-14 22:23:12 -07005458 mask |= POLLERR | POLLPRI;
5459
5460 ipt.pt._qproc = io_async_queue_proc;
5461
5462 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5463 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005464 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005465 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005466 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005467 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005468 kfree(apoll);
5469 return false;
5470 }
5471 spin_unlock_irq(&ctx->completion_lock);
5472 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5473 apoll->poll.events);
5474 return true;
5475}
5476
5477static bool __io_poll_remove_one(struct io_kiocb *req,
5478 struct io_poll_iocb *poll)
5479{
Jens Axboeb41e9852020-02-17 09:52:41 -07005480 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005481
5482 spin_lock(&poll->head->lock);
5483 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005484 if (!list_empty(&poll->wait.entry)) {
5485 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005486 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005487 }
5488 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005489 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005490 return do_complete;
5491}
5492
5493static bool io_poll_remove_one(struct io_kiocb *req)
5494{
5495 bool do_complete;
5496
Jens Axboed4e7cd32020-08-15 11:44:50 -07005497 io_poll_remove_double(req);
5498
Jens Axboed7718a92020-02-14 22:23:12 -07005499 if (req->opcode == IORING_OP_POLL_ADD) {
5500 do_complete = __io_poll_remove_one(req, &req->poll);
5501 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005502 struct async_poll *apoll = req->apoll;
5503
Jens Axboed7718a92020-02-14 22:23:12 -07005504 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005505 do_complete = __io_poll_remove_one(req, &apoll->poll);
5506 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005507 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005508 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005509 kfree(apoll);
5510 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005511 }
5512
Jens Axboeb41e9852020-02-17 09:52:41 -07005513 if (do_complete) {
5514 io_cqring_fill_event(req, -ECANCELED);
5515 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005516 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005517 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005518 }
5519
5520 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005521}
5522
Jens Axboe76e1b642020-09-26 15:05:03 -06005523/*
5524 * Returns true if we found and killed one or more poll requests
5525 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005526static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5527 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005528{
Jens Axboe78076bb2019-12-04 19:56:40 -07005529 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005530 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005531 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005532
5533 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005534 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5535 struct hlist_head *list;
5536
5537 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005538 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005539 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005540 posted += io_poll_remove_one(req);
5541 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005542 }
5543 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005544
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005545 if (posted)
5546 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005547
5548 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005549}
5550
Jens Axboe47f46762019-11-09 17:43:02 -07005551static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5552{
Jens Axboe78076bb2019-12-04 19:56:40 -07005553 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005554 struct io_kiocb *req;
5555
Jens Axboe78076bb2019-12-04 19:56:40 -07005556 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5557 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005558 if (sqe_addr != req->user_data)
5559 continue;
5560 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005561 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005562 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005563 }
5564
5565 return -ENOENT;
5566}
5567
Jens Axboe3529d8c2019-12-19 18:24:38 -07005568static int io_poll_remove_prep(struct io_kiocb *req,
5569 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005570{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005571 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5572 return -EINVAL;
5573 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5574 sqe->poll_events)
5575 return -EINVAL;
5576
Pavel Begunkov018043b2020-10-27 23:17:18 +00005577 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005578 return 0;
5579}
5580
5581/*
5582 * Find a running poll command that matches one specified in sqe->addr,
5583 * and remove it if found.
5584 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005585static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005586{
5587 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005588 int ret;
5589
Jens Axboe221c5eb2019-01-17 09:41:58 -07005590 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005591 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005592 spin_unlock_irq(&ctx->completion_lock);
5593
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005594 if (ret < 0)
5595 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005596 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005597 return 0;
5598}
5599
Jens Axboe221c5eb2019-01-17 09:41:58 -07005600static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5601 void *key)
5602{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005603 struct io_kiocb *req = wait->private;
5604 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005605
Jens Axboed7718a92020-02-14 22:23:12 -07005606 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005607}
5608
Jens Axboe221c5eb2019-01-17 09:41:58 -07005609static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5610 struct poll_table_struct *p)
5611{
5612 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5613
Jens Axboee8c2bc12020-08-15 18:44:09 -07005614 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005615}
5616
Jens Axboe3529d8c2019-12-19 18:24:38 -07005617static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005618{
5619 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005620 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005621
5622 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5623 return -EINVAL;
5624 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5625 return -EINVAL;
5626
Jiufei Xue5769a352020-06-17 17:53:55 +08005627 events = READ_ONCE(sqe->poll32_events);
5628#ifdef __BIG_ENDIAN
5629 events = swahw32(events);
5630#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005631 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5632 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005633 return 0;
5634}
5635
Pavel Begunkov61e98202021-02-10 00:03:08 +00005636static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005637{
5638 struct io_poll_iocb *poll = &req->poll;
5639 struct io_ring_ctx *ctx = req->ctx;
5640 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005641 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005642
Jens Axboed7718a92020-02-14 22:23:12 -07005643 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005644
Jens Axboed7718a92020-02-14 22:23:12 -07005645 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5646 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005647
Jens Axboe8c838782019-03-12 15:48:16 -06005648 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005649 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005650 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005651 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005652 spin_unlock_irq(&ctx->completion_lock);
5653
Jens Axboe8c838782019-03-12 15:48:16 -06005654 if (mask) {
5655 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005656 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005657 }
Jens Axboe8c838782019-03-12 15:48:16 -06005658 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005659}
5660
Jens Axboe5262f562019-09-17 12:26:57 -06005661static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5662{
Jens Axboead8a48a2019-11-15 08:49:11 -07005663 struct io_timeout_data *data = container_of(timer,
5664 struct io_timeout_data, timer);
5665 struct io_kiocb *req = data->req;
5666 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005667 unsigned long flags;
5668
Jens Axboe5262f562019-09-17 12:26:57 -06005669 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005670 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005671 atomic_set(&req->ctx->cq_timeouts,
5672 atomic_read(&req->ctx->cq_timeouts) + 1);
5673
Jens Axboe78e19bb2019-11-06 15:21:34 -07005674 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005675 io_commit_cqring(ctx);
5676 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5677
5678 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005679 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005680 io_put_req(req);
5681 return HRTIMER_NORESTART;
5682}
5683
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005684static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5685 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005686{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005687 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005688 struct io_kiocb *req;
5689 int ret = -ENOENT;
5690
5691 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5692 if (user_data == req->user_data) {
5693 ret = 0;
5694 break;
5695 }
5696 }
5697
5698 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005699 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005700
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005701 io = req->async_data;
5702 ret = hrtimer_try_to_cancel(&io->timer);
5703 if (ret == -1)
5704 return ERR_PTR(-EALREADY);
5705 list_del_init(&req->timeout.list);
5706 return req;
5707}
5708
5709static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5710{
5711 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5712
5713 if (IS_ERR(req))
5714 return PTR_ERR(req);
5715
5716 req_set_fail_links(req);
5717 io_cqring_fill_event(req, -ECANCELED);
5718 io_put_req_deferred(req, 1);
5719 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005720}
5721
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005722static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5723 struct timespec64 *ts, enum hrtimer_mode mode)
5724{
5725 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5726 struct io_timeout_data *data;
5727
5728 if (IS_ERR(req))
5729 return PTR_ERR(req);
5730
5731 req->timeout.off = 0; /* noseq */
5732 data = req->async_data;
5733 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5734 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5735 data->timer.function = io_timeout_fn;
5736 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5737 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005738}
5739
Jens Axboe3529d8c2019-12-19 18:24:38 -07005740static int io_timeout_remove_prep(struct io_kiocb *req,
5741 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005742{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005743 struct io_timeout_rem *tr = &req->timeout_rem;
5744
Jens Axboeb29472e2019-12-17 18:50:29 -07005745 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5746 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005747 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5748 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005749 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005750 return -EINVAL;
5751
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005752 tr->addr = READ_ONCE(sqe->addr);
5753 tr->flags = READ_ONCE(sqe->timeout_flags);
5754 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5755 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5756 return -EINVAL;
5757 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5758 return -EFAULT;
5759 } else if (tr->flags) {
5760 /* timeout removal doesn't support flags */
5761 return -EINVAL;
5762 }
5763
Jens Axboeb29472e2019-12-17 18:50:29 -07005764 return 0;
5765}
5766
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005767static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5768{
5769 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5770 : HRTIMER_MODE_REL;
5771}
5772
Jens Axboe11365042019-10-16 09:08:32 -06005773/*
5774 * Remove or update an existing timeout command
5775 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005776static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005777{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005778 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005779 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005780 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005781
Jens Axboe11365042019-10-16 09:08:32 -06005782 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005783 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005784 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005785 else
5786 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5787 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005788
Jens Axboe47f46762019-11-09 17:43:02 -07005789 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005790 io_commit_cqring(ctx);
5791 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005792 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005793 if (ret < 0)
5794 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005795 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005796 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005797}
5798
Jens Axboe3529d8c2019-12-19 18:24:38 -07005799static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005800 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005801{
Jens Axboead8a48a2019-11-15 08:49:11 -07005802 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005803 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005804 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005805
Jens Axboead8a48a2019-11-15 08:49:11 -07005806 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005807 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005808 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005809 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005810 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005811 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005812 flags = READ_ONCE(sqe->timeout_flags);
5813 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005814 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005815
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005816 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005817
Jens Axboee8c2bc12020-08-15 18:44:09 -07005818 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005819 return -ENOMEM;
5820
Jens Axboee8c2bc12020-08-15 18:44:09 -07005821 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005822 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005823
5824 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005825 return -EFAULT;
5826
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005827 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005828 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5829 return 0;
5830}
5831
Pavel Begunkov61e98202021-02-10 00:03:08 +00005832static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005833{
Jens Axboead8a48a2019-11-15 08:49:11 -07005834 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005835 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005836 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005837 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005838
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005839 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005840
Jens Axboe5262f562019-09-17 12:26:57 -06005841 /*
5842 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005843 * timeout event to be satisfied. If it isn't set, then this is
5844 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005845 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005846 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005847 entry = ctx->timeout_list.prev;
5848 goto add;
5849 }
Jens Axboe5262f562019-09-17 12:26:57 -06005850
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005851 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5852 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005853
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005854 /* Update the last seq here in case io_flush_timeouts() hasn't.
5855 * This is safe because ->completion_lock is held, and submissions
5856 * and completions are never mixed in the same ->completion_lock section.
5857 */
5858 ctx->cq_last_tm_flush = tail;
5859
Jens Axboe5262f562019-09-17 12:26:57 -06005860 /*
5861 * Insertion sort, ensuring the first entry in the list is always
5862 * the one we need first.
5863 */
Jens Axboe5262f562019-09-17 12:26:57 -06005864 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005865 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5866 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005867
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005868 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005869 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005870 /* nxt.seq is behind @tail, otherwise would've been completed */
5871 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005872 break;
5873 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005874add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005875 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005876 data->timer.function = io_timeout_fn;
5877 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005878 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005879 return 0;
5880}
5881
Jens Axboe62755e32019-10-28 21:49:21 -06005882static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005883{
Jens Axboe62755e32019-10-28 21:49:21 -06005884 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005885
Jens Axboe62755e32019-10-28 21:49:21 -06005886 return req->user_data == (unsigned long) data;
5887}
5888
Jens Axboee977d6d2019-11-05 12:39:45 -07005889static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005890{
Jens Axboe62755e32019-10-28 21:49:21 -06005891 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005892 int ret = 0;
5893
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005894 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005895 switch (cancel_ret) {
5896 case IO_WQ_CANCEL_OK:
5897 ret = 0;
5898 break;
5899 case IO_WQ_CANCEL_RUNNING:
5900 ret = -EALREADY;
5901 break;
5902 case IO_WQ_CANCEL_NOTFOUND:
5903 ret = -ENOENT;
5904 break;
5905 }
5906
Jens Axboee977d6d2019-11-05 12:39:45 -07005907 return ret;
5908}
5909
Jens Axboe47f46762019-11-09 17:43:02 -07005910static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5911 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005912 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005913{
5914 unsigned long flags;
5915 int ret;
5916
5917 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5918 if (ret != -ENOENT) {
5919 spin_lock_irqsave(&ctx->completion_lock, flags);
5920 goto done;
5921 }
5922
5923 spin_lock_irqsave(&ctx->completion_lock, flags);
5924 ret = io_timeout_cancel(ctx, sqe_addr);
5925 if (ret != -ENOENT)
5926 goto done;
5927 ret = io_poll_cancel(ctx, sqe_addr);
5928done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005929 if (!ret)
5930 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005931 io_cqring_fill_event(req, ret);
5932 io_commit_cqring(ctx);
5933 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5934 io_cqring_ev_posted(ctx);
5935
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005936 if (ret < 0)
5937 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005938 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005939}
5940
Jens Axboe3529d8c2019-12-19 18:24:38 -07005941static int io_async_cancel_prep(struct io_kiocb *req,
5942 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005943{
Jens Axboefbf23842019-12-17 18:45:56 -07005944 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005945 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005946 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5947 return -EINVAL;
5948 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005949 return -EINVAL;
5950
Jens Axboefbf23842019-12-17 18:45:56 -07005951 req->cancel.addr = READ_ONCE(sqe->addr);
5952 return 0;
5953}
5954
Pavel Begunkov61e98202021-02-10 00:03:08 +00005955static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005956{
5957 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005958
Pavel Begunkov014db002020-03-03 21:33:12 +03005959 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005960 return 0;
5961}
5962
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005963static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005964 const struct io_uring_sqe *sqe)
5965{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005966 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5967 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005968 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5969 return -EINVAL;
5970 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005971 return -EINVAL;
5972
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005973 req->rsrc_update.offset = READ_ONCE(sqe->off);
5974 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5975 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005976 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005977 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005978 return 0;
5979}
5980
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005981static int io_files_update(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06005982 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005983{
5984 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005985 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005986 int ret;
5987
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005988 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005989 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005990
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005991 up.offset = req->rsrc_update.offset;
5992 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005993
5994 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005995 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005996 mutex_unlock(&ctx->uring_lock);
5997
5998 if (ret < 0)
5999 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06006000 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006001 return 0;
6002}
6003
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006004static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006005{
Jens Axboed625c6e2019-12-17 19:53:05 -07006006 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006007 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006008 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006009 case IORING_OP_READV:
6010 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006011 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006012 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006013 case IORING_OP_WRITEV:
6014 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006015 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006016 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006017 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006018 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006019 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006020 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006021 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006022 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006023 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006024 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006025 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006026 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006027 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006028 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006029 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006030 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006031 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006032 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006033 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006035 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006036 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006037 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006038 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006039 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006040 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006041 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006042 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006043 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006044 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006045 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006047 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006048 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006049 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006050 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006051 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006052 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006053 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006054 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006055 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006056 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006057 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006058 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006059 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006060 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006061 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006062 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006063 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006064 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006065 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006066 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006067 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006068 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006069 case IORING_OP_SHUTDOWN:
6070 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006071 case IORING_OP_RENAMEAT:
6072 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006073 case IORING_OP_UNLINKAT:
6074 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006075 }
6076
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006077 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6078 req->opcode);
6079 return-EINVAL;
6080}
6081
Jens Axboedef596e2019-01-09 08:59:42 -07006082static int io_req_defer_prep(struct io_kiocb *req,
6083 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006084{
Jens Axboedef596e2019-01-09 08:59:42 -07006085 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006086 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006087 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006088 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006089 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006090}
6091
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006092static u32 io_get_sequence(struct io_kiocb *req)
6093{
6094 struct io_kiocb *pos;
6095 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006096 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006097
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006098 io_for_each_link(pos, req)
6099 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006100
6101 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6102 return total_submitted - nr_reqs;
6103}
6104
Jens Axboe3529d8c2019-12-19 18:24:38 -07006105static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006106{
6107 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006108 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006109 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006110 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006111
6112 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006113 if (likely(list_empty_careful(&ctx->defer_list) &&
6114 !(req->flags & REQ_F_IO_DRAIN)))
6115 return 0;
6116
6117 seq = io_get_sequence(req);
6118 /* Still a chance to pass the sequence check */
6119 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006120 return 0;
6121
Jens Axboee8c2bc12020-08-15 18:44:09 -07006122 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006123 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006124 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006125 return ret;
6126 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006127 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006128 de = kmalloc(sizeof(*de), GFP_KERNEL);
6129 if (!de)
6130 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006131
6132 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006133 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006134 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006135 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006136 io_queue_async_work(req);
6137 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006138 }
6139
6140 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006141 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006142 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006143 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006144 spin_unlock_irq(&ctx->completion_lock);
6145 return -EIOCBQUEUED;
6146}
Jens Axboeedafcce2019-01-09 09:16:05 -07006147
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006148static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006149{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006150 if (req->flags & REQ_F_BUFFER_SELECTED) {
6151 switch (req->opcode) {
6152 case IORING_OP_READV:
6153 case IORING_OP_READ_FIXED:
6154 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006155 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006156 break;
6157 case IORING_OP_RECVMSG:
6158 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006159 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006160 break;
6161 }
6162 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006163 }
6164
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006165 if (req->flags & REQ_F_NEED_CLEANUP) {
6166 switch (req->opcode) {
6167 case IORING_OP_READV:
6168 case IORING_OP_READ_FIXED:
6169 case IORING_OP_READ:
6170 case IORING_OP_WRITEV:
6171 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006172 case IORING_OP_WRITE: {
6173 struct io_async_rw *io = req->async_data;
6174 if (io->free_iovec)
6175 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006176 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006177 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006178 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006179 case IORING_OP_SENDMSG: {
6180 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006181
6182 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006183 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006184 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006185 case IORING_OP_SPLICE:
6186 case IORING_OP_TEE:
6187 io_put_file(req, req->splice.file_in,
6188 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6189 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006190 case IORING_OP_OPENAT:
6191 case IORING_OP_OPENAT2:
6192 if (req->open.filename)
6193 putname(req->open.filename);
6194 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006195 case IORING_OP_RENAMEAT:
6196 putname(req->rename.oldpath);
6197 putname(req->rename.newpath);
6198 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006199 case IORING_OP_UNLINKAT:
6200 putname(req->unlink.filename);
6201 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006202 }
6203 req->flags &= ~REQ_F_NEED_CLEANUP;
6204 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006205}
6206
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006207static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags,
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006208 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006209{
Jens Axboeedafcce2019-01-09 09:16:05 -07006210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006211 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006212
Jens Axboed625c6e2019-12-17 19:53:05 -07006213 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006214 case IORING_OP_NOP:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006215 ret = io_nop(req, issue_flags, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006216 break;
6217 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006218 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006219 case IORING_OP_READ:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006220 ret = io_read(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006221 break;
6222 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006224 case IORING_OP_WRITE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006225 ret = io_write(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226 break;
6227 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006228 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006229 break;
6230 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006231 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232 break;
6233 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006234 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006235 break;
6236 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006237 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006238 break;
6239 case IORING_OP_SENDMSG:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006240 ret = io_sendmsg(req, issue_flags, cs);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006241 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006242 case IORING_OP_SEND:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006243 ret = io_send(req, issue_flags, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006244 break;
6245 case IORING_OP_RECVMSG:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006246 ret = io_recvmsg(req, issue_flags, cs);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006247 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006248 case IORING_OP_RECV:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006249 ret = io_recv(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250 break;
6251 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006252 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006253 break;
6254 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006255 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256 break;
6257 case IORING_OP_ACCEPT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006258 ret = io_accept(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006259 break;
6260 case IORING_OP_CONNECT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006261 ret = io_connect(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262 break;
6263 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006264 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006266 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006267 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006268 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006269 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006270 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006271 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006272 case IORING_OP_CLOSE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006273 ret = io_close(req, issue_flags, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006274 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006275 case IORING_OP_FILES_UPDATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006276 ret = io_files_update(req, issue_flags, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006277 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006278 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006279 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006280 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006281 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006282 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006283 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006284 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006285 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006286 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006287 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006288 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006289 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006290 case IORING_OP_EPOLL_CTL:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006291 ret = io_epoll_ctl(req, issue_flags, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006292 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006293 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006294 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006295 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006296 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006297 ret = io_provide_buffers(req, issue_flags, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006298 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006299 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006300 ret = io_remove_buffers(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006302 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006303 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006304 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006305 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006306 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006307 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006308 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006309 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006310 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006311 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006312 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006313 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 default:
6315 ret = -EINVAL;
6316 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006317 }
6318
6319 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006320 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321
Jens Axboeb5325762020-05-19 21:20:27 -06006322 /* If the op doesn't have a file, we're not polling for it */
6323 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006324 const bool in_async = io_wq_current_is_worker();
6325
Jens Axboe11ba8202020-01-15 21:51:17 -07006326 /* workqueue context doesn't hold uring_lock, grab it now */
6327 if (in_async)
6328 mutex_lock(&ctx->uring_lock);
6329
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006330 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006331
6332 if (in_async)
6333 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 }
6335
6336 return 0;
6337}
6338
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006339static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006340{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006341 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006342 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006343 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006345 timeout = io_prep_linked_timeout(req);
6346 if (timeout)
6347 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006348
Jens Axboe4014d942021-01-19 15:53:54 -07006349 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006350 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006351
Jens Axboe561fb042019-10-24 07:25:42 -06006352 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006353 do {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006354 ret = io_issue_sqe(req, 0, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006355 /*
6356 * We can get EAGAIN for polled IO even though we're
6357 * forcing a sync submission from here, since we can't
6358 * wait for request slots on the block side.
6359 */
6360 if (ret != -EAGAIN)
6361 break;
6362 cond_resched();
6363 } while (1);
6364 }
Jens Axboe31b51512019-01-18 22:56:34 -07006365
Jens Axboe561fb042019-10-24 07:25:42 -06006366 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006367 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006368
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006369 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6370 lock_ctx = req->ctx;
6371
6372 /*
6373 * io_iopoll_complete() does not hold completion_lock to
6374 * complete polled io, so here for polled io, we can not call
6375 * io_req_complete() directly, otherwise there maybe concurrent
6376 * access to cqring, defer_list, etc, which is not safe. Given
6377 * that io_iopoll_complete() is always called under uring_lock,
6378 * so here for polled io, we also get uring_lock to complete
6379 * it.
6380 */
6381 if (lock_ctx)
6382 mutex_lock(&lock_ctx->uring_lock);
6383
6384 req_set_fail_links(req);
6385 io_req_complete(req, ret);
6386
6387 if (lock_ctx)
6388 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006389 }
Jens Axboe31b51512019-01-18 22:56:34 -07006390}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006391
Jens Axboe65e19f52019-10-26 07:20:21 -06006392static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6393 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006394{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006395 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006396
Jens Axboe05f3fb32019-12-09 11:22:50 -07006397 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006398 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006399}
6400
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006401static struct file *io_file_get(struct io_submit_state *state,
6402 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006403{
6404 struct io_ring_ctx *ctx = req->ctx;
6405 struct file *file;
6406
6407 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006408 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006409 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006410 fd = array_index_nospec(fd, ctx->nr_user_files);
6411 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006412 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006413 } else {
6414 trace_io_uring_file_get(ctx, fd);
6415 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006416 }
6417
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006418 if (file && unlikely(file->f_op == &io_uring_fops))
6419 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006420 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006421}
6422
Jens Axboe2665abf2019-11-05 12:40:47 -07006423static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6424{
Jens Axboead8a48a2019-11-15 08:49:11 -07006425 struct io_timeout_data *data = container_of(timer,
6426 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006427 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006428 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006429 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006430
6431 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006432 prev = req->timeout.head;
6433 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006434
6435 /*
6436 * We don't expect the list to be empty, that will only happen if we
6437 * race with the completion of the linked work.
6438 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006439 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006440 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006441 else
6442 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006443 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6444
6445 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006446 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006447 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006448 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006449 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006450 io_req_complete_post(req, -ETIME, 0);
6451 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006452 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006453 return HRTIMER_NORESTART;
6454}
6455
Jens Axboe7271ef32020-08-10 09:55:22 -06006456static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006457{
Jens Axboe76a46e02019-11-10 23:34:16 -07006458 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006459 * If the back reference is NULL, then our linked request finished
6460 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006461 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006462 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006463 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006464
Jens Axboead8a48a2019-11-15 08:49:11 -07006465 data->timer.function = io_link_timeout_fn;
6466 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6467 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006468 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006469}
6470
6471static void io_queue_linked_timeout(struct io_kiocb *req)
6472{
6473 struct io_ring_ctx *ctx = req->ctx;
6474
6475 spin_lock_irq(&ctx->completion_lock);
6476 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006477 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006478
Jens Axboe2665abf2019-11-05 12:40:47 -07006479 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006480 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006481}
6482
Jens Axboead8a48a2019-11-15 08:49:11 -07006483static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006484{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006485 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006486
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006487 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6488 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006489 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006490
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006491 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006492 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006493 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006494 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006495}
6496
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006497static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006498{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006499 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006500 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006501 int ret;
6502
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006503again:
6504 linked_timeout = io_prep_linked_timeout(req);
6505
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006506 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6507 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006508 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006509 if (old_creds)
6510 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006511 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006512 old_creds = NULL; /* restored original creds */
6513 else
Jens Axboe98447d62020-10-14 10:48:51 -06006514 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006515 }
6516
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006517 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006518
6519 /*
6520 * We async punt it if the file wasn't marked NOWAIT, or if the file
6521 * doesn't support non-blocking read/write attempts
6522 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006523 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006524 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006525 /*
6526 * Queued up for async execution, worker will release
6527 * submit reference when the iocb is actually submitted.
6528 */
6529 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006530 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006531
Pavel Begunkovf063c542020-07-25 14:41:59 +03006532 if (linked_timeout)
6533 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006534 } else if (likely(!ret)) {
6535 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006536 if (req->flags & REQ_F_COMPLETE_INLINE) {
6537 list_add_tail(&req->compl.list, &cs->list);
6538 if (++cs->nr >= 32)
Pavel Begunkov9affd662021-01-19 13:32:46 +00006539 io_submit_flush_completions(cs);
6540 req = NULL;
6541 } else {
6542 req = io_put_req_find_next(req);
6543 }
6544
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006545 if (linked_timeout)
6546 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006547
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006548 if (req) {
6549 if (!(req->flags & REQ_F_FORCE_ASYNC))
6550 goto again;
6551 io_queue_async_work(req);
6552 }
6553 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006554 /* un-prep timeout, so it'll be killed as any other linked */
6555 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006556 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006557 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006558 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006559 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006560
Jens Axboe193155c2020-02-22 23:22:19 -07006561 if (old_creds)
6562 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006563}
6564
Jens Axboef13fad72020-06-22 09:34:30 -06006565static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6566 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006567{
6568 int ret;
6569
Jens Axboe3529d8c2019-12-19 18:24:38 -07006570 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006571 if (ret) {
6572 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006573fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006574 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006575 io_put_req(req);
6576 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006577 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006578 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006579 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006580 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006581 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006582 goto fail_req;
6583 }
Jens Axboece35a472019-12-17 08:04:44 -07006584 io_queue_async_work(req);
6585 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006586 if (sqe) {
6587 ret = io_req_prep(req, sqe);
6588 if (unlikely(ret))
6589 goto fail_req;
6590 }
6591 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006592 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006593}
6594
Jens Axboef13fad72020-06-22 09:34:30 -06006595static inline void io_queue_link_head(struct io_kiocb *req,
6596 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006597{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006598 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006599 io_put_req(req);
6600 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006601 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006602 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006603}
6604
Pavel Begunkov863e0562020-10-27 23:25:35 +00006605struct io_submit_link {
6606 struct io_kiocb *head;
6607 struct io_kiocb *last;
6608};
6609
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006610static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006611 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006612{
Jackie Liua197f662019-11-08 08:09:12 -07006613 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006614 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006615
Jens Axboe9e645e112019-05-10 16:07:28 -06006616 /*
6617 * If we already have a head request, queue this one for async
6618 * submittal once the head completes. If we don't have a head but
6619 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6620 * submitted sync once the chain is complete. If none of those
6621 * conditions are true (normal request), then just queue it.
6622 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006623 if (link->head) {
6624 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006625
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006626 /*
6627 * Taking sequential execution of a link, draining both sides
6628 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6629 * requests in the link. So, it drains the head and the
6630 * next after the link request. The last one is done via
6631 * drain_next flag to persist the effect across calls.
6632 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006633 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006634 head->flags |= REQ_F_IO_DRAIN;
6635 ctx->drain_next = 1;
6636 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006637 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006638 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006639 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006640 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006641 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006642 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006643 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006644 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006645 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006646
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006647 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006648 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006649 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006650 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006651 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006652 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006653 if (unlikely(ctx->drain_next)) {
6654 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006655 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006656 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006657 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006658 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006659 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006660 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006661 link->head = req;
6662 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006663 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006664 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006665 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006666 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006667
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006668 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006669}
6670
Jens Axboe9a56a232019-01-09 09:06:50 -07006671/*
6672 * Batched submission is done, ensure local IO is flushed out.
6673 */
6674static void io_submit_state_end(struct io_submit_state *state)
6675{
Jens Axboef13fad72020-06-22 09:34:30 -06006676 if (!list_empty(&state->comp.list))
6677 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006678 if (state->plug_started)
6679 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006680 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006681 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006682 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006683}
6684
6685/*
6686 * Start submission side cache.
6687 */
6688static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006689 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006690{
Jens Axboe27926b62020-10-28 09:33:23 -06006691 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006692 state->comp.nr = 0;
6693 INIT_LIST_HEAD(&state->comp.list);
6694 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006695 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006696 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006697 state->ios_left = max_ios;
6698}
6699
Jens Axboe2b188cc2019-01-07 10:46:33 -07006700static void io_commit_sqring(struct io_ring_ctx *ctx)
6701{
Hristo Venev75b28af2019-08-26 17:23:46 +00006702 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006703
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006704 /*
6705 * Ensure any loads from the SQEs are done at this point,
6706 * since once we write the new head, the application could
6707 * write new data to them.
6708 */
6709 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006710}
6711
6712/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006713 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006714 * that is mapped by userspace. This means that care needs to be taken to
6715 * ensure that reads are stable, as we cannot rely on userspace always
6716 * being a good citizen. If members of the sqe are validated and then later
6717 * used, it's important that those reads are done through READ_ONCE() to
6718 * prevent a re-load down the line.
6719 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006720static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006721{
Hristo Venev75b28af2019-08-26 17:23:46 +00006722 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006723 unsigned head;
6724
6725 /*
6726 * The cached sq head (or cq tail) serves two purposes:
6727 *
6728 * 1) allows us to batch the cost of updating the user visible
6729 * head updates.
6730 * 2) allows the kernel side to track the head on its own, even
6731 * though the application is the one updating it.
6732 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006733 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006734 if (likely(head < ctx->sq_entries))
6735 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736
6737 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006738 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006739 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006740 return NULL;
6741}
6742
6743static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6744{
6745 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746}
6747
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006748/*
6749 * Check SQE restrictions (opcode and flags).
6750 *
6751 * Returns 'true' if SQE is allowed, 'false' otherwise.
6752 */
6753static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6754 struct io_kiocb *req,
6755 unsigned int sqe_flags)
6756{
6757 if (!ctx->restricted)
6758 return true;
6759
6760 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6761 return false;
6762
6763 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6764 ctx->restrictions.sqe_flags_required)
6765 return false;
6766
6767 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6768 ctx->restrictions.sqe_flags_required))
6769 return false;
6770
6771 return true;
6772}
6773
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006774#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6775 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6776 IOSQE_BUFFER_SELECT)
6777
6778static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6779 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006780 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006781{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006782 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006783 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006784
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006785 req->opcode = READ_ONCE(sqe->opcode);
6786 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006787 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006788 req->file = NULL;
6789 req->ctx = ctx;
6790 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006791 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006792 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006793 /* one is dropped after submission, the other at completion */
6794 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006795 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006796 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006797
6798 if (unlikely(req->opcode >= IORING_OP_LAST))
6799 return -EINVAL;
6800
Jens Axboe28cea78a2020-09-14 10:51:17 -06006801 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006802 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006803
6804 sqe_flags = READ_ONCE(sqe->flags);
6805 /* enforce forwards compatibility on users */
6806 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6807 return -EINVAL;
6808
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006809 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6810 return -EACCES;
6811
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006812 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6813 !io_op_defs[req->opcode].buffer_select)
6814 return -EOPNOTSUPP;
6815
6816 id = READ_ONCE(sqe->personality);
6817 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006818 struct io_identity *iod;
6819
Jens Axboe1e6fa522020-10-15 08:46:24 -06006820 iod = idr_find(&ctx->personality_idr, id);
6821 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006822 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006823 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006824
6825 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006826 get_cred(iod->creds);
6827 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006828 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006829 }
6830
6831 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006832 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006833
Jens Axboe27926b62020-10-28 09:33:23 -06006834 /*
6835 * Plug now if we have more than 1 IO left after this, and the target
6836 * is potentially a read/write to block based storage.
6837 */
6838 if (!state->plug_started && state->ios_left > 1 &&
6839 io_op_defs[req->opcode].plug) {
6840 blk_start_plug(&state->plug);
6841 state->plug_started = true;
6842 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006843
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006844 ret = 0;
6845 if (io_op_defs[req->opcode].needs_file) {
6846 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006847
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006848 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006849 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006850 ret = -EBADF;
6851 }
6852
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006853 state->ios_left--;
6854 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006855}
6856
Jens Axboe0f212202020-09-13 13:09:39 -06006857static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006858{
Jens Axboeac8691c2020-06-01 08:30:41 -06006859 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006860 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006861 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006862
Jens Axboec4a2ed72019-11-21 21:01:26 -07006863 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006864 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006865 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006866 return -EBUSY;
6867 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006868
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006869 /* make sure SQ entry isn't read before tail */
6870 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006871
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006872 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6873 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006874
Jens Axboed8a6df12020-10-15 16:24:45 -06006875 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006876 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006877
Jens Axboe6c271ce2019-01-10 11:22:30 -07006878 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006879 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006880
Jens Axboe6c271ce2019-01-10 11:22:30 -07006881 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006882 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006883 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006884 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006885
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006886 sqe = io_get_sqe(ctx);
6887 if (unlikely(!sqe)) {
6888 io_consume_sqe(ctx);
6889 break;
6890 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006891 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006892 if (unlikely(!req)) {
6893 if (!submitted)
6894 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006895 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006896 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006897 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006898 /* will complete beyond this point, count as submitted */
6899 submitted++;
6900
Pavel Begunkov692d8362020-10-10 18:34:13 +01006901 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006902 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006903fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006904 io_put_req(req);
6905 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006906 break;
6907 }
6908
Jens Axboe354420f2020-01-08 18:55:15 -07006909 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006910 true, ctx->flags & IORING_SETUP_SQPOLL);
Jens Axboef13fad72020-06-22 09:34:30 -06006911 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006912 if (err)
6913 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006914 }
6915
Pavel Begunkov9466f432020-01-25 22:34:01 +03006916 if (unlikely(submitted != nr)) {
6917 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006918 struct io_uring_task *tctx = current->io_uring;
6919 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006920
Jens Axboed8a6df12020-10-15 16:24:45 -06006921 percpu_ref_put_many(&ctx->refs, unused);
6922 percpu_counter_sub(&tctx->inflight, unused);
6923 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006924 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006925 if (link.head)
6926 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006927 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006928
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006929 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6930 io_commit_sqring(ctx);
6931
Jens Axboe6c271ce2019-01-10 11:22:30 -07006932 return submitted;
6933}
6934
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006935static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6936{
6937 /* Tell userspace we may need a wakeup call */
6938 spin_lock_irq(&ctx->completion_lock);
6939 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6940 spin_unlock_irq(&ctx->completion_lock);
6941}
6942
6943static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6944{
6945 spin_lock_irq(&ctx->completion_lock);
6946 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6947 spin_unlock_irq(&ctx->completion_lock);
6948}
6949
Xiaoguang Wang08369242020-11-03 14:15:59 +08006950static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006951{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006952 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006953 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006954
Jens Axboec8d1ba52020-09-14 11:07:26 -06006955 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006956 /* if we're handling multiple rings, cap submit size for fairness */
6957 if (cap_entries && to_submit > 8)
6958 to_submit = 8;
6959
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006960 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6961 unsigned nr_events = 0;
6962
Xiaoguang Wang08369242020-11-03 14:15:59 +08006963 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006964 if (!list_empty(&ctx->iopoll_list))
6965 io_do_iopoll(ctx, &nr_events, 0);
6966
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006967 if (to_submit && !ctx->sqo_dead &&
6968 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006969 ret = io_submit_sqes(ctx, to_submit);
6970 mutex_unlock(&ctx->uring_lock);
6971 }
Jens Axboe90554202020-09-03 12:12:41 -06006972
6973 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6974 wake_up(&ctx->sqo_sq_wait);
6975
Xiaoguang Wang08369242020-11-03 14:15:59 +08006976 return ret;
6977}
6978
6979static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6980{
6981 struct io_ring_ctx *ctx;
6982 unsigned sq_thread_idle = 0;
6983
6984 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6985 if (sq_thread_idle < ctx->sq_thread_idle)
6986 sq_thread_idle = ctx->sq_thread_idle;
6987 }
6988
6989 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006990}
6991
Jens Axboe69fb2132020-09-14 11:16:23 -06006992static void io_sqd_init_new(struct io_sq_data *sqd)
6993{
6994 struct io_ring_ctx *ctx;
6995
6996 while (!list_empty(&sqd->ctx_new_list)) {
6997 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006998 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6999 complete(&ctx->sq_thread_comp);
7000 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007001
7002 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007003}
7004
Jens Axboe6c271ce2019-01-10 11:22:30 -07007005static int io_sq_thread(void *data)
7006{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007007 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007008 struct files_struct *old_files = current->files;
7009 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007010 const struct cred *old_cred = NULL;
7011 struct io_sq_data *sqd = data;
7012 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007013 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007014 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007015
Jens Axboe28cea78a2020-09-14 10:51:17 -06007016 task_lock(current);
7017 current->files = NULL;
7018 current->nsproxy = NULL;
7019 task_unlock(current);
7020
Jens Axboe69fb2132020-09-14 11:16:23 -06007021 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007022 int ret;
7023 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007024
7025 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007026 * Any changes to the sqd lists are synchronized through the
7027 * kthread parking. This synchronizes the thread vs users,
7028 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007029 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007030 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007031 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007032 /*
7033 * When sq thread is unparked, in case the previous park operation
7034 * comes from io_put_sq_data(), which means that sq thread is going
7035 * to be stopped, so here needs to have a check.
7036 */
7037 if (kthread_should_stop())
7038 break;
7039 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007040
Xiaoguang Wang08369242020-11-03 14:15:59 +08007041 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007042 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007043 timeout = jiffies + sqd->sq_thread_idle;
7044 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007045
Xiaoguang Wang08369242020-11-03 14:15:59 +08007046 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007047 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007048 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7049 if (current->cred != ctx->creds) {
7050 if (old_cred)
7051 revert_creds(old_cred);
7052 old_cred = override_creds(ctx->creds);
7053 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007054 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007055#ifdef CONFIG_AUDIT
7056 current->loginuid = ctx->loginuid;
7057 current->sessionid = ctx->sessionid;
7058#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007059
Xiaoguang Wang08369242020-11-03 14:15:59 +08007060 ret = __io_sq_thread(ctx, cap_entries);
7061 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7062 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007063
Jens Axboe28cea78a2020-09-14 10:51:17 -06007064 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007065 }
7066
Xiaoguang Wang08369242020-11-03 14:15:59 +08007067 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007068 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007069 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007070 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007071 if (sqt_spin)
7072 timeout = jiffies + sqd->sq_thread_idle;
7073 continue;
7074 }
7075
Xiaoguang Wang08369242020-11-03 14:15:59 +08007076 needs_sched = true;
7077 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7078 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7079 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7080 !list_empty_careful(&ctx->iopoll_list)) {
7081 needs_sched = false;
7082 break;
7083 }
7084 if (io_sqring_entries(ctx)) {
7085 needs_sched = false;
7086 break;
7087 }
7088 }
7089
Hao Xu8b28fdf2021-01-31 22:39:04 +08007090 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007091 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7092 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007093
Jens Axboe69fb2132020-09-14 11:16:23 -06007094 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007095 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7096 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007097 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007098
7099 finish_wait(&sqd->wait, &wait);
7100 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007101 }
7102
Jens Axboe4c6e2772020-07-01 11:29:10 -06007103 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007104 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007105
Dennis Zhou91d8f512020-09-16 13:41:05 -07007106 if (cur_css)
7107 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007108 if (old_cred)
7109 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007110
Jens Axboe28cea78a2020-09-14 10:51:17 -06007111 task_lock(current);
7112 current->files = old_files;
7113 current->nsproxy = old_nsproxy;
7114 task_unlock(current);
7115
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007116 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007117
Jens Axboe6c271ce2019-01-10 11:22:30 -07007118 return 0;
7119}
7120
Jens Axboebda52162019-09-24 13:47:15 -06007121struct io_wait_queue {
7122 struct wait_queue_entry wq;
7123 struct io_ring_ctx *ctx;
7124 unsigned to_wait;
7125 unsigned nr_timeouts;
7126};
7127
Pavel Begunkov6c503152021-01-04 20:36:36 +00007128static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007129{
7130 struct io_ring_ctx *ctx = iowq->ctx;
7131
7132 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007133 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007134 * started waiting. For timeouts, we always want to return to userspace,
7135 * regardless of event count.
7136 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007137 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007138 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7139}
7140
7141static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7142 int wake_flags, void *key)
7143{
7144 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7145 wq);
7146
Pavel Begunkov6c503152021-01-04 20:36:36 +00007147 /*
7148 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7149 * the task, and the next invocation will do it.
7150 */
7151 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7152 return autoremove_wake_function(curr, mode, wake_flags, key);
7153 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007154}
7155
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007156static int io_run_task_work_sig(void)
7157{
7158 if (io_run_task_work())
7159 return 1;
7160 if (!signal_pending(current))
7161 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007162 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7163 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007164 return -EINTR;
7165}
7166
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007167/* when returns >0, the caller should retry */
7168static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7169 struct io_wait_queue *iowq,
7170 signed long *timeout)
7171{
7172 int ret;
7173
7174 /* make sure we run task_work before checking for signals */
7175 ret = io_run_task_work_sig();
7176 if (ret || io_should_wake(iowq))
7177 return ret;
7178 /* let the caller flush overflows, retry */
7179 if (test_bit(0, &ctx->cq_check_overflow))
7180 return 1;
7181
7182 *timeout = schedule_timeout(*timeout);
7183 return !*timeout ? -ETIME : 1;
7184}
7185
Jens Axboe2b188cc2019-01-07 10:46:33 -07007186/*
7187 * Wait until events become available, if we don't already have some. The
7188 * application must reap them itself, as they reside on the shared cq ring.
7189 */
7190static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007191 const sigset_t __user *sig, size_t sigsz,
7192 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007193{
Jens Axboebda52162019-09-24 13:47:15 -06007194 struct io_wait_queue iowq = {
7195 .wq = {
7196 .private = current,
7197 .func = io_wake_function,
7198 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7199 },
7200 .ctx = ctx,
7201 .to_wait = min_events,
7202 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007203 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007204 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7205 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206
Jens Axboeb41e9852020-02-17 09:52:41 -07007207 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007208 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7209 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007210 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007211 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007212 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007213 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007214
7215 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007216#ifdef CONFIG_COMPAT
7217 if (in_compat_syscall())
7218 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007219 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007220 else
7221#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007222 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007223
Jens Axboe2b188cc2019-01-07 10:46:33 -07007224 if (ret)
7225 return ret;
7226 }
7227
Hao Xuc73ebb62020-11-03 10:54:37 +08007228 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007229 struct timespec64 ts;
7230
Hao Xuc73ebb62020-11-03 10:54:37 +08007231 if (get_timespec64(&ts, uts))
7232 return -EFAULT;
7233 timeout = timespec64_to_jiffies(&ts);
7234 }
7235
Jens Axboebda52162019-09-24 13:47:15 -06007236 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007237 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007238 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007239 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007240 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7241 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007242 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7243 finish_wait(&ctx->wait, &iowq.wq);
7244 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007245
Jens Axboeb7db41c2020-07-04 08:55:50 -06007246 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007247
Hristo Venev75b28af2019-08-26 17:23:46 +00007248 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249}
7250
Jens Axboe6b063142019-01-10 22:13:58 -07007251static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7252{
7253#if defined(CONFIG_UNIX)
7254 if (ctx->ring_sock) {
7255 struct sock *sock = ctx->ring_sock->sk;
7256 struct sk_buff *skb;
7257
7258 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7259 kfree_skb(skb);
7260 }
7261#else
7262 int i;
7263
Jens Axboe65e19f52019-10-26 07:20:21 -06007264 for (i = 0; i < ctx->nr_user_files; i++) {
7265 struct file *file;
7266
7267 file = io_file_from_index(ctx, i);
7268 if (file)
7269 fput(file);
7270 }
Jens Axboe6b063142019-01-10 22:13:58 -07007271#endif
7272}
7273
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007274static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007275{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007276 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007277
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007278 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007279 complete(&data->done);
7280}
7281
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007282static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7283{
7284 spin_lock_bh(&ctx->rsrc_ref_lock);
7285}
7286
7287static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7288{
7289 spin_unlock_bh(&ctx->rsrc_ref_lock);
7290}
7291
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007292static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7293 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007294 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007295{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007296 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007297 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007298 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007299 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007300 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007301}
7302
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007303static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7304 struct io_ring_ctx *ctx,
7305 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007306{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007307 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007308 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007309
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007310 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007311 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007312 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007313 if (ref_node)
7314 percpu_ref_kill(&ref_node->refs);
7315
7316 percpu_ref_kill(&data->refs);
7317
7318 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007319 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007320 do {
7321 ret = wait_for_completion_interruptible(&data->done);
7322 if (!ret)
7323 break;
7324 ret = io_run_task_work_sig();
7325 if (ret < 0) {
7326 percpu_ref_resurrect(&data->refs);
7327 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007328 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007329 return ret;
7330 }
7331 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007332
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007333 destroy_fixed_rsrc_ref_node(backup_node);
7334 return 0;
7335}
7336
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007337static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7338{
7339 struct fixed_rsrc_data *data;
7340
7341 data = kzalloc(sizeof(*data), GFP_KERNEL);
7342 if (!data)
7343 return NULL;
7344
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007345 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007346 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7347 kfree(data);
7348 return NULL;
7349 }
7350 data->ctx = ctx;
7351 init_completion(&data->done);
7352 return data;
7353}
7354
7355static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7356{
7357 percpu_ref_exit(&data->refs);
7358 kfree(data->table);
7359 kfree(data);
7360}
7361
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007362static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7363{
7364 struct fixed_rsrc_data *data = ctx->file_data;
7365 struct fixed_rsrc_ref_node *backup_node;
7366 unsigned nr_tables, i;
7367 int ret;
7368
7369 if (!data)
7370 return -ENXIO;
7371 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7372 if (!backup_node)
7373 return -ENOMEM;
7374 init_fixed_file_ref_node(ctx, backup_node);
7375
7376 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7377 if (ret)
7378 return ret;
7379
Jens Axboe6b063142019-01-10 22:13:58 -07007380 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007381 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7382 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007383 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007384 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007385 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007386 ctx->nr_user_files = 0;
7387 return 0;
7388}
7389
Jens Axboe534ca6d2020-09-02 13:52:19 -06007390static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007391{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007392 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007393 /*
7394 * The park is a bit of a work-around, without it we get
7395 * warning spews on shutdown with SQPOLL set and affinity
7396 * set to a single CPU.
7397 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007398 if (sqd->thread) {
7399 kthread_park(sqd->thread);
7400 kthread_stop(sqd->thread);
7401 }
7402
7403 kfree(sqd);
7404 }
7405}
7406
Jens Axboeaa061652020-09-02 14:50:27 -06007407static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7408{
7409 struct io_ring_ctx *ctx_attach;
7410 struct io_sq_data *sqd;
7411 struct fd f;
7412
7413 f = fdget(p->wq_fd);
7414 if (!f.file)
7415 return ERR_PTR(-ENXIO);
7416 if (f.file->f_op != &io_uring_fops) {
7417 fdput(f);
7418 return ERR_PTR(-EINVAL);
7419 }
7420
7421 ctx_attach = f.file->private_data;
7422 sqd = ctx_attach->sq_data;
7423 if (!sqd) {
7424 fdput(f);
7425 return ERR_PTR(-EINVAL);
7426 }
7427
7428 refcount_inc(&sqd->refs);
7429 fdput(f);
7430 return sqd;
7431}
7432
Jens Axboe534ca6d2020-09-02 13:52:19 -06007433static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7434{
7435 struct io_sq_data *sqd;
7436
Jens Axboeaa061652020-09-02 14:50:27 -06007437 if (p->flags & IORING_SETUP_ATTACH_WQ)
7438 return io_attach_sq_data(p);
7439
Jens Axboe534ca6d2020-09-02 13:52:19 -06007440 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7441 if (!sqd)
7442 return ERR_PTR(-ENOMEM);
7443
7444 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007445 INIT_LIST_HEAD(&sqd->ctx_list);
7446 INIT_LIST_HEAD(&sqd->ctx_new_list);
7447 mutex_init(&sqd->ctx_lock);
7448 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007449 init_waitqueue_head(&sqd->wait);
7450 return sqd;
7451}
7452
Jens Axboe69fb2132020-09-14 11:16:23 -06007453static void io_sq_thread_unpark(struct io_sq_data *sqd)
7454 __releases(&sqd->lock)
7455{
7456 if (!sqd->thread)
7457 return;
7458 kthread_unpark(sqd->thread);
7459 mutex_unlock(&sqd->lock);
7460}
7461
7462static void io_sq_thread_park(struct io_sq_data *sqd)
7463 __acquires(&sqd->lock)
7464{
7465 if (!sqd->thread)
7466 return;
7467 mutex_lock(&sqd->lock);
7468 kthread_park(sqd->thread);
7469}
7470
Jens Axboe534ca6d2020-09-02 13:52:19 -06007471static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7472{
7473 struct io_sq_data *sqd = ctx->sq_data;
7474
7475 if (sqd) {
7476 if (sqd->thread) {
7477 /*
7478 * We may arrive here from the error branch in
7479 * io_sq_offload_create() where the kthread is created
7480 * without being waked up, thus wake it up now to make
7481 * sure the wait will complete.
7482 */
7483 wake_up_process(sqd->thread);
7484 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007485
7486 io_sq_thread_park(sqd);
7487 }
7488
7489 mutex_lock(&sqd->ctx_lock);
7490 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007491 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007492 mutex_unlock(&sqd->ctx_lock);
7493
Xiaoguang Wang08369242020-11-03 14:15:59 +08007494 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007495 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007496
7497 io_put_sq_data(sqd);
7498 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007499 }
7500}
7501
Jens Axboe6b063142019-01-10 22:13:58 -07007502static void io_finish_async(struct io_ring_ctx *ctx)
7503{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007504 io_sq_thread_stop(ctx);
7505
Jens Axboe561fb042019-10-24 07:25:42 -06007506 if (ctx->io_wq) {
7507 io_wq_destroy(ctx->io_wq);
7508 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007509 }
7510}
7511
7512#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007513/*
7514 * Ensure the UNIX gc is aware of our file set, so we are certain that
7515 * the io_uring can be safely unregistered on process exit, even if we have
7516 * loops in the file referencing.
7517 */
7518static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7519{
7520 struct sock *sk = ctx->ring_sock->sk;
7521 struct scm_fp_list *fpl;
7522 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007523 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007524
Jens Axboe6b063142019-01-10 22:13:58 -07007525 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7526 if (!fpl)
7527 return -ENOMEM;
7528
7529 skb = alloc_skb(0, GFP_KERNEL);
7530 if (!skb) {
7531 kfree(fpl);
7532 return -ENOMEM;
7533 }
7534
7535 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007536
Jens Axboe08a45172019-10-03 08:11:03 -06007537 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007538 fpl->user = get_uid(ctx->user);
7539 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007540 struct file *file = io_file_from_index(ctx, i + offset);
7541
7542 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007543 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007544 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007545 unix_inflight(fpl->user, fpl->fp[nr_files]);
7546 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007547 }
7548
Jens Axboe08a45172019-10-03 08:11:03 -06007549 if (nr_files) {
7550 fpl->max = SCM_MAX_FD;
7551 fpl->count = nr_files;
7552 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007553 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007554 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7555 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007556
Jens Axboe08a45172019-10-03 08:11:03 -06007557 for (i = 0; i < nr_files; i++)
7558 fput(fpl->fp[i]);
7559 } else {
7560 kfree_skb(skb);
7561 kfree(fpl);
7562 }
Jens Axboe6b063142019-01-10 22:13:58 -07007563
7564 return 0;
7565}
7566
7567/*
7568 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7569 * causes regular reference counting to break down. We rely on the UNIX
7570 * garbage collection to take care of this problem for us.
7571 */
7572static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7573{
7574 unsigned left, total;
7575 int ret = 0;
7576
7577 total = 0;
7578 left = ctx->nr_user_files;
7579 while (left) {
7580 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007581
7582 ret = __io_sqe_files_scm(ctx, this_files, total);
7583 if (ret)
7584 break;
7585 left -= this_files;
7586 total += this_files;
7587 }
7588
7589 if (!ret)
7590 return 0;
7591
7592 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007593 struct file *file = io_file_from_index(ctx, total);
7594
7595 if (file)
7596 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007597 total++;
7598 }
7599
7600 return ret;
7601}
7602#else
7603static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7604{
7605 return 0;
7606}
7607#endif
7608
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007609static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007610 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007611{
7612 int i;
7613
7614 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007615 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007616 unsigned this_files;
7617
7618 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7619 table->files = kcalloc(this_files, sizeof(struct file *),
7620 GFP_KERNEL);
7621 if (!table->files)
7622 break;
7623 nr_files -= this_files;
7624 }
7625
7626 if (i == nr_tables)
7627 return 0;
7628
7629 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007630 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007631 kfree(table->files);
7632 }
7633 return 1;
7634}
7635
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007636static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007637{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007638 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007639#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007640 struct sock *sock = ctx->ring_sock->sk;
7641 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7642 struct sk_buff *skb;
7643 int i;
7644
7645 __skb_queue_head_init(&list);
7646
7647 /*
7648 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7649 * remove this entry and rearrange the file array.
7650 */
7651 skb = skb_dequeue(head);
7652 while (skb) {
7653 struct scm_fp_list *fp;
7654
7655 fp = UNIXCB(skb).fp;
7656 for (i = 0; i < fp->count; i++) {
7657 int left;
7658
7659 if (fp->fp[i] != file)
7660 continue;
7661
7662 unix_notinflight(fp->user, fp->fp[i]);
7663 left = fp->count - 1 - i;
7664 if (left) {
7665 memmove(&fp->fp[i], &fp->fp[i + 1],
7666 left * sizeof(struct file *));
7667 }
7668 fp->count--;
7669 if (!fp->count) {
7670 kfree_skb(skb);
7671 skb = NULL;
7672 } else {
7673 __skb_queue_tail(&list, skb);
7674 }
7675 fput(file);
7676 file = NULL;
7677 break;
7678 }
7679
7680 if (!file)
7681 break;
7682
7683 __skb_queue_tail(&list, skb);
7684
7685 skb = skb_dequeue(head);
7686 }
7687
7688 if (skb_peek(&list)) {
7689 spin_lock_irq(&head->lock);
7690 while ((skb = __skb_dequeue(&list)) != NULL)
7691 __skb_queue_tail(head, skb);
7692 spin_unlock_irq(&head->lock);
7693 }
7694#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007695 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007696#endif
7697}
7698
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007699static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007700{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007701 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7702 struct io_ring_ctx *ctx = rsrc_data->ctx;
7703 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007704
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007705 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7706 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007707 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007708 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007709 }
7710
Xiaoguang Wang05589552020-03-31 14:05:18 +08007711 percpu_ref_exit(&ref_node->refs);
7712 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007713 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007714}
7715
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007716static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007717{
7718 struct io_ring_ctx *ctx;
7719 struct llist_node *node;
7720
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007721 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7722 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007723
7724 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007725 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007726 struct llist_node *next = node->next;
7727
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007728 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7729 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007730 node = next;
7731 }
7732}
7733
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007734static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7735 unsigned i)
7736{
7737 struct fixed_rsrc_table *table;
7738
7739 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7740 return &table->files[i & IORING_FILE_TABLE_MASK];
7741}
7742
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007743static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007744{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007745 struct fixed_rsrc_ref_node *ref_node;
7746 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007747 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007748 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007749 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007750
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007751 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7752 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007753 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007754
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007755 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007756 ref_node->done = true;
7757
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007758 while (!list_empty(&ctx->rsrc_ref_list)) {
7759 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007760 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007761 /* recycle ref nodes in order */
7762 if (!ref_node->done)
7763 break;
7764 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007765 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007766 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007767 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007768
7769 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007770 delay = 0;
7771
Jens Axboe4a38aed22020-05-14 17:21:15 -06007772 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007773 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007774 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007775 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007776}
7777
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007778static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007779 struct io_ring_ctx *ctx)
7780{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007781 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007782
7783 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7784 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007785 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007786
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007787 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007788 0, GFP_KERNEL)) {
7789 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007790 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007791 }
7792 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007793 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007794 ref_node->done = false;
7795 return ref_node;
7796}
7797
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007798static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7799 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007800{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007801 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007802 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007803}
7804
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007805static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007806{
7807 percpu_ref_exit(&ref_node->refs);
7808 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007809}
7810
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007811
Jens Axboe05f3fb32019-12-09 11:22:50 -07007812static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7813 unsigned nr_args)
7814{
7815 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007816 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007817 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007818 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007819 struct fixed_rsrc_ref_node *ref_node;
7820 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821
7822 if (ctx->file_data)
7823 return -EBUSY;
7824 if (!nr_args)
7825 return -EINVAL;
7826 if (nr_args > IORING_MAX_FIXED_FILES)
7827 return -EMFILE;
7828
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007829 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007830 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007831 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007832 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007833
7834 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007835 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007836 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007837 if (!file_data->table)
7838 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007839
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007840 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007841 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007842
7843 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007844 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7845 ret = -EFAULT;
7846 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007847 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007848 /* allow sparse sets */
7849 if (fd == -1)
7850 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007851
Jens Axboe05f3fb32019-12-09 11:22:50 -07007852 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007853 ret = -EBADF;
7854 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007855 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007856
7857 /*
7858 * Don't allow io_uring instances to be registered. If UNIX
7859 * isn't enabled, then this causes a reference cycle and this
7860 * instance can never get freed. If UNIX is enabled we'll
7861 * handle it just fine, but there's still no point in allowing
7862 * a ring fd as it doesn't support regular read/write anyway.
7863 */
7864 if (file->f_op == &io_uring_fops) {
7865 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007866 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007867 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007868 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007869 }
7870
Jens Axboe05f3fb32019-12-09 11:22:50 -07007871 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007872 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007873 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007874 return ret;
7875 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007876
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007877 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007878 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007879 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007880 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007881 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007882 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007883
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007884 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007885 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007886out_fput:
7887 for (i = 0; i < ctx->nr_user_files; i++) {
7888 file = io_file_from_index(ctx, i);
7889 if (file)
7890 fput(file);
7891 }
7892 for (i = 0; i < nr_tables; i++)
7893 kfree(file_data->table[i].files);
7894 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007895out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007896 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007897 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007898 return ret;
7899}
7900
Jens Axboec3a31e62019-10-03 13:59:56 -06007901static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7902 int index)
7903{
7904#if defined(CONFIG_UNIX)
7905 struct sock *sock = ctx->ring_sock->sk;
7906 struct sk_buff_head *head = &sock->sk_receive_queue;
7907 struct sk_buff *skb;
7908
7909 /*
7910 * See if we can merge this file into an existing skb SCM_RIGHTS
7911 * file set. If there's no room, fall back to allocating a new skb
7912 * and filling it in.
7913 */
7914 spin_lock_irq(&head->lock);
7915 skb = skb_peek(head);
7916 if (skb) {
7917 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7918
7919 if (fpl->count < SCM_MAX_FD) {
7920 __skb_unlink(skb, head);
7921 spin_unlock_irq(&head->lock);
7922 fpl->fp[fpl->count] = get_file(file);
7923 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7924 fpl->count++;
7925 spin_lock_irq(&head->lock);
7926 __skb_queue_head(head, skb);
7927 } else {
7928 skb = NULL;
7929 }
7930 }
7931 spin_unlock_irq(&head->lock);
7932
7933 if (skb) {
7934 fput(file);
7935 return 0;
7936 }
7937
7938 return __io_sqe_files_scm(ctx, 1, index);
7939#else
7940 return 0;
7941#endif
7942}
7943
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007944static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007945{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007946 struct io_rsrc_put *prsrc;
7947 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007948
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007949 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7950 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007951 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007952
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007953 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007954 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007955
Hillf Dantona5318d32020-03-23 17:47:15 +08007956 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007957}
7958
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007959static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7960 struct file *file)
7961{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007962 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007963}
7964
Jens Axboe05f3fb32019-12-09 11:22:50 -07007965static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007966 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007967 unsigned nr_args)
7968{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007969 struct fixed_rsrc_data *data = ctx->file_data;
7970 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007971 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007972 __s32 __user *fds;
7973 int fd, i, err;
7974 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007975 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007976
Jens Axboe05f3fb32019-12-09 11:22:50 -07007977 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007978 return -EOVERFLOW;
7979 if (done > ctx->nr_user_files)
7980 return -EINVAL;
7981
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007982 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007983 if (!ref_node)
7984 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007985 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007986
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007987 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007988 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007989 err = 0;
7990 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7991 err = -EFAULT;
7992 break;
7993 }
noah4e0377a2021-01-26 15:23:28 -05007994 if (fd == IORING_REGISTER_FILES_SKIP)
7995 continue;
7996
Pavel Begunkov67973b92021-01-26 13:51:09 +00007997 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007998 file_slot = io_fixed_file_slot(ctx->file_data, i);
7999
8000 if (*file_slot) {
8001 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008002 if (err)
8003 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008004 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008005 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008006 }
8007 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008008 file = fget(fd);
8009 if (!file) {
8010 err = -EBADF;
8011 break;
8012 }
8013 /*
8014 * Don't allow io_uring instances to be registered. If
8015 * UNIX isn't enabled, then this causes a reference
8016 * cycle and this instance can never get freed. If UNIX
8017 * is enabled we'll handle it just fine, but there's
8018 * still no point in allowing a ring fd as it doesn't
8019 * support regular read/write anyway.
8020 */
8021 if (file->f_op == &io_uring_fops) {
8022 fput(file);
8023 err = -EBADF;
8024 break;
8025 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008026 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008027 if (err) {
8028 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008029 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008030 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008031 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008032 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008033 }
8034
Xiaoguang Wang05589552020-03-31 14:05:18 +08008035 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008036 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008037 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008038 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008039 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008040
8041 return done ? done : err;
8042}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008043
Jens Axboe05f3fb32019-12-09 11:22:50 -07008044static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8045 unsigned nr_args)
8046{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008047 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008048
8049 if (!ctx->file_data)
8050 return -ENXIO;
8051 if (!nr_args)
8052 return -EINVAL;
8053 if (copy_from_user(&up, arg, sizeof(up)))
8054 return -EFAULT;
8055 if (up.resv)
8056 return -EINVAL;
8057
8058 return __io_sqe_files_update(ctx, &up, nr_args);
8059}
Jens Axboec3a31e62019-10-03 13:59:56 -06008060
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008061static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008062{
8063 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8064
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008065 req = io_put_req_find_next(req);
8066 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008067}
8068
Pavel Begunkov24369c22020-01-28 03:15:48 +03008069static int io_init_wq_offload(struct io_ring_ctx *ctx,
8070 struct io_uring_params *p)
8071{
8072 struct io_wq_data data;
8073 struct fd f;
8074 struct io_ring_ctx *ctx_attach;
8075 unsigned int concurrency;
8076 int ret = 0;
8077
8078 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008079 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008080 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008081
8082 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8083 /* Do QD, or 4 * CPUS, whatever is smallest */
8084 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8085
8086 ctx->io_wq = io_wq_create(concurrency, &data);
8087 if (IS_ERR(ctx->io_wq)) {
8088 ret = PTR_ERR(ctx->io_wq);
8089 ctx->io_wq = NULL;
8090 }
8091 return ret;
8092 }
8093
8094 f = fdget(p->wq_fd);
8095 if (!f.file)
8096 return -EBADF;
8097
8098 if (f.file->f_op != &io_uring_fops) {
8099 ret = -EINVAL;
8100 goto out_fput;
8101 }
8102
8103 ctx_attach = f.file->private_data;
8104 /* @io_wq is protected by holding the fd */
8105 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8106 ret = -EINVAL;
8107 goto out_fput;
8108 }
8109
8110 ctx->io_wq = ctx_attach->io_wq;
8111out_fput:
8112 fdput(f);
8113 return ret;
8114}
8115
Jens Axboe0f212202020-09-13 13:09:39 -06008116static int io_uring_alloc_task_context(struct task_struct *task)
8117{
8118 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008119 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008120
8121 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8122 if (unlikely(!tctx))
8123 return -ENOMEM;
8124
Jens Axboed8a6df12020-10-15 16:24:45 -06008125 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8126 if (unlikely(ret)) {
8127 kfree(tctx);
8128 return ret;
8129 }
8130
Jens Axboe0f212202020-09-13 13:09:39 -06008131 xa_init(&tctx->xa);
8132 init_waitqueue_head(&tctx->wait);
8133 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008134 atomic_set(&tctx->in_idle, 0);
8135 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008136 io_init_identity(&tctx->__identity);
8137 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008138 task->io_uring = tctx;
8139 return 0;
8140}
8141
8142void __io_uring_free(struct task_struct *tsk)
8143{
8144 struct io_uring_task *tctx = tsk->io_uring;
8145
8146 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008147 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8148 if (tctx->identity != &tctx->__identity)
8149 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008150 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008151 kfree(tctx);
8152 tsk->io_uring = NULL;
8153}
8154
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008155static int io_sq_offload_create(struct io_ring_ctx *ctx,
8156 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008157{
8158 int ret;
8159
Jens Axboe6c271ce2019-01-10 11:22:30 -07008160 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008161 struct io_sq_data *sqd;
8162
Jens Axboe3ec482d2019-04-08 10:51:01 -06008163 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008164 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008165 goto err;
8166
Jens Axboe534ca6d2020-09-02 13:52:19 -06008167 sqd = io_get_sq_data(p);
8168 if (IS_ERR(sqd)) {
8169 ret = PTR_ERR(sqd);
8170 goto err;
8171 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008172
Jens Axboe534ca6d2020-09-02 13:52:19 -06008173 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008174 io_sq_thread_park(sqd);
8175 mutex_lock(&sqd->ctx_lock);
8176 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8177 mutex_unlock(&sqd->ctx_lock);
8178 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008179
Jens Axboe917257d2019-04-13 09:28:55 -06008180 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8181 if (!ctx->sq_thread_idle)
8182 ctx->sq_thread_idle = HZ;
8183
Jens Axboeaa061652020-09-02 14:50:27 -06008184 if (sqd->thread)
8185 goto done;
8186
Jens Axboe6c271ce2019-01-10 11:22:30 -07008187 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008188 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008189
Jens Axboe917257d2019-04-13 09:28:55 -06008190 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008191 if (cpu >= nr_cpu_ids)
8192 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008193 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008194 goto err;
8195
Jens Axboe69fb2132020-09-14 11:16:23 -06008196 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008197 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008198 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008199 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008200 "io_uring-sq");
8201 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008202 if (IS_ERR(sqd->thread)) {
8203 ret = PTR_ERR(sqd->thread);
8204 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008205 goto err;
8206 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008207 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008208 if (ret)
8209 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008210 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8211 /* Can't have SQ_AFF without SQPOLL */
8212 ret = -EINVAL;
8213 goto err;
8214 }
8215
Jens Axboeaa061652020-09-02 14:50:27 -06008216done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008217 ret = io_init_wq_offload(ctx, p);
8218 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220
8221 return 0;
8222err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008223 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008224 return ret;
8225}
8226
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008227static void io_sq_offload_start(struct io_ring_ctx *ctx)
8228{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008229 struct io_sq_data *sqd = ctx->sq_data;
8230
8231 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8232 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008233}
8234
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008235static inline void __io_unaccount_mem(struct user_struct *user,
8236 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008237{
8238 atomic_long_sub(nr_pages, &user->locked_vm);
8239}
8240
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008241static inline int __io_account_mem(struct user_struct *user,
8242 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008243{
8244 unsigned long page_limit, cur_pages, new_pages;
8245
8246 /* Don't allow more pages than we can safely lock */
8247 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8248
8249 do {
8250 cur_pages = atomic_long_read(&user->locked_vm);
8251 new_pages = cur_pages + nr_pages;
8252 if (new_pages > page_limit)
8253 return -ENOMEM;
8254 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8255 new_pages) != cur_pages);
8256
8257 return 0;
8258}
8259
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008260static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8261 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008262{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008263 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008264 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008265
Jens Axboe2aede0e2020-09-14 10:45:53 -06008266 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008267 if (acct == ACCT_LOCKED) {
8268 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008269 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008270 mmap_write_unlock(ctx->mm_account);
8271 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008272 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008273 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008274 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008275}
8276
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008277static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8278 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008279{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008280 int ret;
8281
8282 if (ctx->limit_mem) {
8283 ret = __io_account_mem(ctx->user, nr_pages);
8284 if (ret)
8285 return ret;
8286 }
8287
Jens Axboe2aede0e2020-09-14 10:45:53 -06008288 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008289 if (acct == ACCT_LOCKED) {
8290 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008291 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008292 mmap_write_unlock(ctx->mm_account);
8293 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008294 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008295 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008296 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008297
8298 return 0;
8299}
8300
Jens Axboe2b188cc2019-01-07 10:46:33 -07008301static void io_mem_free(void *ptr)
8302{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008303 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008304
Mark Rutland52e04ef2019-04-30 17:30:21 +01008305 if (!ptr)
8306 return;
8307
8308 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008309 if (put_page_testzero(page))
8310 free_compound_page(page);
8311}
8312
8313static void *io_mem_alloc(size_t size)
8314{
8315 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8316 __GFP_NORETRY;
8317
8318 return (void *) __get_free_pages(gfp_flags, get_order(size));
8319}
8320
Hristo Venev75b28af2019-08-26 17:23:46 +00008321static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8322 size_t *sq_offset)
8323{
8324 struct io_rings *rings;
8325 size_t off, sq_array_size;
8326
8327 off = struct_size(rings, cqes, cq_entries);
8328 if (off == SIZE_MAX)
8329 return SIZE_MAX;
8330
8331#ifdef CONFIG_SMP
8332 off = ALIGN(off, SMP_CACHE_BYTES);
8333 if (off == 0)
8334 return SIZE_MAX;
8335#endif
8336
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008337 if (sq_offset)
8338 *sq_offset = off;
8339
Hristo Venev75b28af2019-08-26 17:23:46 +00008340 sq_array_size = array_size(sizeof(u32), sq_entries);
8341 if (sq_array_size == SIZE_MAX)
8342 return SIZE_MAX;
8343
8344 if (check_add_overflow(off, sq_array_size, &off))
8345 return SIZE_MAX;
8346
Hristo Venev75b28af2019-08-26 17:23:46 +00008347 return off;
8348}
8349
Jens Axboe2b188cc2019-01-07 10:46:33 -07008350static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8351{
Hristo Venev75b28af2019-08-26 17:23:46 +00008352 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008353
Hristo Venev75b28af2019-08-26 17:23:46 +00008354 pages = (size_t)1 << get_order(
8355 rings_size(sq_entries, cq_entries, NULL));
8356 pages += (size_t)1 << get_order(
8357 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008358
Hristo Venev75b28af2019-08-26 17:23:46 +00008359 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008360}
8361
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008362static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008363{
8364 int i, j;
8365
8366 if (!ctx->user_bufs)
8367 return -ENXIO;
8368
8369 for (i = 0; i < ctx->nr_user_bufs; i++) {
8370 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8371
8372 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008373 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008374
Jens Axboede293932020-09-17 16:19:16 -06008375 if (imu->acct_pages)
8376 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008377 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008378 imu->nr_bvecs = 0;
8379 }
8380
8381 kfree(ctx->user_bufs);
8382 ctx->user_bufs = NULL;
8383 ctx->nr_user_bufs = 0;
8384 return 0;
8385}
8386
8387static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8388 void __user *arg, unsigned index)
8389{
8390 struct iovec __user *src;
8391
8392#ifdef CONFIG_COMPAT
8393 if (ctx->compat) {
8394 struct compat_iovec __user *ciovs;
8395 struct compat_iovec ciov;
8396
8397 ciovs = (struct compat_iovec __user *) arg;
8398 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8399 return -EFAULT;
8400
Jens Axboed55e5f52019-12-11 16:12:15 -07008401 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008402 dst->iov_len = ciov.iov_len;
8403 return 0;
8404 }
8405#endif
8406 src = (struct iovec __user *) arg;
8407 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8408 return -EFAULT;
8409 return 0;
8410}
8411
Jens Axboede293932020-09-17 16:19:16 -06008412/*
8413 * Not super efficient, but this is just a registration time. And we do cache
8414 * the last compound head, so generally we'll only do a full search if we don't
8415 * match that one.
8416 *
8417 * We check if the given compound head page has already been accounted, to
8418 * avoid double accounting it. This allows us to account the full size of the
8419 * page, not just the constituent pages of a huge page.
8420 */
8421static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8422 int nr_pages, struct page *hpage)
8423{
8424 int i, j;
8425
8426 /* check current page array */
8427 for (i = 0; i < nr_pages; i++) {
8428 if (!PageCompound(pages[i]))
8429 continue;
8430 if (compound_head(pages[i]) == hpage)
8431 return true;
8432 }
8433
8434 /* check previously registered pages */
8435 for (i = 0; i < ctx->nr_user_bufs; i++) {
8436 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8437
8438 for (j = 0; j < imu->nr_bvecs; j++) {
8439 if (!PageCompound(imu->bvec[j].bv_page))
8440 continue;
8441 if (compound_head(imu->bvec[j].bv_page) == hpage)
8442 return true;
8443 }
8444 }
8445
8446 return false;
8447}
8448
8449static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8450 int nr_pages, struct io_mapped_ubuf *imu,
8451 struct page **last_hpage)
8452{
8453 int i, ret;
8454
8455 for (i = 0; i < nr_pages; i++) {
8456 if (!PageCompound(pages[i])) {
8457 imu->acct_pages++;
8458 } else {
8459 struct page *hpage;
8460
8461 hpage = compound_head(pages[i]);
8462 if (hpage == *last_hpage)
8463 continue;
8464 *last_hpage = hpage;
8465 if (headpage_already_acct(ctx, pages, i, hpage))
8466 continue;
8467 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8468 }
8469 }
8470
8471 if (!imu->acct_pages)
8472 return 0;
8473
8474 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8475 if (ret)
8476 imu->acct_pages = 0;
8477 return ret;
8478}
8479
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008480static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8481 struct io_mapped_ubuf *imu,
8482 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008483{
8484 struct vm_area_struct **vmas = NULL;
8485 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008486 unsigned long off, start, end, ubuf;
8487 size_t size;
8488 int ret, pret, nr_pages, i;
8489
8490 ubuf = (unsigned long) iov->iov_base;
8491 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8492 start = ubuf >> PAGE_SHIFT;
8493 nr_pages = end - start;
8494
8495 ret = -ENOMEM;
8496
8497 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8498 if (!pages)
8499 goto done;
8500
8501 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8502 GFP_KERNEL);
8503 if (!vmas)
8504 goto done;
8505
8506 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8507 GFP_KERNEL);
8508 if (!imu->bvec)
8509 goto done;
8510
8511 ret = 0;
8512 mmap_read_lock(current->mm);
8513 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8514 pages, vmas);
8515 if (pret == nr_pages) {
8516 /* don't support file backed memory */
8517 for (i = 0; i < nr_pages; i++) {
8518 struct vm_area_struct *vma = vmas[i];
8519
8520 if (vma->vm_file &&
8521 !is_file_hugepages(vma->vm_file)) {
8522 ret = -EOPNOTSUPP;
8523 break;
8524 }
8525 }
8526 } else {
8527 ret = pret < 0 ? pret : -EFAULT;
8528 }
8529 mmap_read_unlock(current->mm);
8530 if (ret) {
8531 /*
8532 * if we did partial map, or found file backed vmas,
8533 * release any pages we did get
8534 */
8535 if (pret > 0)
8536 unpin_user_pages(pages, pret);
8537 kvfree(imu->bvec);
8538 goto done;
8539 }
8540
8541 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8542 if (ret) {
8543 unpin_user_pages(pages, pret);
8544 kvfree(imu->bvec);
8545 goto done;
8546 }
8547
8548 off = ubuf & ~PAGE_MASK;
8549 size = iov->iov_len;
8550 for (i = 0; i < nr_pages; i++) {
8551 size_t vec_len;
8552
8553 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8554 imu->bvec[i].bv_page = pages[i];
8555 imu->bvec[i].bv_len = vec_len;
8556 imu->bvec[i].bv_offset = off;
8557 off = 0;
8558 size -= vec_len;
8559 }
8560 /* store original address for later verification */
8561 imu->ubuf = ubuf;
8562 imu->len = iov->iov_len;
8563 imu->nr_bvecs = nr_pages;
8564 ret = 0;
8565done:
8566 kvfree(pages);
8567 kvfree(vmas);
8568 return ret;
8569}
8570
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008571static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008572{
Jens Axboeedafcce2019-01-09 09:16:05 -07008573 if (ctx->user_bufs)
8574 return -EBUSY;
8575 if (!nr_args || nr_args > UIO_MAXIOV)
8576 return -EINVAL;
8577
8578 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8579 GFP_KERNEL);
8580 if (!ctx->user_bufs)
8581 return -ENOMEM;
8582
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008583 return 0;
8584}
8585
8586static int io_buffer_validate(struct iovec *iov)
8587{
8588 /*
8589 * Don't impose further limits on the size and buffer
8590 * constraints here, we'll -EINVAL later when IO is
8591 * submitted if they are wrong.
8592 */
8593 if (!iov->iov_base || !iov->iov_len)
8594 return -EFAULT;
8595
8596 /* arbitrary limit, but we need something */
8597 if (iov->iov_len > SZ_1G)
8598 return -EFAULT;
8599
8600 return 0;
8601}
8602
8603static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8604 unsigned int nr_args)
8605{
8606 int i, ret;
8607 struct iovec iov;
8608 struct page *last_hpage = NULL;
8609
8610 ret = io_buffers_map_alloc(ctx, nr_args);
8611 if (ret)
8612 return ret;
8613
Jens Axboeedafcce2019-01-09 09:16:05 -07008614 for (i = 0; i < nr_args; i++) {
8615 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008616
8617 ret = io_copy_iov(ctx, &iov, arg, i);
8618 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008619 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008620
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008621 ret = io_buffer_validate(&iov);
8622 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008623 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008624
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008625 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8626 if (ret)
8627 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008628
8629 ctx->nr_user_bufs++;
8630 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008631
8632 if (ret)
8633 io_sqe_buffers_unregister(ctx);
8634
Jens Axboeedafcce2019-01-09 09:16:05 -07008635 return ret;
8636}
8637
Jens Axboe9b402842019-04-11 11:45:41 -06008638static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8639{
8640 __s32 __user *fds = arg;
8641 int fd;
8642
8643 if (ctx->cq_ev_fd)
8644 return -EBUSY;
8645
8646 if (copy_from_user(&fd, fds, sizeof(*fds)))
8647 return -EFAULT;
8648
8649 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8650 if (IS_ERR(ctx->cq_ev_fd)) {
8651 int ret = PTR_ERR(ctx->cq_ev_fd);
8652 ctx->cq_ev_fd = NULL;
8653 return ret;
8654 }
8655
8656 return 0;
8657}
8658
8659static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8660{
8661 if (ctx->cq_ev_fd) {
8662 eventfd_ctx_put(ctx->cq_ev_fd);
8663 ctx->cq_ev_fd = NULL;
8664 return 0;
8665 }
8666
8667 return -ENXIO;
8668}
8669
Jens Axboe5a2e7452020-02-23 16:23:11 -07008670static int __io_destroy_buffers(int id, void *p, void *data)
8671{
8672 struct io_ring_ctx *ctx = data;
8673 struct io_buffer *buf = p;
8674
Jens Axboe067524e2020-03-02 16:32:28 -07008675 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008676 return 0;
8677}
8678
8679static void io_destroy_buffers(struct io_ring_ctx *ctx)
8680{
8681 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8682 idr_destroy(&ctx->io_buffer_idr);
8683}
8684
Jens Axboe2b188cc2019-01-07 10:46:33 -07008685static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8686{
Jens Axboe6b063142019-01-10 22:13:58 -07008687 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008688 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008689
8690 if (ctx->sqo_task) {
8691 put_task_struct(ctx->sqo_task);
8692 ctx->sqo_task = NULL;
8693 mmdrop(ctx->mm_account);
8694 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008695 }
Jens Axboedef596e2019-01-09 08:59:42 -07008696
Dennis Zhou91d8f512020-09-16 13:41:05 -07008697#ifdef CONFIG_BLK_CGROUP
8698 if (ctx->sqo_blkcg_css)
8699 css_put(ctx->sqo_blkcg_css);
8700#endif
8701
Jens Axboe6b063142019-01-10 22:13:58 -07008702 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008703 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008704 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008705 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008706
Jens Axboe2b188cc2019-01-07 10:46:33 -07008707#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008708 if (ctx->ring_sock) {
8709 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008710 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008711 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008712#endif
8713
Hristo Venev75b28af2019-08-26 17:23:46 +00008714 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008715 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008716
8717 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008718 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008719 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008720 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008721 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008722 kfree(ctx);
8723}
8724
8725static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8726{
8727 struct io_ring_ctx *ctx = file->private_data;
8728 __poll_t mask = 0;
8729
8730 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008731 /*
8732 * synchronizes with barrier from wq_has_sleeper call in
8733 * io_commit_cqring
8734 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008735 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008736 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008737 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008738 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8739 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008740 mask |= EPOLLIN | EPOLLRDNORM;
8741
8742 return mask;
8743}
8744
8745static int io_uring_fasync(int fd, struct file *file, int on)
8746{
8747 struct io_ring_ctx *ctx = file->private_data;
8748
8749 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8750}
8751
Yejune Deng0bead8c2020-12-24 11:02:20 +08008752static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008753{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008754 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008755
Jens Axboe1e6fa522020-10-15 08:46:24 -06008756 iod = idr_remove(&ctx->personality_idr, id);
8757 if (iod) {
8758 put_cred(iod->creds);
8759 if (refcount_dec_and_test(&iod->count))
8760 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008761 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008762 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008763
8764 return -EINVAL;
8765}
8766
8767static int io_remove_personalities(int id, void *p, void *data)
8768{
8769 struct io_ring_ctx *ctx = data;
8770
8771 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008772 return 0;
8773}
8774
Jens Axboe85faa7b2020-04-09 18:14:00 -06008775static void io_ring_exit_work(struct work_struct *work)
8776{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008777 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8778 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008779
Jens Axboe56952e92020-06-17 15:00:04 -06008780 /*
8781 * If we're doing polled IO and end up having requests being
8782 * submitted async (out-of-line), then completions can come in while
8783 * we're waiting for refs to drop. We need to reap these manually,
8784 * as nobody else will be looking for them.
8785 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008786 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008787 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008788 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008789 io_ring_ctx_free(ctx);
8790}
8791
Jens Axboe00c18642020-12-20 10:45:02 -07008792static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8793{
8794 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8795
8796 return req->ctx == data;
8797}
8798
Jens Axboe2b188cc2019-01-07 10:46:33 -07008799static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8800{
8801 mutex_lock(&ctx->uring_lock);
8802 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008803
8804 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8805 ctx->sqo_dead = 1;
8806
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008807 /* if force is set, the ring is going away. always drop after that */
8808 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008809 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008810 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008811 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008812 mutex_unlock(&ctx->uring_lock);
8813
Pavel Begunkov6b819282020-11-06 13:00:25 +00008814 io_kill_timeouts(ctx, NULL, NULL);
8815 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008816
8817 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008818 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008819
Jens Axboe15dff282019-11-13 09:09:23 -07008820 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008821 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008822
8823 /*
8824 * Do this upfront, so we won't have a grace period where the ring
8825 * is closed but resources aren't reaped yet. This can cause
8826 * spurious failure in setting up a new ring.
8827 */
Jens Axboe760618f2020-07-24 12:53:31 -06008828 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8829 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008830
Jens Axboe85faa7b2020-04-09 18:14:00 -06008831 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008832 /*
8833 * Use system_unbound_wq to avoid spawning tons of event kworkers
8834 * if we're exiting a ton of rings at the same time. It just adds
8835 * noise and overhead, there's no discernable change in runtime
8836 * over using system_wq.
8837 */
8838 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008839}
8840
8841static int io_uring_release(struct inode *inode, struct file *file)
8842{
8843 struct io_ring_ctx *ctx = file->private_data;
8844
8845 file->private_data = NULL;
8846 io_ring_ctx_wait_and_kill(ctx);
8847 return 0;
8848}
8849
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008850struct io_task_cancel {
8851 struct task_struct *task;
8852 struct files_struct *files;
8853};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008854
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008855static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008856{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008857 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008858 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008859 bool ret;
8860
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008861 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008862 unsigned long flags;
8863 struct io_ring_ctx *ctx = req->ctx;
8864
8865 /* protect against races with linked timeouts */
8866 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008867 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008868 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8869 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008870 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008871 }
8872 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008873}
8874
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008875static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008876 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008877 struct files_struct *files)
8878{
8879 struct io_defer_entry *de = NULL;
8880 LIST_HEAD(list);
8881
8882 spin_lock_irq(&ctx->completion_lock);
8883 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008884 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008885 list_cut_position(&list, &ctx->defer_list, &de->list);
8886 break;
8887 }
8888 }
8889 spin_unlock_irq(&ctx->completion_lock);
8890
8891 while (!list_empty(&list)) {
8892 de = list_first_entry(&list, struct io_defer_entry, list);
8893 list_del_init(&de->list);
8894 req_set_fail_links(de->req);
8895 io_put_req(de->req);
8896 io_req_complete(de->req, -ECANCELED);
8897 kfree(de);
8898 }
8899}
8900
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008901static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8902 struct task_struct *task,
8903 struct files_struct *files)
8904{
8905 struct io_task_cancel cancel = { .task = task, .files = files, };
8906
8907 while (1) {
8908 enum io_wq_cancel cret;
8909 bool ret = false;
8910
8911 if (ctx->io_wq) {
8912 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8913 &cancel, true);
8914 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8915 }
8916
8917 /* SQPOLL thread does its own polling */
8918 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8919 while (!list_empty_careful(&ctx->iopoll_list)) {
8920 io_iopoll_try_reap_events(ctx);
8921 ret = true;
8922 }
8923 }
8924
8925 ret |= io_poll_remove_all(ctx, task, files);
8926 ret |= io_kill_timeouts(ctx, task, files);
8927 ret |= io_run_task_work();
8928 io_cqring_overflow_flush(ctx, true, task, files);
8929 if (!ret)
8930 break;
8931 cond_resched();
8932 }
8933}
8934
Pavel Begunkovca70f002021-01-26 15:28:27 +00008935static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8936 struct task_struct *task,
8937 struct files_struct *files)
8938{
8939 struct io_kiocb *req;
8940 int cnt = 0;
8941
8942 spin_lock_irq(&ctx->inflight_lock);
8943 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8944 cnt += io_match_task(req, task, files);
8945 spin_unlock_irq(&ctx->inflight_lock);
8946 return cnt;
8947}
8948
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008949static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008950 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008951 struct files_struct *files)
8952{
Jens Axboefcb323c2019-10-24 12:39:47 -06008953 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008954 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008955 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008956
Pavel Begunkovca70f002021-01-26 15:28:27 +00008957 inflight = io_uring_count_inflight(ctx, task, files);
8958 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008959 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008960
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008961 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008962 prepare_to_wait(&task->io_uring->wait, &wait,
8963 TASK_UNINTERRUPTIBLE);
8964 if (inflight == io_uring_count_inflight(ctx, task, files))
8965 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008966 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008967 }
8968}
8969
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008970static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8971{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008972 mutex_lock(&ctx->uring_lock);
8973 ctx->sqo_dead = 1;
8974 mutex_unlock(&ctx->uring_lock);
8975
8976 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008977 if (ctx->rings)
8978 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008979}
8980
Jens Axboe0f212202020-09-13 13:09:39 -06008981/*
8982 * We need to iteratively cancel requests, in case a request has dependent
8983 * hard links. These persist even for failure of cancelations, hence keep
8984 * looping until none are found.
8985 */
8986static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8987 struct files_struct *files)
8988{
8989 struct task_struct *task = current;
8990
Jens Axboefdaf0832020-10-30 09:37:30 -06008991 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008992 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008993 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008994 atomic_inc(&task->io_uring->in_idle);
8995 io_sq_thread_park(ctx->sq_data);
8996 }
Jens Axboe0f212202020-09-13 13:09:39 -06008997
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008998 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008999
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009000 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009001 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009002 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009003
9004 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9005 atomic_dec(&task->io_uring->in_idle);
9006 /*
9007 * If the files that are going away are the ones in the thread
9008 * identity, clear them out.
9009 */
9010 if (task->io_uring->identity->files == files)
9011 task->io_uring->identity->files = NULL;
9012 io_sq_thread_unpark(ctx->sq_data);
9013 }
Jens Axboe0f212202020-09-13 13:09:39 -06009014}
9015
9016/*
9017 * Note that this task has used io_uring. We use it for cancelation purposes.
9018 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009019static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009020{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009021 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009022 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009023
9024 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009025 ret = io_uring_alloc_task_context(current);
9026 if (unlikely(ret))
9027 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009028 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009029 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009030 if (tctx->last != file) {
9031 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009032
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009033 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009034 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009035 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9036 file, GFP_KERNEL));
9037 if (ret) {
9038 fput(file);
9039 return ret;
9040 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009041
9042 /* one and only SQPOLL file note, held by sqo_task */
9043 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9044 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009045 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009046 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009047 }
9048
Jens Axboefdaf0832020-10-30 09:37:30 -06009049 /*
9050 * This is race safe in that the task itself is doing this, hence it
9051 * cannot be going through the exit/cancel paths at the same time.
9052 * This cannot be modified while exit/cancel is running.
9053 */
9054 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9055 tctx->sqpoll = true;
9056
Jens Axboe0f212202020-09-13 13:09:39 -06009057 return 0;
9058}
9059
9060/*
9061 * Remove this io_uring_file -> task mapping.
9062 */
9063static void io_uring_del_task_file(struct file *file)
9064{
9065 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009066
9067 if (tctx->last == file)
9068 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009069 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009070 if (file)
9071 fput(file);
9072}
9073
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009074static void io_uring_remove_task_files(struct io_uring_task *tctx)
9075{
9076 struct file *file;
9077 unsigned long index;
9078
9079 xa_for_each(&tctx->xa, index, file)
9080 io_uring_del_task_file(file);
9081}
9082
Jens Axboe0f212202020-09-13 13:09:39 -06009083void __io_uring_files_cancel(struct files_struct *files)
9084{
9085 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009086 struct file *file;
9087 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009088
9089 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009090 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009091 xa_for_each(&tctx->xa, index, file)
9092 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009093 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009094
9095 if (files)
9096 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009097}
9098
9099static s64 tctx_inflight(struct io_uring_task *tctx)
9100{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009101 return percpu_counter_sum(&tctx->inflight);
9102}
9103
9104static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9105{
9106 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009107 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009108 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009109
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009110 if (!ctx->sq_data)
9111 return;
9112 tctx = ctx->sq_data->thread->io_uring;
9113 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009114
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009115 atomic_inc(&tctx->in_idle);
9116 do {
9117 /* read completions before cancelations */
9118 inflight = tctx_inflight(tctx);
9119 if (!inflight)
9120 break;
9121 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009122
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009123 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9124 /*
9125 * If we've seen completions, retry without waiting. This
9126 * avoids a race where a completion comes in before we did
9127 * prepare_to_wait().
9128 */
9129 if (inflight == tctx_inflight(tctx))
9130 schedule();
9131 finish_wait(&tctx->wait, &wait);
9132 } while (1);
9133 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009134}
9135
Jens Axboe0f212202020-09-13 13:09:39 -06009136/*
9137 * Find any io_uring fd that this task has registered or done IO on, and cancel
9138 * requests.
9139 */
9140void __io_uring_task_cancel(void)
9141{
9142 struct io_uring_task *tctx = current->io_uring;
9143 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009144 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009145
9146 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009147 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009148
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009149 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009150 if (tctx->sqpoll) {
9151 struct file *file;
9152 unsigned long index;
9153
9154 xa_for_each(&tctx->xa, index, file)
9155 io_uring_cancel_sqpoll(file->private_data);
9156 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009157
Jens Axboed8a6df12020-10-15 16:24:45 -06009158 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009159 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009160 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009161 if (!inflight)
9162 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009163 __io_uring_files_cancel(NULL);
9164
9165 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9166
9167 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009168 * If we've seen completions, retry without waiting. This
9169 * avoids a race where a completion comes in before we did
9170 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009171 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009172 if (inflight == tctx_inflight(tctx))
9173 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009174 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009175 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009176
Jens Axboefdaf0832020-10-30 09:37:30 -06009177 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009178
9179 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009180}
9181
Jens Axboefcb323c2019-10-24 12:39:47 -06009182static int io_uring_flush(struct file *file, void *data)
9183{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009184 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009185 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009186
Jens Axboe84965ff2021-01-23 15:51:11 -07009187 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9188 io_uring_cancel_task_requests(ctx, NULL);
9189
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009190 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009191 return 0;
9192
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009193 /* we should have cancelled and erased it before PF_EXITING */
9194 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9195 xa_load(&tctx->xa, (unsigned long)file));
9196
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009197 /*
9198 * fput() is pending, will be 2 if the only other ref is our potential
9199 * task file note. If the task is exiting, drop regardless of count.
9200 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009201 if (atomic_long_read(&file->f_count) != 2)
9202 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009203
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009204 if (ctx->flags & IORING_SETUP_SQPOLL) {
9205 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009206 WARN_ON_ONCE(ctx->sqo_task != current &&
9207 xa_load(&tctx->xa, (unsigned long)file));
9208 /* sqo_dead check is for when this happens after cancellation */
9209 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009210 !xa_load(&tctx->xa, (unsigned long)file));
9211
9212 io_disable_sqo_submit(ctx);
9213 }
9214
9215 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9216 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009217 return 0;
9218}
9219
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009220static void *io_uring_validate_mmap_request(struct file *file,
9221 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009222{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009223 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009224 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009225 struct page *page;
9226 void *ptr;
9227
9228 switch (offset) {
9229 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009230 case IORING_OFF_CQ_RING:
9231 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009232 break;
9233 case IORING_OFF_SQES:
9234 ptr = ctx->sq_sqes;
9235 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009236 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009237 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009238 }
9239
9240 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009241 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009242 return ERR_PTR(-EINVAL);
9243
9244 return ptr;
9245}
9246
9247#ifdef CONFIG_MMU
9248
9249static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9250{
9251 size_t sz = vma->vm_end - vma->vm_start;
9252 unsigned long pfn;
9253 void *ptr;
9254
9255 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9256 if (IS_ERR(ptr))
9257 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009258
9259 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9260 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9261}
9262
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009263#else /* !CONFIG_MMU */
9264
9265static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9266{
9267 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9268}
9269
9270static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9271{
9272 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9273}
9274
9275static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9276 unsigned long addr, unsigned long len,
9277 unsigned long pgoff, unsigned long flags)
9278{
9279 void *ptr;
9280
9281 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9282 if (IS_ERR(ptr))
9283 return PTR_ERR(ptr);
9284
9285 return (unsigned long) ptr;
9286}
9287
9288#endif /* !CONFIG_MMU */
9289
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009290static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009291{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009292 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009293 DEFINE_WAIT(wait);
9294
9295 do {
9296 if (!io_sqring_full(ctx))
9297 break;
9298
9299 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9300
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009301 if (unlikely(ctx->sqo_dead)) {
9302 ret = -EOWNERDEAD;
9303 goto out;
9304 }
9305
Jens Axboe90554202020-09-03 12:12:41 -06009306 if (!io_sqring_full(ctx))
9307 break;
9308
9309 schedule();
9310 } while (!signal_pending(current));
9311
9312 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009313out:
9314 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009315}
9316
Hao Xuc73ebb62020-11-03 10:54:37 +08009317static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9318 struct __kernel_timespec __user **ts,
9319 const sigset_t __user **sig)
9320{
9321 struct io_uring_getevents_arg arg;
9322
9323 /*
9324 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9325 * is just a pointer to the sigset_t.
9326 */
9327 if (!(flags & IORING_ENTER_EXT_ARG)) {
9328 *sig = (const sigset_t __user *) argp;
9329 *ts = NULL;
9330 return 0;
9331 }
9332
9333 /*
9334 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9335 * timespec and sigset_t pointers if good.
9336 */
9337 if (*argsz != sizeof(arg))
9338 return -EINVAL;
9339 if (copy_from_user(&arg, argp, sizeof(arg)))
9340 return -EFAULT;
9341 *sig = u64_to_user_ptr(arg.sigmask);
9342 *argsz = arg.sigmask_sz;
9343 *ts = u64_to_user_ptr(arg.ts);
9344 return 0;
9345}
9346
Jens Axboe2b188cc2019-01-07 10:46:33 -07009347SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009348 u32, min_complete, u32, flags, const void __user *, argp,
9349 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009350{
9351 struct io_ring_ctx *ctx;
9352 long ret = -EBADF;
9353 int submitted = 0;
9354 struct fd f;
9355
Jens Axboe4c6e2772020-07-01 11:29:10 -06009356 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009357
Jens Axboe90554202020-09-03 12:12:41 -06009358 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009359 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009360 return -EINVAL;
9361
9362 f = fdget(fd);
9363 if (!f.file)
9364 return -EBADF;
9365
9366 ret = -EOPNOTSUPP;
9367 if (f.file->f_op != &io_uring_fops)
9368 goto out_fput;
9369
9370 ret = -ENXIO;
9371 ctx = f.file->private_data;
9372 if (!percpu_ref_tryget(&ctx->refs))
9373 goto out_fput;
9374
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009375 ret = -EBADFD;
9376 if (ctx->flags & IORING_SETUP_R_DISABLED)
9377 goto out;
9378
Jens Axboe6c271ce2019-01-10 11:22:30 -07009379 /*
9380 * For SQ polling, the thread will do all submissions and completions.
9381 * Just return the requested submit count, and wake the thread if
9382 * we were asked to.
9383 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009384 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009385 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009386 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009387
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009388 ret = -EOWNERDEAD;
9389 if (unlikely(ctx->sqo_dead))
9390 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009391 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009392 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009393 if (flags & IORING_ENTER_SQ_WAIT) {
9394 ret = io_sqpoll_wait_sq(ctx);
9395 if (ret)
9396 goto out;
9397 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009398 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009399 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009400 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009401 if (unlikely(ret))
9402 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009403 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009404 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009406
9407 if (submitted != to_submit)
9408 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009409 }
9410 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009411 const sigset_t __user *sig;
9412 struct __kernel_timespec __user *ts;
9413
9414 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9415 if (unlikely(ret))
9416 goto out;
9417
Jens Axboe2b188cc2019-01-07 10:46:33 -07009418 min_complete = min(min_complete, ctx->cq_entries);
9419
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009420 /*
9421 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9422 * space applications don't need to do io completion events
9423 * polling again, they can rely on io_sq_thread to do polling
9424 * work, which can reduce cpu usage and uring_lock contention.
9425 */
9426 if (ctx->flags & IORING_SETUP_IOPOLL &&
9427 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009428 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009429 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009430 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009431 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009432 }
9433
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009434out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009435 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009436out_fput:
9437 fdput(f);
9438 return submitted ? submitted : ret;
9439}
9440
Tobias Klauserbebdb652020-02-26 18:38:32 +01009441#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009442static int io_uring_show_cred(int id, void *p, void *data)
9443{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009444 struct io_identity *iod = p;
9445 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009446 struct seq_file *m = data;
9447 struct user_namespace *uns = seq_user_ns(m);
9448 struct group_info *gi;
9449 kernel_cap_t cap;
9450 unsigned __capi;
9451 int g;
9452
9453 seq_printf(m, "%5d\n", id);
9454 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9455 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9456 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9457 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9458 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9459 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9460 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9461 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9462 seq_puts(m, "\n\tGroups:\t");
9463 gi = cred->group_info;
9464 for (g = 0; g < gi->ngroups; g++) {
9465 seq_put_decimal_ull(m, g ? " " : "",
9466 from_kgid_munged(uns, gi->gid[g]));
9467 }
9468 seq_puts(m, "\n\tCapEff:\t");
9469 cap = cred->cap_effective;
9470 CAP_FOR_EACH_U32(__capi)
9471 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9472 seq_putc(m, '\n');
9473 return 0;
9474}
9475
9476static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9477{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009478 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009479 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009480 int i;
9481
Jens Axboefad8e0d2020-09-28 08:57:48 -06009482 /*
9483 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9484 * since fdinfo case grabs it in the opposite direction of normal use
9485 * cases. If we fail to get the lock, we just don't iterate any
9486 * structures that could be going away outside the io_uring mutex.
9487 */
9488 has_lock = mutex_trylock(&ctx->uring_lock);
9489
Joseph Qidbbe9c62020-09-29 09:01:22 -06009490 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9491 sq = ctx->sq_data;
9492
9493 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9494 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009495 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009496 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009497 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009498
Jens Axboe87ce9552020-01-30 08:25:34 -07009499 if (f)
9500 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9501 else
9502 seq_printf(m, "%5u: <none>\n", i);
9503 }
9504 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009505 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009506 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9507
9508 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9509 (unsigned int) buf->len);
9510 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009511 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009512 seq_printf(m, "Personalities:\n");
9513 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9514 }
Jens Axboed7718a92020-02-14 22:23:12 -07009515 seq_printf(m, "PollList:\n");
9516 spin_lock_irq(&ctx->completion_lock);
9517 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9518 struct hlist_head *list = &ctx->cancel_hash[i];
9519 struct io_kiocb *req;
9520
9521 hlist_for_each_entry(req, list, hash_node)
9522 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9523 req->task->task_works != NULL);
9524 }
9525 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009526 if (has_lock)
9527 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009528}
9529
9530static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9531{
9532 struct io_ring_ctx *ctx = f->private_data;
9533
9534 if (percpu_ref_tryget(&ctx->refs)) {
9535 __io_uring_show_fdinfo(ctx, m);
9536 percpu_ref_put(&ctx->refs);
9537 }
9538}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009539#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009540
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541static const struct file_operations io_uring_fops = {
9542 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009543 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009544 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009545#ifndef CONFIG_MMU
9546 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9547 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9548#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009549 .poll = io_uring_poll,
9550 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009551#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009552 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009553#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009554};
9555
9556static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9557 struct io_uring_params *p)
9558{
Hristo Venev75b28af2019-08-26 17:23:46 +00009559 struct io_rings *rings;
9560 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009561
Jens Axboebd740482020-08-05 12:58:23 -06009562 /* make sure these are sane, as we already accounted them */
9563 ctx->sq_entries = p->sq_entries;
9564 ctx->cq_entries = p->cq_entries;
9565
Hristo Venev75b28af2019-08-26 17:23:46 +00009566 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9567 if (size == SIZE_MAX)
9568 return -EOVERFLOW;
9569
9570 rings = io_mem_alloc(size);
9571 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009572 return -ENOMEM;
9573
Hristo Venev75b28af2019-08-26 17:23:46 +00009574 ctx->rings = rings;
9575 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9576 rings->sq_ring_mask = p->sq_entries - 1;
9577 rings->cq_ring_mask = p->cq_entries - 1;
9578 rings->sq_ring_entries = p->sq_entries;
9579 rings->cq_ring_entries = p->cq_entries;
9580 ctx->sq_mask = rings->sq_ring_mask;
9581 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009582
9583 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009584 if (size == SIZE_MAX) {
9585 io_mem_free(ctx->rings);
9586 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009587 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009588 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009589
9590 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009591 if (!ctx->sq_sqes) {
9592 io_mem_free(ctx->rings);
9593 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009594 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009595 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009596
Jens Axboe2b188cc2019-01-07 10:46:33 -07009597 return 0;
9598}
9599
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009600static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9601{
9602 int ret, fd;
9603
9604 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9605 if (fd < 0)
9606 return fd;
9607
9608 ret = io_uring_add_task_file(ctx, file);
9609 if (ret) {
9610 put_unused_fd(fd);
9611 return ret;
9612 }
9613 fd_install(fd, file);
9614 return fd;
9615}
9616
Jens Axboe2b188cc2019-01-07 10:46:33 -07009617/*
9618 * Allocate an anonymous fd, this is what constitutes the application
9619 * visible backing of an io_uring instance. The application mmaps this
9620 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9621 * we have to tie this fd to a socket for file garbage collection purposes.
9622 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009623static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009624{
9625 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009626#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009627 int ret;
9628
Jens Axboe2b188cc2019-01-07 10:46:33 -07009629 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9630 &ctx->ring_sock);
9631 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009632 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009633#endif
9634
Jens Axboe2b188cc2019-01-07 10:46:33 -07009635 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9636 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009637#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009638 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009639 sock_release(ctx->ring_sock);
9640 ctx->ring_sock = NULL;
9641 } else {
9642 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009643 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009644#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009645 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009646}
9647
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009648static int io_uring_create(unsigned entries, struct io_uring_params *p,
9649 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009650{
9651 struct user_struct *user = NULL;
9652 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009653 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009654 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009655 int ret;
9656
Jens Axboe8110c1a2019-12-28 15:39:54 -07009657 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009658 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009659 if (entries > IORING_MAX_ENTRIES) {
9660 if (!(p->flags & IORING_SETUP_CLAMP))
9661 return -EINVAL;
9662 entries = IORING_MAX_ENTRIES;
9663 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009664
9665 /*
9666 * Use twice as many entries for the CQ ring. It's possible for the
9667 * application to drive a higher depth than the size of the SQ ring,
9668 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009669 * some flexibility in overcommitting a bit. If the application has
9670 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9671 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009672 */
9673 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009674 if (p->flags & IORING_SETUP_CQSIZE) {
9675 /*
9676 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9677 * to a power-of-two, if it isn't already. We do NOT impose
9678 * any cq vs sq ring sizing.
9679 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009680 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009681 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009682 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9683 if (!(p->flags & IORING_SETUP_CLAMP))
9684 return -EINVAL;
9685 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9686 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009687 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9688 if (p->cq_entries < p->sq_entries)
9689 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009690 } else {
9691 p->cq_entries = 2 * p->sq_entries;
9692 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009693
9694 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009695 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009696
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009697 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009698 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009699 ring_pages(p->sq_entries, p->cq_entries));
9700 if (ret) {
9701 free_uid(user);
9702 return ret;
9703 }
9704 }
9705
9706 ctx = io_ring_ctx_alloc(p);
9707 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009708 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009709 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009710 p->cq_entries));
9711 free_uid(user);
9712 return -ENOMEM;
9713 }
9714 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009715 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009716 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009717#ifdef CONFIG_AUDIT
9718 ctx->loginuid = current->loginuid;
9719 ctx->sessionid = current->sessionid;
9720#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009721 ctx->sqo_task = get_task_struct(current);
9722
9723 /*
9724 * This is just grabbed for accounting purposes. When a process exits,
9725 * the mm is exited and dropped before the files, hence we need to hang
9726 * on to this mm purely for the purposes of being able to unaccount
9727 * memory (locked/pinned vm). It's not used for anything else.
9728 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009729 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009730 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009731
Dennis Zhou91d8f512020-09-16 13:41:05 -07009732#ifdef CONFIG_BLK_CGROUP
9733 /*
9734 * The sq thread will belong to the original cgroup it was inited in.
9735 * If the cgroup goes offline (e.g. disabling the io controller), then
9736 * issued bios will be associated with the closest cgroup later in the
9737 * block layer.
9738 */
9739 rcu_read_lock();
9740 ctx->sqo_blkcg_css = blkcg_css();
9741 ret = css_tryget_online(ctx->sqo_blkcg_css);
9742 rcu_read_unlock();
9743 if (!ret) {
9744 /* don't init against a dying cgroup, have the user try again */
9745 ctx->sqo_blkcg_css = NULL;
9746 ret = -ENODEV;
9747 goto err;
9748 }
9749#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009750
Jens Axboe2b188cc2019-01-07 10:46:33 -07009751 /*
9752 * Account memory _before_ installing the file descriptor. Once
9753 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009754 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009755 * will un-account as well.
9756 */
9757 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9758 ACCT_LOCKED);
9759 ctx->limit_mem = limit_mem;
9760
9761 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009762 if (ret)
9763 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009764
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009765 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009766 if (ret)
9767 goto err;
9768
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009769 if (!(p->flags & IORING_SETUP_R_DISABLED))
9770 io_sq_offload_start(ctx);
9771
Jens Axboe2b188cc2019-01-07 10:46:33 -07009772 memset(&p->sq_off, 0, sizeof(p->sq_off));
9773 p->sq_off.head = offsetof(struct io_rings, sq.head);
9774 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9775 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9776 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9777 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9778 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9779 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9780
9781 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009782 p->cq_off.head = offsetof(struct io_rings, cq.head);
9783 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9784 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9785 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9786 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9787 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009788 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009789
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009790 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9791 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009792 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009793 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9794 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009795
9796 if (copy_to_user(params, p, sizeof(*p))) {
9797 ret = -EFAULT;
9798 goto err;
9799 }
Jens Axboed1719f72020-07-30 13:43:53 -06009800
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009801 file = io_uring_get_file(ctx);
9802 if (IS_ERR(file)) {
9803 ret = PTR_ERR(file);
9804 goto err;
9805 }
9806
Jens Axboed1719f72020-07-30 13:43:53 -06009807 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009808 * Install ring fd as the very last thing, so we don't risk someone
9809 * having closed it before we finish setup
9810 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009811 ret = io_uring_install_fd(ctx, file);
9812 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009813 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009814 /* fput will clean it up */
9815 fput(file);
9816 return ret;
9817 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009818
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009819 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009820 return ret;
9821err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009822 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009823 io_ring_ctx_wait_and_kill(ctx);
9824 return ret;
9825}
9826
9827/*
9828 * Sets up an aio uring context, and returns the fd. Applications asks for a
9829 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9830 * params structure passed in.
9831 */
9832static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9833{
9834 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009835 int i;
9836
9837 if (copy_from_user(&p, params, sizeof(p)))
9838 return -EFAULT;
9839 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9840 if (p.resv[i])
9841 return -EINVAL;
9842 }
9843
Jens Axboe6c271ce2019-01-10 11:22:30 -07009844 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009845 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009846 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9847 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009848 return -EINVAL;
9849
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009850 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009851}
9852
9853SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9854 struct io_uring_params __user *, params)
9855{
9856 return io_uring_setup(entries, params);
9857}
9858
Jens Axboe66f4af92020-01-16 15:36:52 -07009859static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9860{
9861 struct io_uring_probe *p;
9862 size_t size;
9863 int i, ret;
9864
9865 size = struct_size(p, ops, nr_args);
9866 if (size == SIZE_MAX)
9867 return -EOVERFLOW;
9868 p = kzalloc(size, GFP_KERNEL);
9869 if (!p)
9870 return -ENOMEM;
9871
9872 ret = -EFAULT;
9873 if (copy_from_user(p, arg, size))
9874 goto out;
9875 ret = -EINVAL;
9876 if (memchr_inv(p, 0, size))
9877 goto out;
9878
9879 p->last_op = IORING_OP_LAST - 1;
9880 if (nr_args > IORING_OP_LAST)
9881 nr_args = IORING_OP_LAST;
9882
9883 for (i = 0; i < nr_args; i++) {
9884 p->ops[i].op = i;
9885 if (!io_op_defs[i].not_supported)
9886 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9887 }
9888 p->ops_len = i;
9889
9890 ret = 0;
9891 if (copy_to_user(arg, p, size))
9892 ret = -EFAULT;
9893out:
9894 kfree(p);
9895 return ret;
9896}
9897
Jens Axboe071698e2020-01-28 10:04:42 -07009898static int io_register_personality(struct io_ring_ctx *ctx)
9899{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009900 struct io_identity *id;
9901 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009902
Jens Axboe1e6fa522020-10-15 08:46:24 -06009903 id = kmalloc(sizeof(*id), GFP_KERNEL);
9904 if (unlikely(!id))
9905 return -ENOMEM;
9906
9907 io_init_identity(id);
9908 id->creds = get_current_cred();
9909
9910 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9911 if (ret < 0) {
9912 put_cred(id->creds);
9913 kfree(id);
9914 }
9915 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009916}
9917
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009918static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9919 unsigned int nr_args)
9920{
9921 struct io_uring_restriction *res;
9922 size_t size;
9923 int i, ret;
9924
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009925 /* Restrictions allowed only if rings started disabled */
9926 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9927 return -EBADFD;
9928
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009929 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009930 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009931 return -EBUSY;
9932
9933 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9934 return -EINVAL;
9935
9936 size = array_size(nr_args, sizeof(*res));
9937 if (size == SIZE_MAX)
9938 return -EOVERFLOW;
9939
9940 res = memdup_user(arg, size);
9941 if (IS_ERR(res))
9942 return PTR_ERR(res);
9943
9944 ret = 0;
9945
9946 for (i = 0; i < nr_args; i++) {
9947 switch (res[i].opcode) {
9948 case IORING_RESTRICTION_REGISTER_OP:
9949 if (res[i].register_op >= IORING_REGISTER_LAST) {
9950 ret = -EINVAL;
9951 goto out;
9952 }
9953
9954 __set_bit(res[i].register_op,
9955 ctx->restrictions.register_op);
9956 break;
9957 case IORING_RESTRICTION_SQE_OP:
9958 if (res[i].sqe_op >= IORING_OP_LAST) {
9959 ret = -EINVAL;
9960 goto out;
9961 }
9962
9963 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9964 break;
9965 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9966 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9967 break;
9968 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9969 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9970 break;
9971 default:
9972 ret = -EINVAL;
9973 goto out;
9974 }
9975 }
9976
9977out:
9978 /* Reset all restrictions if an error happened */
9979 if (ret != 0)
9980 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9981 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009982 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009983
9984 kfree(res);
9985 return ret;
9986}
9987
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009988static int io_register_enable_rings(struct io_ring_ctx *ctx)
9989{
9990 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9991 return -EBADFD;
9992
9993 if (ctx->restrictions.registered)
9994 ctx->restricted = 1;
9995
9996 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9997
9998 io_sq_offload_start(ctx);
9999
10000 return 0;
10001}
10002
Jens Axboe071698e2020-01-28 10:04:42 -070010003static bool io_register_op_must_quiesce(int op)
10004{
10005 switch (op) {
10006 case IORING_UNREGISTER_FILES:
10007 case IORING_REGISTER_FILES_UPDATE:
10008 case IORING_REGISTER_PROBE:
10009 case IORING_REGISTER_PERSONALITY:
10010 case IORING_UNREGISTER_PERSONALITY:
10011 return false;
10012 default:
10013 return true;
10014 }
10015}
10016
Jens Axboeedafcce2019-01-09 09:16:05 -070010017static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10018 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010019 __releases(ctx->uring_lock)
10020 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010021{
10022 int ret;
10023
Jens Axboe35fa71a2019-04-22 10:23:23 -060010024 /*
10025 * We're inside the ring mutex, if the ref is already dying, then
10026 * someone else killed the ctx or is already going through
10027 * io_uring_register().
10028 */
10029 if (percpu_ref_is_dying(&ctx->refs))
10030 return -ENXIO;
10031
Jens Axboe071698e2020-01-28 10:04:42 -070010032 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010033 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010034
Jens Axboe05f3fb32019-12-09 11:22:50 -070010035 /*
10036 * Drop uring mutex before waiting for references to exit. If
10037 * another thread is currently inside io_uring_enter() it might
10038 * need to grab the uring_lock to make progress. If we hold it
10039 * here across the drain wait, then we can deadlock. It's safe
10040 * to drop the mutex here, since no new references will come in
10041 * after we've killed the percpu ref.
10042 */
10043 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010044 do {
10045 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10046 if (!ret)
10047 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010048 ret = io_run_task_work_sig();
10049 if (ret < 0)
10050 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010051 } while (1);
10052
Jens Axboe05f3fb32019-12-09 11:22:50 -070010053 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010054
Jens Axboec1503682020-01-08 08:26:07 -070010055 if (ret) {
10056 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010057 goto out_quiesce;
10058 }
10059 }
10060
10061 if (ctx->restricted) {
10062 if (opcode >= IORING_REGISTER_LAST) {
10063 ret = -EINVAL;
10064 goto out;
10065 }
10066
10067 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10068 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010069 goto out;
10070 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010071 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010072
10073 switch (opcode) {
10074 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010075 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010076 break;
10077 case IORING_UNREGISTER_BUFFERS:
10078 ret = -EINVAL;
10079 if (arg || nr_args)
10080 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010081 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010082 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010083 case IORING_REGISTER_FILES:
10084 ret = io_sqe_files_register(ctx, arg, nr_args);
10085 break;
10086 case IORING_UNREGISTER_FILES:
10087 ret = -EINVAL;
10088 if (arg || nr_args)
10089 break;
10090 ret = io_sqe_files_unregister(ctx);
10091 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010092 case IORING_REGISTER_FILES_UPDATE:
10093 ret = io_sqe_files_update(ctx, arg, nr_args);
10094 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010095 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010096 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010097 ret = -EINVAL;
10098 if (nr_args != 1)
10099 break;
10100 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010101 if (ret)
10102 break;
10103 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10104 ctx->eventfd_async = 1;
10105 else
10106 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010107 break;
10108 case IORING_UNREGISTER_EVENTFD:
10109 ret = -EINVAL;
10110 if (arg || nr_args)
10111 break;
10112 ret = io_eventfd_unregister(ctx);
10113 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010114 case IORING_REGISTER_PROBE:
10115 ret = -EINVAL;
10116 if (!arg || nr_args > 256)
10117 break;
10118 ret = io_probe(ctx, arg, nr_args);
10119 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010120 case IORING_REGISTER_PERSONALITY:
10121 ret = -EINVAL;
10122 if (arg || nr_args)
10123 break;
10124 ret = io_register_personality(ctx);
10125 break;
10126 case IORING_UNREGISTER_PERSONALITY:
10127 ret = -EINVAL;
10128 if (arg)
10129 break;
10130 ret = io_unregister_personality(ctx, nr_args);
10131 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010132 case IORING_REGISTER_ENABLE_RINGS:
10133 ret = -EINVAL;
10134 if (arg || nr_args)
10135 break;
10136 ret = io_register_enable_rings(ctx);
10137 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010138 case IORING_REGISTER_RESTRICTIONS:
10139 ret = io_register_restrictions(ctx, arg, nr_args);
10140 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010141 default:
10142 ret = -EINVAL;
10143 break;
10144 }
10145
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010146out:
Jens Axboe071698e2020-01-28 10:04:42 -070010147 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010148 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010149 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010150out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010151 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010152 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010153 return ret;
10154}
10155
10156SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10157 void __user *, arg, unsigned int, nr_args)
10158{
10159 struct io_ring_ctx *ctx;
10160 long ret = -EBADF;
10161 struct fd f;
10162
10163 f = fdget(fd);
10164 if (!f.file)
10165 return -EBADF;
10166
10167 ret = -EOPNOTSUPP;
10168 if (f.file->f_op != &io_uring_fops)
10169 goto out_fput;
10170
10171 ctx = f.file->private_data;
10172
10173 mutex_lock(&ctx->uring_lock);
10174 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10175 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010176 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10177 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010178out_fput:
10179 fdput(f);
10180 return ret;
10181}
10182
Jens Axboe2b188cc2019-01-07 10:46:33 -070010183static int __init io_uring_init(void)
10184{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010185#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10186 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10187 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10188} while (0)
10189
10190#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10191 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10192 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10193 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10194 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10195 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10196 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10197 BUILD_BUG_SQE_ELEM(8, __u64, off);
10198 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10199 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010200 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010201 BUILD_BUG_SQE_ELEM(24, __u32, len);
10202 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10203 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10204 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10205 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010206 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10207 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010208 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10209 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10210 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10211 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10212 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10213 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10214 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10215 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010216 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010217 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10218 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10219 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010220 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010221
Jens Axboed3656342019-12-18 09:50:26 -070010222 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010223 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010224 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10225 return 0;
10226};
10227__initcall(io_uring_init);