blob: 862121c48cee0e85fb6adafc1cc2665b9817eb35 [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 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003920static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003921{
3922 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003923
Jens Axboedef596e2019-01-09 08:59:42 -07003924 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3925 return -EINVAL;
3926
Jens Axboe229a7b62020-06-22 10:13:11 -06003927 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003928 return 0;
3929}
3930
Jens Axboe3529d8c2019-12-19 18:24:38 -07003931static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003932{
Jens Axboe6b063142019-01-10 22:13:58 -07003933 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003934
Jens Axboe09bb8392019-03-13 12:39:28 -06003935 if (!req->file)
3936 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003937
Jens Axboe6b063142019-01-10 22:13:58 -07003938 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003939 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003940 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003941 return -EINVAL;
3942
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003943 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3944 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3945 return -EINVAL;
3946
3947 req->sync.off = READ_ONCE(sqe->off);
3948 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003949 return 0;
3950}
3951
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003952static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003953{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003954 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003955 int ret;
3956
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003957 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003958 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003959 return -EAGAIN;
3960
Jens Axboe9adbd452019-12-20 08:45:55 -07003961 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962 end > 0 ? end : LLONG_MAX,
3963 req->sync.flags & IORING_FSYNC_DATASYNC);
3964 if (ret < 0)
3965 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003966 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003967 return 0;
3968}
3969
Jens Axboed63d1b52019-12-10 10:38:56 -07003970static int io_fallocate_prep(struct io_kiocb *req,
3971 const struct io_uring_sqe *sqe)
3972{
3973 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3974 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003975 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3976 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003977
3978 req->sync.off = READ_ONCE(sqe->off);
3979 req->sync.len = READ_ONCE(sqe->addr);
3980 req->sync.mode = READ_ONCE(sqe->len);
3981 return 0;
3982}
3983
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003984static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003985{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003986 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003987
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003988 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003989 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003990 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003991 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3992 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003993 if (ret < 0)
3994 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003995 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003996 return 0;
3997}
3998
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003999static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004000{
Jens Axboef8748882020-01-08 17:47:02 -07004001 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004002 int ret;
4003
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004004 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004005 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004006 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004007 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004008
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004009 /* open.how should be already initialised */
4010 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004011 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004012
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004013 req->open.dfd = READ_ONCE(sqe->fd);
4014 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004015 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004016 if (IS_ERR(req->open.filename)) {
4017 ret = PTR_ERR(req->open.filename);
4018 req->open.filename = NULL;
4019 return ret;
4020 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004021 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004022 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004023 return 0;
4024}
4025
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004026static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4027{
4028 u64 flags, mode;
4029
Jens Axboe14587a462020-09-05 11:36:08 -06004030 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004031 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004032 mode = READ_ONCE(sqe->len);
4033 flags = READ_ONCE(sqe->open_flags);
4034 req->open.how = build_open_how(flags, mode);
4035 return __io_openat_prep(req, sqe);
4036}
4037
Jens Axboecebdb982020-01-08 17:59:24 -07004038static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4039{
4040 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004041 size_t len;
4042 int ret;
4043
Jens Axboe14587a462020-09-05 11:36:08 -06004044 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004045 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004046 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4047 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004048 if (len < OPEN_HOW_SIZE_VER0)
4049 return -EINVAL;
4050
4051 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4052 len);
4053 if (ret)
4054 return ret;
4055
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004056 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004057}
4058
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004059static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004060{
4061 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004062 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004063 bool nonblock_set;
4064 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004065 int ret;
4066
Jens Axboecebdb982020-01-08 17:59:24 -07004067 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004068 if (ret)
4069 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004070 nonblock_set = op.open_flag & O_NONBLOCK;
4071 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004072 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004073 /*
4074 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4075 * it'll always -EAGAIN
4076 */
4077 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4078 return -EAGAIN;
4079 op.lookup_flags |= LOOKUP_CACHED;
4080 op.open_flag |= O_NONBLOCK;
4081 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004082
Jens Axboe4022e7a2020-03-19 19:23:18 -06004083 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004084 if (ret < 0)
4085 goto err;
4086
4087 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004088 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004089 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4090 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004091 /*
4092 * We could hang on to this 'fd', but seems like marginal
4093 * gain for something that is now known to be a slower path.
4094 * So just put it, and we'll get a new one when we retry.
4095 */
4096 put_unused_fd(ret);
4097 return -EAGAIN;
4098 }
4099
Jens Axboe15b71ab2019-12-11 11:20:36 -07004100 if (IS_ERR(file)) {
4101 put_unused_fd(ret);
4102 ret = PTR_ERR(file);
4103 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004104 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004105 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004106 fsnotify_open(file);
4107 fd_install(ret, file);
4108 }
4109err:
4110 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004111 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004112 if (ret < 0)
4113 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004114 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004115 return 0;
4116}
4117
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004118static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004119{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004120 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004121}
4122
Jens Axboe067524e2020-03-02 16:32:28 -07004123static int io_remove_buffers_prep(struct io_kiocb *req,
4124 const struct io_uring_sqe *sqe)
4125{
4126 struct io_provide_buf *p = &req->pbuf;
4127 u64 tmp;
4128
4129 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4130 return -EINVAL;
4131
4132 tmp = READ_ONCE(sqe->fd);
4133 if (!tmp || tmp > USHRT_MAX)
4134 return -EINVAL;
4135
4136 memset(p, 0, sizeof(*p));
4137 p->nbufs = tmp;
4138 p->bgid = READ_ONCE(sqe->buf_group);
4139 return 0;
4140}
4141
4142static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4143 int bgid, unsigned nbufs)
4144{
4145 unsigned i = 0;
4146
4147 /* shouldn't happen */
4148 if (!nbufs)
4149 return 0;
4150
4151 /* the head kbuf is the list itself */
4152 while (!list_empty(&buf->list)) {
4153 struct io_buffer *nxt;
4154
4155 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4156 list_del(&nxt->list);
4157 kfree(nxt);
4158 if (++i == nbufs)
4159 return i;
4160 }
4161 i++;
4162 kfree(buf);
4163 idr_remove(&ctx->io_buffer_idr, bgid);
4164
4165 return i;
4166}
4167
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004168static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004169 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004170{
4171 struct io_provide_buf *p = &req->pbuf;
4172 struct io_ring_ctx *ctx = req->ctx;
4173 struct io_buffer *head;
4174 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004175 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004176
4177 io_ring_submit_lock(ctx, !force_nonblock);
4178
4179 lockdep_assert_held(&ctx->uring_lock);
4180
4181 ret = -ENOENT;
4182 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4183 if (head)
4184 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004185 if (ret < 0)
4186 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004187
4188 /* need to hold the lock to complete IOPOLL requests */
4189 if (ctx->flags & IORING_SETUP_IOPOLL) {
4190 __io_req_complete(req, ret, 0, cs);
4191 io_ring_submit_unlock(ctx, !force_nonblock);
4192 } else {
4193 io_ring_submit_unlock(ctx, !force_nonblock);
4194 __io_req_complete(req, ret, 0, cs);
4195 }
Jens Axboe067524e2020-03-02 16:32:28 -07004196 return 0;
4197}
4198
Jens Axboeddf0322d2020-02-23 16:41:33 -07004199static int io_provide_buffers_prep(struct io_kiocb *req,
4200 const struct io_uring_sqe *sqe)
4201{
4202 struct io_provide_buf *p = &req->pbuf;
4203 u64 tmp;
4204
4205 if (sqe->ioprio || sqe->rw_flags)
4206 return -EINVAL;
4207
4208 tmp = READ_ONCE(sqe->fd);
4209 if (!tmp || tmp > USHRT_MAX)
4210 return -E2BIG;
4211 p->nbufs = tmp;
4212 p->addr = READ_ONCE(sqe->addr);
4213 p->len = READ_ONCE(sqe->len);
4214
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004215 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004216 return -EFAULT;
4217
4218 p->bgid = READ_ONCE(sqe->buf_group);
4219 tmp = READ_ONCE(sqe->off);
4220 if (tmp > USHRT_MAX)
4221 return -E2BIG;
4222 p->bid = tmp;
4223 return 0;
4224}
4225
4226static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4227{
4228 struct io_buffer *buf;
4229 u64 addr = pbuf->addr;
4230 int i, bid = pbuf->bid;
4231
4232 for (i = 0; i < pbuf->nbufs; i++) {
4233 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4234 if (!buf)
4235 break;
4236
4237 buf->addr = addr;
4238 buf->len = pbuf->len;
4239 buf->bid = bid;
4240 addr += pbuf->len;
4241 bid++;
4242 if (!*head) {
4243 INIT_LIST_HEAD(&buf->list);
4244 *head = buf;
4245 } else {
4246 list_add_tail(&buf->list, &(*head)->list);
4247 }
4248 }
4249
4250 return i ? i : -ENOMEM;
4251}
4252
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004253static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004254 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004255{
4256 struct io_provide_buf *p = &req->pbuf;
4257 struct io_ring_ctx *ctx = req->ctx;
4258 struct io_buffer *head, *list;
4259 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004260 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004261
4262 io_ring_submit_lock(ctx, !force_nonblock);
4263
4264 lockdep_assert_held(&ctx->uring_lock);
4265
4266 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4267
4268 ret = io_add_buffers(p, &head);
4269 if (ret < 0)
4270 goto out;
4271
4272 if (!list) {
4273 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4274 GFP_KERNEL);
4275 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004276 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004277 goto out;
4278 }
4279 }
4280out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004281 if (ret < 0)
4282 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004283
4284 /* need to hold the lock to complete IOPOLL requests */
4285 if (ctx->flags & IORING_SETUP_IOPOLL) {
4286 __io_req_complete(req, ret, 0, cs);
4287 io_ring_submit_unlock(ctx, !force_nonblock);
4288 } else {
4289 io_ring_submit_unlock(ctx, !force_nonblock);
4290 __io_req_complete(req, ret, 0, cs);
4291 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004292 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004293}
4294
Jens Axboe3e4827b2020-01-08 15:18:09 -07004295static int io_epoll_ctl_prep(struct io_kiocb *req,
4296 const struct io_uring_sqe *sqe)
4297{
4298#if defined(CONFIG_EPOLL)
4299 if (sqe->ioprio || sqe->buf_index)
4300 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004301 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004302 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004303
4304 req->epoll.epfd = READ_ONCE(sqe->fd);
4305 req->epoll.op = READ_ONCE(sqe->len);
4306 req->epoll.fd = READ_ONCE(sqe->off);
4307
4308 if (ep_op_has_event(req->epoll.op)) {
4309 struct epoll_event __user *ev;
4310
4311 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4312 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4313 return -EFAULT;
4314 }
4315
4316 return 0;
4317#else
4318 return -EOPNOTSUPP;
4319#endif
4320}
4321
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004322static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004323 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004324{
4325#if defined(CONFIG_EPOLL)
4326 struct io_epoll *ie = &req->epoll;
4327 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004328 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004329
4330 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4331 if (force_nonblock && ret == -EAGAIN)
4332 return -EAGAIN;
4333
4334 if (ret < 0)
4335 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004336 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004337 return 0;
4338#else
4339 return -EOPNOTSUPP;
4340#endif
4341}
4342
Jens Axboec1ca7572019-12-25 22:18:28 -07004343static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4344{
4345#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4346 if (sqe->ioprio || sqe->buf_index || sqe->off)
4347 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004348 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4349 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004350
4351 req->madvise.addr = READ_ONCE(sqe->addr);
4352 req->madvise.len = READ_ONCE(sqe->len);
4353 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4354 return 0;
4355#else
4356 return -EOPNOTSUPP;
4357#endif
4358}
4359
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004360static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004361{
4362#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4363 struct io_madvise *ma = &req->madvise;
4364 int ret;
4365
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004366 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004367 return -EAGAIN;
4368
Minchan Kim0726b012020-10-17 16:14:50 -07004369 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004370 if (ret < 0)
4371 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004372 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004373 return 0;
4374#else
4375 return -EOPNOTSUPP;
4376#endif
4377}
4378
Jens Axboe4840e412019-12-25 22:03:45 -07004379static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4380{
4381 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4382 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004383 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4384 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004385
4386 req->fadvise.offset = READ_ONCE(sqe->off);
4387 req->fadvise.len = READ_ONCE(sqe->len);
4388 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4389 return 0;
4390}
4391
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004392static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004393{
4394 struct io_fadvise *fa = &req->fadvise;
4395 int ret;
4396
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004397 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004398 switch (fa->advice) {
4399 case POSIX_FADV_NORMAL:
4400 case POSIX_FADV_RANDOM:
4401 case POSIX_FADV_SEQUENTIAL:
4402 break;
4403 default:
4404 return -EAGAIN;
4405 }
4406 }
Jens Axboe4840e412019-12-25 22:03:45 -07004407
4408 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4409 if (ret < 0)
4410 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004411 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004412 return 0;
4413}
4414
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004415static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4416{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004417 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004418 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004419 if (sqe->ioprio || sqe->buf_index)
4420 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004421 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004422 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004423
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004424 req->statx.dfd = READ_ONCE(sqe->fd);
4425 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004426 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004427 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4428 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004429
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004430 return 0;
4431}
4432
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004433static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004434{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004435 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004436 int ret;
4437
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004438 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004439 /* only need file table for an actual valid fd */
4440 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4441 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004442 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004443 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004444
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004445 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4446 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004447
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004448 if (ret < 0)
4449 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004450 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004451 return 0;
4452}
4453
Jens Axboeb5dba592019-12-11 14:02:38 -07004454static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4455{
Jens Axboe14587a462020-09-05 11:36:08 -06004456 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004457 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004458 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4459 sqe->rw_flags || sqe->buf_index)
4460 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004461 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004462 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004463
4464 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004465 return 0;
4466}
4467
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004468static int io_close(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004469 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004470{
Jens Axboe9eac1902021-01-19 15:50:37 -07004471 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004472 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004473 struct fdtable *fdt;
4474 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004475 int ret;
4476
Jens Axboe9eac1902021-01-19 15:50:37 -07004477 file = NULL;
4478 ret = -EBADF;
4479 spin_lock(&files->file_lock);
4480 fdt = files_fdtable(files);
4481 if (close->fd >= fdt->max_fds) {
4482 spin_unlock(&files->file_lock);
4483 goto err;
4484 }
4485 file = fdt->fd[close->fd];
4486 if (!file) {
4487 spin_unlock(&files->file_lock);
4488 goto err;
4489 }
4490
4491 if (file->f_op == &io_uring_fops) {
4492 spin_unlock(&files->file_lock);
4493 file = NULL;
4494 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004495 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004496
4497 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004498 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004499 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004500 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004501 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004502
Jens Axboe9eac1902021-01-19 15:50:37 -07004503 ret = __close_fd_get_file(close->fd, &file);
4504 spin_unlock(&files->file_lock);
4505 if (ret < 0) {
4506 if (ret == -ENOENT)
4507 ret = -EBADF;
4508 goto err;
4509 }
4510
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004511 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004512 ret = filp_close(file, current->files);
4513err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004514 if (ret < 0)
4515 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004516 if (file)
4517 fput(file);
Jens Axboe229a7b62020-06-22 10:13:11 -06004518 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004519 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004520}
4521
Jens Axboe3529d8c2019-12-19 18:24:38 -07004522static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004523{
4524 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004525
4526 if (!req->file)
4527 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004528
4529 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4530 return -EINVAL;
4531 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4532 return -EINVAL;
4533
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004534 req->sync.off = READ_ONCE(sqe->off);
4535 req->sync.len = READ_ONCE(sqe->len);
4536 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004537 return 0;
4538}
4539
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004540static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004541{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004542 int ret;
4543
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004544 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004545 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004546 return -EAGAIN;
4547
Jens Axboe9adbd452019-12-20 08:45:55 -07004548 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004549 req->sync.flags);
4550 if (ret < 0)
4551 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004552 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004553 return 0;
4554}
4555
YueHaibing469956e2020-03-04 15:53:52 +08004556#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004557static int io_setup_async_msg(struct io_kiocb *req,
4558 struct io_async_msghdr *kmsg)
4559{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004560 struct io_async_msghdr *async_msg = req->async_data;
4561
4562 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004563 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004564 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004565 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004566 return -ENOMEM;
4567 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004568 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004569 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004570 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004571 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004572 /* if were using fast_iov, set it to the new one */
4573 if (!async_msg->free_iov)
4574 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4575
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004576 return -EAGAIN;
4577}
4578
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004579static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4580 struct io_async_msghdr *iomsg)
4581{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004582 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004583 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004584 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004585 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004586}
4587
Jens Axboe3529d8c2019-12-19 18:24:38 -07004588static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004589{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004590 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004591 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004592 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004593
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004594 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4595 return -EINVAL;
4596
Jens Axboee47293f2019-12-20 08:58:21 -07004597 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004598 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004599 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004600
Jens Axboed8768362020-02-27 14:17:49 -07004601#ifdef CONFIG_COMPAT
4602 if (req->ctx->compat)
4603 sr->msg_flags |= MSG_CMSG_COMPAT;
4604#endif
4605
Jens Axboee8c2bc12020-08-15 18:44:09 -07004606 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004607 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004608 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004609 if (!ret)
4610 req->flags |= REQ_F_NEED_CLEANUP;
4611 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004612}
4613
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004614static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004615 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004616{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004617 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004618 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004619 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004620 int ret;
4621
Florent Revestdba4a922020-12-04 12:36:04 +01004622 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004623 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004624 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004625
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004626 kmsg = req->async_data;
4627 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004629 if (ret)
4630 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004632 }
4633
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004634 flags = req->sr_msg.msg_flags;
4635 if (flags & MSG_DONTWAIT)
4636 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004637 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004638 flags |= MSG_DONTWAIT;
4639
4640 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004641 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004642 return io_setup_async_msg(req, kmsg);
4643 if (ret == -ERESTARTSYS)
4644 ret = -EINTR;
4645
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004646 /* fast path, check for non-NULL to avoid function call */
4647 if (kmsg->free_iov)
4648 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004649 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004650 if (ret < 0)
4651 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004652 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004653 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004654}
4655
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004656static int io_send(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004657 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004658{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004659 struct io_sr_msg *sr = &req->sr_msg;
4660 struct msghdr msg;
4661 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004662 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004663 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004664 int ret;
4665
Florent Revestdba4a922020-12-04 12:36:04 +01004666 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004667 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004668 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004669
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004670 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4671 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004672 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004673
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004674 msg.msg_name = NULL;
4675 msg.msg_control = NULL;
4676 msg.msg_controllen = 0;
4677 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004678
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004679 flags = req->sr_msg.msg_flags;
4680 if (flags & MSG_DONTWAIT)
4681 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004682 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004683 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004684
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004685 msg.msg_flags = flags;
4686 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004687 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004688 return -EAGAIN;
4689 if (ret == -ERESTARTSYS)
4690 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004691
Jens Axboe03b12302019-12-02 18:50:25 -07004692 if (ret < 0)
4693 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004694 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004695 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004696}
4697
Pavel Begunkov1400e692020-07-12 20:41:05 +03004698static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4699 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004700{
4701 struct io_sr_msg *sr = &req->sr_msg;
4702 struct iovec __user *uiov;
4703 size_t iov_len;
4704 int ret;
4705
Pavel Begunkov1400e692020-07-12 20:41:05 +03004706 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4707 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004708 if (ret)
4709 return ret;
4710
4711 if (req->flags & REQ_F_BUFFER_SELECT) {
4712 if (iov_len > 1)
4713 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004714 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004715 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004716 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004717 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004718 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004719 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004720 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004721 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004722 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004723 if (ret > 0)
4724 ret = 0;
4725 }
4726
4727 return ret;
4728}
4729
4730#ifdef CONFIG_COMPAT
4731static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004732 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004733{
4734 struct compat_msghdr __user *msg_compat;
4735 struct io_sr_msg *sr = &req->sr_msg;
4736 struct compat_iovec __user *uiov;
4737 compat_uptr_t ptr;
4738 compat_size_t len;
4739 int ret;
4740
Pavel Begunkov270a5942020-07-12 20:41:04 +03004741 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004742 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004743 &ptr, &len);
4744 if (ret)
4745 return ret;
4746
4747 uiov = compat_ptr(ptr);
4748 if (req->flags & REQ_F_BUFFER_SELECT) {
4749 compat_ssize_t clen;
4750
4751 if (len > 1)
4752 return -EINVAL;
4753 if (!access_ok(uiov, sizeof(*uiov)))
4754 return -EFAULT;
4755 if (__get_user(clen, &uiov->iov_len))
4756 return -EFAULT;
4757 if (clen < 0)
4758 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004759 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004760 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004761 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004762 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004763 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004764 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004765 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004766 if (ret < 0)
4767 return ret;
4768 }
4769
4770 return 0;
4771}
Jens Axboe03b12302019-12-02 18:50:25 -07004772#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004773
Pavel Begunkov1400e692020-07-12 20:41:05 +03004774static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4775 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004776{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004777 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004778
4779#ifdef CONFIG_COMPAT
4780 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004781 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004782#endif
4783
Pavel Begunkov1400e692020-07-12 20:41:05 +03004784 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004785}
4786
Jens Axboebcda7ba2020-02-23 16:42:51 -07004787static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004788 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004789{
4790 struct io_sr_msg *sr = &req->sr_msg;
4791 struct io_buffer *kbuf;
4792
Jens Axboebcda7ba2020-02-23 16:42:51 -07004793 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4794 if (IS_ERR(kbuf))
4795 return kbuf;
4796
4797 sr->kbuf = kbuf;
4798 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004799 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004800}
4801
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004802static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4803{
4804 return io_put_kbuf(req, req->sr_msg.kbuf);
4805}
4806
Jens Axboe3529d8c2019-12-19 18:24:38 -07004807static int io_recvmsg_prep(struct io_kiocb *req,
4808 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004809{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004810 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004811 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004812 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004813
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004814 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4815 return -EINVAL;
4816
Jens Axboe3529d8c2019-12-19 18:24:38 -07004817 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004818 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004819 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004820 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004821
Jens Axboed8768362020-02-27 14:17:49 -07004822#ifdef CONFIG_COMPAT
4823 if (req->ctx->compat)
4824 sr->msg_flags |= MSG_CMSG_COMPAT;
4825#endif
4826
Jens Axboee8c2bc12020-08-15 18:44:09 -07004827 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004828 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004829 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004830 if (!ret)
4831 req->flags |= REQ_F_NEED_CLEANUP;
4832 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004833}
4834
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004835static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004836 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004837{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004838 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004839 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004840 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004841 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004842 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004843 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004844
Florent Revestdba4a922020-12-04 12:36:04 +01004845 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004846 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004847 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004848
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004849 kmsg = req->async_data;
4850 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004851 ret = io_recvmsg_copy_hdr(req, &iomsg);
4852 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004853 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004854 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004855 }
4856
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004857 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004858 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004859 if (IS_ERR(kbuf))
4860 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004861 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004862 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4863 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004864 1, req->sr_msg.len);
4865 }
4866
4867 flags = req->sr_msg.msg_flags;
4868 if (flags & MSG_DONTWAIT)
4869 req->flags |= REQ_F_NOWAIT;
4870 else if (force_nonblock)
4871 flags |= MSG_DONTWAIT;
4872
4873 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4874 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004875 if (force_nonblock && ret == -EAGAIN)
4876 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004877 if (ret == -ERESTARTSYS)
4878 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004879
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004880 if (req->flags & REQ_F_BUFFER_SELECTED)
4881 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004882 /* fast path, check for non-NULL to avoid function call */
4883 if (kmsg->free_iov)
4884 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004885 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004886 if (ret < 0)
4887 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004888 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004889 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004890}
4891
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004892static int io_recv(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004893 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004894{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004895 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004896 struct io_sr_msg *sr = &req->sr_msg;
4897 struct msghdr msg;
4898 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004899 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004900 struct iovec iov;
4901 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004902 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004903 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004904
Florent Revestdba4a922020-12-04 12:36:04 +01004905 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004906 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004907 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004908
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004909 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004910 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004911 if (IS_ERR(kbuf))
4912 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004913 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004914 }
4915
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004916 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004917 if (unlikely(ret))
4918 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004919
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004920 msg.msg_name = NULL;
4921 msg.msg_control = NULL;
4922 msg.msg_controllen = 0;
4923 msg.msg_namelen = 0;
4924 msg.msg_iocb = NULL;
4925 msg.msg_flags = 0;
4926
4927 flags = req->sr_msg.msg_flags;
4928 if (flags & MSG_DONTWAIT)
4929 req->flags |= REQ_F_NOWAIT;
4930 else if (force_nonblock)
4931 flags |= MSG_DONTWAIT;
4932
4933 ret = sock_recvmsg(sock, &msg, flags);
4934 if (force_nonblock && ret == -EAGAIN)
4935 return -EAGAIN;
4936 if (ret == -ERESTARTSYS)
4937 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004938out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004939 if (req->flags & REQ_F_BUFFER_SELECTED)
4940 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004941 if (ret < 0)
4942 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004943 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004944 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004945}
4946
Jens Axboe3529d8c2019-12-19 18:24:38 -07004947static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004948{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004949 struct io_accept *accept = &req->accept;
4950
Jens Axboe14587a462020-09-05 11:36:08 -06004951 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004952 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004953 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004954 return -EINVAL;
4955
Jens Axboed55e5f52019-12-11 16:12:15 -07004956 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4957 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004958 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004959 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004960 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004961}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004962
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004963static int io_accept(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06004964 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004965{
4966 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004967 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004968 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004969 int ret;
4970
Jiufei Xuee697dee2020-06-10 13:41:59 +08004971 if (req->file->f_flags & O_NONBLOCK)
4972 req->flags |= REQ_F_NOWAIT;
4973
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004974 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004975 accept->addr_len, accept->flags,
4976 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004977 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004978 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004979 if (ret < 0) {
4980 if (ret == -ERESTARTSYS)
4981 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004982 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004983 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004984 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004985 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004986}
4987
Jens Axboe3529d8c2019-12-19 18:24:38 -07004988static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004989{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004990 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004991 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004992
Jens Axboe14587a462020-09-05 11:36:08 -06004993 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004994 return -EINVAL;
4995 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4996 return -EINVAL;
4997
Jens Axboe3529d8c2019-12-19 18:24:38 -07004998 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4999 conn->addr_len = READ_ONCE(sqe->addr2);
5000
5001 if (!io)
5002 return 0;
5003
5004 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005005 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005006}
5007
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005008static int io_connect(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06005009 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005010{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005011 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005012 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005013 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005014 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005015
Jens Axboee8c2bc12020-08-15 18:44:09 -07005016 if (req->async_data) {
5017 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005018 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005019 ret = move_addr_to_kernel(req->connect.addr,
5020 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005021 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005022 if (ret)
5023 goto out;
5024 io = &__io;
5025 }
5026
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005027 file_flags = force_nonblock ? O_NONBLOCK : 0;
5028
Jens Axboee8c2bc12020-08-15 18:44:09 -07005029 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005030 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005031 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005032 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005033 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005034 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005035 ret = -ENOMEM;
5036 goto out;
5037 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005038 io = req->async_data;
5039 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005040 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005041 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005042 if (ret == -ERESTARTSYS)
5043 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005044out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005045 if (ret < 0)
5046 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005047 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005048 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005049}
YueHaibing469956e2020-03-04 15:53:52 +08005050#else /* !CONFIG_NET */
5051static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5052{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005053 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005054}
5055
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005056static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005057 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005058{
YueHaibing469956e2020-03-04 15:53:52 +08005059 return -EOPNOTSUPP;
5060}
5061
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005062static int io_send(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005063 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005064{
5065 return -EOPNOTSUPP;
5066}
5067
5068static int io_recvmsg_prep(struct io_kiocb *req,
5069 const struct io_uring_sqe *sqe)
5070{
5071 return -EOPNOTSUPP;
5072}
5073
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005074static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005075 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005076{
5077 return -EOPNOTSUPP;
5078}
5079
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005080static int io_recv(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005081 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005082{
5083 return -EOPNOTSUPP;
5084}
5085
5086static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5087{
5088 return -EOPNOTSUPP;
5089}
5090
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005091static int io_accept(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005092 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005093{
5094 return -EOPNOTSUPP;
5095}
5096
5097static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5098{
5099 return -EOPNOTSUPP;
5100}
5101
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005102static int io_connect(struct io_kiocb *req, unsigned int issue_flags,
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005103 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005104{
5105 return -EOPNOTSUPP;
5106}
5107#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005108
Jens Axboed7718a92020-02-14 22:23:12 -07005109struct io_poll_table {
5110 struct poll_table_struct pt;
5111 struct io_kiocb *req;
5112 int error;
5113};
5114
Jens Axboed7718a92020-02-14 22:23:12 -07005115static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5116 __poll_t mask, task_work_func_t func)
5117{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005118 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005119
5120 /* for instances that support it check for an event match first: */
5121 if (mask && !(mask & poll->events))
5122 return 0;
5123
5124 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5125
5126 list_del_init(&poll->wait.entry);
5127
Jens Axboed7718a92020-02-14 22:23:12 -07005128 req->result = mask;
5129 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005130 percpu_ref_get(&req->ctx->refs);
5131
Jens Axboed7718a92020-02-14 22:23:12 -07005132 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005133 * If this fails, then the task is exiting. When a task exits, the
5134 * work gets canceled, so just cancel this request as well instead
5135 * of executing it. We can't safely execute it anyway, as we may not
5136 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005137 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005138 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005139 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005140 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005141 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005142 }
Jens Axboed7718a92020-02-14 22:23:12 -07005143 return 1;
5144}
5145
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005146static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5147 __acquires(&req->ctx->completion_lock)
5148{
5149 struct io_ring_ctx *ctx = req->ctx;
5150
5151 if (!req->result && !READ_ONCE(poll->canceled)) {
5152 struct poll_table_struct pt = { ._key = poll->events };
5153
5154 req->result = vfs_poll(req->file, &pt) & poll->events;
5155 }
5156
5157 spin_lock_irq(&ctx->completion_lock);
5158 if (!req->result && !READ_ONCE(poll->canceled)) {
5159 add_wait_queue(poll->head, &poll->wait);
5160 return true;
5161 }
5162
5163 return false;
5164}
5165
Jens Axboed4e7cd32020-08-15 11:44:50 -07005166static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005167{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005168 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005169 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005170 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005171 return req->apoll->double_poll;
5172}
5173
5174static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5175{
5176 if (req->opcode == IORING_OP_POLL_ADD)
5177 return &req->poll;
5178 return &req->apoll->poll;
5179}
5180
5181static void io_poll_remove_double(struct io_kiocb *req)
5182{
5183 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005184
5185 lockdep_assert_held(&req->ctx->completion_lock);
5186
5187 if (poll && poll->head) {
5188 struct wait_queue_head *head = poll->head;
5189
5190 spin_lock(&head->lock);
5191 list_del_init(&poll->wait.entry);
5192 if (poll->wait.private)
5193 refcount_dec(&req->refs);
5194 poll->head = NULL;
5195 spin_unlock(&head->lock);
5196 }
5197}
5198
5199static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5200{
5201 struct io_ring_ctx *ctx = req->ctx;
5202
Jens Axboed4e7cd32020-08-15 11:44:50 -07005203 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005204 req->poll.done = true;
5205 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5206 io_commit_cqring(ctx);
5207}
5208
Jens Axboe18bceab2020-05-15 11:56:54 -06005209static void io_poll_task_func(struct callback_head *cb)
5210{
5211 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005212 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005213 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005214
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005215 if (io_poll_rewait(req, &req->poll)) {
5216 spin_unlock_irq(&ctx->completion_lock);
5217 } else {
5218 hash_del(&req->hash_node);
5219 io_poll_complete(req, req->result, 0);
5220 spin_unlock_irq(&ctx->completion_lock);
5221
5222 nxt = io_put_req_find_next(req);
5223 io_cqring_ev_posted(ctx);
5224 if (nxt)
5225 __io_req_task_submit(nxt);
5226 }
5227
Jens Axboe6d816e02020-08-11 08:04:14 -06005228 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005229}
5230
5231static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5232 int sync, void *key)
5233{
5234 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005235 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005236 __poll_t mask = key_to_poll(key);
5237
5238 /* for instances that support it check for an event match first: */
5239 if (mask && !(mask & poll->events))
5240 return 0;
5241
Jens Axboe8706e042020-09-28 08:38:54 -06005242 list_del_init(&wait->entry);
5243
Jens Axboe807abcb2020-07-17 17:09:27 -06005244 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005245 bool done;
5246
Jens Axboe807abcb2020-07-17 17:09:27 -06005247 spin_lock(&poll->head->lock);
5248 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005249 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005250 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005251 /* make sure double remove sees this as being gone */
5252 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005253 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005254 if (!done) {
5255 /* use wait func handler, so it matches the rq type */
5256 poll->wait.func(&poll->wait, mode, sync, key);
5257 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005258 }
5259 refcount_dec(&req->refs);
5260 return 1;
5261}
5262
5263static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5264 wait_queue_func_t wake_func)
5265{
5266 poll->head = NULL;
5267 poll->done = false;
5268 poll->canceled = false;
5269 poll->events = events;
5270 INIT_LIST_HEAD(&poll->wait.entry);
5271 init_waitqueue_func_entry(&poll->wait, wake_func);
5272}
5273
5274static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005275 struct wait_queue_head *head,
5276 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005277{
5278 struct io_kiocb *req = pt->req;
5279
5280 /*
5281 * If poll->head is already set, it's because the file being polled
5282 * uses multiple waitqueues for poll handling (eg one for read, one
5283 * for write). Setup a separate io_poll_iocb if this happens.
5284 */
5285 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005286 struct io_poll_iocb *poll_one = poll;
5287
Jens Axboe18bceab2020-05-15 11:56:54 -06005288 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005289 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005290 pt->error = -EINVAL;
5291 return;
5292 }
5293 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5294 if (!poll) {
5295 pt->error = -ENOMEM;
5296 return;
5297 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005298 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005299 refcount_inc(&req->refs);
5300 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005301 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005302 }
5303
5304 pt->error = 0;
5305 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005306
5307 if (poll->events & EPOLLEXCLUSIVE)
5308 add_wait_queue_exclusive(head, &poll->wait);
5309 else
5310 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005311}
5312
5313static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5314 struct poll_table_struct *p)
5315{
5316 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005317 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005318
Jens Axboe807abcb2020-07-17 17:09:27 -06005319 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005320}
5321
Jens Axboed7718a92020-02-14 22:23:12 -07005322static void io_async_task_func(struct callback_head *cb)
5323{
5324 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5325 struct async_poll *apoll = req->apoll;
5326 struct io_ring_ctx *ctx = req->ctx;
5327
5328 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5329
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005330 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005331 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005332 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005333 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005334 }
5335
Jens Axboe31067252020-05-17 17:43:31 -06005336 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005337 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005338 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005339
Jens Axboed4e7cd32020-08-15 11:44:50 -07005340 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005341 spin_unlock_irq(&ctx->completion_lock);
5342
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005343 if (!READ_ONCE(apoll->poll.canceled))
5344 __io_req_task_submit(req);
5345 else
5346 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005347
Jens Axboe6d816e02020-08-11 08:04:14 -06005348 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005349 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005350 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005351}
5352
5353static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5354 void *key)
5355{
5356 struct io_kiocb *req = wait->private;
5357 struct io_poll_iocb *poll = &req->apoll->poll;
5358
5359 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5360 key_to_poll(key));
5361
5362 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5363}
5364
5365static void io_poll_req_insert(struct io_kiocb *req)
5366{
5367 struct io_ring_ctx *ctx = req->ctx;
5368 struct hlist_head *list;
5369
5370 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5371 hlist_add_head(&req->hash_node, list);
5372}
5373
5374static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5375 struct io_poll_iocb *poll,
5376 struct io_poll_table *ipt, __poll_t mask,
5377 wait_queue_func_t wake_func)
5378 __acquires(&ctx->completion_lock)
5379{
5380 struct io_ring_ctx *ctx = req->ctx;
5381 bool cancel = false;
5382
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005383 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005384 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005385 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005386 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005387
5388 ipt->pt._key = mask;
5389 ipt->req = req;
5390 ipt->error = -EINVAL;
5391
Jens Axboed7718a92020-02-14 22:23:12 -07005392 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5393
5394 spin_lock_irq(&ctx->completion_lock);
5395 if (likely(poll->head)) {
5396 spin_lock(&poll->head->lock);
5397 if (unlikely(list_empty(&poll->wait.entry))) {
5398 if (ipt->error)
5399 cancel = true;
5400 ipt->error = 0;
5401 mask = 0;
5402 }
5403 if (mask || ipt->error)
5404 list_del_init(&poll->wait.entry);
5405 else if (cancel)
5406 WRITE_ONCE(poll->canceled, true);
5407 else if (!poll->done) /* actually waiting for an event */
5408 io_poll_req_insert(req);
5409 spin_unlock(&poll->head->lock);
5410 }
5411
5412 return mask;
5413}
5414
5415static bool io_arm_poll_handler(struct io_kiocb *req)
5416{
5417 const struct io_op_def *def = &io_op_defs[req->opcode];
5418 struct io_ring_ctx *ctx = req->ctx;
5419 struct async_poll *apoll;
5420 struct io_poll_table ipt;
5421 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005422 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005423
5424 if (!req->file || !file_can_poll(req->file))
5425 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005426 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005427 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005428 if (def->pollin)
5429 rw = READ;
5430 else if (def->pollout)
5431 rw = WRITE;
5432 else
5433 return false;
5434 /* if we can't nonblock try, then no point in arming a poll handler */
5435 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005436 return false;
5437
5438 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5439 if (unlikely(!apoll))
5440 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005441 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005442
5443 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005444 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005445
Nathan Chancellor8755d972020-03-02 16:01:19 -07005446 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005447 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005448 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005449 if (def->pollout)
5450 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005451
5452 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5453 if ((req->opcode == IORING_OP_RECVMSG) &&
5454 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5455 mask &= ~POLLIN;
5456
Jens Axboed7718a92020-02-14 22:23:12 -07005457 mask |= POLLERR | POLLPRI;
5458
5459 ipt.pt._qproc = io_async_queue_proc;
5460
5461 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5462 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005463 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005464 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005465 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005466 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005467 kfree(apoll);
5468 return false;
5469 }
5470 spin_unlock_irq(&ctx->completion_lock);
5471 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5472 apoll->poll.events);
5473 return true;
5474}
5475
5476static bool __io_poll_remove_one(struct io_kiocb *req,
5477 struct io_poll_iocb *poll)
5478{
Jens Axboeb41e9852020-02-17 09:52:41 -07005479 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005480
5481 spin_lock(&poll->head->lock);
5482 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005483 if (!list_empty(&poll->wait.entry)) {
5484 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005485 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005486 }
5487 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005488 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005489 return do_complete;
5490}
5491
5492static bool io_poll_remove_one(struct io_kiocb *req)
5493{
5494 bool do_complete;
5495
Jens Axboed4e7cd32020-08-15 11:44:50 -07005496 io_poll_remove_double(req);
5497
Jens Axboed7718a92020-02-14 22:23:12 -07005498 if (req->opcode == IORING_OP_POLL_ADD) {
5499 do_complete = __io_poll_remove_one(req, &req->poll);
5500 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005501 struct async_poll *apoll = req->apoll;
5502
Jens Axboed7718a92020-02-14 22:23:12 -07005503 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005504 do_complete = __io_poll_remove_one(req, &apoll->poll);
5505 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005506 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005507 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005508 kfree(apoll);
5509 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005510 }
5511
Jens Axboeb41e9852020-02-17 09:52:41 -07005512 if (do_complete) {
5513 io_cqring_fill_event(req, -ECANCELED);
5514 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005515 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005516 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005517 }
5518
5519 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005520}
5521
Jens Axboe76e1b642020-09-26 15:05:03 -06005522/*
5523 * Returns true if we found and killed one or more poll requests
5524 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005525static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5526 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005527{
Jens Axboe78076bb2019-12-04 19:56:40 -07005528 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005529 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005530 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005531
5532 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005533 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5534 struct hlist_head *list;
5535
5536 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005537 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005538 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005539 posted += io_poll_remove_one(req);
5540 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005541 }
5542 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005543
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005544 if (posted)
5545 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005546
5547 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005548}
5549
Jens Axboe47f46762019-11-09 17:43:02 -07005550static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5551{
Jens Axboe78076bb2019-12-04 19:56:40 -07005552 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005553 struct io_kiocb *req;
5554
Jens Axboe78076bb2019-12-04 19:56:40 -07005555 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5556 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005557 if (sqe_addr != req->user_data)
5558 continue;
5559 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005560 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005561 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005562 }
5563
5564 return -ENOENT;
5565}
5566
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567static int io_poll_remove_prep(struct io_kiocb *req,
5568 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005569{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005570 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5571 return -EINVAL;
5572 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5573 sqe->poll_events)
5574 return -EINVAL;
5575
Pavel Begunkov018043b2020-10-27 23:17:18 +00005576 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005577 return 0;
5578}
5579
5580/*
5581 * Find a running poll command that matches one specified in sqe->addr,
5582 * and remove it if found.
5583 */
5584static int io_poll_remove(struct io_kiocb *req)
5585{
5586 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005587 int ret;
5588
Jens Axboe221c5eb2019-01-17 09:41:58 -07005589 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005590 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005591 spin_unlock_irq(&ctx->completion_lock);
5592
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005593 if (ret < 0)
5594 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005595 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005596 return 0;
5597}
5598
Jens Axboe221c5eb2019-01-17 09:41:58 -07005599static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5600 void *key)
5601{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005602 struct io_kiocb *req = wait->private;
5603 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005604
Jens Axboed7718a92020-02-14 22:23:12 -07005605 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005606}
5607
Jens Axboe221c5eb2019-01-17 09:41:58 -07005608static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5609 struct poll_table_struct *p)
5610{
5611 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5612
Jens Axboee8c2bc12020-08-15 18:44:09 -07005613 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005614}
5615
Jens Axboe3529d8c2019-12-19 18:24:38 -07005616static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005617{
5618 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005619 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005620
5621 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5622 return -EINVAL;
5623 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5624 return -EINVAL;
5625
Jiufei Xue5769a352020-06-17 17:53:55 +08005626 events = READ_ONCE(sqe->poll32_events);
5627#ifdef __BIG_ENDIAN
5628 events = swahw32(events);
5629#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005630 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5631 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005632 return 0;
5633}
5634
Pavel Begunkov014db002020-03-03 21:33:12 +03005635static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005636{
5637 struct io_poll_iocb *poll = &req->poll;
5638 struct io_ring_ctx *ctx = req->ctx;
5639 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005640 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005641
Jens Axboed7718a92020-02-14 22:23:12 -07005642 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005643
Jens Axboed7718a92020-02-14 22:23:12 -07005644 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5645 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005646
Jens Axboe8c838782019-03-12 15:48:16 -06005647 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005648 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005649 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005650 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005651 spin_unlock_irq(&ctx->completion_lock);
5652
Jens Axboe8c838782019-03-12 15:48:16 -06005653 if (mask) {
5654 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005655 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005656 }
Jens Axboe8c838782019-03-12 15:48:16 -06005657 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005658}
5659
Jens Axboe5262f562019-09-17 12:26:57 -06005660static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5661{
Jens Axboead8a48a2019-11-15 08:49:11 -07005662 struct io_timeout_data *data = container_of(timer,
5663 struct io_timeout_data, timer);
5664 struct io_kiocb *req = data->req;
5665 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005666 unsigned long flags;
5667
Jens Axboe5262f562019-09-17 12:26:57 -06005668 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005669 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005670 atomic_set(&req->ctx->cq_timeouts,
5671 atomic_read(&req->ctx->cq_timeouts) + 1);
5672
Jens Axboe78e19bb2019-11-06 15:21:34 -07005673 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005674 io_commit_cqring(ctx);
5675 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5676
5677 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005678 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005679 io_put_req(req);
5680 return HRTIMER_NORESTART;
5681}
5682
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005683static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5684 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005685{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005686 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005687 struct io_kiocb *req;
5688 int ret = -ENOENT;
5689
5690 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5691 if (user_data == req->user_data) {
5692 ret = 0;
5693 break;
5694 }
5695 }
5696
5697 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005698 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005699
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005700 io = req->async_data;
5701 ret = hrtimer_try_to_cancel(&io->timer);
5702 if (ret == -1)
5703 return ERR_PTR(-EALREADY);
5704 list_del_init(&req->timeout.list);
5705 return req;
5706}
5707
5708static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5709{
5710 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5711
5712 if (IS_ERR(req))
5713 return PTR_ERR(req);
5714
5715 req_set_fail_links(req);
5716 io_cqring_fill_event(req, -ECANCELED);
5717 io_put_req_deferred(req, 1);
5718 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005719}
5720
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005721static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5722 struct timespec64 *ts, enum hrtimer_mode mode)
5723{
5724 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5725 struct io_timeout_data *data;
5726
5727 if (IS_ERR(req))
5728 return PTR_ERR(req);
5729
5730 req->timeout.off = 0; /* noseq */
5731 data = req->async_data;
5732 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5733 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5734 data->timer.function = io_timeout_fn;
5735 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5736 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005737}
5738
Jens Axboe3529d8c2019-12-19 18:24:38 -07005739static int io_timeout_remove_prep(struct io_kiocb *req,
5740 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005741{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005742 struct io_timeout_rem *tr = &req->timeout_rem;
5743
Jens Axboeb29472e2019-12-17 18:50:29 -07005744 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5745 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005746 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5747 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005748 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005749 return -EINVAL;
5750
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005751 tr->addr = READ_ONCE(sqe->addr);
5752 tr->flags = READ_ONCE(sqe->timeout_flags);
5753 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5754 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5755 return -EINVAL;
5756 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5757 return -EFAULT;
5758 } else if (tr->flags) {
5759 /* timeout removal doesn't support flags */
5760 return -EINVAL;
5761 }
5762
Jens Axboeb29472e2019-12-17 18:50:29 -07005763 return 0;
5764}
5765
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005766static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5767{
5768 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5769 : HRTIMER_MODE_REL;
5770}
5771
Jens Axboe11365042019-10-16 09:08:32 -06005772/*
5773 * Remove or update an existing timeout command
5774 */
Jens Axboefc4df992019-12-10 14:38:45 -07005775static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005776{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005777 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005778 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005779 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005780
Jens Axboe11365042019-10-16 09:08:32 -06005781 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005782 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005783 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005784 else
5785 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5786 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005787
Jens Axboe47f46762019-11-09 17:43:02 -07005788 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005789 io_commit_cqring(ctx);
5790 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005791 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005792 if (ret < 0)
5793 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005794 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005795 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005796}
5797
Jens Axboe3529d8c2019-12-19 18:24:38 -07005798static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005799 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005800{
Jens Axboead8a48a2019-11-15 08:49:11 -07005801 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005802 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005803 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005804
Jens Axboead8a48a2019-11-15 08:49:11 -07005805 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005806 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005807 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005808 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005809 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005810 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005811 flags = READ_ONCE(sqe->timeout_flags);
5812 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005813 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005814
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005815 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005816
Jens Axboee8c2bc12020-08-15 18:44:09 -07005817 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005818 return -ENOMEM;
5819
Jens Axboee8c2bc12020-08-15 18:44:09 -07005820 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005821 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005822
5823 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005824 return -EFAULT;
5825
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005826 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005827 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5828 return 0;
5829}
5830
Jens Axboefc4df992019-12-10 14:38:45 -07005831static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005832{
Jens Axboead8a48a2019-11-15 08:49:11 -07005833 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005834 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005835 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005836 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005837
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005838 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005839
Jens Axboe5262f562019-09-17 12:26:57 -06005840 /*
5841 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005842 * timeout event to be satisfied. If it isn't set, then this is
5843 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005844 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005845 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005846 entry = ctx->timeout_list.prev;
5847 goto add;
5848 }
Jens Axboe5262f562019-09-17 12:26:57 -06005849
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005850 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5851 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005852
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005853 /* Update the last seq here in case io_flush_timeouts() hasn't.
5854 * This is safe because ->completion_lock is held, and submissions
5855 * and completions are never mixed in the same ->completion_lock section.
5856 */
5857 ctx->cq_last_tm_flush = tail;
5858
Jens Axboe5262f562019-09-17 12:26:57 -06005859 /*
5860 * Insertion sort, ensuring the first entry in the list is always
5861 * the one we need first.
5862 */
Jens Axboe5262f562019-09-17 12:26:57 -06005863 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005864 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5865 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005866
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005867 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005868 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005869 /* nxt.seq is behind @tail, otherwise would've been completed */
5870 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005871 break;
5872 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005873add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005874 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005875 data->timer.function = io_timeout_fn;
5876 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005877 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005878 return 0;
5879}
5880
Jens Axboe62755e32019-10-28 21:49:21 -06005881static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005882{
Jens Axboe62755e32019-10-28 21:49:21 -06005883 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005884
Jens Axboe62755e32019-10-28 21:49:21 -06005885 return req->user_data == (unsigned long) data;
5886}
5887
Jens Axboee977d6d2019-11-05 12:39:45 -07005888static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005889{
Jens Axboe62755e32019-10-28 21:49:21 -06005890 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005891 int ret = 0;
5892
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005893 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005894 switch (cancel_ret) {
5895 case IO_WQ_CANCEL_OK:
5896 ret = 0;
5897 break;
5898 case IO_WQ_CANCEL_RUNNING:
5899 ret = -EALREADY;
5900 break;
5901 case IO_WQ_CANCEL_NOTFOUND:
5902 ret = -ENOENT;
5903 break;
5904 }
5905
Jens Axboee977d6d2019-11-05 12:39:45 -07005906 return ret;
5907}
5908
Jens Axboe47f46762019-11-09 17:43:02 -07005909static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5910 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005911 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005912{
5913 unsigned long flags;
5914 int ret;
5915
5916 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5917 if (ret != -ENOENT) {
5918 spin_lock_irqsave(&ctx->completion_lock, flags);
5919 goto done;
5920 }
5921
5922 spin_lock_irqsave(&ctx->completion_lock, flags);
5923 ret = io_timeout_cancel(ctx, sqe_addr);
5924 if (ret != -ENOENT)
5925 goto done;
5926 ret = io_poll_cancel(ctx, sqe_addr);
5927done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005928 if (!ret)
5929 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005930 io_cqring_fill_event(req, ret);
5931 io_commit_cqring(ctx);
5932 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5933 io_cqring_ev_posted(ctx);
5934
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005935 if (ret < 0)
5936 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005937 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005938}
5939
Jens Axboe3529d8c2019-12-19 18:24:38 -07005940static int io_async_cancel_prep(struct io_kiocb *req,
5941 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005942{
Jens Axboefbf23842019-12-17 18:45:56 -07005943 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005944 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005945 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5946 return -EINVAL;
5947 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005948 return -EINVAL;
5949
Jens Axboefbf23842019-12-17 18:45:56 -07005950 req->cancel.addr = READ_ONCE(sqe->addr);
5951 return 0;
5952}
5953
Pavel Begunkov014db002020-03-03 21:33:12 +03005954static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005955{
5956 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005957
Pavel Begunkov014db002020-03-03 21:33:12 +03005958 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005959 return 0;
5960}
5961
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005962static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005963 const struct io_uring_sqe *sqe)
5964{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005965 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5966 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005967 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5968 return -EINVAL;
5969 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005970 return -EINVAL;
5971
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005972 req->rsrc_update.offset = READ_ONCE(sqe->off);
5973 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5974 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005975 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005976 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005977 return 0;
5978}
5979
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005980static int io_files_update(struct io_kiocb *req, unsigned int issue_flags,
Jens Axboe229a7b62020-06-22 10:13:11 -06005981 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005982{
5983 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005984 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005985 int ret;
5986
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005987 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005988 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005989
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005990 up.offset = req->rsrc_update.offset;
5991 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005992
5993 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005994 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005995 mutex_unlock(&ctx->uring_lock);
5996
5997 if (ret < 0)
5998 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005999 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006000 return 0;
6001}
6002
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006003static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006004{
Jens Axboed625c6e2019-12-17 19:53:05 -07006005 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006006 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006007 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006008 case IORING_OP_READV:
6009 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006010 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006011 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006012 case IORING_OP_WRITEV:
6013 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006014 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006015 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006016 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006017 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006018 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006019 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006020 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006021 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006022 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006023 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006024 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006025 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006026 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006027 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006028 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006029 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006030 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006031 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006032 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006033 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006034 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006035 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006036 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006037 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006038 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006039 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006040 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006041 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006042 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006043 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006044 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006045 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006046 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006047 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006048 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006049 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006050 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006051 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006052 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006053 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006054 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006055 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006056 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006057 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006058 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006059 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006060 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006061 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006062 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006063 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006064 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006065 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006066 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006067 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006068 case IORING_OP_SHUTDOWN:
6069 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006070 case IORING_OP_RENAMEAT:
6071 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006072 case IORING_OP_UNLINKAT:
6073 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006074 }
6075
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006076 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6077 req->opcode);
6078 return-EINVAL;
6079}
6080
Jens Axboedef596e2019-01-09 08:59:42 -07006081static int io_req_defer_prep(struct io_kiocb *req,
6082 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006083{
Jens Axboedef596e2019-01-09 08:59:42 -07006084 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006086 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006087 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006088 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006089}
6090
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006091static u32 io_get_sequence(struct io_kiocb *req)
6092{
6093 struct io_kiocb *pos;
6094 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006095 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006096
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006097 io_for_each_link(pos, req)
6098 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006099
6100 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6101 return total_submitted - nr_reqs;
6102}
6103
Jens Axboe3529d8c2019-12-19 18:24:38 -07006104static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006105{
6106 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006107 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006108 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006109 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110
6111 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006112 if (likely(list_empty_careful(&ctx->defer_list) &&
6113 !(req->flags & REQ_F_IO_DRAIN)))
6114 return 0;
6115
6116 seq = io_get_sequence(req);
6117 /* Still a chance to pass the sequence check */
6118 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119 return 0;
6120
Jens Axboee8c2bc12020-08-15 18:44:09 -07006121 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006122 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006123 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006124 return ret;
6125 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006126 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006127 de = kmalloc(sizeof(*de), GFP_KERNEL);
6128 if (!de)
6129 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006130
6131 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006132 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006133 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006134 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006135 io_queue_async_work(req);
6136 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006137 }
6138
6139 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006140 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006141 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006142 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006143 spin_unlock_irq(&ctx->completion_lock);
6144 return -EIOCBQUEUED;
6145}
Jens Axboeedafcce2019-01-09 09:16:05 -07006146
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006147static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006148{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006149 if (req->flags & REQ_F_BUFFER_SELECTED) {
6150 switch (req->opcode) {
6151 case IORING_OP_READV:
6152 case IORING_OP_READ_FIXED:
6153 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006154 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006155 break;
6156 case IORING_OP_RECVMSG:
6157 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006158 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006159 break;
6160 }
6161 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006162 }
6163
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006164 if (req->flags & REQ_F_NEED_CLEANUP) {
6165 switch (req->opcode) {
6166 case IORING_OP_READV:
6167 case IORING_OP_READ_FIXED:
6168 case IORING_OP_READ:
6169 case IORING_OP_WRITEV:
6170 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006171 case IORING_OP_WRITE: {
6172 struct io_async_rw *io = req->async_data;
6173 if (io->free_iovec)
6174 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006175 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006176 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006177 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006178 case IORING_OP_SENDMSG: {
6179 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006180
6181 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006182 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006183 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006184 case IORING_OP_SPLICE:
6185 case IORING_OP_TEE:
6186 io_put_file(req, req->splice.file_in,
6187 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6188 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006189 case IORING_OP_OPENAT:
6190 case IORING_OP_OPENAT2:
6191 if (req->open.filename)
6192 putname(req->open.filename);
6193 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006194 case IORING_OP_RENAMEAT:
6195 putname(req->rename.oldpath);
6196 putname(req->rename.newpath);
6197 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006198 case IORING_OP_UNLINKAT:
6199 putname(req->unlink.filename);
6200 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006201 }
6202 req->flags &= ~REQ_F_NEED_CLEANUP;
6203 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006204}
6205
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006206static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags,
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006207 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006208{
Jens Axboeedafcce2019-01-09 09:16:05 -07006209 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006210 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006211
Jens Axboed625c6e2019-12-17 19:53:05 -07006212 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006213 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006214 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006215 break;
6216 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006217 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006218 case IORING_OP_READ:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006219 ret = io_read(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220 break;
6221 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006222 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006223 case IORING_OP_WRITE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006224 ret = io_write(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006225 break;
6226 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006227 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006228 break;
6229 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006230 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006231 break;
6232 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006233 ret = io_poll_remove(req);
6234 break;
6235 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006236 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006237 break;
6238 case IORING_OP_SENDMSG:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006239 ret = io_sendmsg(req, issue_flags, cs);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006240 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006241 case IORING_OP_SEND:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006242 ret = io_send(req, issue_flags, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006243 break;
6244 case IORING_OP_RECVMSG:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006245 ret = io_recvmsg(req, issue_flags, cs);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006246 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006247 case IORING_OP_RECV:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006248 ret = io_recv(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249 break;
6250 case IORING_OP_TIMEOUT:
6251 ret = io_timeout(req);
6252 break;
6253 case IORING_OP_TIMEOUT_REMOVE:
6254 ret = io_timeout_remove(req);
6255 break;
6256 case IORING_OP_ACCEPT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006257 ret = io_accept(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006258 break;
6259 case IORING_OP_CONNECT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006260 ret = io_connect(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261 break;
6262 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006263 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006264 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006265 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006266 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006267 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006268 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006269 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006270 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006271 case IORING_OP_CLOSE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006272 ret = io_close(req, issue_flags, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006273 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006274 case IORING_OP_FILES_UPDATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006275 ret = io_files_update(req, issue_flags, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006276 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006277 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006278 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006279 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006280 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006281 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006282 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006283 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006284 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006285 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006286 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006287 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006288 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006289 case IORING_OP_EPOLL_CTL:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006290 ret = io_epoll_ctl(req, issue_flags, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006291 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006292 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006293 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006294 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006295 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006296 ret = io_provide_buffers(req, issue_flags, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006297 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006298 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006299 ret = io_remove_buffers(req, issue_flags, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006301 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006302 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006303 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006304 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006305 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006306 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006307 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006308 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006309 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006310 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006311 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006312 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006313 default:
6314 ret = -EINVAL;
6315 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006316 }
6317
6318 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006319 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320
Jens Axboeb5325762020-05-19 21:20:27 -06006321 /* If the op doesn't have a file, we're not polling for it */
6322 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006323 const bool in_async = io_wq_current_is_worker();
6324
Jens Axboe11ba8202020-01-15 21:51:17 -07006325 /* workqueue context doesn't hold uring_lock, grab it now */
6326 if (in_async)
6327 mutex_lock(&ctx->uring_lock);
6328
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006329 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006330
6331 if (in_async)
6332 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006333 }
6334
6335 return 0;
6336}
6337
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006338static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006339{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006340 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006341 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006342 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006343
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006344 timeout = io_prep_linked_timeout(req);
6345 if (timeout)
6346 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006347
Jens Axboe4014d942021-01-19 15:53:54 -07006348 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006349 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006350
Jens Axboe561fb042019-10-24 07:25:42 -06006351 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006352 do {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006353 ret = io_issue_sqe(req, 0, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006354 /*
6355 * We can get EAGAIN for polled IO even though we're
6356 * forcing a sync submission from here, since we can't
6357 * wait for request slots on the block side.
6358 */
6359 if (ret != -EAGAIN)
6360 break;
6361 cond_resched();
6362 } while (1);
6363 }
Jens Axboe31b51512019-01-18 22:56:34 -07006364
Jens Axboe561fb042019-10-24 07:25:42 -06006365 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006366 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006367
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006368 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6369 lock_ctx = req->ctx;
6370
6371 /*
6372 * io_iopoll_complete() does not hold completion_lock to
6373 * complete polled io, so here for polled io, we can not call
6374 * io_req_complete() directly, otherwise there maybe concurrent
6375 * access to cqring, defer_list, etc, which is not safe. Given
6376 * that io_iopoll_complete() is always called under uring_lock,
6377 * so here for polled io, we also get uring_lock to complete
6378 * it.
6379 */
6380 if (lock_ctx)
6381 mutex_lock(&lock_ctx->uring_lock);
6382
6383 req_set_fail_links(req);
6384 io_req_complete(req, ret);
6385
6386 if (lock_ctx)
6387 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006388 }
Jens Axboe31b51512019-01-18 22:56:34 -07006389}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390
Jens Axboe65e19f52019-10-26 07:20:21 -06006391static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6392 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006393{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006394 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006395
Jens Axboe05f3fb32019-12-09 11:22:50 -07006396 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006397 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006398}
6399
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006400static struct file *io_file_get(struct io_submit_state *state,
6401 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006402{
6403 struct io_ring_ctx *ctx = req->ctx;
6404 struct file *file;
6405
6406 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006407 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006408 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006409 fd = array_index_nospec(fd, ctx->nr_user_files);
6410 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006411 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006412 } else {
6413 trace_io_uring_file_get(ctx, fd);
6414 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006415 }
6416
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006417 if (file && unlikely(file->f_op == &io_uring_fops))
6418 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006419 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006420}
6421
Jens Axboe2665abf2019-11-05 12:40:47 -07006422static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6423{
Jens Axboead8a48a2019-11-15 08:49:11 -07006424 struct io_timeout_data *data = container_of(timer,
6425 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006426 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006427 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006428 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006429
6430 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006431 prev = req->timeout.head;
6432 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006433
6434 /*
6435 * We don't expect the list to be empty, that will only happen if we
6436 * race with the completion of the linked work.
6437 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006438 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006439 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006440 else
6441 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006442 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6443
6444 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006445 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006446 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006447 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006448 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006449 io_req_complete_post(req, -ETIME, 0);
6450 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006451 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006452 return HRTIMER_NORESTART;
6453}
6454
Jens Axboe7271ef32020-08-10 09:55:22 -06006455static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006456{
Jens Axboe76a46e02019-11-10 23:34:16 -07006457 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006458 * If the back reference is NULL, then our linked request finished
6459 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006460 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006461 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006462 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006463
Jens Axboead8a48a2019-11-15 08:49:11 -07006464 data->timer.function = io_link_timeout_fn;
6465 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6466 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006467 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006468}
6469
6470static void io_queue_linked_timeout(struct io_kiocb *req)
6471{
6472 struct io_ring_ctx *ctx = req->ctx;
6473
6474 spin_lock_irq(&ctx->completion_lock);
6475 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006476 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006477
Jens Axboe2665abf2019-11-05 12:40:47 -07006478 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006479 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006480}
6481
Jens Axboead8a48a2019-11-15 08:49:11 -07006482static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006483{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006484 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006485
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006486 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6487 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006488 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006489
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006490 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006491 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006492 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006493 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006494}
6495
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006496static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006497{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006498 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006499 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006500 int ret;
6501
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006502again:
6503 linked_timeout = io_prep_linked_timeout(req);
6504
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006505 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6506 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006507 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006508 if (old_creds)
6509 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006510 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006511 old_creds = NULL; /* restored original creds */
6512 else
Jens Axboe98447d62020-10-14 10:48:51 -06006513 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006514 }
6515
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006516 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006517
6518 /*
6519 * We async punt it if the file wasn't marked NOWAIT, or if the file
6520 * doesn't support non-blocking read/write attempts
6521 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006522 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006523 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006524 /*
6525 * Queued up for async execution, worker will release
6526 * submit reference when the iocb is actually submitted.
6527 */
6528 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006529 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006530
Pavel Begunkovf063c542020-07-25 14:41:59 +03006531 if (linked_timeout)
6532 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006533 } else if (likely(!ret)) {
6534 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006535 if (req->flags & REQ_F_COMPLETE_INLINE) {
6536 list_add_tail(&req->compl.list, &cs->list);
6537 if (++cs->nr >= 32)
Pavel Begunkov9affd662021-01-19 13:32:46 +00006538 io_submit_flush_completions(cs);
6539 req = NULL;
6540 } else {
6541 req = io_put_req_find_next(req);
6542 }
6543
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006544 if (linked_timeout)
6545 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006546
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006547 if (req) {
6548 if (!(req->flags & REQ_F_FORCE_ASYNC))
6549 goto again;
6550 io_queue_async_work(req);
6551 }
6552 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006553 /* un-prep timeout, so it'll be killed as any other linked */
6554 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006555 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006556 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006557 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006558 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006559
Jens Axboe193155c2020-02-22 23:22:19 -07006560 if (old_creds)
6561 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562}
6563
Jens Axboef13fad72020-06-22 09:34:30 -06006564static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6565 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006566{
6567 int ret;
6568
Jens Axboe3529d8c2019-12-19 18:24:38 -07006569 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006570 if (ret) {
6571 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006572fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006573 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006574 io_put_req(req);
6575 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006576 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006577 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006578 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006579 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006580 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006581 goto fail_req;
6582 }
Jens Axboece35a472019-12-17 08:04:44 -07006583 io_queue_async_work(req);
6584 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006585 if (sqe) {
6586 ret = io_req_prep(req, sqe);
6587 if (unlikely(ret))
6588 goto fail_req;
6589 }
6590 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006591 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006592}
6593
Jens Axboef13fad72020-06-22 09:34:30 -06006594static inline void io_queue_link_head(struct io_kiocb *req,
6595 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006596{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006597 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006598 io_put_req(req);
6599 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006600 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006601 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006602}
6603
Pavel Begunkov863e0562020-10-27 23:25:35 +00006604struct io_submit_link {
6605 struct io_kiocb *head;
6606 struct io_kiocb *last;
6607};
6608
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006609static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006610 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006611{
Jackie Liua197f662019-11-08 08:09:12 -07006612 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006613 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006614
Jens Axboe9e645e112019-05-10 16:07:28 -06006615 /*
6616 * If we already have a head request, queue this one for async
6617 * submittal once the head completes. If we don't have a head but
6618 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6619 * submitted sync once the chain is complete. If none of those
6620 * conditions are true (normal request), then just queue it.
6621 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006622 if (link->head) {
6623 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006624
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006625 /*
6626 * Taking sequential execution of a link, draining both sides
6627 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6628 * requests in the link. So, it drains the head and the
6629 * next after the link request. The last one is done via
6630 * drain_next flag to persist the effect across calls.
6631 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006632 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006633 head->flags |= REQ_F_IO_DRAIN;
6634 ctx->drain_next = 1;
6635 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006636 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006637 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006638 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006639 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006640 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006641 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006642 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006643 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006644 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006645
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006646 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006647 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006648 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006649 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006650 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006651 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006652 if (unlikely(ctx->drain_next)) {
6653 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006654 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006655 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006656 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006657 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006658 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006659 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006660 link->head = req;
6661 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006662 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006663 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006664 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006665 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006666
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006667 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006668}
6669
Jens Axboe9a56a232019-01-09 09:06:50 -07006670/*
6671 * Batched submission is done, ensure local IO is flushed out.
6672 */
6673static void io_submit_state_end(struct io_submit_state *state)
6674{
Jens Axboef13fad72020-06-22 09:34:30 -06006675 if (!list_empty(&state->comp.list))
6676 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006677 if (state->plug_started)
6678 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006679 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006680 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006681 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006682}
6683
6684/*
6685 * Start submission side cache.
6686 */
6687static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006688 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006689{
Jens Axboe27926b62020-10-28 09:33:23 -06006690 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006691 state->comp.nr = 0;
6692 INIT_LIST_HEAD(&state->comp.list);
6693 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006694 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006695 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006696 state->ios_left = max_ios;
6697}
6698
Jens Axboe2b188cc2019-01-07 10:46:33 -07006699static void io_commit_sqring(struct io_ring_ctx *ctx)
6700{
Hristo Venev75b28af2019-08-26 17:23:46 +00006701 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006702
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006703 /*
6704 * Ensure any loads from the SQEs are done at this point,
6705 * since once we write the new head, the application could
6706 * write new data to them.
6707 */
6708 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006709}
6710
6711/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006712 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006713 * that is mapped by userspace. This means that care needs to be taken to
6714 * ensure that reads are stable, as we cannot rely on userspace always
6715 * being a good citizen. If members of the sqe are validated and then later
6716 * used, it's important that those reads are done through READ_ONCE() to
6717 * prevent a re-load down the line.
6718 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006719static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006720{
Hristo Venev75b28af2019-08-26 17:23:46 +00006721 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006722 unsigned head;
6723
6724 /*
6725 * The cached sq head (or cq tail) serves two purposes:
6726 *
6727 * 1) allows us to batch the cost of updating the user visible
6728 * head updates.
6729 * 2) allows the kernel side to track the head on its own, even
6730 * though the application is the one updating it.
6731 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006732 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006733 if (likely(head < ctx->sq_entries))
6734 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006735
6736 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006737 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006738 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006739 return NULL;
6740}
6741
6742static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6743{
6744 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006745}
6746
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006747/*
6748 * Check SQE restrictions (opcode and flags).
6749 *
6750 * Returns 'true' if SQE is allowed, 'false' otherwise.
6751 */
6752static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6753 struct io_kiocb *req,
6754 unsigned int sqe_flags)
6755{
6756 if (!ctx->restricted)
6757 return true;
6758
6759 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6760 return false;
6761
6762 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6763 ctx->restrictions.sqe_flags_required)
6764 return false;
6765
6766 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6767 ctx->restrictions.sqe_flags_required))
6768 return false;
6769
6770 return true;
6771}
6772
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006773#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6774 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6775 IOSQE_BUFFER_SELECT)
6776
6777static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6778 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006779 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006780{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006781 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006782 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006783
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006784 req->opcode = READ_ONCE(sqe->opcode);
6785 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006786 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006787 req->file = NULL;
6788 req->ctx = ctx;
6789 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006790 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006791 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006792 /* one is dropped after submission, the other at completion */
6793 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006794 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006795 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006796
6797 if (unlikely(req->opcode >= IORING_OP_LAST))
6798 return -EINVAL;
6799
Jens Axboe28cea78a2020-09-14 10:51:17 -06006800 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006801 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006802
6803 sqe_flags = READ_ONCE(sqe->flags);
6804 /* enforce forwards compatibility on users */
6805 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6806 return -EINVAL;
6807
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006808 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6809 return -EACCES;
6810
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006811 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6812 !io_op_defs[req->opcode].buffer_select)
6813 return -EOPNOTSUPP;
6814
6815 id = READ_ONCE(sqe->personality);
6816 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006817 struct io_identity *iod;
6818
Jens Axboe1e6fa522020-10-15 08:46:24 -06006819 iod = idr_find(&ctx->personality_idr, id);
6820 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006821 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006822 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006823
6824 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006825 get_cred(iod->creds);
6826 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006827 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006828 }
6829
6830 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006831 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006832
Jens Axboe27926b62020-10-28 09:33:23 -06006833 /*
6834 * Plug now if we have more than 1 IO left after this, and the target
6835 * is potentially a read/write to block based storage.
6836 */
6837 if (!state->plug_started && state->ios_left > 1 &&
6838 io_op_defs[req->opcode].plug) {
6839 blk_start_plug(&state->plug);
6840 state->plug_started = true;
6841 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006842
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006843 ret = 0;
6844 if (io_op_defs[req->opcode].needs_file) {
6845 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006846
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006847 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006848 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006849 ret = -EBADF;
6850 }
6851
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006852 state->ios_left--;
6853 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006854}
6855
Jens Axboe0f212202020-09-13 13:09:39 -06006856static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006857{
Jens Axboeac8691c2020-06-01 08:30:41 -06006858 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006859 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006860 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006861
Jens Axboec4a2ed72019-11-21 21:01:26 -07006862 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006863 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006864 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006865 return -EBUSY;
6866 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006867
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006868 /* make sure SQ entry isn't read before tail */
6869 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006870
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006871 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6872 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006873
Jens Axboed8a6df12020-10-15 16:24:45 -06006874 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006875 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006876
Jens Axboe6c271ce2019-01-10 11:22:30 -07006877 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006878 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006879
Jens Axboe6c271ce2019-01-10 11:22:30 -07006880 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006881 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006882 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006883 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006884
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006885 sqe = io_get_sqe(ctx);
6886 if (unlikely(!sqe)) {
6887 io_consume_sqe(ctx);
6888 break;
6889 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006890 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006891 if (unlikely(!req)) {
6892 if (!submitted)
6893 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006894 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006895 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006896 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006897 /* will complete beyond this point, count as submitted */
6898 submitted++;
6899
Pavel Begunkov692d8362020-10-10 18:34:13 +01006900 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006901 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006902fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006903 io_put_req(req);
6904 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006905 break;
6906 }
6907
Jens Axboe354420f2020-01-08 18:55:15 -07006908 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006909 true, ctx->flags & IORING_SETUP_SQPOLL);
Jens Axboef13fad72020-06-22 09:34:30 -06006910 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006911 if (err)
6912 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006913 }
6914
Pavel Begunkov9466f432020-01-25 22:34:01 +03006915 if (unlikely(submitted != nr)) {
6916 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006917 struct io_uring_task *tctx = current->io_uring;
6918 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006919
Jens Axboed8a6df12020-10-15 16:24:45 -06006920 percpu_ref_put_many(&ctx->refs, unused);
6921 percpu_counter_sub(&tctx->inflight, unused);
6922 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006923 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006924 if (link.head)
6925 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006926 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006927
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006928 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6929 io_commit_sqring(ctx);
6930
Jens Axboe6c271ce2019-01-10 11:22:30 -07006931 return submitted;
6932}
6933
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006934static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6935{
6936 /* Tell userspace we may need a wakeup call */
6937 spin_lock_irq(&ctx->completion_lock);
6938 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6939 spin_unlock_irq(&ctx->completion_lock);
6940}
6941
6942static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6943{
6944 spin_lock_irq(&ctx->completion_lock);
6945 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6946 spin_unlock_irq(&ctx->completion_lock);
6947}
6948
Xiaoguang Wang08369242020-11-03 14:15:59 +08006949static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006950{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006951 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006952 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006953
Jens Axboec8d1ba52020-09-14 11:07:26 -06006954 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006955 /* if we're handling multiple rings, cap submit size for fairness */
6956 if (cap_entries && to_submit > 8)
6957 to_submit = 8;
6958
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006959 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6960 unsigned nr_events = 0;
6961
Xiaoguang Wang08369242020-11-03 14:15:59 +08006962 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006963 if (!list_empty(&ctx->iopoll_list))
6964 io_do_iopoll(ctx, &nr_events, 0);
6965
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006966 if (to_submit && !ctx->sqo_dead &&
6967 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006968 ret = io_submit_sqes(ctx, to_submit);
6969 mutex_unlock(&ctx->uring_lock);
6970 }
Jens Axboe90554202020-09-03 12:12:41 -06006971
6972 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6973 wake_up(&ctx->sqo_sq_wait);
6974
Xiaoguang Wang08369242020-11-03 14:15:59 +08006975 return ret;
6976}
6977
6978static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6979{
6980 struct io_ring_ctx *ctx;
6981 unsigned sq_thread_idle = 0;
6982
6983 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6984 if (sq_thread_idle < ctx->sq_thread_idle)
6985 sq_thread_idle = ctx->sq_thread_idle;
6986 }
6987
6988 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006989}
6990
Jens Axboe69fb2132020-09-14 11:16:23 -06006991static void io_sqd_init_new(struct io_sq_data *sqd)
6992{
6993 struct io_ring_ctx *ctx;
6994
6995 while (!list_empty(&sqd->ctx_new_list)) {
6996 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006997 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6998 complete(&ctx->sq_thread_comp);
6999 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007000
7001 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007002}
7003
Jens Axboe6c271ce2019-01-10 11:22:30 -07007004static int io_sq_thread(void *data)
7005{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007006 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007007 struct files_struct *old_files = current->files;
7008 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007009 const struct cred *old_cred = NULL;
7010 struct io_sq_data *sqd = data;
7011 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007012 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007013 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007014
Jens Axboe28cea78a2020-09-14 10:51:17 -06007015 task_lock(current);
7016 current->files = NULL;
7017 current->nsproxy = NULL;
7018 task_unlock(current);
7019
Jens Axboe69fb2132020-09-14 11:16:23 -06007020 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007021 int ret;
7022 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007023
7024 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007025 * Any changes to the sqd lists are synchronized through the
7026 * kthread parking. This synchronizes the thread vs users,
7027 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007028 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007029 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007030 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007031 /*
7032 * When sq thread is unparked, in case the previous park operation
7033 * comes from io_put_sq_data(), which means that sq thread is going
7034 * to be stopped, so here needs to have a check.
7035 */
7036 if (kthread_should_stop())
7037 break;
7038 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007039
Xiaoguang Wang08369242020-11-03 14:15:59 +08007040 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007041 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007042 timeout = jiffies + sqd->sq_thread_idle;
7043 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007044
Xiaoguang Wang08369242020-11-03 14:15:59 +08007045 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007046 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007047 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7048 if (current->cred != ctx->creds) {
7049 if (old_cred)
7050 revert_creds(old_cred);
7051 old_cred = override_creds(ctx->creds);
7052 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007053 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007054#ifdef CONFIG_AUDIT
7055 current->loginuid = ctx->loginuid;
7056 current->sessionid = ctx->sessionid;
7057#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007058
Xiaoguang Wang08369242020-11-03 14:15:59 +08007059 ret = __io_sq_thread(ctx, cap_entries);
7060 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7061 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007062
Jens Axboe28cea78a2020-09-14 10:51:17 -06007063 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007064 }
7065
Xiaoguang Wang08369242020-11-03 14:15:59 +08007066 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007067 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007068 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007069 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007070 if (sqt_spin)
7071 timeout = jiffies + sqd->sq_thread_idle;
7072 continue;
7073 }
7074
Xiaoguang Wang08369242020-11-03 14:15:59 +08007075 needs_sched = true;
7076 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7077 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7078 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7079 !list_empty_careful(&ctx->iopoll_list)) {
7080 needs_sched = false;
7081 break;
7082 }
7083 if (io_sqring_entries(ctx)) {
7084 needs_sched = false;
7085 break;
7086 }
7087 }
7088
Hao Xu8b28fdf2021-01-31 22:39:04 +08007089 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007090 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7091 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007092
Jens Axboe69fb2132020-09-14 11:16:23 -06007093 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007094 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7095 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007096 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007097
7098 finish_wait(&sqd->wait, &wait);
7099 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007100 }
7101
Jens Axboe4c6e2772020-07-01 11:29:10 -06007102 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007103 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007104
Dennis Zhou91d8f512020-09-16 13:41:05 -07007105 if (cur_css)
7106 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007107 if (old_cred)
7108 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007109
Jens Axboe28cea78a2020-09-14 10:51:17 -06007110 task_lock(current);
7111 current->files = old_files;
7112 current->nsproxy = old_nsproxy;
7113 task_unlock(current);
7114
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007115 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007116
Jens Axboe6c271ce2019-01-10 11:22:30 -07007117 return 0;
7118}
7119
Jens Axboebda52162019-09-24 13:47:15 -06007120struct io_wait_queue {
7121 struct wait_queue_entry wq;
7122 struct io_ring_ctx *ctx;
7123 unsigned to_wait;
7124 unsigned nr_timeouts;
7125};
7126
Pavel Begunkov6c503152021-01-04 20:36:36 +00007127static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007128{
7129 struct io_ring_ctx *ctx = iowq->ctx;
7130
7131 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007132 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007133 * started waiting. For timeouts, we always want to return to userspace,
7134 * regardless of event count.
7135 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007136 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007137 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7138}
7139
7140static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7141 int wake_flags, void *key)
7142{
7143 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7144 wq);
7145
Pavel Begunkov6c503152021-01-04 20:36:36 +00007146 /*
7147 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7148 * the task, and the next invocation will do it.
7149 */
7150 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7151 return autoremove_wake_function(curr, mode, wake_flags, key);
7152 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007153}
7154
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007155static int io_run_task_work_sig(void)
7156{
7157 if (io_run_task_work())
7158 return 1;
7159 if (!signal_pending(current))
7160 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007161 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7162 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007163 return -EINTR;
7164}
7165
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007166/* when returns >0, the caller should retry */
7167static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7168 struct io_wait_queue *iowq,
7169 signed long *timeout)
7170{
7171 int ret;
7172
7173 /* make sure we run task_work before checking for signals */
7174 ret = io_run_task_work_sig();
7175 if (ret || io_should_wake(iowq))
7176 return ret;
7177 /* let the caller flush overflows, retry */
7178 if (test_bit(0, &ctx->cq_check_overflow))
7179 return 1;
7180
7181 *timeout = schedule_timeout(*timeout);
7182 return !*timeout ? -ETIME : 1;
7183}
7184
Jens Axboe2b188cc2019-01-07 10:46:33 -07007185/*
7186 * Wait until events become available, if we don't already have some. The
7187 * application must reap them itself, as they reside on the shared cq ring.
7188 */
7189static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007190 const sigset_t __user *sig, size_t sigsz,
7191 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007192{
Jens Axboebda52162019-09-24 13:47:15 -06007193 struct io_wait_queue iowq = {
7194 .wq = {
7195 .private = current,
7196 .func = io_wake_function,
7197 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7198 },
7199 .ctx = ctx,
7200 .to_wait = min_events,
7201 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007202 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007203 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7204 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007205
Jens Axboeb41e9852020-02-17 09:52:41 -07007206 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007207 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7208 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007209 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007210 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007211 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007212 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007213
7214 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007215#ifdef CONFIG_COMPAT
7216 if (in_compat_syscall())
7217 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007218 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007219 else
7220#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007221 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007222
Jens Axboe2b188cc2019-01-07 10:46:33 -07007223 if (ret)
7224 return ret;
7225 }
7226
Hao Xuc73ebb62020-11-03 10:54:37 +08007227 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007228 struct timespec64 ts;
7229
Hao Xuc73ebb62020-11-03 10:54:37 +08007230 if (get_timespec64(&ts, uts))
7231 return -EFAULT;
7232 timeout = timespec64_to_jiffies(&ts);
7233 }
7234
Jens Axboebda52162019-09-24 13:47:15 -06007235 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007236 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007237 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007238 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007239 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7240 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007241 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7242 finish_wait(&ctx->wait, &iowq.wq);
7243 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007244
Jens Axboeb7db41c2020-07-04 08:55:50 -06007245 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007246
Hristo Venev75b28af2019-08-26 17:23:46 +00007247 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007248}
7249
Jens Axboe6b063142019-01-10 22:13:58 -07007250static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7251{
7252#if defined(CONFIG_UNIX)
7253 if (ctx->ring_sock) {
7254 struct sock *sock = ctx->ring_sock->sk;
7255 struct sk_buff *skb;
7256
7257 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7258 kfree_skb(skb);
7259 }
7260#else
7261 int i;
7262
Jens Axboe65e19f52019-10-26 07:20:21 -06007263 for (i = 0; i < ctx->nr_user_files; i++) {
7264 struct file *file;
7265
7266 file = io_file_from_index(ctx, i);
7267 if (file)
7268 fput(file);
7269 }
Jens Axboe6b063142019-01-10 22:13:58 -07007270#endif
7271}
7272
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007273static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007274{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007275 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007276
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007277 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007278 complete(&data->done);
7279}
7280
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007281static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7282{
7283 spin_lock_bh(&ctx->rsrc_ref_lock);
7284}
7285
7286static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7287{
7288 spin_unlock_bh(&ctx->rsrc_ref_lock);
7289}
7290
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007291static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7292 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007293 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007294{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007295 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007296 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007297 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007298 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007299 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007300}
7301
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007302static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7303 struct io_ring_ctx *ctx,
7304 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007305{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007306 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007307 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007308
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007309 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007310 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007311 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007312 if (ref_node)
7313 percpu_ref_kill(&ref_node->refs);
7314
7315 percpu_ref_kill(&data->refs);
7316
7317 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007318 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007319 do {
7320 ret = wait_for_completion_interruptible(&data->done);
7321 if (!ret)
7322 break;
7323 ret = io_run_task_work_sig();
7324 if (ret < 0) {
7325 percpu_ref_resurrect(&data->refs);
7326 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007327 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007328 return ret;
7329 }
7330 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007332 destroy_fixed_rsrc_ref_node(backup_node);
7333 return 0;
7334}
7335
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007336static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7337{
7338 struct fixed_rsrc_data *data;
7339
7340 data = kzalloc(sizeof(*data), GFP_KERNEL);
7341 if (!data)
7342 return NULL;
7343
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007344 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007345 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7346 kfree(data);
7347 return NULL;
7348 }
7349 data->ctx = ctx;
7350 init_completion(&data->done);
7351 return data;
7352}
7353
7354static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7355{
7356 percpu_ref_exit(&data->refs);
7357 kfree(data->table);
7358 kfree(data);
7359}
7360
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007361static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7362{
7363 struct fixed_rsrc_data *data = ctx->file_data;
7364 struct fixed_rsrc_ref_node *backup_node;
7365 unsigned nr_tables, i;
7366 int ret;
7367
7368 if (!data)
7369 return -ENXIO;
7370 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7371 if (!backup_node)
7372 return -ENOMEM;
7373 init_fixed_file_ref_node(ctx, backup_node);
7374
7375 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7376 if (ret)
7377 return ret;
7378
Jens Axboe6b063142019-01-10 22:13:58 -07007379 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007380 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7381 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007382 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007383 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007384 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007385 ctx->nr_user_files = 0;
7386 return 0;
7387}
7388
Jens Axboe534ca6d2020-09-02 13:52:19 -06007389static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007390{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007391 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007392 /*
7393 * The park is a bit of a work-around, without it we get
7394 * warning spews on shutdown with SQPOLL set and affinity
7395 * set to a single CPU.
7396 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007397 if (sqd->thread) {
7398 kthread_park(sqd->thread);
7399 kthread_stop(sqd->thread);
7400 }
7401
7402 kfree(sqd);
7403 }
7404}
7405
Jens Axboeaa061652020-09-02 14:50:27 -06007406static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7407{
7408 struct io_ring_ctx *ctx_attach;
7409 struct io_sq_data *sqd;
7410 struct fd f;
7411
7412 f = fdget(p->wq_fd);
7413 if (!f.file)
7414 return ERR_PTR(-ENXIO);
7415 if (f.file->f_op != &io_uring_fops) {
7416 fdput(f);
7417 return ERR_PTR(-EINVAL);
7418 }
7419
7420 ctx_attach = f.file->private_data;
7421 sqd = ctx_attach->sq_data;
7422 if (!sqd) {
7423 fdput(f);
7424 return ERR_PTR(-EINVAL);
7425 }
7426
7427 refcount_inc(&sqd->refs);
7428 fdput(f);
7429 return sqd;
7430}
7431
Jens Axboe534ca6d2020-09-02 13:52:19 -06007432static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7433{
7434 struct io_sq_data *sqd;
7435
Jens Axboeaa061652020-09-02 14:50:27 -06007436 if (p->flags & IORING_SETUP_ATTACH_WQ)
7437 return io_attach_sq_data(p);
7438
Jens Axboe534ca6d2020-09-02 13:52:19 -06007439 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7440 if (!sqd)
7441 return ERR_PTR(-ENOMEM);
7442
7443 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007444 INIT_LIST_HEAD(&sqd->ctx_list);
7445 INIT_LIST_HEAD(&sqd->ctx_new_list);
7446 mutex_init(&sqd->ctx_lock);
7447 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007448 init_waitqueue_head(&sqd->wait);
7449 return sqd;
7450}
7451
Jens Axboe69fb2132020-09-14 11:16:23 -06007452static void io_sq_thread_unpark(struct io_sq_data *sqd)
7453 __releases(&sqd->lock)
7454{
7455 if (!sqd->thread)
7456 return;
7457 kthread_unpark(sqd->thread);
7458 mutex_unlock(&sqd->lock);
7459}
7460
7461static void io_sq_thread_park(struct io_sq_data *sqd)
7462 __acquires(&sqd->lock)
7463{
7464 if (!sqd->thread)
7465 return;
7466 mutex_lock(&sqd->lock);
7467 kthread_park(sqd->thread);
7468}
7469
Jens Axboe534ca6d2020-09-02 13:52:19 -06007470static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7471{
7472 struct io_sq_data *sqd = ctx->sq_data;
7473
7474 if (sqd) {
7475 if (sqd->thread) {
7476 /*
7477 * We may arrive here from the error branch in
7478 * io_sq_offload_create() where the kthread is created
7479 * without being waked up, thus wake it up now to make
7480 * sure the wait will complete.
7481 */
7482 wake_up_process(sqd->thread);
7483 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007484
7485 io_sq_thread_park(sqd);
7486 }
7487
7488 mutex_lock(&sqd->ctx_lock);
7489 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007490 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007491 mutex_unlock(&sqd->ctx_lock);
7492
Xiaoguang Wang08369242020-11-03 14:15:59 +08007493 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007494 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007495
7496 io_put_sq_data(sqd);
7497 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007498 }
7499}
7500
Jens Axboe6b063142019-01-10 22:13:58 -07007501static void io_finish_async(struct io_ring_ctx *ctx)
7502{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007503 io_sq_thread_stop(ctx);
7504
Jens Axboe561fb042019-10-24 07:25:42 -06007505 if (ctx->io_wq) {
7506 io_wq_destroy(ctx->io_wq);
7507 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007508 }
7509}
7510
7511#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007512/*
7513 * Ensure the UNIX gc is aware of our file set, so we are certain that
7514 * the io_uring can be safely unregistered on process exit, even if we have
7515 * loops in the file referencing.
7516 */
7517static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7518{
7519 struct sock *sk = ctx->ring_sock->sk;
7520 struct scm_fp_list *fpl;
7521 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007522 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007523
Jens Axboe6b063142019-01-10 22:13:58 -07007524 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7525 if (!fpl)
7526 return -ENOMEM;
7527
7528 skb = alloc_skb(0, GFP_KERNEL);
7529 if (!skb) {
7530 kfree(fpl);
7531 return -ENOMEM;
7532 }
7533
7534 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007535
Jens Axboe08a45172019-10-03 08:11:03 -06007536 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007537 fpl->user = get_uid(ctx->user);
7538 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007539 struct file *file = io_file_from_index(ctx, i + offset);
7540
7541 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007542 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007543 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007544 unix_inflight(fpl->user, fpl->fp[nr_files]);
7545 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007546 }
7547
Jens Axboe08a45172019-10-03 08:11:03 -06007548 if (nr_files) {
7549 fpl->max = SCM_MAX_FD;
7550 fpl->count = nr_files;
7551 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007552 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007553 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7554 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007555
Jens Axboe08a45172019-10-03 08:11:03 -06007556 for (i = 0; i < nr_files; i++)
7557 fput(fpl->fp[i]);
7558 } else {
7559 kfree_skb(skb);
7560 kfree(fpl);
7561 }
Jens Axboe6b063142019-01-10 22:13:58 -07007562
7563 return 0;
7564}
7565
7566/*
7567 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7568 * causes regular reference counting to break down. We rely on the UNIX
7569 * garbage collection to take care of this problem for us.
7570 */
7571static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7572{
7573 unsigned left, total;
7574 int ret = 0;
7575
7576 total = 0;
7577 left = ctx->nr_user_files;
7578 while (left) {
7579 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007580
7581 ret = __io_sqe_files_scm(ctx, this_files, total);
7582 if (ret)
7583 break;
7584 left -= this_files;
7585 total += this_files;
7586 }
7587
7588 if (!ret)
7589 return 0;
7590
7591 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007592 struct file *file = io_file_from_index(ctx, total);
7593
7594 if (file)
7595 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007596 total++;
7597 }
7598
7599 return ret;
7600}
7601#else
7602static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7603{
7604 return 0;
7605}
7606#endif
7607
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007608static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007609 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007610{
7611 int i;
7612
7613 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007614 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007615 unsigned this_files;
7616
7617 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7618 table->files = kcalloc(this_files, sizeof(struct file *),
7619 GFP_KERNEL);
7620 if (!table->files)
7621 break;
7622 nr_files -= this_files;
7623 }
7624
7625 if (i == nr_tables)
7626 return 0;
7627
7628 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007629 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007630 kfree(table->files);
7631 }
7632 return 1;
7633}
7634
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007635static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007636{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007637 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007638#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007639 struct sock *sock = ctx->ring_sock->sk;
7640 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7641 struct sk_buff *skb;
7642 int i;
7643
7644 __skb_queue_head_init(&list);
7645
7646 /*
7647 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7648 * remove this entry and rearrange the file array.
7649 */
7650 skb = skb_dequeue(head);
7651 while (skb) {
7652 struct scm_fp_list *fp;
7653
7654 fp = UNIXCB(skb).fp;
7655 for (i = 0; i < fp->count; i++) {
7656 int left;
7657
7658 if (fp->fp[i] != file)
7659 continue;
7660
7661 unix_notinflight(fp->user, fp->fp[i]);
7662 left = fp->count - 1 - i;
7663 if (left) {
7664 memmove(&fp->fp[i], &fp->fp[i + 1],
7665 left * sizeof(struct file *));
7666 }
7667 fp->count--;
7668 if (!fp->count) {
7669 kfree_skb(skb);
7670 skb = NULL;
7671 } else {
7672 __skb_queue_tail(&list, skb);
7673 }
7674 fput(file);
7675 file = NULL;
7676 break;
7677 }
7678
7679 if (!file)
7680 break;
7681
7682 __skb_queue_tail(&list, skb);
7683
7684 skb = skb_dequeue(head);
7685 }
7686
7687 if (skb_peek(&list)) {
7688 spin_lock_irq(&head->lock);
7689 while ((skb = __skb_dequeue(&list)) != NULL)
7690 __skb_queue_tail(head, skb);
7691 spin_unlock_irq(&head->lock);
7692 }
7693#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007694 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007695#endif
7696}
7697
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007698static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007699{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007700 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7701 struct io_ring_ctx *ctx = rsrc_data->ctx;
7702 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007703
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007704 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7705 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007706 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007707 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007708 }
7709
Xiaoguang Wang05589552020-03-31 14:05:18 +08007710 percpu_ref_exit(&ref_node->refs);
7711 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007712 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007713}
7714
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007715static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007716{
7717 struct io_ring_ctx *ctx;
7718 struct llist_node *node;
7719
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007720 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7721 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007722
7723 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007724 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007725 struct llist_node *next = node->next;
7726
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007727 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7728 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007729 node = next;
7730 }
7731}
7732
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007733static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7734 unsigned i)
7735{
7736 struct fixed_rsrc_table *table;
7737
7738 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7739 return &table->files[i & IORING_FILE_TABLE_MASK];
7740}
7741
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007742static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007743{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007744 struct fixed_rsrc_ref_node *ref_node;
7745 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007746 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007747 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007748 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007749
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007750 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7751 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007752 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007753
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007754 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007755 ref_node->done = true;
7756
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007757 while (!list_empty(&ctx->rsrc_ref_list)) {
7758 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007759 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007760 /* recycle ref nodes in order */
7761 if (!ref_node->done)
7762 break;
7763 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007764 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007765 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007766 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007767
7768 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007769 delay = 0;
7770
Jens Axboe4a38aed22020-05-14 17:21:15 -06007771 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007772 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007773 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007774 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007775}
7776
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007777static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007778 struct io_ring_ctx *ctx)
7779{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007780 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007781
7782 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7783 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007784 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007785
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007786 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007787 0, GFP_KERNEL)) {
7788 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007789 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007790 }
7791 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007792 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007793 ref_node->done = false;
7794 return ref_node;
7795}
7796
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007797static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7798 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007799{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007800 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007801 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007802}
7803
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007804static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007805{
7806 percpu_ref_exit(&ref_node->refs);
7807 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007808}
7809
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007810
Jens Axboe05f3fb32019-12-09 11:22:50 -07007811static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7812 unsigned nr_args)
7813{
7814 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007815 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007816 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007817 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007818 struct fixed_rsrc_ref_node *ref_node;
7819 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007820
7821 if (ctx->file_data)
7822 return -EBUSY;
7823 if (!nr_args)
7824 return -EINVAL;
7825 if (nr_args > IORING_MAX_FIXED_FILES)
7826 return -EMFILE;
7827
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007828 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007829 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007830 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007831 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007832
7833 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007834 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007835 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007836 if (!file_data->table)
7837 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007838
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007839 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007840 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007841
7842 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007843 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7844 ret = -EFAULT;
7845 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007846 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007847 /* allow sparse sets */
7848 if (fd == -1)
7849 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850
Jens Axboe05f3fb32019-12-09 11:22:50 -07007851 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007852 ret = -EBADF;
7853 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007854 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007855
7856 /*
7857 * Don't allow io_uring instances to be registered. If UNIX
7858 * isn't enabled, then this causes a reference cycle and this
7859 * instance can never get freed. If UNIX is enabled we'll
7860 * handle it just fine, but there's still no point in allowing
7861 * a ring fd as it doesn't support regular read/write anyway.
7862 */
7863 if (file->f_op == &io_uring_fops) {
7864 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007865 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007866 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007867 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007868 }
7869
Jens Axboe05f3fb32019-12-09 11:22:50 -07007870 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007871 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007872 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007873 return ret;
7874 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007875
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007876 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007877 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007878 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007879 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007880 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007881 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007882
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007883 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007884 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007885out_fput:
7886 for (i = 0; i < ctx->nr_user_files; i++) {
7887 file = io_file_from_index(ctx, i);
7888 if (file)
7889 fput(file);
7890 }
7891 for (i = 0; i < nr_tables; i++)
7892 kfree(file_data->table[i].files);
7893 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007894out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007895 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007896 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007897 return ret;
7898}
7899
Jens Axboec3a31e62019-10-03 13:59:56 -06007900static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7901 int index)
7902{
7903#if defined(CONFIG_UNIX)
7904 struct sock *sock = ctx->ring_sock->sk;
7905 struct sk_buff_head *head = &sock->sk_receive_queue;
7906 struct sk_buff *skb;
7907
7908 /*
7909 * See if we can merge this file into an existing skb SCM_RIGHTS
7910 * file set. If there's no room, fall back to allocating a new skb
7911 * and filling it in.
7912 */
7913 spin_lock_irq(&head->lock);
7914 skb = skb_peek(head);
7915 if (skb) {
7916 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7917
7918 if (fpl->count < SCM_MAX_FD) {
7919 __skb_unlink(skb, head);
7920 spin_unlock_irq(&head->lock);
7921 fpl->fp[fpl->count] = get_file(file);
7922 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7923 fpl->count++;
7924 spin_lock_irq(&head->lock);
7925 __skb_queue_head(head, skb);
7926 } else {
7927 skb = NULL;
7928 }
7929 }
7930 spin_unlock_irq(&head->lock);
7931
7932 if (skb) {
7933 fput(file);
7934 return 0;
7935 }
7936
7937 return __io_sqe_files_scm(ctx, 1, index);
7938#else
7939 return 0;
7940#endif
7941}
7942
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007943static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007944{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007945 struct io_rsrc_put *prsrc;
7946 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007947
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007948 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7949 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007950 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007951
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007952 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007953 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007954
Hillf Dantona5318d32020-03-23 17:47:15 +08007955 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007956}
7957
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007958static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7959 struct file *file)
7960{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007961 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007962}
7963
Jens Axboe05f3fb32019-12-09 11:22:50 -07007964static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007965 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007966 unsigned nr_args)
7967{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007968 struct fixed_rsrc_data *data = ctx->file_data;
7969 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007970 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007971 __s32 __user *fds;
7972 int fd, i, err;
7973 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007974 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007975
Jens Axboe05f3fb32019-12-09 11:22:50 -07007976 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007977 return -EOVERFLOW;
7978 if (done > ctx->nr_user_files)
7979 return -EINVAL;
7980
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007981 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007982 if (!ref_node)
7983 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007984 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007985
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007986 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007987 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007988 err = 0;
7989 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7990 err = -EFAULT;
7991 break;
7992 }
noah4e0377a2021-01-26 15:23:28 -05007993 if (fd == IORING_REGISTER_FILES_SKIP)
7994 continue;
7995
Pavel Begunkov67973b92021-01-26 13:51:09 +00007996 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007997 file_slot = io_fixed_file_slot(ctx->file_data, i);
7998
7999 if (*file_slot) {
8000 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008001 if (err)
8002 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008003 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008004 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008005 }
8006 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008007 file = fget(fd);
8008 if (!file) {
8009 err = -EBADF;
8010 break;
8011 }
8012 /*
8013 * Don't allow io_uring instances to be registered. If
8014 * UNIX isn't enabled, then this causes a reference
8015 * cycle and this instance can never get freed. If UNIX
8016 * is enabled we'll handle it just fine, but there's
8017 * still no point in allowing a ring fd as it doesn't
8018 * support regular read/write anyway.
8019 */
8020 if (file->f_op == &io_uring_fops) {
8021 fput(file);
8022 err = -EBADF;
8023 break;
8024 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008025 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008026 if (err) {
8027 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008028 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008029 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008030 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008031 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008032 }
8033
Xiaoguang Wang05589552020-03-31 14:05:18 +08008034 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008035 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008036 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008037 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008038 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008039
8040 return done ? done : err;
8041}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008042
Jens Axboe05f3fb32019-12-09 11:22:50 -07008043static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8044 unsigned nr_args)
8045{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008046 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008047
8048 if (!ctx->file_data)
8049 return -ENXIO;
8050 if (!nr_args)
8051 return -EINVAL;
8052 if (copy_from_user(&up, arg, sizeof(up)))
8053 return -EFAULT;
8054 if (up.resv)
8055 return -EINVAL;
8056
8057 return __io_sqe_files_update(ctx, &up, nr_args);
8058}
Jens Axboec3a31e62019-10-03 13:59:56 -06008059
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008060static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008061{
8062 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8063
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008064 req = io_put_req_find_next(req);
8065 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008066}
8067
Pavel Begunkov24369c22020-01-28 03:15:48 +03008068static int io_init_wq_offload(struct io_ring_ctx *ctx,
8069 struct io_uring_params *p)
8070{
8071 struct io_wq_data data;
8072 struct fd f;
8073 struct io_ring_ctx *ctx_attach;
8074 unsigned int concurrency;
8075 int ret = 0;
8076
8077 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008078 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008079 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008080
8081 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8082 /* Do QD, or 4 * CPUS, whatever is smallest */
8083 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8084
8085 ctx->io_wq = io_wq_create(concurrency, &data);
8086 if (IS_ERR(ctx->io_wq)) {
8087 ret = PTR_ERR(ctx->io_wq);
8088 ctx->io_wq = NULL;
8089 }
8090 return ret;
8091 }
8092
8093 f = fdget(p->wq_fd);
8094 if (!f.file)
8095 return -EBADF;
8096
8097 if (f.file->f_op != &io_uring_fops) {
8098 ret = -EINVAL;
8099 goto out_fput;
8100 }
8101
8102 ctx_attach = f.file->private_data;
8103 /* @io_wq is protected by holding the fd */
8104 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8105 ret = -EINVAL;
8106 goto out_fput;
8107 }
8108
8109 ctx->io_wq = ctx_attach->io_wq;
8110out_fput:
8111 fdput(f);
8112 return ret;
8113}
8114
Jens Axboe0f212202020-09-13 13:09:39 -06008115static int io_uring_alloc_task_context(struct task_struct *task)
8116{
8117 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008118 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008119
8120 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8121 if (unlikely(!tctx))
8122 return -ENOMEM;
8123
Jens Axboed8a6df12020-10-15 16:24:45 -06008124 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8125 if (unlikely(ret)) {
8126 kfree(tctx);
8127 return ret;
8128 }
8129
Jens Axboe0f212202020-09-13 13:09:39 -06008130 xa_init(&tctx->xa);
8131 init_waitqueue_head(&tctx->wait);
8132 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008133 atomic_set(&tctx->in_idle, 0);
8134 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008135 io_init_identity(&tctx->__identity);
8136 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008137 task->io_uring = tctx;
8138 return 0;
8139}
8140
8141void __io_uring_free(struct task_struct *tsk)
8142{
8143 struct io_uring_task *tctx = tsk->io_uring;
8144
8145 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008146 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8147 if (tctx->identity != &tctx->__identity)
8148 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008149 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008150 kfree(tctx);
8151 tsk->io_uring = NULL;
8152}
8153
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008154static int io_sq_offload_create(struct io_ring_ctx *ctx,
8155 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008156{
8157 int ret;
8158
Jens Axboe6c271ce2019-01-10 11:22:30 -07008159 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008160 struct io_sq_data *sqd;
8161
Jens Axboe3ec482d2019-04-08 10:51:01 -06008162 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008163 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008164 goto err;
8165
Jens Axboe534ca6d2020-09-02 13:52:19 -06008166 sqd = io_get_sq_data(p);
8167 if (IS_ERR(sqd)) {
8168 ret = PTR_ERR(sqd);
8169 goto err;
8170 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008171
Jens Axboe534ca6d2020-09-02 13:52:19 -06008172 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008173 io_sq_thread_park(sqd);
8174 mutex_lock(&sqd->ctx_lock);
8175 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8176 mutex_unlock(&sqd->ctx_lock);
8177 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008178
Jens Axboe917257d2019-04-13 09:28:55 -06008179 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8180 if (!ctx->sq_thread_idle)
8181 ctx->sq_thread_idle = HZ;
8182
Jens Axboeaa061652020-09-02 14:50:27 -06008183 if (sqd->thread)
8184 goto done;
8185
Jens Axboe6c271ce2019-01-10 11:22:30 -07008186 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008187 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008188
Jens Axboe917257d2019-04-13 09:28:55 -06008189 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008190 if (cpu >= nr_cpu_ids)
8191 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008192 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008193 goto err;
8194
Jens Axboe69fb2132020-09-14 11:16:23 -06008195 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008196 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008197 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008198 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008199 "io_uring-sq");
8200 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008201 if (IS_ERR(sqd->thread)) {
8202 ret = PTR_ERR(sqd->thread);
8203 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008204 goto err;
8205 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008206 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008207 if (ret)
8208 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008209 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8210 /* Can't have SQ_AFF without SQPOLL */
8211 ret = -EINVAL;
8212 goto err;
8213 }
8214
Jens Axboeaa061652020-09-02 14:50:27 -06008215done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008216 ret = io_init_wq_offload(ctx, p);
8217 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008218 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219
8220 return 0;
8221err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008222 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223 return ret;
8224}
8225
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008226static void io_sq_offload_start(struct io_ring_ctx *ctx)
8227{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008228 struct io_sq_data *sqd = ctx->sq_data;
8229
8230 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8231 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008232}
8233
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008234static inline void __io_unaccount_mem(struct user_struct *user,
8235 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008236{
8237 atomic_long_sub(nr_pages, &user->locked_vm);
8238}
8239
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008240static inline int __io_account_mem(struct user_struct *user,
8241 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008242{
8243 unsigned long page_limit, cur_pages, new_pages;
8244
8245 /* Don't allow more pages than we can safely lock */
8246 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8247
8248 do {
8249 cur_pages = atomic_long_read(&user->locked_vm);
8250 new_pages = cur_pages + nr_pages;
8251 if (new_pages > page_limit)
8252 return -ENOMEM;
8253 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8254 new_pages) != cur_pages);
8255
8256 return 0;
8257}
8258
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008259static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8260 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008261{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008262 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008263 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008264
Jens Axboe2aede0e2020-09-14 10:45:53 -06008265 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008266 if (acct == ACCT_LOCKED) {
8267 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008268 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008269 mmap_write_unlock(ctx->mm_account);
8270 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008271 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008272 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008273 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008274}
8275
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008276static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8277 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008278{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008279 int ret;
8280
8281 if (ctx->limit_mem) {
8282 ret = __io_account_mem(ctx->user, nr_pages);
8283 if (ret)
8284 return ret;
8285 }
8286
Jens Axboe2aede0e2020-09-14 10:45:53 -06008287 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008288 if (acct == ACCT_LOCKED) {
8289 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008290 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008291 mmap_write_unlock(ctx->mm_account);
8292 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008293 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008294 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008295 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008296
8297 return 0;
8298}
8299
Jens Axboe2b188cc2019-01-07 10:46:33 -07008300static void io_mem_free(void *ptr)
8301{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008302 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008303
Mark Rutland52e04ef2019-04-30 17:30:21 +01008304 if (!ptr)
8305 return;
8306
8307 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008308 if (put_page_testzero(page))
8309 free_compound_page(page);
8310}
8311
8312static void *io_mem_alloc(size_t size)
8313{
8314 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8315 __GFP_NORETRY;
8316
8317 return (void *) __get_free_pages(gfp_flags, get_order(size));
8318}
8319
Hristo Venev75b28af2019-08-26 17:23:46 +00008320static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8321 size_t *sq_offset)
8322{
8323 struct io_rings *rings;
8324 size_t off, sq_array_size;
8325
8326 off = struct_size(rings, cqes, cq_entries);
8327 if (off == SIZE_MAX)
8328 return SIZE_MAX;
8329
8330#ifdef CONFIG_SMP
8331 off = ALIGN(off, SMP_CACHE_BYTES);
8332 if (off == 0)
8333 return SIZE_MAX;
8334#endif
8335
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008336 if (sq_offset)
8337 *sq_offset = off;
8338
Hristo Venev75b28af2019-08-26 17:23:46 +00008339 sq_array_size = array_size(sizeof(u32), sq_entries);
8340 if (sq_array_size == SIZE_MAX)
8341 return SIZE_MAX;
8342
8343 if (check_add_overflow(off, sq_array_size, &off))
8344 return SIZE_MAX;
8345
Hristo Venev75b28af2019-08-26 17:23:46 +00008346 return off;
8347}
8348
Jens Axboe2b188cc2019-01-07 10:46:33 -07008349static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8350{
Hristo Venev75b28af2019-08-26 17:23:46 +00008351 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008352
Hristo Venev75b28af2019-08-26 17:23:46 +00008353 pages = (size_t)1 << get_order(
8354 rings_size(sq_entries, cq_entries, NULL));
8355 pages += (size_t)1 << get_order(
8356 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008357
Hristo Venev75b28af2019-08-26 17:23:46 +00008358 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008359}
8360
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008361static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008362{
8363 int i, j;
8364
8365 if (!ctx->user_bufs)
8366 return -ENXIO;
8367
8368 for (i = 0; i < ctx->nr_user_bufs; i++) {
8369 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8370
8371 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008372 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008373
Jens Axboede293932020-09-17 16:19:16 -06008374 if (imu->acct_pages)
8375 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008376 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008377 imu->nr_bvecs = 0;
8378 }
8379
8380 kfree(ctx->user_bufs);
8381 ctx->user_bufs = NULL;
8382 ctx->nr_user_bufs = 0;
8383 return 0;
8384}
8385
8386static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8387 void __user *arg, unsigned index)
8388{
8389 struct iovec __user *src;
8390
8391#ifdef CONFIG_COMPAT
8392 if (ctx->compat) {
8393 struct compat_iovec __user *ciovs;
8394 struct compat_iovec ciov;
8395
8396 ciovs = (struct compat_iovec __user *) arg;
8397 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8398 return -EFAULT;
8399
Jens Axboed55e5f52019-12-11 16:12:15 -07008400 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008401 dst->iov_len = ciov.iov_len;
8402 return 0;
8403 }
8404#endif
8405 src = (struct iovec __user *) arg;
8406 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8407 return -EFAULT;
8408 return 0;
8409}
8410
Jens Axboede293932020-09-17 16:19:16 -06008411/*
8412 * Not super efficient, but this is just a registration time. And we do cache
8413 * the last compound head, so generally we'll only do a full search if we don't
8414 * match that one.
8415 *
8416 * We check if the given compound head page has already been accounted, to
8417 * avoid double accounting it. This allows us to account the full size of the
8418 * page, not just the constituent pages of a huge page.
8419 */
8420static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8421 int nr_pages, struct page *hpage)
8422{
8423 int i, j;
8424
8425 /* check current page array */
8426 for (i = 0; i < nr_pages; i++) {
8427 if (!PageCompound(pages[i]))
8428 continue;
8429 if (compound_head(pages[i]) == hpage)
8430 return true;
8431 }
8432
8433 /* check previously registered pages */
8434 for (i = 0; i < ctx->nr_user_bufs; i++) {
8435 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8436
8437 for (j = 0; j < imu->nr_bvecs; j++) {
8438 if (!PageCompound(imu->bvec[j].bv_page))
8439 continue;
8440 if (compound_head(imu->bvec[j].bv_page) == hpage)
8441 return true;
8442 }
8443 }
8444
8445 return false;
8446}
8447
8448static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8449 int nr_pages, struct io_mapped_ubuf *imu,
8450 struct page **last_hpage)
8451{
8452 int i, ret;
8453
8454 for (i = 0; i < nr_pages; i++) {
8455 if (!PageCompound(pages[i])) {
8456 imu->acct_pages++;
8457 } else {
8458 struct page *hpage;
8459
8460 hpage = compound_head(pages[i]);
8461 if (hpage == *last_hpage)
8462 continue;
8463 *last_hpage = hpage;
8464 if (headpage_already_acct(ctx, pages, i, hpage))
8465 continue;
8466 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8467 }
8468 }
8469
8470 if (!imu->acct_pages)
8471 return 0;
8472
8473 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8474 if (ret)
8475 imu->acct_pages = 0;
8476 return ret;
8477}
8478
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008479static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8480 struct io_mapped_ubuf *imu,
8481 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008482{
8483 struct vm_area_struct **vmas = NULL;
8484 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008485 unsigned long off, start, end, ubuf;
8486 size_t size;
8487 int ret, pret, nr_pages, i;
8488
8489 ubuf = (unsigned long) iov->iov_base;
8490 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8491 start = ubuf >> PAGE_SHIFT;
8492 nr_pages = end - start;
8493
8494 ret = -ENOMEM;
8495
8496 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8497 if (!pages)
8498 goto done;
8499
8500 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8501 GFP_KERNEL);
8502 if (!vmas)
8503 goto done;
8504
8505 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8506 GFP_KERNEL);
8507 if (!imu->bvec)
8508 goto done;
8509
8510 ret = 0;
8511 mmap_read_lock(current->mm);
8512 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8513 pages, vmas);
8514 if (pret == nr_pages) {
8515 /* don't support file backed memory */
8516 for (i = 0; i < nr_pages; i++) {
8517 struct vm_area_struct *vma = vmas[i];
8518
8519 if (vma->vm_file &&
8520 !is_file_hugepages(vma->vm_file)) {
8521 ret = -EOPNOTSUPP;
8522 break;
8523 }
8524 }
8525 } else {
8526 ret = pret < 0 ? pret : -EFAULT;
8527 }
8528 mmap_read_unlock(current->mm);
8529 if (ret) {
8530 /*
8531 * if we did partial map, or found file backed vmas,
8532 * release any pages we did get
8533 */
8534 if (pret > 0)
8535 unpin_user_pages(pages, pret);
8536 kvfree(imu->bvec);
8537 goto done;
8538 }
8539
8540 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8541 if (ret) {
8542 unpin_user_pages(pages, pret);
8543 kvfree(imu->bvec);
8544 goto done;
8545 }
8546
8547 off = ubuf & ~PAGE_MASK;
8548 size = iov->iov_len;
8549 for (i = 0; i < nr_pages; i++) {
8550 size_t vec_len;
8551
8552 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8553 imu->bvec[i].bv_page = pages[i];
8554 imu->bvec[i].bv_len = vec_len;
8555 imu->bvec[i].bv_offset = off;
8556 off = 0;
8557 size -= vec_len;
8558 }
8559 /* store original address for later verification */
8560 imu->ubuf = ubuf;
8561 imu->len = iov->iov_len;
8562 imu->nr_bvecs = nr_pages;
8563 ret = 0;
8564done:
8565 kvfree(pages);
8566 kvfree(vmas);
8567 return ret;
8568}
8569
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008570static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008571{
Jens Axboeedafcce2019-01-09 09:16:05 -07008572 if (ctx->user_bufs)
8573 return -EBUSY;
8574 if (!nr_args || nr_args > UIO_MAXIOV)
8575 return -EINVAL;
8576
8577 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8578 GFP_KERNEL);
8579 if (!ctx->user_bufs)
8580 return -ENOMEM;
8581
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008582 return 0;
8583}
8584
8585static int io_buffer_validate(struct iovec *iov)
8586{
8587 /*
8588 * Don't impose further limits on the size and buffer
8589 * constraints here, we'll -EINVAL later when IO is
8590 * submitted if they are wrong.
8591 */
8592 if (!iov->iov_base || !iov->iov_len)
8593 return -EFAULT;
8594
8595 /* arbitrary limit, but we need something */
8596 if (iov->iov_len > SZ_1G)
8597 return -EFAULT;
8598
8599 return 0;
8600}
8601
8602static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8603 unsigned int nr_args)
8604{
8605 int i, ret;
8606 struct iovec iov;
8607 struct page *last_hpage = NULL;
8608
8609 ret = io_buffers_map_alloc(ctx, nr_args);
8610 if (ret)
8611 return ret;
8612
Jens Axboeedafcce2019-01-09 09:16:05 -07008613 for (i = 0; i < nr_args; i++) {
8614 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008615
8616 ret = io_copy_iov(ctx, &iov, arg, i);
8617 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008618 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008619
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008620 ret = io_buffer_validate(&iov);
8621 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008622 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008623
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008624 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8625 if (ret)
8626 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008627
8628 ctx->nr_user_bufs++;
8629 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008630
8631 if (ret)
8632 io_sqe_buffers_unregister(ctx);
8633
Jens Axboeedafcce2019-01-09 09:16:05 -07008634 return ret;
8635}
8636
Jens Axboe9b402842019-04-11 11:45:41 -06008637static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8638{
8639 __s32 __user *fds = arg;
8640 int fd;
8641
8642 if (ctx->cq_ev_fd)
8643 return -EBUSY;
8644
8645 if (copy_from_user(&fd, fds, sizeof(*fds)))
8646 return -EFAULT;
8647
8648 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8649 if (IS_ERR(ctx->cq_ev_fd)) {
8650 int ret = PTR_ERR(ctx->cq_ev_fd);
8651 ctx->cq_ev_fd = NULL;
8652 return ret;
8653 }
8654
8655 return 0;
8656}
8657
8658static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8659{
8660 if (ctx->cq_ev_fd) {
8661 eventfd_ctx_put(ctx->cq_ev_fd);
8662 ctx->cq_ev_fd = NULL;
8663 return 0;
8664 }
8665
8666 return -ENXIO;
8667}
8668
Jens Axboe5a2e7452020-02-23 16:23:11 -07008669static int __io_destroy_buffers(int id, void *p, void *data)
8670{
8671 struct io_ring_ctx *ctx = data;
8672 struct io_buffer *buf = p;
8673
Jens Axboe067524e2020-03-02 16:32:28 -07008674 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008675 return 0;
8676}
8677
8678static void io_destroy_buffers(struct io_ring_ctx *ctx)
8679{
8680 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8681 idr_destroy(&ctx->io_buffer_idr);
8682}
8683
Jens Axboe2b188cc2019-01-07 10:46:33 -07008684static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8685{
Jens Axboe6b063142019-01-10 22:13:58 -07008686 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008687 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008688
8689 if (ctx->sqo_task) {
8690 put_task_struct(ctx->sqo_task);
8691 ctx->sqo_task = NULL;
8692 mmdrop(ctx->mm_account);
8693 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008694 }
Jens Axboedef596e2019-01-09 08:59:42 -07008695
Dennis Zhou91d8f512020-09-16 13:41:05 -07008696#ifdef CONFIG_BLK_CGROUP
8697 if (ctx->sqo_blkcg_css)
8698 css_put(ctx->sqo_blkcg_css);
8699#endif
8700
Jens Axboe6b063142019-01-10 22:13:58 -07008701 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008702 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008703 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008704 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008705
Jens Axboe2b188cc2019-01-07 10:46:33 -07008706#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008707 if (ctx->ring_sock) {
8708 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008709 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008710 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008711#endif
8712
Hristo Venev75b28af2019-08-26 17:23:46 +00008713 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008714 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008715
8716 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008717 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008718 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008719 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008720 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008721 kfree(ctx);
8722}
8723
8724static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8725{
8726 struct io_ring_ctx *ctx = file->private_data;
8727 __poll_t mask = 0;
8728
8729 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008730 /*
8731 * synchronizes with barrier from wq_has_sleeper call in
8732 * io_commit_cqring
8733 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008734 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008735 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008736 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008737 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8738 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008739 mask |= EPOLLIN | EPOLLRDNORM;
8740
8741 return mask;
8742}
8743
8744static int io_uring_fasync(int fd, struct file *file, int on)
8745{
8746 struct io_ring_ctx *ctx = file->private_data;
8747
8748 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8749}
8750
Yejune Deng0bead8c2020-12-24 11:02:20 +08008751static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008752{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008753 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008754
Jens Axboe1e6fa522020-10-15 08:46:24 -06008755 iod = idr_remove(&ctx->personality_idr, id);
8756 if (iod) {
8757 put_cred(iod->creds);
8758 if (refcount_dec_and_test(&iod->count))
8759 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008760 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008761 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008762
8763 return -EINVAL;
8764}
8765
8766static int io_remove_personalities(int id, void *p, void *data)
8767{
8768 struct io_ring_ctx *ctx = data;
8769
8770 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008771 return 0;
8772}
8773
Jens Axboe85faa7b2020-04-09 18:14:00 -06008774static void io_ring_exit_work(struct work_struct *work)
8775{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008776 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8777 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008778
Jens Axboe56952e92020-06-17 15:00:04 -06008779 /*
8780 * If we're doing polled IO and end up having requests being
8781 * submitted async (out-of-line), then completions can come in while
8782 * we're waiting for refs to drop. We need to reap these manually,
8783 * as nobody else will be looking for them.
8784 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008785 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008786 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008787 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008788 io_ring_ctx_free(ctx);
8789}
8790
Jens Axboe00c18642020-12-20 10:45:02 -07008791static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8792{
8793 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8794
8795 return req->ctx == data;
8796}
8797
Jens Axboe2b188cc2019-01-07 10:46:33 -07008798static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8799{
8800 mutex_lock(&ctx->uring_lock);
8801 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008802
8803 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8804 ctx->sqo_dead = 1;
8805
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008806 /* if force is set, the ring is going away. always drop after that */
8807 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008808 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008809 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008810 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008811 mutex_unlock(&ctx->uring_lock);
8812
Pavel Begunkov6b819282020-11-06 13:00:25 +00008813 io_kill_timeouts(ctx, NULL, NULL);
8814 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008815
8816 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008817 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008818
Jens Axboe15dff282019-11-13 09:09:23 -07008819 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008820 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008821
8822 /*
8823 * Do this upfront, so we won't have a grace period where the ring
8824 * is closed but resources aren't reaped yet. This can cause
8825 * spurious failure in setting up a new ring.
8826 */
Jens Axboe760618f2020-07-24 12:53:31 -06008827 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8828 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008829
Jens Axboe85faa7b2020-04-09 18:14:00 -06008830 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008831 /*
8832 * Use system_unbound_wq to avoid spawning tons of event kworkers
8833 * if we're exiting a ton of rings at the same time. It just adds
8834 * noise and overhead, there's no discernable change in runtime
8835 * over using system_wq.
8836 */
8837 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008838}
8839
8840static int io_uring_release(struct inode *inode, struct file *file)
8841{
8842 struct io_ring_ctx *ctx = file->private_data;
8843
8844 file->private_data = NULL;
8845 io_ring_ctx_wait_and_kill(ctx);
8846 return 0;
8847}
8848
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008849struct io_task_cancel {
8850 struct task_struct *task;
8851 struct files_struct *files;
8852};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008853
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008854static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008855{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008856 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008857 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008858 bool ret;
8859
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008860 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008861 unsigned long flags;
8862 struct io_ring_ctx *ctx = req->ctx;
8863
8864 /* protect against races with linked timeouts */
8865 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008866 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008867 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8868 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008869 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008870 }
8871 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008872}
8873
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008874static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008875 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008876 struct files_struct *files)
8877{
8878 struct io_defer_entry *de = NULL;
8879 LIST_HEAD(list);
8880
8881 spin_lock_irq(&ctx->completion_lock);
8882 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008883 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008884 list_cut_position(&list, &ctx->defer_list, &de->list);
8885 break;
8886 }
8887 }
8888 spin_unlock_irq(&ctx->completion_lock);
8889
8890 while (!list_empty(&list)) {
8891 de = list_first_entry(&list, struct io_defer_entry, list);
8892 list_del_init(&de->list);
8893 req_set_fail_links(de->req);
8894 io_put_req(de->req);
8895 io_req_complete(de->req, -ECANCELED);
8896 kfree(de);
8897 }
8898}
8899
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008900static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8901 struct task_struct *task,
8902 struct files_struct *files)
8903{
8904 struct io_task_cancel cancel = { .task = task, .files = files, };
8905
8906 while (1) {
8907 enum io_wq_cancel cret;
8908 bool ret = false;
8909
8910 if (ctx->io_wq) {
8911 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8912 &cancel, true);
8913 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8914 }
8915
8916 /* SQPOLL thread does its own polling */
8917 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8918 while (!list_empty_careful(&ctx->iopoll_list)) {
8919 io_iopoll_try_reap_events(ctx);
8920 ret = true;
8921 }
8922 }
8923
8924 ret |= io_poll_remove_all(ctx, task, files);
8925 ret |= io_kill_timeouts(ctx, task, files);
8926 ret |= io_run_task_work();
8927 io_cqring_overflow_flush(ctx, true, task, files);
8928 if (!ret)
8929 break;
8930 cond_resched();
8931 }
8932}
8933
Pavel Begunkovca70f002021-01-26 15:28:27 +00008934static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8935 struct task_struct *task,
8936 struct files_struct *files)
8937{
8938 struct io_kiocb *req;
8939 int cnt = 0;
8940
8941 spin_lock_irq(&ctx->inflight_lock);
8942 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8943 cnt += io_match_task(req, task, files);
8944 spin_unlock_irq(&ctx->inflight_lock);
8945 return cnt;
8946}
8947
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008948static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008949 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008950 struct files_struct *files)
8951{
Jens Axboefcb323c2019-10-24 12:39:47 -06008952 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008953 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008954 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008955
Pavel Begunkovca70f002021-01-26 15:28:27 +00008956 inflight = io_uring_count_inflight(ctx, task, files);
8957 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008958 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008959
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008960 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008961 prepare_to_wait(&task->io_uring->wait, &wait,
8962 TASK_UNINTERRUPTIBLE);
8963 if (inflight == io_uring_count_inflight(ctx, task, files))
8964 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008965 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008966 }
8967}
8968
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008969static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8970{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008971 mutex_lock(&ctx->uring_lock);
8972 ctx->sqo_dead = 1;
8973 mutex_unlock(&ctx->uring_lock);
8974
8975 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008976 if (ctx->rings)
8977 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008978}
8979
Jens Axboe0f212202020-09-13 13:09:39 -06008980/*
8981 * We need to iteratively cancel requests, in case a request has dependent
8982 * hard links. These persist even for failure of cancelations, hence keep
8983 * looping until none are found.
8984 */
8985static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8986 struct files_struct *files)
8987{
8988 struct task_struct *task = current;
8989
Jens Axboefdaf0832020-10-30 09:37:30 -06008990 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008991 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008992 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008993 atomic_inc(&task->io_uring->in_idle);
8994 io_sq_thread_park(ctx->sq_data);
8995 }
Jens Axboe0f212202020-09-13 13:09:39 -06008996
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008997 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008998
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008999 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009000 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009001 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009002
9003 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9004 atomic_dec(&task->io_uring->in_idle);
9005 /*
9006 * If the files that are going away are the ones in the thread
9007 * identity, clear them out.
9008 */
9009 if (task->io_uring->identity->files == files)
9010 task->io_uring->identity->files = NULL;
9011 io_sq_thread_unpark(ctx->sq_data);
9012 }
Jens Axboe0f212202020-09-13 13:09:39 -06009013}
9014
9015/*
9016 * Note that this task has used io_uring. We use it for cancelation purposes.
9017 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009018static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009019{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009020 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009021 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009022
9023 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009024 ret = io_uring_alloc_task_context(current);
9025 if (unlikely(ret))
9026 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009027 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009028 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009029 if (tctx->last != file) {
9030 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009031
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009032 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009033 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009034 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9035 file, GFP_KERNEL));
9036 if (ret) {
9037 fput(file);
9038 return ret;
9039 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009040
9041 /* one and only SQPOLL file note, held by sqo_task */
9042 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9043 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009044 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009045 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009046 }
9047
Jens Axboefdaf0832020-10-30 09:37:30 -06009048 /*
9049 * This is race safe in that the task itself is doing this, hence it
9050 * cannot be going through the exit/cancel paths at the same time.
9051 * This cannot be modified while exit/cancel is running.
9052 */
9053 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9054 tctx->sqpoll = true;
9055
Jens Axboe0f212202020-09-13 13:09:39 -06009056 return 0;
9057}
9058
9059/*
9060 * Remove this io_uring_file -> task mapping.
9061 */
9062static void io_uring_del_task_file(struct file *file)
9063{
9064 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009065
9066 if (tctx->last == file)
9067 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009068 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009069 if (file)
9070 fput(file);
9071}
9072
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009073static void io_uring_remove_task_files(struct io_uring_task *tctx)
9074{
9075 struct file *file;
9076 unsigned long index;
9077
9078 xa_for_each(&tctx->xa, index, file)
9079 io_uring_del_task_file(file);
9080}
9081
Jens Axboe0f212202020-09-13 13:09:39 -06009082void __io_uring_files_cancel(struct files_struct *files)
9083{
9084 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009085 struct file *file;
9086 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009087
9088 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009089 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009090 xa_for_each(&tctx->xa, index, file)
9091 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009092 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009093
9094 if (files)
9095 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009096}
9097
9098static s64 tctx_inflight(struct io_uring_task *tctx)
9099{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009100 return percpu_counter_sum(&tctx->inflight);
9101}
9102
9103static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9104{
9105 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009106 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009107 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009108
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009109 if (!ctx->sq_data)
9110 return;
9111 tctx = ctx->sq_data->thread->io_uring;
9112 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009113
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009114 atomic_inc(&tctx->in_idle);
9115 do {
9116 /* read completions before cancelations */
9117 inflight = tctx_inflight(tctx);
9118 if (!inflight)
9119 break;
9120 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009121
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009122 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9123 /*
9124 * If we've seen completions, retry without waiting. This
9125 * avoids a race where a completion comes in before we did
9126 * prepare_to_wait().
9127 */
9128 if (inflight == tctx_inflight(tctx))
9129 schedule();
9130 finish_wait(&tctx->wait, &wait);
9131 } while (1);
9132 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009133}
9134
Jens Axboe0f212202020-09-13 13:09:39 -06009135/*
9136 * Find any io_uring fd that this task has registered or done IO on, and cancel
9137 * requests.
9138 */
9139void __io_uring_task_cancel(void)
9140{
9141 struct io_uring_task *tctx = current->io_uring;
9142 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009143 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009144
9145 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009146 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009147
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009148 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009149 if (tctx->sqpoll) {
9150 struct file *file;
9151 unsigned long index;
9152
9153 xa_for_each(&tctx->xa, index, file)
9154 io_uring_cancel_sqpoll(file->private_data);
9155 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009156
Jens Axboed8a6df12020-10-15 16:24:45 -06009157 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009158 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009159 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009160 if (!inflight)
9161 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009162 __io_uring_files_cancel(NULL);
9163
9164 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9165
9166 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009167 * If we've seen completions, retry without waiting. This
9168 * avoids a race where a completion comes in before we did
9169 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009170 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009171 if (inflight == tctx_inflight(tctx))
9172 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009173 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009174 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009175
Jens Axboefdaf0832020-10-30 09:37:30 -06009176 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009177
9178 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009179}
9180
Jens Axboefcb323c2019-10-24 12:39:47 -06009181static int io_uring_flush(struct file *file, void *data)
9182{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009183 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009184 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009185
Jens Axboe84965ff2021-01-23 15:51:11 -07009186 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9187 io_uring_cancel_task_requests(ctx, NULL);
9188
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009189 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009190 return 0;
9191
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009192 /* we should have cancelled and erased it before PF_EXITING */
9193 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9194 xa_load(&tctx->xa, (unsigned long)file));
9195
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009196 /*
9197 * fput() is pending, will be 2 if the only other ref is our potential
9198 * task file note. If the task is exiting, drop regardless of count.
9199 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009200 if (atomic_long_read(&file->f_count) != 2)
9201 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009202
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009203 if (ctx->flags & IORING_SETUP_SQPOLL) {
9204 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009205 WARN_ON_ONCE(ctx->sqo_task != current &&
9206 xa_load(&tctx->xa, (unsigned long)file));
9207 /* sqo_dead check is for when this happens after cancellation */
9208 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009209 !xa_load(&tctx->xa, (unsigned long)file));
9210
9211 io_disable_sqo_submit(ctx);
9212 }
9213
9214 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9215 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009216 return 0;
9217}
9218
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009219static void *io_uring_validate_mmap_request(struct file *file,
9220 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009221{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009222 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009223 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009224 struct page *page;
9225 void *ptr;
9226
9227 switch (offset) {
9228 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009229 case IORING_OFF_CQ_RING:
9230 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009231 break;
9232 case IORING_OFF_SQES:
9233 ptr = ctx->sq_sqes;
9234 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009235 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009236 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009237 }
9238
9239 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009240 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009241 return ERR_PTR(-EINVAL);
9242
9243 return ptr;
9244}
9245
9246#ifdef CONFIG_MMU
9247
9248static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9249{
9250 size_t sz = vma->vm_end - vma->vm_start;
9251 unsigned long pfn;
9252 void *ptr;
9253
9254 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9255 if (IS_ERR(ptr))
9256 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009257
9258 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9259 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9260}
9261
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009262#else /* !CONFIG_MMU */
9263
9264static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9265{
9266 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9267}
9268
9269static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9270{
9271 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9272}
9273
9274static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9275 unsigned long addr, unsigned long len,
9276 unsigned long pgoff, unsigned long flags)
9277{
9278 void *ptr;
9279
9280 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9281 if (IS_ERR(ptr))
9282 return PTR_ERR(ptr);
9283
9284 return (unsigned long) ptr;
9285}
9286
9287#endif /* !CONFIG_MMU */
9288
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009289static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009290{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009291 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009292 DEFINE_WAIT(wait);
9293
9294 do {
9295 if (!io_sqring_full(ctx))
9296 break;
9297
9298 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9299
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009300 if (unlikely(ctx->sqo_dead)) {
9301 ret = -EOWNERDEAD;
9302 goto out;
9303 }
9304
Jens Axboe90554202020-09-03 12:12:41 -06009305 if (!io_sqring_full(ctx))
9306 break;
9307
9308 schedule();
9309 } while (!signal_pending(current));
9310
9311 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009312out:
9313 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009314}
9315
Hao Xuc73ebb62020-11-03 10:54:37 +08009316static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9317 struct __kernel_timespec __user **ts,
9318 const sigset_t __user **sig)
9319{
9320 struct io_uring_getevents_arg arg;
9321
9322 /*
9323 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9324 * is just a pointer to the sigset_t.
9325 */
9326 if (!(flags & IORING_ENTER_EXT_ARG)) {
9327 *sig = (const sigset_t __user *) argp;
9328 *ts = NULL;
9329 return 0;
9330 }
9331
9332 /*
9333 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9334 * timespec and sigset_t pointers if good.
9335 */
9336 if (*argsz != sizeof(arg))
9337 return -EINVAL;
9338 if (copy_from_user(&arg, argp, sizeof(arg)))
9339 return -EFAULT;
9340 *sig = u64_to_user_ptr(arg.sigmask);
9341 *argsz = arg.sigmask_sz;
9342 *ts = u64_to_user_ptr(arg.ts);
9343 return 0;
9344}
9345
Jens Axboe2b188cc2019-01-07 10:46:33 -07009346SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009347 u32, min_complete, u32, flags, const void __user *, argp,
9348 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009349{
9350 struct io_ring_ctx *ctx;
9351 long ret = -EBADF;
9352 int submitted = 0;
9353 struct fd f;
9354
Jens Axboe4c6e2772020-07-01 11:29:10 -06009355 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009356
Jens Axboe90554202020-09-03 12:12:41 -06009357 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009358 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009359 return -EINVAL;
9360
9361 f = fdget(fd);
9362 if (!f.file)
9363 return -EBADF;
9364
9365 ret = -EOPNOTSUPP;
9366 if (f.file->f_op != &io_uring_fops)
9367 goto out_fput;
9368
9369 ret = -ENXIO;
9370 ctx = f.file->private_data;
9371 if (!percpu_ref_tryget(&ctx->refs))
9372 goto out_fput;
9373
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009374 ret = -EBADFD;
9375 if (ctx->flags & IORING_SETUP_R_DISABLED)
9376 goto out;
9377
Jens Axboe6c271ce2019-01-10 11:22:30 -07009378 /*
9379 * For SQ polling, the thread will do all submissions and completions.
9380 * Just return the requested submit count, and wake the thread if
9381 * we were asked to.
9382 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009383 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009384 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009385 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009386
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009387 ret = -EOWNERDEAD;
9388 if (unlikely(ctx->sqo_dead))
9389 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009390 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009391 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009392 if (flags & IORING_ENTER_SQ_WAIT) {
9393 ret = io_sqpoll_wait_sq(ctx);
9394 if (ret)
9395 goto out;
9396 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009397 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009398 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009399 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009400 if (unlikely(ret))
9401 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009402 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009403 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009405
9406 if (submitted != to_submit)
9407 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408 }
9409 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009410 const sigset_t __user *sig;
9411 struct __kernel_timespec __user *ts;
9412
9413 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9414 if (unlikely(ret))
9415 goto out;
9416
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417 min_complete = min(min_complete, ctx->cq_entries);
9418
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009419 /*
9420 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9421 * space applications don't need to do io completion events
9422 * polling again, they can rely on io_sq_thread to do polling
9423 * work, which can reduce cpu usage and uring_lock contention.
9424 */
9425 if (ctx->flags & IORING_SETUP_IOPOLL &&
9426 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009427 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009428 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009429 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009430 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009431 }
9432
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009433out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009434 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009435out_fput:
9436 fdput(f);
9437 return submitted ? submitted : ret;
9438}
9439
Tobias Klauserbebdb652020-02-26 18:38:32 +01009440#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009441static int io_uring_show_cred(int id, void *p, void *data)
9442{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009443 struct io_identity *iod = p;
9444 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009445 struct seq_file *m = data;
9446 struct user_namespace *uns = seq_user_ns(m);
9447 struct group_info *gi;
9448 kernel_cap_t cap;
9449 unsigned __capi;
9450 int g;
9451
9452 seq_printf(m, "%5d\n", id);
9453 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9454 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9455 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9456 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9457 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9458 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9459 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9460 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9461 seq_puts(m, "\n\tGroups:\t");
9462 gi = cred->group_info;
9463 for (g = 0; g < gi->ngroups; g++) {
9464 seq_put_decimal_ull(m, g ? " " : "",
9465 from_kgid_munged(uns, gi->gid[g]));
9466 }
9467 seq_puts(m, "\n\tCapEff:\t");
9468 cap = cred->cap_effective;
9469 CAP_FOR_EACH_U32(__capi)
9470 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9471 seq_putc(m, '\n');
9472 return 0;
9473}
9474
9475static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9476{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009477 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009478 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009479 int i;
9480
Jens Axboefad8e0d2020-09-28 08:57:48 -06009481 /*
9482 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9483 * since fdinfo case grabs it in the opposite direction of normal use
9484 * cases. If we fail to get the lock, we just don't iterate any
9485 * structures that could be going away outside the io_uring mutex.
9486 */
9487 has_lock = mutex_trylock(&ctx->uring_lock);
9488
Joseph Qidbbe9c62020-09-29 09:01:22 -06009489 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9490 sq = ctx->sq_data;
9491
9492 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9493 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009494 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009495 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009496 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009497
Jens Axboe87ce9552020-01-30 08:25:34 -07009498 if (f)
9499 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9500 else
9501 seq_printf(m, "%5u: <none>\n", i);
9502 }
9503 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009504 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009505 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9506
9507 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9508 (unsigned int) buf->len);
9509 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009510 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009511 seq_printf(m, "Personalities:\n");
9512 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9513 }
Jens Axboed7718a92020-02-14 22:23:12 -07009514 seq_printf(m, "PollList:\n");
9515 spin_lock_irq(&ctx->completion_lock);
9516 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9517 struct hlist_head *list = &ctx->cancel_hash[i];
9518 struct io_kiocb *req;
9519
9520 hlist_for_each_entry(req, list, hash_node)
9521 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9522 req->task->task_works != NULL);
9523 }
9524 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009525 if (has_lock)
9526 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009527}
9528
9529static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9530{
9531 struct io_ring_ctx *ctx = f->private_data;
9532
9533 if (percpu_ref_tryget(&ctx->refs)) {
9534 __io_uring_show_fdinfo(ctx, m);
9535 percpu_ref_put(&ctx->refs);
9536 }
9537}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009538#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009539
Jens Axboe2b188cc2019-01-07 10:46:33 -07009540static const struct file_operations io_uring_fops = {
9541 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009542 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009543 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009544#ifndef CONFIG_MMU
9545 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9546 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9547#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009548 .poll = io_uring_poll,
9549 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009550#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009551 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009552#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553};
9554
9555static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9556 struct io_uring_params *p)
9557{
Hristo Venev75b28af2019-08-26 17:23:46 +00009558 struct io_rings *rings;
9559 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009560
Jens Axboebd740482020-08-05 12:58:23 -06009561 /* make sure these are sane, as we already accounted them */
9562 ctx->sq_entries = p->sq_entries;
9563 ctx->cq_entries = p->cq_entries;
9564
Hristo Venev75b28af2019-08-26 17:23:46 +00009565 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9566 if (size == SIZE_MAX)
9567 return -EOVERFLOW;
9568
9569 rings = io_mem_alloc(size);
9570 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009571 return -ENOMEM;
9572
Hristo Venev75b28af2019-08-26 17:23:46 +00009573 ctx->rings = rings;
9574 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9575 rings->sq_ring_mask = p->sq_entries - 1;
9576 rings->cq_ring_mask = p->cq_entries - 1;
9577 rings->sq_ring_entries = p->sq_entries;
9578 rings->cq_ring_entries = p->cq_entries;
9579 ctx->sq_mask = rings->sq_ring_mask;
9580 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009581
9582 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009583 if (size == SIZE_MAX) {
9584 io_mem_free(ctx->rings);
9585 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009586 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009587 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009588
9589 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009590 if (!ctx->sq_sqes) {
9591 io_mem_free(ctx->rings);
9592 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009593 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009594 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009595
Jens Axboe2b188cc2019-01-07 10:46:33 -07009596 return 0;
9597}
9598
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009599static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9600{
9601 int ret, fd;
9602
9603 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9604 if (fd < 0)
9605 return fd;
9606
9607 ret = io_uring_add_task_file(ctx, file);
9608 if (ret) {
9609 put_unused_fd(fd);
9610 return ret;
9611 }
9612 fd_install(fd, file);
9613 return fd;
9614}
9615
Jens Axboe2b188cc2019-01-07 10:46:33 -07009616/*
9617 * Allocate an anonymous fd, this is what constitutes the application
9618 * visible backing of an io_uring instance. The application mmaps this
9619 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9620 * we have to tie this fd to a socket for file garbage collection purposes.
9621 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009622static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009623{
9624 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009625#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009626 int ret;
9627
Jens Axboe2b188cc2019-01-07 10:46:33 -07009628 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9629 &ctx->ring_sock);
9630 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009631 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009632#endif
9633
Jens Axboe2b188cc2019-01-07 10:46:33 -07009634 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9635 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009636#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009637 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009638 sock_release(ctx->ring_sock);
9639 ctx->ring_sock = NULL;
9640 } else {
9641 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009642 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009643#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009644 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009645}
9646
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009647static int io_uring_create(unsigned entries, struct io_uring_params *p,
9648 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649{
9650 struct user_struct *user = NULL;
9651 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009652 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009653 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009654 int ret;
9655
Jens Axboe8110c1a2019-12-28 15:39:54 -07009656 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009657 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009658 if (entries > IORING_MAX_ENTRIES) {
9659 if (!(p->flags & IORING_SETUP_CLAMP))
9660 return -EINVAL;
9661 entries = IORING_MAX_ENTRIES;
9662 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009663
9664 /*
9665 * Use twice as many entries for the CQ ring. It's possible for the
9666 * application to drive a higher depth than the size of the SQ ring,
9667 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009668 * some flexibility in overcommitting a bit. If the application has
9669 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9670 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009671 */
9672 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009673 if (p->flags & IORING_SETUP_CQSIZE) {
9674 /*
9675 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9676 * to a power-of-two, if it isn't already. We do NOT impose
9677 * any cq vs sq ring sizing.
9678 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009679 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009680 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009681 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9682 if (!(p->flags & IORING_SETUP_CLAMP))
9683 return -EINVAL;
9684 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9685 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009686 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9687 if (p->cq_entries < p->sq_entries)
9688 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009689 } else {
9690 p->cq_entries = 2 * p->sq_entries;
9691 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009692
9693 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009694 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009695
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009696 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009697 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009698 ring_pages(p->sq_entries, p->cq_entries));
9699 if (ret) {
9700 free_uid(user);
9701 return ret;
9702 }
9703 }
9704
9705 ctx = io_ring_ctx_alloc(p);
9706 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009707 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009708 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009709 p->cq_entries));
9710 free_uid(user);
9711 return -ENOMEM;
9712 }
9713 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009714 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009715 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009716#ifdef CONFIG_AUDIT
9717 ctx->loginuid = current->loginuid;
9718 ctx->sessionid = current->sessionid;
9719#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009720 ctx->sqo_task = get_task_struct(current);
9721
9722 /*
9723 * This is just grabbed for accounting purposes. When a process exits,
9724 * the mm is exited and dropped before the files, hence we need to hang
9725 * on to this mm purely for the purposes of being able to unaccount
9726 * memory (locked/pinned vm). It's not used for anything else.
9727 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009728 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009729 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009730
Dennis Zhou91d8f512020-09-16 13:41:05 -07009731#ifdef CONFIG_BLK_CGROUP
9732 /*
9733 * The sq thread will belong to the original cgroup it was inited in.
9734 * If the cgroup goes offline (e.g. disabling the io controller), then
9735 * issued bios will be associated with the closest cgroup later in the
9736 * block layer.
9737 */
9738 rcu_read_lock();
9739 ctx->sqo_blkcg_css = blkcg_css();
9740 ret = css_tryget_online(ctx->sqo_blkcg_css);
9741 rcu_read_unlock();
9742 if (!ret) {
9743 /* don't init against a dying cgroup, have the user try again */
9744 ctx->sqo_blkcg_css = NULL;
9745 ret = -ENODEV;
9746 goto err;
9747 }
9748#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009749
Jens Axboe2b188cc2019-01-07 10:46:33 -07009750 /*
9751 * Account memory _before_ installing the file descriptor. Once
9752 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009753 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009754 * will un-account as well.
9755 */
9756 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9757 ACCT_LOCKED);
9758 ctx->limit_mem = limit_mem;
9759
9760 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009761 if (ret)
9762 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009763
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009764 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009765 if (ret)
9766 goto err;
9767
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009768 if (!(p->flags & IORING_SETUP_R_DISABLED))
9769 io_sq_offload_start(ctx);
9770
Jens Axboe2b188cc2019-01-07 10:46:33 -07009771 memset(&p->sq_off, 0, sizeof(p->sq_off));
9772 p->sq_off.head = offsetof(struct io_rings, sq.head);
9773 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9774 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9775 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9776 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9777 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9778 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9779
9780 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009781 p->cq_off.head = offsetof(struct io_rings, cq.head);
9782 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9783 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9784 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9785 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9786 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009787 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009788
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009789 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9790 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009791 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009792 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9793 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009794
9795 if (copy_to_user(params, p, sizeof(*p))) {
9796 ret = -EFAULT;
9797 goto err;
9798 }
Jens Axboed1719f72020-07-30 13:43:53 -06009799
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009800 file = io_uring_get_file(ctx);
9801 if (IS_ERR(file)) {
9802 ret = PTR_ERR(file);
9803 goto err;
9804 }
9805
Jens Axboed1719f72020-07-30 13:43:53 -06009806 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009807 * Install ring fd as the very last thing, so we don't risk someone
9808 * having closed it before we finish setup
9809 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009810 ret = io_uring_install_fd(ctx, file);
9811 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009812 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009813 /* fput will clean it up */
9814 fput(file);
9815 return ret;
9816 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009817
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009818 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009819 return ret;
9820err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009821 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009822 io_ring_ctx_wait_and_kill(ctx);
9823 return ret;
9824}
9825
9826/*
9827 * Sets up an aio uring context, and returns the fd. Applications asks for a
9828 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9829 * params structure passed in.
9830 */
9831static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9832{
9833 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009834 int i;
9835
9836 if (copy_from_user(&p, params, sizeof(p)))
9837 return -EFAULT;
9838 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9839 if (p.resv[i])
9840 return -EINVAL;
9841 }
9842
Jens Axboe6c271ce2019-01-10 11:22:30 -07009843 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009844 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009845 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9846 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009847 return -EINVAL;
9848
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009849 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009850}
9851
9852SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9853 struct io_uring_params __user *, params)
9854{
9855 return io_uring_setup(entries, params);
9856}
9857
Jens Axboe66f4af92020-01-16 15:36:52 -07009858static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9859{
9860 struct io_uring_probe *p;
9861 size_t size;
9862 int i, ret;
9863
9864 size = struct_size(p, ops, nr_args);
9865 if (size == SIZE_MAX)
9866 return -EOVERFLOW;
9867 p = kzalloc(size, GFP_KERNEL);
9868 if (!p)
9869 return -ENOMEM;
9870
9871 ret = -EFAULT;
9872 if (copy_from_user(p, arg, size))
9873 goto out;
9874 ret = -EINVAL;
9875 if (memchr_inv(p, 0, size))
9876 goto out;
9877
9878 p->last_op = IORING_OP_LAST - 1;
9879 if (nr_args > IORING_OP_LAST)
9880 nr_args = IORING_OP_LAST;
9881
9882 for (i = 0; i < nr_args; i++) {
9883 p->ops[i].op = i;
9884 if (!io_op_defs[i].not_supported)
9885 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9886 }
9887 p->ops_len = i;
9888
9889 ret = 0;
9890 if (copy_to_user(arg, p, size))
9891 ret = -EFAULT;
9892out:
9893 kfree(p);
9894 return ret;
9895}
9896
Jens Axboe071698e2020-01-28 10:04:42 -07009897static int io_register_personality(struct io_ring_ctx *ctx)
9898{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009899 struct io_identity *id;
9900 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009901
Jens Axboe1e6fa522020-10-15 08:46:24 -06009902 id = kmalloc(sizeof(*id), GFP_KERNEL);
9903 if (unlikely(!id))
9904 return -ENOMEM;
9905
9906 io_init_identity(id);
9907 id->creds = get_current_cred();
9908
9909 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9910 if (ret < 0) {
9911 put_cred(id->creds);
9912 kfree(id);
9913 }
9914 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009915}
9916
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009917static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9918 unsigned int nr_args)
9919{
9920 struct io_uring_restriction *res;
9921 size_t size;
9922 int i, ret;
9923
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009924 /* Restrictions allowed only if rings started disabled */
9925 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9926 return -EBADFD;
9927
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009928 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009929 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009930 return -EBUSY;
9931
9932 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9933 return -EINVAL;
9934
9935 size = array_size(nr_args, sizeof(*res));
9936 if (size == SIZE_MAX)
9937 return -EOVERFLOW;
9938
9939 res = memdup_user(arg, size);
9940 if (IS_ERR(res))
9941 return PTR_ERR(res);
9942
9943 ret = 0;
9944
9945 for (i = 0; i < nr_args; i++) {
9946 switch (res[i].opcode) {
9947 case IORING_RESTRICTION_REGISTER_OP:
9948 if (res[i].register_op >= IORING_REGISTER_LAST) {
9949 ret = -EINVAL;
9950 goto out;
9951 }
9952
9953 __set_bit(res[i].register_op,
9954 ctx->restrictions.register_op);
9955 break;
9956 case IORING_RESTRICTION_SQE_OP:
9957 if (res[i].sqe_op >= IORING_OP_LAST) {
9958 ret = -EINVAL;
9959 goto out;
9960 }
9961
9962 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9963 break;
9964 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9965 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9966 break;
9967 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9968 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9969 break;
9970 default:
9971 ret = -EINVAL;
9972 goto out;
9973 }
9974 }
9975
9976out:
9977 /* Reset all restrictions if an error happened */
9978 if (ret != 0)
9979 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9980 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009981 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009982
9983 kfree(res);
9984 return ret;
9985}
9986
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009987static int io_register_enable_rings(struct io_ring_ctx *ctx)
9988{
9989 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9990 return -EBADFD;
9991
9992 if (ctx->restrictions.registered)
9993 ctx->restricted = 1;
9994
9995 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9996
9997 io_sq_offload_start(ctx);
9998
9999 return 0;
10000}
10001
Jens Axboe071698e2020-01-28 10:04:42 -070010002static bool io_register_op_must_quiesce(int op)
10003{
10004 switch (op) {
10005 case IORING_UNREGISTER_FILES:
10006 case IORING_REGISTER_FILES_UPDATE:
10007 case IORING_REGISTER_PROBE:
10008 case IORING_REGISTER_PERSONALITY:
10009 case IORING_UNREGISTER_PERSONALITY:
10010 return false;
10011 default:
10012 return true;
10013 }
10014}
10015
Jens Axboeedafcce2019-01-09 09:16:05 -070010016static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10017 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010018 __releases(ctx->uring_lock)
10019 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010020{
10021 int ret;
10022
Jens Axboe35fa71a2019-04-22 10:23:23 -060010023 /*
10024 * We're inside the ring mutex, if the ref is already dying, then
10025 * someone else killed the ctx or is already going through
10026 * io_uring_register().
10027 */
10028 if (percpu_ref_is_dying(&ctx->refs))
10029 return -ENXIO;
10030
Jens Axboe071698e2020-01-28 10:04:42 -070010031 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010032 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010033
Jens Axboe05f3fb32019-12-09 11:22:50 -070010034 /*
10035 * Drop uring mutex before waiting for references to exit. If
10036 * another thread is currently inside io_uring_enter() it might
10037 * need to grab the uring_lock to make progress. If we hold it
10038 * here across the drain wait, then we can deadlock. It's safe
10039 * to drop the mutex here, since no new references will come in
10040 * after we've killed the percpu ref.
10041 */
10042 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010043 do {
10044 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10045 if (!ret)
10046 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010047 ret = io_run_task_work_sig();
10048 if (ret < 0)
10049 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010050 } while (1);
10051
Jens Axboe05f3fb32019-12-09 11:22:50 -070010052 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010053
Jens Axboec1503682020-01-08 08:26:07 -070010054 if (ret) {
10055 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010056 goto out_quiesce;
10057 }
10058 }
10059
10060 if (ctx->restricted) {
10061 if (opcode >= IORING_REGISTER_LAST) {
10062 ret = -EINVAL;
10063 goto out;
10064 }
10065
10066 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10067 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010068 goto out;
10069 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010070 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010071
10072 switch (opcode) {
10073 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010074 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010075 break;
10076 case IORING_UNREGISTER_BUFFERS:
10077 ret = -EINVAL;
10078 if (arg || nr_args)
10079 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010080 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010081 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010082 case IORING_REGISTER_FILES:
10083 ret = io_sqe_files_register(ctx, arg, nr_args);
10084 break;
10085 case IORING_UNREGISTER_FILES:
10086 ret = -EINVAL;
10087 if (arg || nr_args)
10088 break;
10089 ret = io_sqe_files_unregister(ctx);
10090 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010091 case IORING_REGISTER_FILES_UPDATE:
10092 ret = io_sqe_files_update(ctx, arg, nr_args);
10093 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010094 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010095 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010096 ret = -EINVAL;
10097 if (nr_args != 1)
10098 break;
10099 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010100 if (ret)
10101 break;
10102 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10103 ctx->eventfd_async = 1;
10104 else
10105 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010106 break;
10107 case IORING_UNREGISTER_EVENTFD:
10108 ret = -EINVAL;
10109 if (arg || nr_args)
10110 break;
10111 ret = io_eventfd_unregister(ctx);
10112 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010113 case IORING_REGISTER_PROBE:
10114 ret = -EINVAL;
10115 if (!arg || nr_args > 256)
10116 break;
10117 ret = io_probe(ctx, arg, nr_args);
10118 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010119 case IORING_REGISTER_PERSONALITY:
10120 ret = -EINVAL;
10121 if (arg || nr_args)
10122 break;
10123 ret = io_register_personality(ctx);
10124 break;
10125 case IORING_UNREGISTER_PERSONALITY:
10126 ret = -EINVAL;
10127 if (arg)
10128 break;
10129 ret = io_unregister_personality(ctx, nr_args);
10130 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010131 case IORING_REGISTER_ENABLE_RINGS:
10132 ret = -EINVAL;
10133 if (arg || nr_args)
10134 break;
10135 ret = io_register_enable_rings(ctx);
10136 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010137 case IORING_REGISTER_RESTRICTIONS:
10138 ret = io_register_restrictions(ctx, arg, nr_args);
10139 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010140 default:
10141 ret = -EINVAL;
10142 break;
10143 }
10144
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010145out:
Jens Axboe071698e2020-01-28 10:04:42 -070010146 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010147 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010148 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010149out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010150 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010151 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010152 return ret;
10153}
10154
10155SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10156 void __user *, arg, unsigned int, nr_args)
10157{
10158 struct io_ring_ctx *ctx;
10159 long ret = -EBADF;
10160 struct fd f;
10161
10162 f = fdget(fd);
10163 if (!f.file)
10164 return -EBADF;
10165
10166 ret = -EOPNOTSUPP;
10167 if (f.file->f_op != &io_uring_fops)
10168 goto out_fput;
10169
10170 ctx = f.file->private_data;
10171
10172 mutex_lock(&ctx->uring_lock);
10173 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10174 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010175 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10176 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010177out_fput:
10178 fdput(f);
10179 return ret;
10180}
10181
Jens Axboe2b188cc2019-01-07 10:46:33 -070010182static int __init io_uring_init(void)
10183{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010184#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10185 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10186 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10187} while (0)
10188
10189#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10190 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10191 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10192 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10193 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10194 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10195 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10196 BUILD_BUG_SQE_ELEM(8, __u64, off);
10197 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10198 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010199 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010200 BUILD_BUG_SQE_ELEM(24, __u32, len);
10201 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10202 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10203 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10204 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010205 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10206 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010207 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10208 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10209 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10210 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10211 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10212 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10213 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10214 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010215 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010216 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10217 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10218 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010219 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010220
Jens Axboed3656342019-12-18 09:50:26 -070010221 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010222 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010223 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10224 return 0;
10225};
10226__initcall(io_uring_init);