blob: a8d07273ddc05049c4b45991b86d1e6f6b7ac014 [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
Jens Axboeedafcce2019-01-09 09:16:05 -0700190struct io_mapped_ubuf {
191 u64 ubuf;
192 size_t len;
193 struct bio_vec *bvec;
194 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600195 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700196};
197
Jens Axboe65e19f52019-10-26 07:20:21 -0600198struct fixed_file_table {
199 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700200};
201
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202struct fixed_file_ref_node {
203 struct percpu_ref refs;
204 struct list_head node;
205 struct list_head file_list;
206 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600207 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000208 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800209};
210
Jens Axboe05f3fb32019-12-09 11:22:50 -0700211struct fixed_file_data {
212 struct fixed_file_table *table;
213 struct io_ring_ctx *ctx;
214
Pavel Begunkovb2e96852020-10-10 18:34:16 +0100215 struct fixed_file_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700217 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct list_head ref_list;
219 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700220};
221
Jens Axboe5a2e7452020-02-23 16:23:11 -0700222struct io_buffer {
223 struct list_head list;
224 __u64 addr;
Thadeu Lima de Souza Cascardo7e916d02021-05-05 09:47:06 -0300225 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700226 __u16 bid;
227};
228
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200229struct io_restriction {
230 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
231 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
232 u8 sqe_flags_allowed;
233 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200234 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200235};
236
Jens Axboe534ca6d2020-09-02 13:52:19 -0600237struct io_sq_data {
238 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600239 struct mutex lock;
240
241 /* ctx's that are using this sqd */
242 struct list_head ctx_list;
243 struct list_head ctx_new_list;
244 struct mutex ctx_lock;
245
Jens Axboe534ca6d2020-09-02 13:52:19 -0600246 struct task_struct *thread;
247 struct wait_queue_head wait;
248};
249
Jens Axboe2b188cc2019-01-07 10:46:33 -0700250struct io_ring_ctx {
251 struct {
252 struct percpu_ref refs;
253 } ____cacheline_aligned_in_smp;
254
255 struct {
256 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700258 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800259 unsigned int cq_overflow_flushed: 1;
260 unsigned int drain_next: 1;
261 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200262 unsigned int restricted: 1;
Pavel Begunkova63d9152021-01-26 11:17:03 +0000263 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700264
Hristo Venev75b28af2019-08-26 17:23:46 +0000265 /*
266 * Ring buffer of indices into array of io_uring_sqe, which is
267 * mmapped by the application using the IORING_OFF_SQES offset.
268 *
269 * This indirection could e.g. be used to assign fixed
270 * io_uring_sqe entries to operations and only submit them to
271 * the queue when needed.
272 *
273 * The kernel modifies neither the indices array nor the entries
274 * array.
275 */
276 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700277 unsigned cached_sq_head;
278 unsigned sq_entries;
279 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700280 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600281 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100282 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700283 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600284
285 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600286 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700287 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288
Jens Axboead3eb2c2019-12-18 17:12:20 -0700289 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 } ____cacheline_aligned_in_smp;
291
Hristo Venev75b28af2019-08-26 17:23:46 +0000292 struct io_rings *rings;
293
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600295 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600296
297 /*
298 * For SQPOLL usage - we hold a reference to the parent task, so we
299 * have access to the ->files
300 */
301 struct task_struct *sqo_task;
302
303 /* Only used for accounting purposes */
304 struct mm_struct *mm_account;
305
Dennis Zhou91d8f512020-09-16 13:41:05 -0700306#ifdef CONFIG_BLK_CGROUP
307 struct cgroup_subsys_state *sqo_blkcg_css;
308#endif
309
Jens Axboe534ca6d2020-09-02 13:52:19 -0600310 struct io_sq_data *sq_data; /* if using sq thread polling */
311
Jens Axboe90554202020-09-03 12:12:41 -0600312 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600313 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600314 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315
Jens Axboe6b063142019-01-10 22:13:58 -0700316 /*
317 * If used, fixed file set. Writers must ensure that ->refs is dead,
318 * readers must ensure that ->refs is alive as long as the file* is
319 * used. Only updated through io_uring_register(2).
320 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700321 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700322 unsigned nr_user_files;
323
Jens Axboeedafcce2019-01-09 09:16:05 -0700324 /* if used, fixed mapped user buffers */
325 unsigned nr_user_bufs;
326 struct io_mapped_ubuf *user_bufs;
327
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 struct user_struct *user;
329
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700330 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700331
Jens Axboe4ea33a92020-10-15 13:46:44 -0600332#ifdef CONFIG_AUDIT
333 kuid_t loginuid;
334 unsigned int sessionid;
335#endif
336
Jens Axboe0f158b42020-05-14 17:18:39 -0600337 struct completion ref_comp;
338 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700339
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700340 /* if all else fails... */
341 struct io_kiocb *fallback_req;
342
Jens Axboe206aefd2019-11-07 18:27:42 -0700343#if defined(CONFIG_UNIX)
344 struct socket *ring_sock;
345#endif
346
Jens Axboe90731882021-07-13 17:18:35 +0800347 struct xarray io_buffers;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700348
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +0800349 struct xarray personalities;
350 u32 pers_next;
Jens Axboe071698e2020-01-28 10:04:42 -0700351
Jens Axboe206aefd2019-11-07 18:27:42 -0700352 struct {
353 unsigned cached_cq_tail;
354 unsigned cq_entries;
355 unsigned cq_mask;
356 atomic_t cq_timeouts;
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -0500357 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700358 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700359 struct wait_queue_head cq_wait;
360 struct fasync_struct *cq_fasync;
361 struct eventfd_ctx *cq_ev_fd;
362 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363
364 struct {
365 struct mutex uring_lock;
366 wait_queue_head_t wait;
367 } ____cacheline_aligned_in_smp;
368
369 struct {
370 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700371
Jens Axboedef596e2019-01-09 08:59:42 -0700372 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300373 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700374 * io_uring instances that don't use IORING_SETUP_SQPOLL.
375 * For SQPOLL, only the single threaded io_sq_thread() will
376 * manipulate the list, hence no extra locking is needed there.
377 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300378 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700379 struct hlist_head *cancel_hash;
380 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700381 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600382
383 spinlock_t inflight_lock;
384 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700385 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600386
Jens Axboe4a38aed22020-05-14 17:21:15 -0600387 struct delayed_work file_put_work;
388 struct llist_head file_put_llist;
389
Jens Axboe85faa7b2020-04-09 18:14:00 -0600390 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200391 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700392};
393
Jens Axboe09bb8392019-03-13 12:39:28 -0600394/*
395 * First field must be the file pointer in all the
396 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
397 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700398struct io_poll_iocb {
399 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700400 union {
401 struct wait_queue_head *head;
402 u64 addr;
403 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700404 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600405 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700406 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700407 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700408};
409
Jens Axboeb5dba592019-12-11 14:02:38 -0700410struct io_close {
411 struct file *file;
412 struct file *put_file;
413 int fd;
414};
415
Jens Axboead8a48a2019-11-15 08:49:11 -0700416struct io_timeout_data {
417 struct io_kiocb *req;
418 struct hrtimer timer;
419 struct timespec64 ts;
420 enum hrtimer_mode mode;
421};
422
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700423struct io_accept {
424 struct file *file;
425 struct sockaddr __user *addr;
426 int __user *addr_len;
427 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600428 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700429};
430
431struct io_sync {
432 struct file *file;
433 loff_t len;
434 loff_t off;
435 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700436 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700437};
438
Jens Axboefbf23842019-12-17 18:45:56 -0700439struct io_cancel {
440 struct file *file;
441 u64 addr;
442};
443
Jens Axboeb29472e2019-12-17 18:50:29 -0700444struct io_timeout {
445 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300446 u32 off;
447 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300448 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700449};
450
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100451struct io_timeout_rem {
452 struct file *file;
453 u64 addr;
454};
455
Jens Axboe9adbd452019-12-20 08:45:55 -0700456struct io_rw {
457 /* NOTE: kiocb has the file as the first member, so don't do it here */
458 struct kiocb kiocb;
459 u64 addr;
460 u64 len;
461};
462
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700463struct io_connect {
464 struct file *file;
465 struct sockaddr __user *addr;
466 int addr_len;
467};
468
Jens Axboee47293f2019-12-20 08:58:21 -0700469struct io_sr_msg {
470 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700471 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300472 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700473 void __user *buf;
474 };
Jens Axboee47293f2019-12-20 08:58:21 -0700475 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700476 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700477 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700478 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700479};
480
Jens Axboe15b71ab2019-12-11 11:20:36 -0700481struct io_open {
482 struct file *file;
483 int dfd;
Jens Axboe944d1442020-11-13 16:48:44 -0700484 bool ignore_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700485 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700486 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600487 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700488};
489
Jens Axboe05f3fb32019-12-09 11:22:50 -0700490struct io_files_update {
491 struct file *file;
492 u64 arg;
493 u32 nr_args;
494 u32 offset;
495};
496
Jens Axboe4840e412019-12-25 22:03:45 -0700497struct io_fadvise {
498 struct file *file;
499 u64 offset;
500 u32 len;
501 u32 advice;
502};
503
Jens Axboec1ca7572019-12-25 22:18:28 -0700504struct io_madvise {
505 struct file *file;
506 u64 addr;
507 u32 len;
508 u32 advice;
509};
510
Jens Axboe3e4827b2020-01-08 15:18:09 -0700511struct io_epoll {
512 struct file *file;
513 int epfd;
514 int op;
515 int fd;
516 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517};
518
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300519struct io_splice {
520 struct file *file_out;
521 struct file *file_in;
522 loff_t off_out;
523 loff_t off_in;
524 u64 len;
525 unsigned int flags;
526};
527
Jens Axboeddf0322d2020-02-23 16:41:33 -0700528struct io_provide_buf {
529 struct file *file;
530 __u64 addr;
Pavel Begunkovcbbc13b2021-04-15 13:07:39 +0100531 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700532 __u32 bgid;
533 __u16 nbufs;
534 __u16 bid;
535};
536
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700537struct io_statx {
538 struct file *file;
539 int dfd;
540 unsigned int mask;
541 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700542 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700543 struct statx __user *buffer;
544};
545
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300546struct io_completion {
547 struct file *file;
548 struct list_head list;
Pavel Begunkovec72cb52021-02-28 22:35:15 +0000549 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300550};
551
Jens Axboef499a022019-12-02 16:28:46 -0700552struct io_async_connect {
553 struct sockaddr_storage address;
554};
555
Jens Axboe03b12302019-12-02 18:50:25 -0700556struct io_async_msghdr {
557 struct iovec fast_iov[UIO_FASTIOV];
558 struct iovec *iov;
559 struct sockaddr __user *uaddr;
560 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700561 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700562};
563
Jens Axboef67676d2019-12-02 11:03:47 -0700564struct io_async_rw {
565 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600566 const struct iovec *free_iovec;
567 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600568 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600569 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700570};
571
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300572enum {
573 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
574 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
575 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
576 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
577 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700578 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300579
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300580 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300581 REQ_F_FAIL_LINK_BIT,
582 REQ_F_INFLIGHT_BIT,
583 REQ_F_CUR_POS_BIT,
584 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300586 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300587 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700588 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700589 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600590 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800591 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100592 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700593
594 /* not a real bit, just to check we're not overflowing the space */
595 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300596};
597
598enum {
599 /* ctx owns file */
600 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
601 /* drain existing IO first */
602 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
603 /* linked sqes */
604 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
605 /* doesn't sever on completion < 0 */
606 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
607 /* IOSQE_ASYNC */
608 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700609 /* IOSQE_BUFFER_SELECT */
610 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300612 /* head of a link */
613 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614 /* fail rest of links */
615 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
616 /* on inflight list */
617 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
618 /* read/write uses file position */
619 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
620 /* must not punt to workers */
621 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100622 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300623 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624 /* regular file */
625 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300626 /* needs cleanup */
627 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700628 /* already went through poll handler */
629 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700630 /* buffer already selected */
631 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600632 /* doesn't need file table for this request */
633 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800634 /* io_wq_work is initialized */
635 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100636 /* linked timeout is active, i.e. prepared by link's head */
637 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700638};
639
640struct async_poll {
641 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600642 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300643};
644
Jens Axboe09bb8392019-03-13 12:39:28 -0600645/*
646 * NOTE! Each of the iocb union members has the file pointer
647 * as the first entry in their struct definition. So you can
648 * access the file pointer through any of the sub-structs,
649 * or directly as just 'ki_filp' in this struct.
650 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700651struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700652 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600653 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700654 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700655 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700656 struct io_accept accept;
657 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700658 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700659 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100660 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700661 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700662 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700663 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700664 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700665 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700666 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700667 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700668 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300669 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700670 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700671 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300672 /* use only after cleaning per-op data, see io_clean_op() */
673 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700674 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700675
Jens Axboee8c2bc12020-08-15 18:44:09 -0700676 /* opcode allocated if it needs to store data for async defer */
677 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700678 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800679 /* polled IO has completed */
680 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700681
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700682 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300683 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700684
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct io_ring_ctx *ctx;
686 unsigned int flags;
687 refcount_t refs;
688 struct task_struct *task;
689 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700690
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300691 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700692
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300693 /*
694 * 1. used with ctx->iopoll_list with reads/writes
695 * 2. to track reqs with ->files (see io_op_def::file_table)
696 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300697 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600698
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300699 struct percpu_ref *fixed_file_refs;
700 struct callback_head task_work;
701 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
702 struct hlist_node hash_node;
703 struct async_poll *apoll;
704 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700705};
706
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300707struct io_defer_entry {
708 struct list_head list;
709 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300710 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300711};
712
Jens Axboedef596e2019-01-09 08:59:42 -0700713#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714
Jens Axboe013538b2020-06-22 09:29:15 -0600715struct io_comp_state {
716 unsigned int nr;
717 struct list_head list;
718 struct io_ring_ctx *ctx;
719};
720
Jens Axboe9a56a232019-01-09 09:06:50 -0700721struct io_submit_state {
722 struct blk_plug plug;
723
724 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700725 * io_kiocb alloc cache
726 */
727 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300728 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700729
730 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600731 * Batch completion logic
732 */
733 struct io_comp_state comp;
734
735 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700736 * File reference cache
737 */
738 struct file *file;
739 unsigned int fd;
740 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700741 unsigned int ios_left;
742};
743
Jens Axboed3656342019-12-18 09:50:26 -0700744struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700745 /* needs req->file assigned */
746 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600747 /* don't fail if file grab fails */
748 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700749 /* hash wq insertion if file is a regular file */
750 unsigned hash_reg_file : 1;
751 /* unbound wq insertion if file is a non-regular file */
752 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700753 /* opcode is not supported by this kernel */
754 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700755 /* set if opcode supports polled "wait" */
756 unsigned pollin : 1;
757 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700758 /* op supports buffer selection */
759 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700760 /* must always have async data allocated */
761 unsigned needs_async_data : 1;
762 /* size of async data needed, if any */
763 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600764 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700765};
766
Jens Axboe09186822020-10-13 15:01:40 -0600767static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_NOP] = {},
769 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700772 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700773 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700774 .needs_async_data = 1,
775 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600776 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_file = 1,
780 .hash_reg_file = 1,
781 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700782 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700783 .needs_async_data = 1,
784 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600785 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
786 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700789 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600790 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700793 .needs_file = 1,
794 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700795 .pollin = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700796 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600797 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700798 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300799 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .needs_file = 1,
801 .hash_reg_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700803 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700804 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600805 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
806 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .needs_file = 1,
810 .unbound_nonreg_file = 1,
811 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_POLL_REMOVE] = {},
813 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700814 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600815 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700818 .needs_file = 1,
819 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700820 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700821 .needs_async_data = 1,
822 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600823 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
824 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700827 .needs_file = 1,
828 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700829 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700830 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700831 .needs_async_data = 1,
832 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600833 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
834 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700837 .needs_async_data = 1,
838 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600839 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700840 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300841 [IORING_OP_TIMEOUT_REMOVE] = {},
842 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_file = 1,
844 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700845 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600846 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_ASYNC_CANCEL] = {},
849 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700850 .needs_async_data = 1,
851 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600852 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_file = 1,
856 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700857 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700858 .needs_async_data = 1,
859 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600860 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600864 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600867 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
868 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600871 .needs_file = 1,
872 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600873 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700874 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300875 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600876 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600879 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
880 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700883 .needs_file = 1,
884 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700885 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700886 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700887 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600888 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700891 .needs_file = 1,
Jens Axboe24fbd77d2021-08-30 19:37:41 -0600892 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700895 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600896 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
897 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700898 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300899 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700900 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600901 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600904 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700907 .needs_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700909 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600910 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700911 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300912 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700913 .needs_file = 1,
914 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700915 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700916 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600917 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700918 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300919 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600920 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
921 IO_WQ_WORK_BLKCG,
Jens Axboecebdb982020-01-08 17:59:24 -0700922 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700923 [IORING_OP_EPOLL_CTL] = {
924 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600925 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700926 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300927 [IORING_OP_SPLICE] = {
928 .needs_file = 1,
929 .hash_reg_file = 1,
930 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600931 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700932 },
933 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700934 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300935 [IORING_OP_TEE] = {
936 .needs_file = 1,
937 .hash_reg_file = 1,
938 .unbound_nonreg_file = 1,
939 },
Jens Axboed3656342019-12-18 09:50:26 -0700940};
941
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700942enum io_mem_account {
943 ACCT_LOCKED,
944 ACCT_PINNED,
945};
946
Pavel Begunkovce00a7d2020-12-30 21:34:15 +0000947static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node);
948static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
949 struct io_ring_ctx *ctx);
950
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300951static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
952 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700953static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800954static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +0100955static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -0600956static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700957static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600958static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700959static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700960static int __io_sqe_files_update(struct io_ring_ctx *ctx,
961 struct io_uring_files_update *ip,
962 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300963static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100964static struct file *io_file_get(struct io_submit_state *state,
965 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300966static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600967static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600968
Jens Axboeb63534c2020-06-04 11:28:00 -0600969static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
970 struct iovec **iovec, struct iov_iter *iter,
971 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600972static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
973 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600974 struct iov_iter *iter, bool force);
Pavel Begunkovd92d0082021-01-26 11:17:10 +0000975static void io_req_drop_files(struct io_kiocb *req);
Pavel Begunkovbc79ff02021-01-26 23:35:10 +0000976static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700977
978static struct kmem_cache *req_cachep;
979
Jens Axboe09186822020-10-13 15:01:40 -0600980static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700981
982struct sock *io_uring_get_socket(struct file *file)
983{
984#if defined(CONFIG_UNIX)
985 if (file->f_op == &io_uring_fops) {
986 struct io_ring_ctx *ctx = file->private_data;
987
988 return ctx->ring_sock->sk;
989 }
990#endif
991 return NULL;
992}
993EXPORT_SYMBOL(io_uring_get_socket);
994
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300995static inline void io_clean_op(struct io_kiocb *req)
996{
Pavel Begunkovd92d0082021-01-26 11:17:10 +0000997 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300998 __io_clean_op(req);
999}
1000
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001001static inline bool __io_match_files(struct io_kiocb *req,
1002 struct files_struct *files)
1003{
Jens Axboed16692a2021-02-09 04:47:41 +00001004 if (req->file && req->file->f_op == &io_uring_fops)
1005 return true;
1006
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001007 return ((req->flags & REQ_F_WORK_INITIALIZED) &&
1008 (req->work.flags & IO_WQ_WORK_FILES)) &&
1009 req->work.identity->files == files;
1010}
1011
1012static bool io_match_task(struct io_kiocb *head,
1013 struct task_struct *task,
1014 struct files_struct *files)
1015{
1016 struct io_kiocb *link;
1017
Jens Axboef0ff1a92021-02-09 04:47:42 +00001018 if (task && head->task != task) {
1019 /* in terms of cancelation, always match if req task is dead */
1020 if (head->task->flags & PF_EXITING)
1021 return true;
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001022 return false;
Jens Axboef0ff1a92021-02-09 04:47:42 +00001023 }
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001024 if (!files)
1025 return true;
1026 if (__io_match_files(head, files))
1027 return true;
1028 if (head->flags & REQ_F_LINK_HEAD) {
1029 list_for_each_entry(link, &head->link_list, link_list) {
1030 if (__io_match_files(link, files))
1031 return true;
1032 }
1033 }
1034 return false;
1035}
1036
1037
Jens Axboe4349f302020-07-09 15:07:01 -06001038static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001039{
1040 struct mm_struct *mm = current->mm;
1041
1042 if (mm) {
1043 kthread_unuse_mm(mm);
1044 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001045 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001046 }
1047}
1048
1049static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1050{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001051 struct mm_struct *mm;
1052
Pavel Begunkova3647cd2021-01-11 04:00:31 +00001053 if (current->flags & PF_EXITING)
1054 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001055 if (current->mm)
1056 return 0;
1057
1058 /* Should never happen */
1059 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1060 return -EFAULT;
1061
1062 task_lock(ctx->sqo_task);
1063 mm = ctx->sqo_task->mm;
1064 if (unlikely(!mm || !mmget_not_zero(mm)))
1065 mm = NULL;
1066 task_unlock(ctx->sqo_task);
1067
1068 if (mm) {
1069 kthread_use_mm(mm);
1070 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001071 }
1072
Jens Axboe4b70cf92020-11-02 10:39:05 -07001073 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001074}
1075
1076static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1077 struct io_kiocb *req)
1078{
Jens Axboe0f203762020-10-14 09:23:55 -06001079 if (!(io_op_defs[req->opcode].work_flags & IO_WQ_WORK_MM))
Jens Axboec40f6372020-06-25 15:39:59 -06001080 return 0;
1081 return __io_sq_thread_acquire_mm(ctx);
1082}
1083
Dennis Zhou91d8f512020-09-16 13:41:05 -07001084static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1085 struct cgroup_subsys_state **cur_css)
1086
1087{
1088#ifdef CONFIG_BLK_CGROUP
1089 /* puts the old one when swapping */
1090 if (*cur_css != ctx->sqo_blkcg_css) {
1091 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1092 *cur_css = ctx->sqo_blkcg_css;
1093 }
1094#endif
1095}
1096
1097static void io_sq_thread_unassociate_blkcg(void)
1098{
1099#ifdef CONFIG_BLK_CGROUP
1100 kthread_associate_blkcg(NULL);
1101#endif
1102}
1103
Jens Axboec40f6372020-06-25 15:39:59 -06001104static inline void req_set_fail_links(struct io_kiocb *req)
1105{
1106 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1107 req->flags |= REQ_F_FAIL_LINK;
1108}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001109
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001110/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001111 * None of these are dereferenced, they are simply used to check if any of
1112 * them have changed. If we're under current and check they are still the
1113 * same, we're fine to grab references to them for actual out-of-line use.
1114 */
1115static void io_init_identity(struct io_identity *id)
1116{
1117 id->files = current->files;
1118 id->mm = current->mm;
1119#ifdef CONFIG_BLK_CGROUP
1120 rcu_read_lock();
1121 id->blkcg_css = blkcg_css();
1122 rcu_read_unlock();
1123#endif
1124 id->creds = current_cred();
1125 id->nsproxy = current->nsproxy;
1126 id->fs = current->fs;
1127 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001128#ifdef CONFIG_AUDIT
1129 id->loginuid = current->loginuid;
1130 id->sessionid = current->sessionid;
1131#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001132 refcount_set(&id->count, 1);
1133}
1134
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001135static inline void __io_req_init_async(struct io_kiocb *req)
1136{
1137 memset(&req->work, 0, sizeof(req->work));
1138 req->flags |= REQ_F_WORK_INITIALIZED;
1139}
1140
Jens Axboe1e6fa522020-10-15 08:46:24 -06001141/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001142 * Note: must call io_req_init_async() for the first time you
1143 * touch any members of io_wq_work.
1144 */
1145static inline void io_req_init_async(struct io_kiocb *req)
1146{
Jens Axboe500a3732020-10-15 17:38:03 -06001147 struct io_uring_task *tctx = current->io_uring;
1148
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001149 if (req->flags & REQ_F_WORK_INITIALIZED)
1150 return;
1151
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001152 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001153
1154 /* Grab a ref if this isn't our static identity */
1155 req->work.identity = tctx->identity;
1156 if (tctx->identity != &tctx->__identity)
1157 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001158}
1159
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001160static inline bool io_async_submit(struct io_ring_ctx *ctx)
1161{
1162 return ctx->flags & IORING_SETUP_SQPOLL;
1163}
1164
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1166{
1167 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1168
Jens Axboe0f158b42020-05-14 17:18:39 -06001169 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170}
1171
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001172static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1173{
1174 return !req->timeout.off;
1175}
1176
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1178{
1179 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001180 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001181
1182 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1183 if (!ctx)
1184 return NULL;
1185
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001186 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1187 if (!ctx->fallback_req)
1188 goto err;
1189
Jens Axboe78076bb2019-12-04 19:56:40 -07001190 /*
1191 * Use 5 bits less than the max cq entries, that should give us around
1192 * 32 entries per hash list if totally full and uniformly spread.
1193 */
1194 hash_bits = ilog2(p->cq_entries);
1195 hash_bits -= 5;
1196 if (hash_bits <= 0)
1197 hash_bits = 1;
1198 ctx->cancel_hash_bits = hash_bits;
1199 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1200 GFP_KERNEL);
1201 if (!ctx->cancel_hash)
1202 goto err;
1203 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1204
Roman Gushchin21482892019-05-07 10:01:48 -07001205 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001206 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1207 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001208
1209 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001210 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001211 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001213 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001214 init_completion(&ctx->ref_comp);
1215 init_completion(&ctx->sq_thread_comp);
Jens Axboe90731882021-07-13 17:18:35 +08001216 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08001217 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001218 mutex_init(&ctx->uring_lock);
1219 init_waitqueue_head(&ctx->wait);
1220 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001221 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001222 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001223 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001224 spin_lock_init(&ctx->inflight_lock);
1225 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001226 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1227 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001229err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001230 if (ctx->fallback_req)
1231 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001232 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001233 kfree(ctx);
1234 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001235}
1236
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001237static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001238{
Jens Axboe2bc99302020-07-09 09:43:27 -06001239 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1240 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001241
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001242 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001243 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001244 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001245
Bob Liu9d858b22019-11-13 18:06:25 +08001246 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001247}
1248
Jens Axboede0617e2019-04-06 21:51:27 -06001249static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250{
Hristo Venev75b28af2019-08-26 17:23:46 +00001251 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252
Pavel Begunkov07910152020-01-17 03:52:46 +03001253 /* order cqe stores with ring update */
1254 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255}
1256
Jens Axboe5c3462c2020-10-15 09:02:33 -06001257static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001258{
Jens Axboe500a3732020-10-15 17:38:03 -06001259 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001260 return;
1261 if (refcount_dec_and_test(&req->work.identity->count))
1262 kfree(req->work.identity);
1263}
1264
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001265static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001266{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001267 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001268 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001269
1270 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001271
Jens Axboedfead8a2020-10-14 10:12:37 -06001272 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001273 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001274 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001275 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001276#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001277 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001278 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001279 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001280 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001281#endif
1282 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001283 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001284 req->work.flags &= ~IO_WQ_WORK_CREDS;
1285 }
1286 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001287 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001288
Jens Axboe98447d62020-10-14 10:48:51 -06001289 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001290 if (--fs->users)
1291 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001292 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001293 if (fs)
1294 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001295 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001296 }
Pavel Begunkovd92d0082021-01-26 11:17:10 +00001297 if (req->flags & REQ_F_INFLIGHT)
1298 io_req_drop_files(req);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001299
Jens Axboe5c3462c2020-10-15 09:02:33 -06001300 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001301}
1302
1303/*
1304 * Create a private copy of io_identity, since some fields don't match
1305 * the current context.
1306 */
1307static bool io_identity_cow(struct io_kiocb *req)
1308{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001309 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001310 const struct cred *creds = NULL;
1311 struct io_identity *id;
1312
1313 if (req->work.flags & IO_WQ_WORK_CREDS)
1314 creds = req->work.identity->creds;
1315
1316 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1317 if (unlikely(!id)) {
1318 req->work.flags |= IO_WQ_WORK_CANCEL;
1319 return false;
1320 }
1321
1322 /*
1323 * We can safely just re-init the creds we copied Either the field
1324 * matches the current one, or we haven't grabbed it yet. The only
1325 * exception is ->creds, through registered personalities, so handle
1326 * that one separately.
1327 */
1328 io_init_identity(id);
1329 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001330 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001331
1332 /* add one for this request */
1333 refcount_inc(&id->count);
1334
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001335 /* drop tctx and req identity references, if needed */
1336 if (tctx->identity != &tctx->__identity &&
1337 refcount_dec_and_test(&tctx->identity->count))
1338 kfree(tctx->identity);
1339 if (req->work.identity != &tctx->__identity &&
1340 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001341 kfree(req->work.identity);
1342
1343 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001344 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001345 return true;
1346}
1347
1348static bool io_grab_identity(struct io_kiocb *req)
1349{
1350 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001351 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001352 struct io_ring_ctx *ctx = req->ctx;
1353
Jens Axboe69228332020-10-20 14:28:41 -06001354 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1355 if (id->fsize != rlimit(RLIMIT_FSIZE))
1356 return false;
1357 req->work.flags |= IO_WQ_WORK_FSIZE;
1358 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001359#ifdef CONFIG_BLK_CGROUP
1360 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1361 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1362 rcu_read_lock();
1363 if (id->blkcg_css != blkcg_css()) {
1364 rcu_read_unlock();
1365 return false;
1366 }
1367 /*
1368 * This should be rare, either the cgroup is dying or the task
1369 * is moving cgroups. Just punt to root for the handful of ios.
1370 */
1371 if (css_tryget_online(id->blkcg_css))
1372 req->work.flags |= IO_WQ_WORK_BLKCG;
1373 rcu_read_unlock();
1374 }
1375#endif
1376 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1377 if (id->creds != current_cred())
1378 return false;
1379 get_cred(id->creds);
1380 req->work.flags |= IO_WQ_WORK_CREDS;
1381 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001382#ifdef CONFIG_AUDIT
1383 if (!uid_eq(current->loginuid, id->loginuid) ||
1384 current->sessionid != id->sessionid)
1385 return false;
1386#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001387 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1388 (def->work_flags & IO_WQ_WORK_FS)) {
1389 if (current->fs != id->fs)
1390 return false;
1391 spin_lock(&id->fs->lock);
1392 if (!id->fs->in_exec) {
1393 id->fs->users++;
1394 req->work.flags |= IO_WQ_WORK_FS;
1395 } else {
1396 req->work.flags |= IO_WQ_WORK_CANCEL;
1397 }
1398 spin_unlock(&current->fs->lock);
1399 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001400 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1401 (def->work_flags & IO_WQ_WORK_FILES) &&
1402 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1403 if (id->files != current->files ||
1404 id->nsproxy != current->nsproxy)
1405 return false;
1406 atomic_inc(&id->files->count);
1407 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001408
Jens Axboed16692a2021-02-09 04:47:41 +00001409 if (!(req->flags & REQ_F_INFLIGHT)) {
1410 req->flags |= REQ_F_INFLIGHT;
1411
1412 spin_lock_irq(&ctx->inflight_lock);
1413 list_add(&req->inflight_entry, &ctx->inflight_list);
1414 spin_unlock_irq(&ctx->inflight_lock);
1415 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001416 req->work.flags |= IO_WQ_WORK_FILES;
1417 }
Jens Axboe7247bc62020-12-29 10:50:46 -07001418 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1419 (def->work_flags & IO_WQ_WORK_MM)) {
1420 if (id->mm != current->mm)
1421 return false;
1422 mmgrab(id->mm);
1423 req->work.flags |= IO_WQ_WORK_MM;
1424 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001425
1426 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001427}
1428
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001429static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001430{
Jens Axboed3656342019-12-18 09:50:26 -07001431 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001432 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5c3462c2020-10-15 09:02:33 -06001433 struct io_identity *id;
Jens Axboe54a91f32019-09-10 09:15:04 -06001434
Pavel Begunkov16d59802020-07-12 16:16:47 +03001435 io_req_init_async(req);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001436 id = req->work.identity;
Pavel Begunkov16d59802020-07-12 16:16:47 +03001437
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001438 if (req->flags & REQ_F_FORCE_ASYNC)
1439 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1440
Jens Axboed3656342019-12-18 09:50:26 -07001441 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001442 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001443 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe6fbdce32021-04-01 08:38:34 -06001444 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001445 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001446 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001447 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001448
Jens Axboe1e6fa522020-10-15 08:46:24 -06001449 /* if we fail grabbing identity, we must COW, regrab, and retry */
1450 if (io_grab_identity(req))
1451 return;
1452
1453 if (!io_identity_cow(req))
1454 return;
1455
1456 /* can't fail at this point */
1457 if (!io_grab_identity(req))
1458 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001459}
1460
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001461static void io_prep_async_link(struct io_kiocb *req)
1462{
1463 struct io_kiocb *cur;
1464
1465 io_prep_async_work(req);
1466 if (req->flags & REQ_F_LINK_HEAD)
1467 list_for_each_entry(cur, &req->link_list, link_list)
1468 io_prep_async_work(cur);
1469}
1470
Jens Axboe7271ef32020-08-10 09:55:22 -06001471static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001472{
Jackie Liua197f662019-11-08 08:09:12 -07001473 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001474 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001475
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001476 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1477 &req->work, req->flags);
1478 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001479 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001480}
1481
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001482static void io_queue_async_work(struct io_kiocb *req)
1483{
Jens Axboe7271ef32020-08-10 09:55:22 -06001484 struct io_kiocb *link;
1485
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001486 /* init ->work of the whole link before punting */
1487 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001488 link = __io_queue_async_work(req);
1489
1490 if (link)
1491 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001492}
1493
Pavel Begunkov7345d4b2021-03-25 18:32:42 +00001494static void io_kill_timeout(struct io_kiocb *req, int status)
Jens Axboe5262f562019-09-17 12:26:57 -06001495{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001496 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001497 int ret;
1498
Jens Axboee8c2bc12020-08-15 18:44:09 -07001499 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001500 if (ret != -1) {
Pavel Begunkov548ee202021-09-13 09:45:41 -06001501 if (status)
1502 req_set_fail_links(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001503 atomic_set(&req->ctx->cq_timeouts,
1504 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001505 list_del_init(&req->timeout.list);
Pavel Begunkov7345d4b2021-03-25 18:32:42 +00001506 io_cqring_fill_event(req, status);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001507 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001508 }
1509}
1510
Jens Axboe76e1b642020-09-26 15:05:03 -06001511/*
1512 * Returns true if we found and killed one or more timeouts
1513 */
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00001514static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1515 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001516{
1517 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001518 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001519
1520 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001521 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00001522 if (io_match_task(req, tsk, files)) {
Pavel Begunkov7345d4b2021-03-25 18:32:42 +00001523 io_kill_timeout(req, -ECANCELED);
Jens Axboe76e1b642020-09-26 15:05:03 -06001524 canceled++;
1525 }
Jens Axboef3606e32020-09-22 08:18:24 -06001526 }
Jens Axboe5262f562019-09-17 12:26:57 -06001527 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001528 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001529}
1530
Pavel Begunkov04518942020-05-26 20:34:05 +03001531static void __io_queue_deferred(struct io_ring_ctx *ctx)
1532{
1533 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001534 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1535 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001536
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001537 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001538 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001539 list_del_init(&de->list);
Pavel Begunkovbc79ff02021-01-26 23:35:10 +00001540 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001541 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001542 } while (!list_empty(&ctx->defer_list));
1543}
1544
Pavel Begunkov360428f2020-05-30 14:54:17 +03001545static void io_flush_timeouts(struct io_ring_ctx *ctx)
1546{
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05001547 u32 seq;
1548
1549 if (list_empty(&ctx->timeout_list))
1550 return;
1551
1552 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1553
1554 do {
1555 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001556 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001557 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001558
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001559 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001560 break;
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05001561
1562 /*
1563 * Since seq can easily wrap around over time, subtract
1564 * the last seq at which timeouts were flushed before comparing.
1565 * Assuming not more than 2^31-1 events have happened since,
1566 * these subtractions won't have wrapped, so we can check if
1567 * target is in [last_seq, current_seq] by comparing the two.
1568 */
1569 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1570 events_got = seq - ctx->cq_last_tm_flush;
1571 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001572 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001573
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001574 list_del_init(&req->timeout.list);
Pavel Begunkov7345d4b2021-03-25 18:32:42 +00001575 io_kill_timeout(req, 0);
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05001576 } while (!list_empty(&ctx->timeout_list));
1577
1578 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001579}
1580
Jens Axboede0617e2019-04-06 21:51:27 -06001581static void io_commit_cqring(struct io_ring_ctx *ctx)
1582{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001583 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001584 __io_commit_cqring(ctx);
1585
Pavel Begunkov04518942020-05-26 20:34:05 +03001586 if (unlikely(!list_empty(&ctx->defer_list)))
1587 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001588}
1589
Jens Axboe90554202020-09-03 12:12:41 -06001590static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1591{
1592 struct io_rings *r = ctx->rings;
1593
1594 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1595}
1596
Jens Axboe2b188cc2019-01-07 10:46:33 -07001597static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1598{
Hristo Venev75b28af2019-08-26 17:23:46 +00001599 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001600 unsigned tail;
1601
1602 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001603 /*
1604 * writes to the cq entry need to come after reading head; the
1605 * control dependency is enough as we're using WRITE_ONCE to
1606 * fill the cq entry
1607 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001608 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001609 return NULL;
1610
1611 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001612 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001613}
1614
Jens Axboef2842ab2020-01-08 11:04:00 -07001615static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1616{
Jens Axboef0b493e2020-02-01 21:30:11 -07001617 if (!ctx->cq_ev_fd)
1618 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001619 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1620 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001621 if (!ctx->eventfd_async)
1622 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001623 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001624}
1625
Jens Axboeb41e9852020-02-17 09:52:41 -07001626static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001627{
Pavel Begunkov7bccd1c2021-01-26 11:17:09 +00001628 if (wq_has_sleeper(&ctx->cq_wait)) {
1629 wake_up_interruptible(&ctx->cq_wait);
1630 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1631 }
Jens Axboe8c838782019-03-12 15:48:16 -06001632 if (waitqueue_active(&ctx->wait))
1633 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001634 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1635 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001636 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001637 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001638}
1639
Pavel Begunkov46930142020-07-30 18:43:49 +03001640static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1641{
1642 if (list_empty(&ctx->cq_overflow_list)) {
1643 clear_bit(0, &ctx->sq_check_overflow);
1644 clear_bit(0, &ctx->cq_check_overflow);
1645 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1646 }
1647}
1648
Jens Axboec4a2ed72019-11-21 21:01:26 -07001649/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00001650static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1651 struct task_struct *tsk,
1652 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001653{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001654 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001655 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001656 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001657 unsigned long flags;
1658 LIST_HEAD(list);
1659
1660 if (!force) {
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001661 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1662 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001663 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664 }
1665
1666 spin_lock_irqsave(&ctx->completion_lock, flags);
1667
Jens Axboec4a2ed72019-11-21 21:01:26 -07001668 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001669 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001670 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001671 continue;
1672
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001673 cqe = io_get_cqring(ctx);
1674 if (!cqe && !force)
1675 break;
1676
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001677 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001678 if (cqe) {
1679 WRITE_ONCE(cqe->user_data, req->user_data);
1680 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001681 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001682 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001683 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001684 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001685 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001686 }
1687 }
1688
1689 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001690 io_cqring_mark_overflow(ctx);
1691
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001692 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1693 io_cqring_ev_posted(ctx);
1694
1695 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001696 req = list_first_entry(&list, struct io_kiocb, compl.list);
1697 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001698 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001699 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001700
1701 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001702}
1703
Pavel Begunkov85e25e22021-01-12 21:17:26 +00001704static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1705 struct task_struct *tsk,
1706 struct files_struct *files)
1707{
1708 if (test_bit(0, &ctx->cq_check_overflow)) {
1709 /* iopoll syncs against uring_lock, not completion_lock */
1710 if (ctx->flags & IORING_SETUP_IOPOLL)
1711 mutex_lock(&ctx->uring_lock);
1712 __io_cqring_overflow_flush(ctx, force, tsk, files);
1713 if (ctx->flags & IORING_SETUP_IOPOLL)
1714 mutex_unlock(&ctx->uring_lock);
1715 }
1716}
1717
Pavel Begunkovec72cb52021-02-28 22:35:15 +00001718static void __io_cqring_fill_event(struct io_kiocb *req, long res,
1719 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722 struct io_uring_cqe *cqe;
1723
Jens Axboe78e19bb2019-11-06 15:21:34 -07001724 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001725
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726 /*
1727 * If we can't get a cq entry, userspace overflowed the
1728 * submission (by quite a lot). Increment the overflow count in
1729 * the ring.
1730 */
1731 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001732 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001733 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001735 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001736 } else if (ctx->cq_overflow_flushed ||
1737 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001738 /*
1739 * If we're in ring overflow flush mode, or in task cancel mode,
1740 * then we cannot store the request for later flushing, we need
1741 * to drop it on the floor.
1742 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001743 ctx->cached_cq_overflow++;
1744 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001745 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001746 if (list_empty(&ctx->cq_overflow_list)) {
1747 set_bit(0, &ctx->sq_check_overflow);
1748 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001749 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001750 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001751 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001752 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001753 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001754 refcount_inc(&req->refs);
1755 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756 }
1757}
1758
Jens Axboebcda7ba2020-02-23 16:42:51 -07001759static void io_cqring_fill_event(struct io_kiocb *req, long res)
1760{
1761 __io_cqring_fill_event(req, res, 0);
1762}
1763
Jens Axboee1e16092020-06-22 09:17:17 -06001764static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001765{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001766 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767 unsigned long flags;
1768
1769 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001770 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001771 io_commit_cqring(ctx);
1772 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1773
Jens Axboe8c838782019-03-12 15:48:16 -06001774 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775}
1776
Jens Axboe229a7b62020-06-22 10:13:11 -06001777static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001778{
Jens Axboe229a7b62020-06-22 10:13:11 -06001779 struct io_ring_ctx *ctx = cs->ctx;
1780
1781 spin_lock_irq(&ctx->completion_lock);
1782 while (!list_empty(&cs->list)) {
1783 struct io_kiocb *req;
1784
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001785 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1786 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001787 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001788
1789 /*
1790 * io_free_req() doesn't care about completion_lock unless one
1791 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1792 * because of a potential deadlock with req->work.fs->lock
1793 */
1794 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1795 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001796 spin_unlock_irq(&ctx->completion_lock);
1797 io_put_req(req);
1798 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001799 } else {
1800 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001801 }
1802 }
1803 io_commit_cqring(ctx);
1804 spin_unlock_irq(&ctx->completion_lock);
1805
1806 io_cqring_ev_posted(ctx);
1807 cs->nr = 0;
1808}
1809
1810static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1811 struct io_comp_state *cs)
1812{
1813 if (!cs) {
1814 io_cqring_add_event(req, res, cflags);
1815 io_put_req(req);
1816 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001817 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001818 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001819 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001820 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001821 if (++cs->nr >= 32)
1822 io_submit_flush_completions(cs);
1823 }
Jens Axboee1e16092020-06-22 09:17:17 -06001824}
1825
1826static void io_req_complete(struct io_kiocb *req, long res)
1827{
Jens Axboe229a7b62020-06-22 10:13:11 -06001828 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001829}
1830
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001831static inline bool io_is_fallback_req(struct io_kiocb *req)
1832{
1833 return req == (struct io_kiocb *)
1834 ((unsigned long) req->ctx->fallback_req & ~1UL);
1835}
1836
1837static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1838{
1839 struct io_kiocb *req;
1840
1841 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001842 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001843 return req;
1844
1845 return NULL;
1846}
1847
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001848static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1849 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001850{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001851 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001852 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001853 size_t sz;
1854 int ret;
1855
1856 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001857 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1858
1859 /*
1860 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1861 * retry single alloc to be on the safe side.
1862 */
1863 if (unlikely(ret <= 0)) {
1864 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1865 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001866 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001867 ret = 1;
1868 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001869 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870 }
1871
Pavel Begunkov291b2822020-09-30 22:57:01 +03001872 state->free_reqs--;
1873 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001874fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001875 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876}
1877
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001878static inline void io_put_file(struct io_kiocb *req, struct file *file,
1879 bool fixed)
1880{
1881 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001882 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001883 else
1884 fput(file);
1885}
1886
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001887static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001889 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001890
Jens Axboee8c2bc12020-08-15 18:44:09 -07001891 if (req->async_data)
1892 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001893 if (req->file)
1894 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001895
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001896 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001897}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001898
Pavel Begunkov216578e2020-10-13 09:44:00 +01001899static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001900{
Jens Axboe0f212202020-09-13 13:09:39 -06001901 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001902 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001903
Pavel Begunkov216578e2020-10-13 09:44:00 +01001904 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001905
Jens Axboed8a6df12020-10-15 16:24:45 -06001906 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06001907 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06001908 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001909 put_task_struct(req->task);
1910
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001911 if (likely(!io_is_fallback_req(req)))
1912 kmem_cache_free(req_cachep, req);
1913 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001914 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1915 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001916}
1917
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001918static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001919{
Jackie Liua197f662019-11-08 08:09:12 -07001920 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001921 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001922 bool cancelled = false;
1923 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001924
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001925 spin_lock_irqsave(&ctx->completion_lock, flags);
1926 link = list_first_entry_or_null(&req->link_list, struct io_kiocb,
1927 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01001928 /*
1929 * Can happen if a linked timeout fired and link had been like
1930 * req -> link t-out -> link t-out [-> ...]
1931 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001932 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1933 struct io_timeout_data *io = link->async_data;
1934 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001935
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001936 list_del_init(&link->link_list);
1937 ret = hrtimer_try_to_cancel(&io->timer);
1938 if (ret != -1) {
1939 io_cqring_fill_event(link, -ECANCELED);
1940 io_commit_cqring(ctx);
1941 cancelled = true;
1942 }
1943 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001944 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001945 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001946
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001947 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001948 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001949 io_put_req(link);
1950 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001951}
1952
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001953static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001954{
1955 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001956
Jens Axboe9e645e112019-05-10 16:07:28 -06001957 /*
1958 * The list should never be empty when we are called here. But could
1959 * potentially happen if the chain is messed up, check to be on the
1960 * safe side.
1961 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001962 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001963 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001964
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001965 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1966 list_del_init(&req->link_list);
1967 if (!list_empty(&nxt->link_list))
1968 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001969 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001970}
1971
1972/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001973 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001974 */
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001975static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001976{
Jens Axboe2665abf2019-11-05 12:40:47 -07001977 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001978 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001979
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001980 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001981 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001982 struct io_kiocb *link = list_first_entry(&req->link_list,
1983 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001984
Pavel Begunkov44932332019-12-05 16:16:35 +03001985 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001986 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001987
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001988 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001989
1990 /*
1991 * It's ok to free under spinlock as they're not linked anymore,
1992 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1993 * work.fs->lock.
1994 */
1995 if (link->flags & REQ_F_WORK_INITIALIZED)
1996 io_put_req_deferred(link, 2);
1997 else
1998 io_double_put_req(link);
Jens Axboe9e645e112019-05-10 16:07:28 -06001999 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002000
2001 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002002 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002003
Jens Axboe2665abf2019-11-05 12:40:47 -07002004 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002005}
2006
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002007static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002008{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03002009 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002010 if (req->flags & REQ_F_LINK_TIMEOUT)
2011 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002012
Jens Axboe9e645e112019-05-10 16:07:28 -06002013 /*
2014 * If LINK is set, we have dependent requests in this chain. If we
2015 * didn't fail this request, queue the first one up, moving any other
2016 * dependencies to the next request. In case of failure, fail the rest
2017 * of the chain.
2018 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002019 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
2020 return io_req_link_next(req);
2021 io_fail_links(req);
2022 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002023}
Jens Axboe2665abf2019-11-05 12:40:47 -07002024
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002025static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
2026{
2027 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
2028 return NULL;
2029 return __io_req_find_next(req);
2030}
2031
Jens Axboe87c43112020-09-30 21:00:14 -06002032static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002033{
2034 struct task_struct *tsk = req->task;
2035 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002036 enum task_work_notify_mode notify;
2037 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002038
Jens Axboe6200b0a2020-09-13 14:38:30 -06002039 if (tsk->flags & PF_EXITING)
2040 return -ESRCH;
2041
Jens Axboec2c4c832020-07-01 15:37:11 -06002042 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002043 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2044 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2045 * processing task_work. There's no reliable way to tell if TWA_RESUME
2046 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002047 */
Jens Axboe91989c72020-10-16 09:02:26 -06002048 notify = TWA_NONE;
Jens Axboefd7d6de2020-08-23 11:00:37 -06002049 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002050 notify = TWA_SIGNAL;
2051
Jens Axboe87c43112020-09-30 21:00:14 -06002052 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002053 if (!ret)
2054 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002055
Jens Axboec2c4c832020-07-01 15:37:11 -06002056 return ret;
2057}
2058
Jens Axboec40f6372020-06-25 15:39:59 -06002059static void __io_req_task_cancel(struct io_kiocb *req, int error)
2060{
2061 struct io_ring_ctx *ctx = req->ctx;
2062
2063 spin_lock_irq(&ctx->completion_lock);
2064 io_cqring_fill_event(req, error);
2065 io_commit_cqring(ctx);
2066 spin_unlock_irq(&ctx->completion_lock);
2067
2068 io_cqring_ev_posted(ctx);
2069 req_set_fail_links(req);
2070 io_double_put_req(req);
2071}
2072
2073static void io_req_task_cancel(struct callback_head *cb)
2074{
2075 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002076 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002077
2078 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002079 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002080}
2081
2082static void __io_req_task_submit(struct io_kiocb *req)
2083{
2084 struct io_ring_ctx *ctx = req->ctx;
2085
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002086 mutex_lock(&ctx->uring_lock);
Pavel Begunkova63d9152021-01-26 11:17:03 +00002087 if (!ctx->sqo_dead && !__io_sq_thread_acquire_mm(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002088 __io_queue_sqe(req, NULL);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002089 else
Jens Axboec40f6372020-06-25 15:39:59 -06002090 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002091 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov5592eae2021-02-09 04:47:50 +00002092
2093 if (ctx->flags & IORING_SETUP_SQPOLL)
2094 io_sq_thread_drop_mm();
Jens Axboe9e645e112019-05-10 16:07:28 -06002095}
2096
Jens Axboec40f6372020-06-25 15:39:59 -06002097static void io_req_task_submit(struct callback_head *cb)
2098{
2099 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002100 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002101
2102 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002103 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002104}
2105
2106static void io_req_task_queue(struct io_kiocb *req)
2107{
Jens Axboec40f6372020-06-25 15:39:59 -06002108 int ret;
2109
2110 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002111 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002112
Jens Axboe87c43112020-09-30 21:00:14 -06002113 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06002114 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002115 struct task_struct *tsk;
2116
Jens Axboec40f6372020-06-25 15:39:59 -06002117 init_task_work(&req->task_work, io_req_task_cancel);
2118 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002119 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002120 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002121 }
Jens Axboec40f6372020-06-25 15:39:59 -06002122}
2123
Pavel Begunkovc3524382020-06-28 12:52:32 +03002124static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002125{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002126 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002127
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002128 if (nxt)
2129 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002130}
2131
Jens Axboe9e645e112019-05-10 16:07:28 -06002132static void io_free_req(struct io_kiocb *req)
2133{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002134 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002135 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002136}
2137
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002138struct req_batch {
2139 void *reqs[IO_IOPOLL_BATCH];
2140 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002141
2142 struct task_struct *task;
2143 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002144};
2145
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002146static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002147{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002148 rb->to_free = 0;
2149 rb->task_refs = 0;
2150 rb->task = NULL;
2151}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002152
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002153static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2154 struct req_batch *rb)
2155{
2156 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2157 percpu_ref_put_many(&ctx->refs, rb->to_free);
2158 rb->to_free = 0;
2159}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002160
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002161static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2162 struct req_batch *rb)
2163{
2164 if (rb->to_free)
2165 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002166 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002167 struct io_uring_task *tctx = rb->task->io_uring;
2168
2169 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboeca758722021-01-16 11:52:11 -07002170 if (atomic_read(&tctx->in_idle))
2171 wake_up(&tctx->wait);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002172 put_task_struct_many(rb->task, rb->task_refs);
2173 rb->task = NULL;
2174 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002175}
2176
2177static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2178{
2179 if (unlikely(io_is_fallback_req(req))) {
2180 io_free_req(req);
2181 return;
2182 }
2183 if (req->flags & REQ_F_LINK_HEAD)
2184 io_queue_next(req);
2185
Jens Axboee3bc8e92020-09-24 08:45:57 -06002186 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002187 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002188 struct io_uring_task *tctx = rb->task->io_uring;
2189
2190 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboeca758722021-01-16 11:52:11 -07002191 if (atomic_read(&tctx->in_idle))
2192 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002193 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002194 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002195 rb->task = req->task;
2196 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002197 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002198 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002199
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002200 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002201 rb->reqs[rb->to_free++] = req;
2202 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2203 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002204}
2205
Jens Axboeba816ad2019-09-28 11:36:45 -06002206/*
2207 * Drop reference to request, return next in chain (if there is one) if this
2208 * was the last reference to this request.
2209 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002210static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002211{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002212 struct io_kiocb *nxt = NULL;
2213
Jens Axboe2a44f462020-02-25 13:25:41 -07002214 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002215 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002216 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002217 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002218 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002219}
2220
Jens Axboe2b188cc2019-01-07 10:46:33 -07002221static void io_put_req(struct io_kiocb *req)
2222{
Jens Axboedef596e2019-01-09 08:59:42 -07002223 if (refcount_dec_and_test(&req->refs))
2224 io_free_req(req);
2225}
2226
Pavel Begunkov216578e2020-10-13 09:44:00 +01002227static void io_put_req_deferred_cb(struct callback_head *cb)
2228{
2229 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2230
2231 io_free_req(req);
2232}
2233
2234static void io_free_req_deferred(struct io_kiocb *req)
2235{
2236 int ret;
2237
2238 init_task_work(&req->task_work, io_put_req_deferred_cb);
2239 ret = io_req_task_work_add(req, true);
2240 if (unlikely(ret)) {
2241 struct task_struct *tsk;
2242
2243 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002244 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002245 wake_up_process(tsk);
2246 }
2247}
2248
2249static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2250{
2251 if (refcount_sub_and_test(refs, &req->refs))
2252 io_free_req_deferred(req);
2253}
2254
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002255static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002256{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002257 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002258
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002259 /*
2260 * A ref is owned by io-wq in which context we're. So, if that's the
2261 * last one, it's safe to steal next work. False negatives are Ok,
2262 * it just will be re-punted async in io_put_work()
2263 */
2264 if (refcount_read(&req->refs) != 1)
2265 return NULL;
2266
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002267 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002268 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002269}
2270
Jens Axboe978db572019-11-14 22:39:04 -07002271static void io_double_put_req(struct io_kiocb *req)
2272{
2273 /* drop both submit and complete references */
2274 if (refcount_sub_and_test(2, &req->refs))
2275 io_free_req(req);
2276}
2277
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002278static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002279{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002280 struct io_rings *rings = ctx->rings;
2281
Jens Axboea3a0e432019-08-20 11:03:11 -06002282 /* See comment at the top of this file */
2283 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002284 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002285}
2286
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002287static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2288{
2289 struct io_rings *rings = ctx->rings;
2290
2291 /* make sure SQ entry isn't read before tail */
2292 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2293}
2294
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002295static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002296{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002297 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002298
Jens Axboebcda7ba2020-02-23 16:42:51 -07002299 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2300 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002301 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002302 kfree(kbuf);
2303 return cflags;
2304}
2305
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002306static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2307{
2308 struct io_buffer *kbuf;
2309
2310 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2311 return io_put_kbuf(req, kbuf);
2312}
2313
Jens Axboe4c6e2772020-07-01 11:29:10 -06002314static inline bool io_run_task_work(void)
2315{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002316 /*
2317 * Not safe to run on exiting task, and the task_work handling will
2318 * not add work to such a task.
2319 */
2320 if (unlikely(current->flags & PF_EXITING))
2321 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002322 if (current->task_works) {
2323 __set_current_state(TASK_RUNNING);
2324 task_work_run();
2325 return true;
2326 }
2327
2328 return false;
2329}
2330
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002331static void io_iopoll_queue(struct list_head *again)
2332{
2333 struct io_kiocb *req;
2334
2335 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002336 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2337 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002338 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002339 } while (!list_empty(again));
2340}
2341
Jens Axboedef596e2019-01-09 08:59:42 -07002342/*
2343 * Find and free completed poll iocbs
2344 */
2345static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2346 struct list_head *done)
2347{
Jens Axboe8237e042019-12-28 10:48:22 -07002348 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002349 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002350 LIST_HEAD(again);
2351
2352 /* order with ->result store in io_complete_rw_iopoll() */
2353 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002354
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002355 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002356 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002357 int cflags = 0;
2358
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002359 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002360 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002361 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002362 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002363 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002364 continue;
2365 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002366 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002367
Jens Axboebcda7ba2020-02-23 16:42:51 -07002368 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002369 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002370
2371 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002372 (*nr_events)++;
2373
Pavel Begunkovc3524382020-06-28 12:52:32 +03002374 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002375 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002376 }
Jens Axboedef596e2019-01-09 08:59:42 -07002377
Jens Axboe09bb8392019-03-13 12:39:28 -06002378 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002379 if (ctx->flags & IORING_SETUP_SQPOLL)
2380 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002381 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002382
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002383 if (!list_empty(&again))
2384 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002385}
2386
Jens Axboedef596e2019-01-09 08:59:42 -07002387static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2388 long min)
2389{
2390 struct io_kiocb *req, *tmp;
2391 LIST_HEAD(done);
2392 bool spin;
2393 int ret;
2394
2395 /*
2396 * Only spin for completions if we don't have multiple devices hanging
2397 * off our complete list, and we're under the requested amount.
2398 */
2399 spin = !ctx->poll_multi_file && *nr_events < min;
2400
2401 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002402 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002403 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002404
2405 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002406 * Move completed and retryable entries to our local lists.
2407 * If we find a request that requires polling, break out
2408 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002409 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002410 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002411 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002412 continue;
2413 }
2414 if (!list_empty(&done))
2415 break;
2416
2417 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2418 if (ret < 0)
2419 break;
2420
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002421 /* iopoll may have completed current req */
2422 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002423 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002424
Jens Axboedef596e2019-01-09 08:59:42 -07002425 if (ret && spin)
2426 spin = false;
2427 ret = 0;
2428 }
2429
2430 if (!list_empty(&done))
2431 io_iopoll_complete(ctx, nr_events, &done);
2432
2433 return ret;
2434}
2435
2436/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002437 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002438 * non-spinning poll check - we'll still enter the driver poll loop, but only
2439 * as a non-spinning completion check.
2440 */
2441static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2442 long min)
2443{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002444 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002445 int ret;
2446
2447 ret = io_do_iopoll(ctx, nr_events, min);
2448 if (ret < 0)
2449 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002450 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002451 return 0;
2452 }
2453
2454 return 1;
2455}
2456
2457/*
2458 * We can't just wait for polled events to come to us, we have to actively
2459 * find and complete them.
2460 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002461static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002462{
2463 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2464 return;
2465
2466 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002467 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002468 unsigned int nr_events = 0;
2469
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002470 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002471
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002472 /* let it sleep and repeat later if can't complete a request */
2473 if (nr_events == 0)
2474 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002475 /*
2476 * Ensure we allow local-to-the-cpu processing to take place,
2477 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002478 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002479 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002480 if (need_resched()) {
2481 mutex_unlock(&ctx->uring_lock);
2482 cond_resched();
2483 mutex_lock(&ctx->uring_lock);
2484 }
Jens Axboedef596e2019-01-09 08:59:42 -07002485 }
2486 mutex_unlock(&ctx->uring_lock);
2487}
2488
Pavel Begunkov7668b922020-07-07 16:36:21 +03002489static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002490{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002491 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002492 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002493
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002494 /*
2495 * We disallow the app entering submit/complete with polling, but we
2496 * still need to lock the ring to prevent racing with polled issue
2497 * that got punted to a workqueue.
2498 */
2499 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002500 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002501 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002502 * Don't enter poll loop if we already have events pending.
2503 * If we do, we can potentially be spinning for commands that
2504 * already triggered a CQE (eg in error).
2505 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002506 if (test_bit(0, &ctx->cq_check_overflow))
2507 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2508 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002509 break;
2510
2511 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002512 * If a submit got punted to a workqueue, we can have the
2513 * application entering polling for a command before it gets
2514 * issued. That app will hold the uring_lock for the duration
2515 * of the poll right here, so we need to take a breather every
2516 * now and then to ensure that the issue has a chance to add
2517 * the poll to the issued list. Otherwise we can spin here
2518 * forever, while the workqueue is stuck trying to acquire the
2519 * very same mutex.
2520 */
2521 if (!(++iters & 7)) {
2522 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002523 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002524 mutex_lock(&ctx->uring_lock);
2525 }
2526
Pavel Begunkov7668b922020-07-07 16:36:21 +03002527 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002528 if (ret <= 0)
2529 break;
2530 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002531 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002532
Jens Axboe500f9fb2019-08-19 12:15:59 -06002533 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002534 return ret;
2535}
2536
Jens Axboe491381ce2019-10-17 09:20:46 -06002537static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538{
Jens Axboe491381ce2019-10-17 09:20:46 -06002539 /*
2540 * Tell lockdep we inherited freeze protection from submission
2541 * thread.
2542 */
2543 if (req->flags & REQ_F_ISREG) {
2544 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545
Jens Axboe491381ce2019-10-17 09:20:46 -06002546 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002548 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549}
2550
Jens Axboea1d7c392020-06-22 11:09:46 -06002551static void io_complete_rw_common(struct kiocb *kiocb, long res,
2552 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002553{
Jens Axboe9adbd452019-12-20 08:45:55 -07002554 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002555 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556
Jens Axboe491381ce2019-10-17 09:20:46 -06002557 if (kiocb->ki_flags & IOCB_WRITE)
2558 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002559
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002560 if (res != req->result)
2561 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002562 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002563 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002564 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002565}
2566
Jens Axboeb63534c2020-06-04 11:28:00 -06002567#ifdef CONFIG_BLOCK
2568static bool io_resubmit_prep(struct io_kiocb *req, int error)
2569{
2570 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2571 ssize_t ret = -ECANCELED;
2572 struct iov_iter iter;
2573 int rw;
2574
2575 if (error) {
2576 ret = error;
2577 goto end_req;
2578 }
2579
2580 switch (req->opcode) {
2581 case IORING_OP_READV:
2582 case IORING_OP_READ_FIXED:
2583 case IORING_OP_READ:
2584 rw = READ;
2585 break;
2586 case IORING_OP_WRITEV:
2587 case IORING_OP_WRITE_FIXED:
2588 case IORING_OP_WRITE:
2589 rw = WRITE;
2590 break;
2591 default:
2592 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2593 req->opcode);
2594 goto end_req;
2595 }
2596
Jens Axboee8c2bc12020-08-15 18:44:09 -07002597 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002598 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2599 if (ret < 0)
2600 goto end_req;
2601 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2602 if (!ret)
2603 return true;
2604 kfree(iovec);
2605 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002606 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002607 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002608end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002609 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002610 return false;
2611}
Jens Axboeb63534c2020-06-04 11:28:00 -06002612#endif
2613
2614static bool io_rw_reissue(struct io_kiocb *req, long res)
2615{
2616#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002617 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002618 int ret;
2619
Jens Axboe355afae2020-09-02 09:30:31 -06002620 if (!S_ISBLK(mode) && !S_ISREG(mode))
2621 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002622 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2623 return false;
Jens Axboe3c08f772021-02-23 19:17:35 -07002624 /*
2625 * If ref is dying, we might be running poll reap from the exit work.
2626 * Don't attempt to reissue from that path, just let it fail with
2627 * -EAGAIN.
2628 */
2629 if (percpu_ref_is_dying(&req->ctx->refs))
2630 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002631
Jens Axboefdee9462020-08-27 16:46:24 -06002632 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002633
Jens Axboefdee9462020-08-27 16:46:24 -06002634 if (io_resubmit_prep(req, ret)) {
2635 refcount_inc(&req->refs);
2636 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002637 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002638 }
2639
Jens Axboeb63534c2020-06-04 11:28:00 -06002640#endif
2641 return false;
2642}
2643
Jens Axboea1d7c392020-06-22 11:09:46 -06002644static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2645 struct io_comp_state *cs)
2646{
2647 if (!io_rw_reissue(req, res))
2648 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002649}
2650
2651static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2652{
Jens Axboe9adbd452019-12-20 08:45:55 -07002653 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002654
Jens Axboea1d7c392020-06-22 11:09:46 -06002655 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002656}
2657
Jens Axboedef596e2019-01-09 08:59:42 -07002658static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2659{
Jens Axboe9adbd452019-12-20 08:45:55 -07002660 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002661
Jens Axboe491381ce2019-10-17 09:20:46 -06002662 if (kiocb->ki_flags & IOCB_WRITE)
2663 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002664
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002665 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002666 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002667
2668 WRITE_ONCE(req->result, res);
2669 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002670 smp_wmb();
2671 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002672}
2673
2674/*
2675 * After the iocb has been issued, it's safe to be found on the poll list.
2676 * Adding the kiocb to the list AFTER submission ensures that we don't
2677 * find it from a io_iopoll_getevents() thread before the issuer is done
2678 * accessing the kiocb cookie.
2679 */
2680static void io_iopoll_req_issued(struct io_kiocb *req)
2681{
2682 struct io_ring_ctx *ctx = req->ctx;
2683
2684 /*
2685 * Track whether we have multiple files in our lists. This will impact
2686 * how we do polling eventually, not spinning if we're on potentially
2687 * different devices.
2688 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002689 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002690 ctx->poll_multi_file = false;
2691 } else if (!ctx->poll_multi_file) {
2692 struct io_kiocb *list_req;
2693
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002694 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002695 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002696 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002697 ctx->poll_multi_file = true;
2698 }
2699
2700 /*
2701 * For fast devices, IO may have already completed. If it has, add
2702 * it to the front so we find it first.
2703 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002704 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002705 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002706 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002707 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002708
2709 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002710 wq_has_sleeper(&ctx->sq_data->wait))
2711 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002712}
2713
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002714static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002715{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002716 if (state->has_refs)
2717 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002718 state->file = NULL;
2719}
2720
2721static inline void io_state_file_put(struct io_submit_state *state)
2722{
2723 if (state->file)
2724 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002725}
2726
2727/*
2728 * Get as many references to a file as we have IOs left in this submission,
2729 * assuming most submissions are for one file, or at least that each file
2730 * has more than one submission.
2731 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002732static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002733{
2734 if (!state)
2735 return fget(fd);
2736
2737 if (state->file) {
2738 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002739 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002740 return state->file;
2741 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002742 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002743 }
2744 state->file = fget_many(fd, state->ios_left);
2745 if (!state->file)
2746 return NULL;
2747
2748 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002749 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002750 return state->file;
2751}
2752
Jens Axboe4503b762020-06-01 10:00:27 -06002753static bool io_bdev_nowait(struct block_device *bdev)
2754{
2755#ifdef CONFIG_BLOCK
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002756 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002757#else
2758 return true;
2759#endif
2760}
2761
Jens Axboe2b188cc2019-01-07 10:46:33 -07002762/*
2763 * If we tracked the file through the SCM inflight mechanism, we could support
2764 * any file. For now, just ensure that anything potentially problematic is done
2765 * inline.
2766 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002767static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768{
2769 umode_t mode = file_inode(file)->i_mode;
2770
Jens Axboe4503b762020-06-01 10:00:27 -06002771 if (S_ISBLK(mode)) {
2772 if (io_bdev_nowait(file->f_inode->i_bdev))
2773 return true;
2774 return false;
2775 }
Pavel Begunkova75457f2021-06-09 12:07:25 +01002776 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002777 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002778 if (S_ISREG(mode)) {
2779 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2780 file->f_op != &io_uring_fops)
2781 return true;
2782 return false;
2783 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784
Jens Axboec5b85622020-06-09 19:23:05 -06002785 /* any ->read/write should understand O_NONBLOCK */
2786 if (file->f_flags & O_NONBLOCK)
2787 return true;
2788
Jens Axboeaf197f52020-04-28 13:15:06 -06002789 if (!(file->f_mode & FMODE_NOWAIT))
2790 return false;
2791
2792 if (rw == READ)
2793 return file->f_op->read_iter != NULL;
2794
2795 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002796}
2797
Pavel Begunkova88fc402020-09-30 22:57:53 +03002798static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002799{
Jens Axboedef596e2019-01-09 08:59:42 -07002800 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002801 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002802 unsigned ioprio;
2803 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804
Jens Axboe491381ce2019-10-17 09:20:46 -06002805 if (S_ISREG(file_inode(req->file)->i_mode))
2806 req->flags |= REQ_F_ISREG;
2807
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002809 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2810 req->flags |= REQ_F_CUR_POS;
2811 kiocb->ki_pos = req->file->f_pos;
2812 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002813 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002814 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2815 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2816 if (unlikely(ret))
2817 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002818
2819 ioprio = READ_ONCE(sqe->ioprio);
2820 if (ioprio) {
2821 ret = ioprio_check_cap(ioprio);
2822 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002823 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002824
2825 kiocb->ki_ioprio = ioprio;
2826 } else
2827 kiocb->ki_ioprio = get_current_ioprio();
2828
Stefan Bühler8449eed2019-04-27 20:34:19 +02002829 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002830 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002831 req->flags |= REQ_F_NOWAIT;
2832
Jens Axboedef596e2019-01-09 08:59:42 -07002833 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002834 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2835 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002836 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002837
Jens Axboedef596e2019-01-09 08:59:42 -07002838 kiocb->ki_flags |= IOCB_HIPRI;
2839 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002840 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002841 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002842 if (kiocb->ki_flags & IOCB_HIPRI)
2843 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002844 kiocb->ki_complete = io_complete_rw;
2845 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002846
Jens Axboe3529d8c2019-12-19 18:24:38 -07002847 req->rw.addr = READ_ONCE(sqe->addr);
2848 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002849 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002851}
2852
2853static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2854{
2855 switch (ret) {
2856 case -EIOCBQUEUED:
2857 break;
2858 case -ERESTARTSYS:
2859 case -ERESTARTNOINTR:
2860 case -ERESTARTNOHAND:
2861 case -ERESTART_RESTARTBLOCK:
2862 /*
2863 * We can't just restart the syscall, since previously
2864 * submitted sqes may already be in progress. Just fail this
2865 * IO with EINTR.
2866 */
2867 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002868 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002869 default:
2870 kiocb->ki_complete(kiocb, ret, 0);
2871 }
2872}
2873
Jens Axboea1d7c392020-06-22 11:09:46 -06002874static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2875 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002876{
Jens Axboeba042912019-12-25 16:33:42 -07002877 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002878 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002879
Jens Axboe227c0c92020-08-13 11:51:40 -06002880 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002881 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002882 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002883 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002884 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002885 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002886 }
2887
Jens Axboeba042912019-12-25 16:33:42 -07002888 if (req->flags & REQ_F_CUR_POS)
2889 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002890 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002891 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002892 else
2893 io_rw_done(kiocb, ret);
2894}
2895
Jens Axboe9adbd452019-12-20 08:45:55 -07002896static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002897 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002898{
Jens Axboe9adbd452019-12-20 08:45:55 -07002899 struct io_ring_ctx *ctx = req->ctx;
2900 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002901 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002902 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002903 size_t offset;
2904 u64 buf_addr;
2905
Jens Axboeedafcce2019-01-09 09:16:05 -07002906 if (unlikely(buf_index >= ctx->nr_user_bufs))
2907 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002908 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2909 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002910 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002911
2912 /* overflow */
2913 if (buf_addr + len < buf_addr)
2914 return -EFAULT;
2915 /* not inside the mapped region */
2916 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2917 return -EFAULT;
2918
2919 /*
2920 * May not be a start of buffer, set size appropriately
2921 * and advance us to the beginning.
2922 */
2923 offset = buf_addr - imu->ubuf;
2924 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002925
2926 if (offset) {
2927 /*
2928 * Don't use iov_iter_advance() here, as it's really slow for
2929 * using the latter parts of a big fixed buffer - it iterates
2930 * over each segment manually. We can cheat a bit here, because
2931 * we know that:
2932 *
2933 * 1) it's a BVEC iter, we set it up
2934 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2935 * first and last bvec
2936 *
2937 * So just find our index, and adjust the iterator afterwards.
2938 * If the offset is within the first bvec (or the whole first
2939 * bvec, just use iov_iter_advance(). This makes it easier
2940 * since we can just skip the first segment, which may not
2941 * be PAGE_SIZE aligned.
2942 */
2943 const struct bio_vec *bvec = imu->bvec;
2944
2945 if (offset <= bvec->bv_len) {
2946 iov_iter_advance(iter, offset);
2947 } else {
2948 unsigned long seg_skip;
2949
2950 /* skip first vec */
2951 offset -= bvec->bv_len;
2952 seg_skip = 1 + (offset >> PAGE_SHIFT);
2953
2954 iter->bvec = bvec + seg_skip;
2955 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002956 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002957 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002958 }
2959 }
2960
Jens Axboe5e559562019-11-13 16:12:46 -07002961 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002962}
2963
Jens Axboebcda7ba2020-02-23 16:42:51 -07002964static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2965{
2966 if (needs_lock)
2967 mutex_unlock(&ctx->uring_lock);
2968}
2969
2970static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2971{
2972 /*
2973 * "Normal" inline submissions always hold the uring_lock, since we
2974 * grab it from the system call. Same is true for the SQPOLL offload.
2975 * The only exception is when we've detached the request and issue it
2976 * from an async worker thread, grab the lock for that case.
2977 */
2978 if (needs_lock)
2979 mutex_lock(&ctx->uring_lock);
2980}
2981
2982static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2983 int bgid, struct io_buffer *kbuf,
2984 bool needs_lock)
2985{
2986 struct io_buffer *head;
2987
2988 if (req->flags & REQ_F_BUFFER_SELECTED)
2989 return kbuf;
2990
2991 io_ring_submit_lock(req->ctx, needs_lock);
2992
2993 lockdep_assert_held(&req->ctx->uring_lock);
2994
Jens Axboe90731882021-07-13 17:18:35 +08002995 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002996 if (head) {
2997 if (!list_empty(&head->list)) {
2998 kbuf = list_last_entry(&head->list, struct io_buffer,
2999 list);
3000 list_del(&kbuf->list);
3001 } else {
3002 kbuf = head;
Jens Axboe90731882021-07-13 17:18:35 +08003003 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003004 }
3005 if (*len > kbuf->len)
3006 *len = kbuf->len;
3007 } else {
3008 kbuf = ERR_PTR(-ENOBUFS);
3009 }
3010
3011 io_ring_submit_unlock(req->ctx, needs_lock);
3012
3013 return kbuf;
3014}
3015
Jens Axboe4d954c22020-02-27 07:31:19 -07003016static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3017 bool needs_lock)
3018{
3019 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003020 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003021
3022 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003023 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003024 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3025 if (IS_ERR(kbuf))
3026 return kbuf;
3027 req->rw.addr = (u64) (unsigned long) kbuf;
3028 req->flags |= REQ_F_BUFFER_SELECTED;
3029 return u64_to_user_ptr(kbuf->addr);
3030}
3031
3032#ifdef CONFIG_COMPAT
3033static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3034 bool needs_lock)
3035{
3036 struct compat_iovec __user *uiov;
3037 compat_ssize_t clen;
3038 void __user *buf;
3039 ssize_t len;
3040
3041 uiov = u64_to_user_ptr(req->rw.addr);
3042 if (!access_ok(uiov, sizeof(*uiov)))
3043 return -EFAULT;
3044 if (__get_user(clen, &uiov->iov_len))
3045 return -EFAULT;
3046 if (clen < 0)
3047 return -EINVAL;
3048
3049 len = clen;
3050 buf = io_rw_buffer_select(req, &len, needs_lock);
3051 if (IS_ERR(buf))
3052 return PTR_ERR(buf);
3053 iov[0].iov_base = buf;
3054 iov[0].iov_len = (compat_size_t) len;
3055 return 0;
3056}
3057#endif
3058
3059static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3060 bool needs_lock)
3061{
3062 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3063 void __user *buf;
3064 ssize_t len;
3065
3066 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3067 return -EFAULT;
3068
3069 len = iov[0].iov_len;
3070 if (len < 0)
3071 return -EINVAL;
3072 buf = io_rw_buffer_select(req, &len, needs_lock);
3073 if (IS_ERR(buf))
3074 return PTR_ERR(buf);
3075 iov[0].iov_base = buf;
3076 iov[0].iov_len = len;
3077 return 0;
3078}
3079
3080static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3081 bool needs_lock)
3082{
Jens Axboedddb3e22020-06-04 11:27:01 -06003083 if (req->flags & REQ_F_BUFFER_SELECTED) {
3084 struct io_buffer *kbuf;
3085
3086 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3087 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3088 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003089 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003090 }
Pavel Begunkov72a016d2020-12-19 03:15:43 +00003091 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003092 return -EINVAL;
3093
3094#ifdef CONFIG_COMPAT
3095 if (req->ctx->compat)
3096 return io_compat_import(req, iov, needs_lock);
3097#endif
3098
3099 return __io_iov_buffer_select(req, iov, needs_lock);
3100}
3101
Jens Axboe8452fd02020-08-18 13:58:33 -07003102static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
3103 struct iovec **iovec, struct iov_iter *iter,
3104 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003105{
Jens Axboe9adbd452019-12-20 08:45:55 -07003106 void __user *buf = u64_to_user_ptr(req->rw.addr);
3107 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003108 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003109 u8 opcode;
3110
Jens Axboed625c6e2019-12-17 19:53:05 -07003111 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003112 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003113 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003114 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003116
Jens Axboebcda7ba2020-02-23 16:42:51 -07003117 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003118 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003119 return -EINVAL;
3120
Jens Axboe3a6820f2019-12-22 15:19:35 -07003121 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003122 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003123 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003124 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003125 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003126 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003127 }
3128
Jens Axboe3a6820f2019-12-22 15:19:35 -07003129 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3130 *iovec = NULL;
David Laight6930a2a2020-11-07 13:16:25 +00003131 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003132 }
3133
Jens Axboe4d954c22020-02-27 07:31:19 -07003134 if (req->flags & REQ_F_BUFFER_SELECT) {
3135 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003136 if (!ret) {
3137 ret = (*iovec)->iov_len;
3138 iov_iter_init(iter, rw, *iovec, 1, ret);
3139 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003140 *iovec = NULL;
3141 return ret;
3142 }
3143
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003144 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3145 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003146}
3147
Jens Axboe8452fd02020-08-18 13:58:33 -07003148static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3149 struct iovec **iovec, struct iov_iter *iter,
3150 bool needs_lock)
3151{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003152 struct io_async_rw *iorw = req->async_data;
3153
3154 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003155 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3156 *iovec = NULL;
David Laight6930a2a2020-11-07 13:16:25 +00003157 return 0;
Jens Axboe8452fd02020-08-18 13:58:33 -07003158}
3159
Jens Axboe0fef9482020-08-26 10:36:20 -06003160static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3161{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003162 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003163}
3164
Jens Axboe32960612019-09-23 11:05:34 -06003165/*
3166 * For files that don't have ->read_iter() and ->write_iter(), handle them
3167 * by looping over ->read() or ->write() manually.
3168 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003169static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003170{
Jens Axboe4017eb92020-10-22 14:14:12 -06003171 struct kiocb *kiocb = &req->rw.kiocb;
3172 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003173 ssize_t ret = 0;
3174
3175 /*
3176 * Don't support polled IO through this interface, and we can't
3177 * support non-blocking either. For the latter, this just causes
3178 * the kiocb to be handled from an async context.
3179 */
3180 if (kiocb->ki_flags & IOCB_HIPRI)
3181 return -EOPNOTSUPP;
3182 if (kiocb->ki_flags & IOCB_NOWAIT)
3183 return -EAGAIN;
3184
3185 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003186 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003187 ssize_t nr;
3188
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003189 if (!iov_iter_is_bvec(iter)) {
3190 iovec = iov_iter_iovec(iter);
3191 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003192 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3193 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003194 }
3195
Jens Axboe32960612019-09-23 11:05:34 -06003196 if (rw == READ) {
3197 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003198 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003199 } else {
3200 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003201 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003202 }
3203
3204 if (nr < 0) {
3205 if (!ret)
3206 ret = nr;
3207 break;
3208 }
Jens Axboece8f81b2021-09-12 06:45:07 -06003209 if (!iov_iter_is_bvec(iter)) {
3210 iov_iter_advance(iter, nr);
3211 } else {
3212 req->rw.len -= nr;
3213 req->rw.addr += nr;
3214 }
Jens Axboe32960612019-09-23 11:05:34 -06003215 ret += nr;
3216 if (nr != iovec.iov_len)
3217 break;
Jens Axboe32960612019-09-23 11:05:34 -06003218 }
3219
3220 return ret;
3221}
3222
Jens Axboeff6165b2020-08-13 09:47:43 -06003223static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3224 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003225{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003226 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003227
Jens Axboeff6165b2020-08-13 09:47:43 -06003228 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003229 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003230 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003231 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003232 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003233 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003234 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003235 unsigned iov_off = 0;
3236
3237 rw->iter.iov = rw->fast_iov;
3238 if (iter->iov != fast_iov) {
3239 iov_off = iter->iov - fast_iov;
3240 rw->iter.iov += iov_off;
3241 }
3242 if (rw->fast_iov != fast_iov)
3243 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003244 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003245 } else {
3246 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003247 }
3248}
3249
Jens Axboee8c2bc12020-08-15 18:44:09 -07003250static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003251{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003252 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3253 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3254 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003255}
3256
Jens Axboee8c2bc12020-08-15 18:44:09 -07003257static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003258{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003259 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003260 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003261
Jens Axboee8c2bc12020-08-15 18:44:09 -07003262 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003263}
3264
Jens Axboeff6165b2020-08-13 09:47:43 -06003265static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3266 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003267 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003268{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003269 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003270 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003271 if (!req->async_data) {
3272 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003273 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003274
Jens Axboeff6165b2020-08-13 09:47:43 -06003275 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003276 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003277 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003278}
3279
Pavel Begunkov73debe62020-09-30 22:57:54 +03003280static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003281{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003282 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003283 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003284 ssize_t ret;
3285
Pavel Begunkov73debe62020-09-30 22:57:54 +03003286 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003287 if (unlikely(ret < 0))
3288 return ret;
3289
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003290 iorw->bytes_done = 0;
3291 iorw->free_iovec = iov;
3292 if (iov)
3293 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003294 return 0;
3295}
3296
Pavel Begunkov73debe62020-09-30 22:57:54 +03003297static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003298{
3299 ssize_t ret;
3300
Pavel Begunkova88fc402020-09-30 22:57:53 +03003301 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003302 if (ret)
3303 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003304
Jens Axboe3529d8c2019-12-19 18:24:38 -07003305 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3306 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003307
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003308 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003309 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003310 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003311 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003312}
3313
Jens Axboec1dd91d2020-08-03 16:43:59 -06003314/*
3315 * This is our waitqueue callback handler, registered through lock_page_async()
3316 * when we initially tried to do the IO with the iocb armed our waitqueue.
3317 * This gets called when the page is unlocked, and we generally expect that to
3318 * happen when the page IO is completed and the page is now uptodate. This will
3319 * queue a task_work based retry of the operation, attempting to copy the data
3320 * again. If the latter fails because the page was NOT uptodate, then we will
3321 * do a thread based blocking retry of the operation. That's the unexpected
3322 * slow path.
3323 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003324static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3325 int sync, void *arg)
3326{
3327 struct wait_page_queue *wpq;
3328 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003329 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003330 int ret;
3331
3332 wpq = container_of(wait, struct wait_page_queue, wait);
3333
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003334 if (!wake_page_match(wpq, key))
3335 return 0;
3336
Hao Xuc8d317a2020-09-29 20:00:45 +08003337 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003338 list_del_init(&wait->entry);
3339
Pavel Begunkove7375122020-07-12 20:42:04 +03003340 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003341 percpu_ref_get(&req->ctx->refs);
3342
Jens Axboebcf5a062020-05-22 09:24:42 -06003343 /* submit ref gets dropped, acquire a new one */
3344 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003345 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003346 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003347 struct task_struct *tsk;
3348
Jens Axboebcf5a062020-05-22 09:24:42 -06003349 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003350 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003351 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003352 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003353 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003354 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003355 return 1;
3356}
3357
Jens Axboec1dd91d2020-08-03 16:43:59 -06003358/*
3359 * This controls whether a given IO request should be armed for async page
3360 * based retry. If we return false here, the request is handed to the async
3361 * worker threads for retry. If we're doing buffered reads on a regular file,
3362 * we prepare a private wait_page_queue entry and retry the operation. This
3363 * will either succeed because the page is now uptodate and unlocked, or it
3364 * will register a callback when the page is unlocked at IO completion. Through
3365 * that callback, io_uring uses task_work to setup a retry of the operation.
3366 * That retry will attempt the buffered read again. The retry will generally
3367 * succeed, or in rare cases where it fails, we then fall back to using the
3368 * async worker threads for a blocking retry.
3369 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003370static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003371{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003372 struct io_async_rw *rw = req->async_data;
3373 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003374 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003375
3376 /* never retry for NOWAIT, we just complete with -EAGAIN */
3377 if (req->flags & REQ_F_NOWAIT)
3378 return false;
3379
Jens Axboe227c0c92020-08-13 11:51:40 -06003380 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003381 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003382 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003383
Jens Axboebcf5a062020-05-22 09:24:42 -06003384 /*
3385 * just use poll if we can, and don't attempt if the fs doesn't
3386 * support callback based unlocks
3387 */
3388 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3389 return false;
3390
Jens Axboe3b2a4432020-08-16 10:58:43 -07003391 wait->wait.func = io_async_buf_func;
3392 wait->wait.private = req;
3393 wait->wait.flags = 0;
3394 INIT_LIST_HEAD(&wait->wait.entry);
3395 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003396 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003397 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003398 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003399}
3400
3401static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3402{
3403 if (req->file->f_op->read_iter)
3404 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003405 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003406 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003407 else
3408 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003409}
3410
Jens Axboea1d7c392020-06-22 11:09:46 -06003411static int io_read(struct io_kiocb *req, bool force_nonblock,
3412 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003413{
3414 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003415 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003416 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003417 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003418 ssize_t io_size, ret, ret2;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003419 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003420
Jens Axboee8c2bc12020-08-15 18:44:09 -07003421 if (rw)
3422 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003423
3424 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003425 if (ret < 0)
3426 return ret;
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003427 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003428 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003429 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003430
Jens Axboefd6c2e42019-12-18 12:19:41 -07003431 /* Ensure we clear previously set non-block flag */
3432 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003433 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003434 else
3435 kiocb->ki_flags |= IOCB_NOWAIT;
3436
Jens Axboefd6c2e42019-12-18 12:19:41 -07003437
Pavel Begunkov24c74672020-06-21 13:09:51 +03003438 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003439 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3440 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003441 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003442
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003443 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003444 if (unlikely(ret))
3445 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446
Jens Axboe227c0c92020-08-13 11:51:40 -06003447 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003448
Jens Axboe227c0c92020-08-13 11:51:40 -06003449 if (!ret) {
3450 goto done;
3451 } else if (ret == -EIOCBQUEUED) {
3452 ret = 0;
3453 goto out_free;
3454 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003455 /* IOPOLL retry should happen for io-wq threads */
3456 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003457 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003458 /* no retry on NONBLOCK marked file */
3459 if (req->file->f_flags & O_NONBLOCK)
3460 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003461 /* some cases will consume bytes even on error returns */
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003462 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003463 ret = 0;
3464 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003465 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003466 /* make sure -ERESTARTSYS -> -EINTR is done */
3467 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003468 }
3469
3470 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003471 if (!iov_iter_count(iter) || !force_nonblock ||
Pavel Begunkov2df15ef2021-01-21 12:01:08 +00003472 (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG))
Jens Axboe227c0c92020-08-13 11:51:40 -06003473 goto done;
3474
3475 io_size -= ret;
3476copy_iov:
3477 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3478 if (ret2) {
3479 ret = ret2;
3480 goto out_free;
3481 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003482 if (no_async)
3483 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003484 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003485 /* it's copied and will be cleaned with ->io */
3486 iovec = NULL;
3487 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003488 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003489retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003490 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003491 /* if we can retry, do so with the callbacks armed */
3492 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003493 kiocb->ki_flags &= ~IOCB_WAITQ;
3494 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003495 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003496
3497 /*
3498 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3499 * get -EIOCBQUEUED, then we'll get a notification when the desired
3500 * page gets unlocked. We can also get a partial read here, and if we
3501 * do, then just retry at the new offset.
3502 */
3503 ret = io_iter_do_read(req, iter);
3504 if (ret == -EIOCBQUEUED) {
3505 ret = 0;
3506 goto out_free;
3507 } else if (ret > 0 && ret < io_size) {
3508 /* we got some bytes, but not all. retry. */
Jens Axboe76f49662021-03-04 21:02:58 -07003509 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboe227c0c92020-08-13 11:51:40 -06003510 goto retry;
3511 }
3512done:
3513 kiocb_done(kiocb, ret, cs);
3514 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003515out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003516 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003517 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003518 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003519 return ret;
3520}
3521
Pavel Begunkov73debe62020-09-30 22:57:54 +03003522static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003523{
3524 ssize_t ret;
3525
Pavel Begunkova88fc402020-09-30 22:57:53 +03003526 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003527 if (ret)
3528 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003529
Jens Axboe3529d8c2019-12-19 18:24:38 -07003530 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3531 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003532
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003533 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003534 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003535 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003536 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003537}
3538
Jens Axboea1d7c392020-06-22 11:09:46 -06003539static int io_write(struct io_kiocb *req, bool force_nonblock,
3540 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541{
3542 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003543 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003544 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003545 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003546 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003547
Jens Axboee8c2bc12020-08-15 18:44:09 -07003548 if (rw)
3549 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003550
3551 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003552 if (ret < 0)
3553 return ret;
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003554 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003555 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003556
Jens Axboefd6c2e42019-12-18 12:19:41 -07003557 /* Ensure we clear previously set non-block flag */
3558 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003559 kiocb->ki_flags &= ~IOCB_NOWAIT;
3560 else
3561 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003562
Pavel Begunkov24c74672020-06-21 13:09:51 +03003563 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003564 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003565 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003566
Jens Axboe10d59342019-12-09 20:16:22 -07003567 /* file path doesn't support NOWAIT for non-direct_IO */
3568 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3569 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003570 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003571
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003572 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003573 if (unlikely(ret))
3574 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003575
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003576 /*
3577 * Open-code file_start_write here to grab freeze protection,
3578 * which will be released by another thread in
3579 * io_complete_rw(). Fool lockdep by telling it the lock got
3580 * released so that it doesn't complain about the held lock when
3581 * we return to userspace.
3582 */
3583 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003584 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003585 __sb_writers_release(file_inode(req->file)->i_sb,
3586 SB_FREEZE_WRITE);
3587 }
3588 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003589
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003590 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003591 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003592 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003593 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003594 else
3595 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003596
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003597 /*
3598 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3599 * retry them without IOCB_NOWAIT.
3600 */
3601 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3602 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003603 /* no retry on NONBLOCK marked file */
3604 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3605 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003606 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003607 /* IOPOLL retry should happen for io-wq threads */
3608 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3609 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003610done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003611 kiocb_done(kiocb, ret2, cs);
3612 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003613copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003614 /* some cases will consume bytes even on error returns */
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003615 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003616 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003617 if (!ret)
3618 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003619 }
Jens Axboe31b51512019-01-18 22:56:34 -07003620out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003621 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003622 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003623 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003624 return ret;
3625}
3626
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003627static int __io_splice_prep(struct io_kiocb *req,
3628 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003629{
3630 struct io_splice* sp = &req->splice;
3631 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003632
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003633 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3634 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003635
3636 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003637 sp->len = READ_ONCE(sqe->len);
3638 sp->flags = READ_ONCE(sqe->splice_flags);
3639
3640 if (unlikely(sp->flags & ~valid_flags))
3641 return -EINVAL;
3642
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003643 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3644 (sp->flags & SPLICE_F_FD_IN_FIXED));
3645 if (!sp->file_in)
3646 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003647 req->flags |= REQ_F_NEED_CLEANUP;
3648
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003649 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3650 /*
3651 * Splice operation will be punted aync, and here need to
3652 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3653 */
3654 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003655 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003656 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003657
3658 return 0;
3659}
3660
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003661static int io_tee_prep(struct io_kiocb *req,
3662 const struct io_uring_sqe *sqe)
3663{
3664 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3665 return -EINVAL;
3666 return __io_splice_prep(req, sqe);
3667}
3668
3669static int io_tee(struct io_kiocb *req, bool force_nonblock)
3670{
3671 struct io_splice *sp = &req->splice;
3672 struct file *in = sp->file_in;
3673 struct file *out = sp->file_out;
3674 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3675 long ret = 0;
3676
3677 if (force_nonblock)
3678 return -EAGAIN;
3679 if (sp->len)
3680 ret = do_tee(in, out, sp->len, flags);
3681
3682 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3683 req->flags &= ~REQ_F_NEED_CLEANUP;
3684
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003685 if (ret != sp->len)
3686 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003687 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003688 return 0;
3689}
3690
3691static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3692{
3693 struct io_splice* sp = &req->splice;
3694
3695 sp->off_in = READ_ONCE(sqe->splice_off_in);
3696 sp->off_out = READ_ONCE(sqe->off);
3697 return __io_splice_prep(req, sqe);
3698}
3699
Pavel Begunkov014db002020-03-03 21:33:12 +03003700static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003701{
3702 struct io_splice *sp = &req->splice;
3703 struct file *in = sp->file_in;
3704 struct file *out = sp->file_out;
3705 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3706 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003707 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003708
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003709 if (force_nonblock)
3710 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003711
3712 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3713 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003714
Jens Axboe948a7742020-05-17 14:21:38 -06003715 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003716 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003717
3718 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3719 req->flags &= ~REQ_F_NEED_CLEANUP;
3720
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003721 if (ret != sp->len)
3722 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003723 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003724 return 0;
3725}
3726
Jens Axboe2b188cc2019-01-07 10:46:33 -07003727/*
3728 * IORING_OP_NOP just posts a completion event, nothing else.
3729 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003730static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003731{
3732 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003733
Jens Axboedef596e2019-01-09 08:59:42 -07003734 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3735 return -EINVAL;
3736
Jens Axboe229a7b62020-06-22 10:13:11 -06003737 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003738 return 0;
3739}
3740
Jens Axboe3529d8c2019-12-19 18:24:38 -07003741static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003742{
Jens Axboe6b063142019-01-10 22:13:58 -07003743 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003744
Jens Axboe09bb8392019-03-13 12:39:28 -06003745 if (!req->file)
3746 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003747
Jens Axboe6b063142019-01-10 22:13:58 -07003748 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003749 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003750 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
3751 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003752 return -EINVAL;
3753
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003754 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3755 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3756 return -EINVAL;
3757
3758 req->sync.off = READ_ONCE(sqe->off);
3759 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003760 return 0;
3761}
3762
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003763static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003764{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003765 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003766 int ret;
3767
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003768 /* fsync always requires a blocking context */
3769 if (force_nonblock)
3770 return -EAGAIN;
3771
Jens Axboe9adbd452019-12-20 08:45:55 -07003772 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003773 end > 0 ? end : LLONG_MAX,
3774 req->sync.flags & IORING_FSYNC_DATASYNC);
3775 if (ret < 0)
3776 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003777 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003778 return 0;
3779}
3780
Jens Axboed63d1b52019-12-10 10:38:56 -07003781static int io_fallocate_prep(struct io_kiocb *req,
3782 const struct io_uring_sqe *sqe)
3783{
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003784 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
3785 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07003786 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003787 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3788 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003789
3790 req->sync.off = READ_ONCE(sqe->off);
3791 req->sync.len = READ_ONCE(sqe->addr);
3792 req->sync.mode = READ_ONCE(sqe->len);
3793 return 0;
3794}
3795
Pavel Begunkov014db002020-03-03 21:33:12 +03003796static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003797{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003798 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003799
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003800 /* fallocate always requiring blocking context */
3801 if (force_nonblock)
3802 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003803 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3804 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003805 if (ret < 0)
3806 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003807 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003808 return 0;
3809}
3810
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003811static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003812{
Jens Axboef8748882020-01-08 17:47:02 -07003813 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003814 int ret;
3815
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003816 if (unlikely(sqe->ioprio || sqe->buf_index || sqe->splice_fd_in))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003817 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003818 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003819 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003820
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003821 /* open.how should be already initialised */
3822 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003823 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003824
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003825 req->open.dfd = READ_ONCE(sqe->fd);
3826 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003827 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003828 if (IS_ERR(req->open.filename)) {
3829 ret = PTR_ERR(req->open.filename);
3830 req->open.filename = NULL;
3831 return ret;
3832 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003833 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07003834 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003835 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003836 return 0;
3837}
3838
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003839static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3840{
3841 u64 flags, mode;
3842
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003843 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3844 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003845 mode = READ_ONCE(sqe->len);
3846 flags = READ_ONCE(sqe->open_flags);
3847 req->open.how = build_open_how(flags, mode);
3848 return __io_openat_prep(req, sqe);
3849}
3850
Jens Axboecebdb982020-01-08 17:59:24 -07003851static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3852{
3853 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003854 size_t len;
3855 int ret;
3856
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003857 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3858 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003859 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3860 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003861 if (len < OPEN_HOW_SIZE_VER0)
3862 return -EINVAL;
3863
3864 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3865 len);
3866 if (ret)
3867 return ret;
3868
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003869 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003870}
3871
Pavel Begunkov014db002020-03-03 21:33:12 +03003872static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003873{
3874 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003875 struct file *file;
3876 int ret;
3877
Jens Axboe944d1442020-11-13 16:48:44 -07003878 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003879 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003880
Jens Axboecebdb982020-01-08 17:59:24 -07003881 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003882 if (ret)
3883 goto err;
3884
Jens Axboe4022e7a2020-03-19 19:23:18 -06003885 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003886 if (ret < 0)
3887 goto err;
3888
3889 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3890 if (IS_ERR(file)) {
3891 put_unused_fd(ret);
3892 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07003893 /*
3894 * A work-around to ensure that /proc/self works that way
3895 * that it should - if we get -EOPNOTSUPP back, then assume
3896 * that proc_self_get_link() failed us because we're in async
3897 * context. We should be safe to retry this from the task
3898 * itself with force_nonblock == false set, as it should not
3899 * block on lookup. Would be nice to know this upfront and
3900 * avoid the async dance, but doesn't seem feasible.
3901 */
3902 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
3903 req->open.ignore_nonblock = true;
3904 refcount_inc(&req->refs);
3905 io_req_task_queue(req);
3906 return 0;
3907 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003908 } else {
3909 fsnotify_open(file);
3910 fd_install(ret, file);
3911 }
3912err:
3913 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003914 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003915 if (ret < 0)
3916 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003917 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003918 return 0;
3919}
3920
Pavel Begunkov014db002020-03-03 21:33:12 +03003921static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003922{
Pavel Begunkov014db002020-03-03 21:33:12 +03003923 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003924}
3925
Jens Axboe067524e2020-03-02 16:32:28 -07003926static int io_remove_buffers_prep(struct io_kiocb *req,
3927 const struct io_uring_sqe *sqe)
3928{
3929 struct io_provide_buf *p = &req->pbuf;
3930 u64 tmp;
3931
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003932 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
3933 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07003934 return -EINVAL;
3935
3936 tmp = READ_ONCE(sqe->fd);
3937 if (!tmp || tmp > USHRT_MAX)
3938 return -EINVAL;
3939
3940 memset(p, 0, sizeof(*p));
3941 p->nbufs = tmp;
3942 p->bgid = READ_ONCE(sqe->buf_group);
3943 return 0;
3944}
3945
3946static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3947 int bgid, unsigned nbufs)
3948{
3949 unsigned i = 0;
3950
3951 /* shouldn't happen */
3952 if (!nbufs)
3953 return 0;
3954
3955 /* the head kbuf is the list itself */
3956 while (!list_empty(&buf->list)) {
3957 struct io_buffer *nxt;
3958
3959 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3960 list_del(&nxt->list);
3961 kfree(nxt);
3962 if (++i == nbufs)
3963 return i;
3964 }
3965 i++;
3966 kfree(buf);
Jens Axboe90731882021-07-13 17:18:35 +08003967 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07003968
3969 return i;
3970}
3971
Jens Axboe229a7b62020-06-22 10:13:11 -06003972static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3973 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003974{
3975 struct io_provide_buf *p = &req->pbuf;
3976 struct io_ring_ctx *ctx = req->ctx;
3977 struct io_buffer *head;
3978 int ret = 0;
3979
3980 io_ring_submit_lock(ctx, !force_nonblock);
3981
3982 lockdep_assert_held(&ctx->uring_lock);
3983
3984 ret = -ENOENT;
Jens Axboe90731882021-07-13 17:18:35 +08003985 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07003986 if (head)
3987 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003988 if (ret < 0)
3989 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00003990
3991 /* need to hold the lock to complete IOPOLL requests */
3992 if (ctx->flags & IORING_SETUP_IOPOLL) {
3993 __io_req_complete(req, ret, 0, cs);
3994 io_ring_submit_unlock(ctx, !force_nonblock);
3995 } else {
3996 io_ring_submit_unlock(ctx, !force_nonblock);
3997 __io_req_complete(req, ret, 0, cs);
3998 }
Jens Axboe067524e2020-03-02 16:32:28 -07003999 return 0;
4000}
4001
Jens Axboeddf0322d2020-02-23 16:41:33 -07004002static int io_provide_buffers_prep(struct io_kiocb *req,
4003 const struct io_uring_sqe *sqe)
4004{
Pavel Begunkovcbbc13b2021-04-15 13:07:39 +01004005 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004006 struct io_provide_buf *p = &req->pbuf;
4007 u64 tmp;
4008
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004009 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004010 return -EINVAL;
4011
4012 tmp = READ_ONCE(sqe->fd);
4013 if (!tmp || tmp > USHRT_MAX)
4014 return -E2BIG;
4015 p->nbufs = tmp;
4016 p->addr = READ_ONCE(sqe->addr);
4017 p->len = READ_ONCE(sqe->len);
4018
Pavel Begunkovcbbc13b2021-04-15 13:07:39 +01004019 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4020 &size))
4021 return -EOVERFLOW;
4022 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4023 return -EOVERFLOW;
4024
Pavel Begunkovdcf2dfc2021-03-19 10:21:19 +00004025 size = (unsigned long)p->len * p->nbufs;
4026 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004027 return -EFAULT;
4028
4029 p->bgid = READ_ONCE(sqe->buf_group);
4030 tmp = READ_ONCE(sqe->off);
4031 if (tmp > USHRT_MAX)
4032 return -E2BIG;
4033 p->bid = tmp;
4034 return 0;
4035}
4036
4037static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4038{
4039 struct io_buffer *buf;
4040 u64 addr = pbuf->addr;
4041 int i, bid = pbuf->bid;
4042
4043 for (i = 0; i < pbuf->nbufs; i++) {
4044 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4045 if (!buf)
4046 break;
4047
4048 buf->addr = addr;
Thadeu Lima de Souza Cascardo7e916d02021-05-05 09:47:06 -03004049 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004050 buf->bid = bid;
4051 addr += pbuf->len;
4052 bid++;
4053 if (!*head) {
4054 INIT_LIST_HEAD(&buf->list);
4055 *head = buf;
4056 } else {
4057 list_add_tail(&buf->list, &(*head)->list);
4058 }
4059 }
4060
4061 return i ? i : -ENOMEM;
4062}
4063
Jens Axboe229a7b62020-06-22 10:13:11 -06004064static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4065 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004066{
4067 struct io_provide_buf *p = &req->pbuf;
4068 struct io_ring_ctx *ctx = req->ctx;
4069 struct io_buffer *head, *list;
4070 int ret = 0;
4071
4072 io_ring_submit_lock(ctx, !force_nonblock);
4073
4074 lockdep_assert_held(&ctx->uring_lock);
4075
Jens Axboe90731882021-07-13 17:18:35 +08004076 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004077
4078 ret = io_add_buffers(p, &head);
Jens Axboe90731882021-07-13 17:18:35 +08004079 if (ret >= 0 && !list) {
4080 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4081 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004082 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004083 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004084 if (ret < 0)
4085 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00004086
4087 /* need to hold the lock to complete IOPOLL requests */
4088 if (ctx->flags & IORING_SETUP_IOPOLL) {
4089 __io_req_complete(req, ret, 0, cs);
4090 io_ring_submit_unlock(ctx, !force_nonblock);
4091 } else {
4092 io_ring_submit_unlock(ctx, !force_nonblock);
4093 __io_req_complete(req, ret, 0, cs);
4094 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004095 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096}
4097
Jens Axboe3e4827b2020-01-08 15:18:09 -07004098static int io_epoll_ctl_prep(struct io_kiocb *req,
4099 const struct io_uring_sqe *sqe)
4100{
4101#if defined(CONFIG_EPOLL)
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004102 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004103 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004104 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004105 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004106
4107 req->epoll.epfd = READ_ONCE(sqe->fd);
4108 req->epoll.op = READ_ONCE(sqe->len);
4109 req->epoll.fd = READ_ONCE(sqe->off);
4110
4111 if (ep_op_has_event(req->epoll.op)) {
4112 struct epoll_event __user *ev;
4113
4114 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4115 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4116 return -EFAULT;
4117 }
4118
4119 return 0;
4120#else
4121 return -EOPNOTSUPP;
4122#endif
4123}
4124
Jens Axboe229a7b62020-06-22 10:13:11 -06004125static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4126 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004127{
4128#if defined(CONFIG_EPOLL)
4129 struct io_epoll *ie = &req->epoll;
4130 int ret;
4131
4132 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4133 if (force_nonblock && ret == -EAGAIN)
4134 return -EAGAIN;
4135
4136 if (ret < 0)
4137 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004138 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004139 return 0;
4140#else
4141 return -EOPNOTSUPP;
4142#endif
4143}
4144
Jens Axboec1ca7572019-12-25 22:18:28 -07004145static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4146{
4147#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004148 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004149 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004150 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4151 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004152
4153 req->madvise.addr = READ_ONCE(sqe->addr);
4154 req->madvise.len = READ_ONCE(sqe->len);
4155 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4156 return 0;
4157#else
4158 return -EOPNOTSUPP;
4159#endif
4160}
4161
Pavel Begunkov014db002020-03-03 21:33:12 +03004162static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004163{
4164#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4165 struct io_madvise *ma = &req->madvise;
4166 int ret;
4167
4168 if (force_nonblock)
4169 return -EAGAIN;
4170
Minchan Kim0726b012020-10-17 16:14:50 -07004171 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004172 if (ret < 0)
4173 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004174 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004175 return 0;
4176#else
4177 return -EOPNOTSUPP;
4178#endif
4179}
4180
Jens Axboe4840e412019-12-25 22:03:45 -07004181static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4182{
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004183 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004184 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004185 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4186 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004187
4188 req->fadvise.offset = READ_ONCE(sqe->off);
4189 req->fadvise.len = READ_ONCE(sqe->len);
4190 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4191 return 0;
4192}
4193
Pavel Begunkov014db002020-03-03 21:33:12 +03004194static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004195{
4196 struct io_fadvise *fa = &req->fadvise;
4197 int ret;
4198
Jens Axboe3e694262020-02-01 09:22:49 -07004199 if (force_nonblock) {
4200 switch (fa->advice) {
4201 case POSIX_FADV_NORMAL:
4202 case POSIX_FADV_RANDOM:
4203 case POSIX_FADV_SEQUENTIAL:
4204 break;
4205 default:
4206 return -EAGAIN;
4207 }
4208 }
Jens Axboe4840e412019-12-25 22:03:45 -07004209
4210 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4211 if (ret < 0)
4212 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004213 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004214 return 0;
4215}
4216
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004217static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4218{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004219 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004220 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004221 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004222 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004223 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004224 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004225
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004226 req->statx.dfd = READ_ONCE(sqe->fd);
4227 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004228 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004229 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4230 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004231
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004232 return 0;
4233}
4234
Pavel Begunkov014db002020-03-03 21:33:12 +03004235static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004236{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004237 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004238 int ret;
4239
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004240 if (force_nonblock) {
4241 /* only need file table for an actual valid fd */
4242 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4243 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004244 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004245 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004246
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004247 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4248 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004249
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004250 if (ret < 0)
4251 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004252 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004253 return 0;
4254}
4255
Jens Axboeb5dba592019-12-11 14:02:38 -07004256static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4257{
4258 /*
4259 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004260 * leave the 'file' in an undeterminate state, and here need to modify
4261 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004262 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004263 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004264
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004265 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4266 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004267 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004268 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004269 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004270 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004271 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004272
4273 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004274 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004275 return -EBADF;
4276
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004277 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004278 return 0;
4279}
4280
Jens Axboe229a7b62020-06-22 10:13:11 -06004281static int io_close(struct io_kiocb *req, bool force_nonblock,
4282 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004283{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004284 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004285 int ret;
4286
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004287 /* might be already done during nonblock submission */
4288 if (!close->put_file) {
4289 ret = __close_fd_get_file(close->fd, &close->put_file);
4290 if (ret < 0)
4291 return (ret == -ENOENT) ? -EBADF : ret;
4292 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004293
4294 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004295 if (close->put_file->f_op->flush && force_nonblock) {
Jens Axboef3ac7a52021-01-19 10:10:54 -07004296 /* not safe to cancel at this point */
4297 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004298 /* was never set, but play safe */
4299 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004300 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004301 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004302 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004303 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004304
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004305 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004306 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004307 if (ret < 0)
4308 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004309 fput(close->put_file);
4310 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004311 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004312 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004313}
4314
Jens Axboe3529d8c2019-12-19 18:24:38 -07004315static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004316{
4317 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004318
4319 if (!req->file)
4320 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004321
4322 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4323 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004324 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4325 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004326 return -EINVAL;
4327
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004328 req->sync.off = READ_ONCE(sqe->off);
4329 req->sync.len = READ_ONCE(sqe->len);
4330 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004331 return 0;
4332}
4333
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004334static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004335{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004336 int ret;
4337
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004338 /* sync_file_range always requires a blocking context */
4339 if (force_nonblock)
4340 return -EAGAIN;
4341
Jens Axboe9adbd452019-12-20 08:45:55 -07004342 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004343 req->sync.flags);
4344 if (ret < 0)
4345 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004346 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004347 return 0;
4348}
4349
YueHaibing469956e2020-03-04 15:53:52 +08004350#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004351static int io_setup_async_msg(struct io_kiocb *req,
4352 struct io_async_msghdr *kmsg)
4353{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004354 struct io_async_msghdr *async_msg = req->async_data;
4355
4356 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004357 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004358 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004359 if (kmsg->iov != kmsg->fast_iov)
4360 kfree(kmsg->iov);
4361 return -ENOMEM;
4362 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004363 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004364 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004365 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004366 return -EAGAIN;
4367}
4368
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004369static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4370 struct io_async_msghdr *iomsg)
4371{
4372 iomsg->iov = iomsg->fast_iov;
4373 iomsg->msg.msg_name = &iomsg->addr;
4374 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4375 req->sr_msg.msg_flags, &iomsg->iov);
4376}
4377
Jens Axboe3529d8c2019-12-19 18:24:38 -07004378static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004379{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004380 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004381 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004382 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004383
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004384 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4385 return -EINVAL;
4386
Jens Axboee47293f2019-12-20 08:58:21 -07004387 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004388 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004389 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004390
Jens Axboed8768362020-02-27 14:17:49 -07004391#ifdef CONFIG_COMPAT
4392 if (req->ctx->compat)
4393 sr->msg_flags |= MSG_CMSG_COMPAT;
4394#endif
4395
Jens Axboee8c2bc12020-08-15 18:44:09 -07004396 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004397 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004398 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004399 if (!ret)
4400 req->flags |= REQ_F_NEED_CLEANUP;
4401 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004402}
4403
Jens Axboe229a7b62020-06-22 10:13:11 -06004404static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4405 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004406{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004407 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004408 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004409 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004410 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004411 int ret;
4412
Jens Axboe03b12302019-12-02 18:50:25 -07004413 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004414 if (unlikely(!sock))
4415 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004416
Jens Axboee8c2bc12020-08-15 18:44:09 -07004417 if (req->async_data) {
4418 kmsg = req->async_data;
4419 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004420 /* if iov is set, it's allocated already */
4421 if (!kmsg->iov)
4422 kmsg->iov = kmsg->fast_iov;
4423 kmsg->msg.msg_iter.iov = kmsg->iov;
4424 } else {
4425 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004426 if (ret)
4427 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004428 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004429 }
4430
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004431 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 if (flags & MSG_DONTWAIT)
4433 req->flags |= REQ_F_NOWAIT;
4434 else if (force_nonblock)
4435 flags |= MSG_DONTWAIT;
4436
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004437 if (flags & MSG_WAITALL)
4438 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4439
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004440 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4441 if (force_nonblock && ret == -EAGAIN)
4442 return io_setup_async_msg(req, kmsg);
4443 if (ret == -ERESTARTSYS)
4444 ret = -EINTR;
4445
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004446 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004447 kfree(kmsg->iov);
4448 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004449 if (ret < min_ret)
Jens Axboefddafac2020-01-04 20:19:44 -07004450 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004451 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004452 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004453}
4454
Jens Axboe229a7b62020-06-22 10:13:11 -06004455static int io_send(struct io_kiocb *req, bool force_nonblock,
4456 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004457{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004458 struct io_sr_msg *sr = &req->sr_msg;
4459 struct msghdr msg;
4460 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004461 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004462 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004463 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004464 int ret;
4465
4466 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004467 if (unlikely(!sock))
4468 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004469
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004470 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4471 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004472 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004473
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004474 msg.msg_name = NULL;
4475 msg.msg_control = NULL;
4476 msg.msg_controllen = 0;
4477 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004478
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004479 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004480 if (flags & MSG_DONTWAIT)
4481 req->flags |= REQ_F_NOWAIT;
4482 else if (force_nonblock)
4483 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004484
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004485 if (flags & MSG_WAITALL)
4486 min_ret = iov_iter_count(&msg.msg_iter);
4487
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004488 msg.msg_flags = flags;
4489 ret = sock_sendmsg(sock, &msg);
4490 if (force_nonblock && ret == -EAGAIN)
4491 return -EAGAIN;
4492 if (ret == -ERESTARTSYS)
4493 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004494
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004495 if (ret < min_ret)
Jens Axboe03b12302019-12-02 18:50:25 -07004496 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004497 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004498 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004499}
4500
Pavel Begunkov1400e692020-07-12 20:41:05 +03004501static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4502 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004503{
4504 struct io_sr_msg *sr = &req->sr_msg;
4505 struct iovec __user *uiov;
4506 size_t iov_len;
4507 int ret;
4508
Pavel Begunkov1400e692020-07-12 20:41:05 +03004509 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4510 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004511 if (ret)
4512 return ret;
4513
4514 if (req->flags & REQ_F_BUFFER_SELECT) {
4515 if (iov_len > 1)
4516 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004517 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004518 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004519 sr->len = iomsg->iov[0].iov_len;
4520 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004521 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004522 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004523 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004524 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4525 &iomsg->iov, &iomsg->msg.msg_iter,
4526 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004527 if (ret > 0)
4528 ret = 0;
4529 }
4530
4531 return ret;
4532}
4533
4534#ifdef CONFIG_COMPAT
4535static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004536 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004537{
4538 struct compat_msghdr __user *msg_compat;
4539 struct io_sr_msg *sr = &req->sr_msg;
4540 struct compat_iovec __user *uiov;
4541 compat_uptr_t ptr;
4542 compat_size_t len;
4543 int ret;
4544
Pavel Begunkov270a5942020-07-12 20:41:04 +03004545 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004546 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004547 &ptr, &len);
4548 if (ret)
4549 return ret;
4550
4551 uiov = compat_ptr(ptr);
4552 if (req->flags & REQ_F_BUFFER_SELECT) {
4553 compat_ssize_t clen;
4554
4555 if (len > 1)
4556 return -EINVAL;
4557 if (!access_ok(uiov, sizeof(*uiov)))
4558 return -EFAULT;
4559 if (__get_user(clen, &uiov->iov_len))
4560 return -EFAULT;
4561 if (clen < 0)
4562 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004563 sr->len = clen;
4564 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004565 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004566 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004567 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4568 UIO_FASTIOV, &iomsg->iov,
4569 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004570 if (ret < 0)
4571 return ret;
4572 }
4573
4574 return 0;
4575}
Jens Axboe03b12302019-12-02 18:50:25 -07004576#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004577
Pavel Begunkov1400e692020-07-12 20:41:05 +03004578static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4579 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004580{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004581 iomsg->msg.msg_name = &iomsg->addr;
4582 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004583
4584#ifdef CONFIG_COMPAT
4585 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004586 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004587#endif
4588
Pavel Begunkov1400e692020-07-12 20:41:05 +03004589 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004590}
4591
Jens Axboebcda7ba2020-02-23 16:42:51 -07004592static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004593 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004594{
4595 struct io_sr_msg *sr = &req->sr_msg;
4596 struct io_buffer *kbuf;
4597
Jens Axboebcda7ba2020-02-23 16:42:51 -07004598 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4599 if (IS_ERR(kbuf))
4600 return kbuf;
4601
4602 sr->kbuf = kbuf;
4603 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004604 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004605}
4606
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004607static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4608{
4609 return io_put_kbuf(req, req->sr_msg.kbuf);
4610}
4611
Jens Axboe3529d8c2019-12-19 18:24:38 -07004612static int io_recvmsg_prep(struct io_kiocb *req,
4613 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004614{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004615 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004616 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004617 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004618
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004619 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4620 return -EINVAL;
4621
Jens Axboe3529d8c2019-12-19 18:24:38 -07004622 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004623 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004624 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004625 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004626
Jens Axboed8768362020-02-27 14:17:49 -07004627#ifdef CONFIG_COMPAT
4628 if (req->ctx->compat)
4629 sr->msg_flags |= MSG_CMSG_COMPAT;
4630#endif
4631
Jens Axboee8c2bc12020-08-15 18:44:09 -07004632 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004633 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004634 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004635 if (!ret)
4636 req->flags |= REQ_F_NEED_CLEANUP;
4637 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004638}
4639
Jens Axboe229a7b62020-06-22 10:13:11 -06004640static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4641 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004642{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004643 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004644 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004645 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004646 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004647 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004648 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004649
Jens Axboe0fa03c62019-04-19 13:34:07 -06004650 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004651 if (unlikely(!sock))
4652 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004653
Jens Axboee8c2bc12020-08-15 18:44:09 -07004654 if (req->async_data) {
4655 kmsg = req->async_data;
4656 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004657 /* if iov is set, it's allocated already */
4658 if (!kmsg->iov)
4659 kmsg->iov = kmsg->fast_iov;
4660 kmsg->msg.msg_iter.iov = kmsg->iov;
4661 } else {
4662 ret = io_recvmsg_copy_hdr(req, &iomsg);
4663 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004664 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004665 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004666 }
4667
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004668 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004669 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004670 if (IS_ERR(kbuf))
4671 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004672 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4673 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4674 1, req->sr_msg.len);
4675 }
4676
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004677 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004678 if (flags & MSG_DONTWAIT)
4679 req->flags |= REQ_F_NOWAIT;
4680 else if (force_nonblock)
4681 flags |= MSG_DONTWAIT;
4682
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004683 if (flags & MSG_WAITALL)
4684 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4685
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004686 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4687 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004688 if (force_nonblock && ret == -EAGAIN)
4689 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004690 if (ret == -ERESTARTSYS)
4691 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004692
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004693 if (req->flags & REQ_F_BUFFER_SELECTED)
4694 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004695 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004696 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004697 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004698 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004699 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004700 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004701 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004702}
4703
Jens Axboe229a7b62020-06-22 10:13:11 -06004704static int io_recv(struct io_kiocb *req, bool force_nonblock,
4705 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004706{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004707 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004708 struct io_sr_msg *sr = &req->sr_msg;
4709 struct msghdr msg;
4710 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004711 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004712 struct iovec iov;
4713 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004714 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004715 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004716
Jens Axboefddafac2020-01-04 20:19:44 -07004717 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004718 if (unlikely(!sock))
4719 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004720
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004721 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004722 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004723 if (IS_ERR(kbuf))
4724 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004725 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004726 }
4727
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004728 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004729 if (unlikely(ret))
4730 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004731
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004732 msg.msg_name = NULL;
4733 msg.msg_control = NULL;
4734 msg.msg_controllen = 0;
4735 msg.msg_namelen = 0;
4736 msg.msg_iocb = NULL;
4737 msg.msg_flags = 0;
4738
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004739 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004740 if (flags & MSG_DONTWAIT)
4741 req->flags |= REQ_F_NOWAIT;
4742 else if (force_nonblock)
4743 flags |= MSG_DONTWAIT;
4744
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004745 if (flags & MSG_WAITALL)
4746 min_ret = iov_iter_count(&msg.msg_iter);
4747
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004748 ret = sock_recvmsg(sock, &msg, flags);
4749 if (force_nonblock && ret == -EAGAIN)
4750 return -EAGAIN;
4751 if (ret == -ERESTARTSYS)
4752 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004753out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004754 if (req->flags & REQ_F_BUFFER_SELECTED)
4755 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004756 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Jens Axboefddafac2020-01-04 20:19:44 -07004757 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004758 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004759 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004760}
4761
Jens Axboe3529d8c2019-12-19 18:24:38 -07004762static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004763{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004764 struct io_accept *accept = &req->accept;
4765
Jens Axboe17f2fe32019-10-17 14:42:58 -06004766 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4767 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004768 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004769 return -EINVAL;
4770
Jens Axboed55e5f52019-12-11 16:12:15 -07004771 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4772 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004773 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004774 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004775 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004776}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004777
Jens Axboe229a7b62020-06-22 10:13:11 -06004778static int io_accept(struct io_kiocb *req, bool force_nonblock,
4779 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004780{
4781 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004782 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004783 int ret;
4784
Jiufei Xuee697dee2020-06-10 13:41:59 +08004785 if (req->file->f_flags & O_NONBLOCK)
4786 req->flags |= REQ_F_NOWAIT;
4787
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004788 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004789 accept->addr_len, accept->flags,
4790 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004791 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004792 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004793 if (ret < 0) {
4794 if (ret == -ERESTARTSYS)
4795 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004796 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004797 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004798 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004799 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004800}
4801
Jens Axboe3529d8c2019-12-19 18:24:38 -07004802static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004803{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004804 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004805 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004806
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004807 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4808 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004809 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
4810 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004811 return -EINVAL;
4812
Jens Axboe3529d8c2019-12-19 18:24:38 -07004813 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4814 conn->addr_len = READ_ONCE(sqe->addr2);
4815
4816 if (!io)
4817 return 0;
4818
4819 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004820 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004821}
4822
Jens Axboe229a7b62020-06-22 10:13:11 -06004823static int io_connect(struct io_kiocb *req, bool force_nonblock,
4824 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004825{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004826 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004827 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004828 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004829
Jens Axboee8c2bc12020-08-15 18:44:09 -07004830 if (req->async_data) {
4831 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004832 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004833 ret = move_addr_to_kernel(req->connect.addr,
4834 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004835 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004836 if (ret)
4837 goto out;
4838 io = &__io;
4839 }
4840
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004841 file_flags = force_nonblock ? O_NONBLOCK : 0;
4842
Jens Axboee8c2bc12020-08-15 18:44:09 -07004843 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004844 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004845 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004846 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004847 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004848 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004849 ret = -ENOMEM;
4850 goto out;
4851 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004852 io = req->async_data;
4853 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004854 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004855 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004856 if (ret == -ERESTARTSYS)
4857 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004858out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004859 if (ret < 0)
4860 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004861 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004862 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004863}
YueHaibing469956e2020-03-04 15:53:52 +08004864#else /* !CONFIG_NET */
4865static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4866{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004867 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004868}
4869
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004870static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4871 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004872{
YueHaibing469956e2020-03-04 15:53:52 +08004873 return -EOPNOTSUPP;
4874}
4875
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004876static int io_send(struct io_kiocb *req, bool force_nonblock,
4877 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004878{
4879 return -EOPNOTSUPP;
4880}
4881
4882static int io_recvmsg_prep(struct io_kiocb *req,
4883 const struct io_uring_sqe *sqe)
4884{
4885 return -EOPNOTSUPP;
4886}
4887
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004888static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4889 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004890{
4891 return -EOPNOTSUPP;
4892}
4893
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004894static int io_recv(struct io_kiocb *req, bool force_nonblock,
4895 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004896{
4897 return -EOPNOTSUPP;
4898}
4899
4900static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4901{
4902 return -EOPNOTSUPP;
4903}
4904
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004905static int io_accept(struct io_kiocb *req, bool force_nonblock,
4906 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004907{
4908 return -EOPNOTSUPP;
4909}
4910
4911static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4912{
4913 return -EOPNOTSUPP;
4914}
4915
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004916static int io_connect(struct io_kiocb *req, bool force_nonblock,
4917 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004918{
4919 return -EOPNOTSUPP;
4920}
4921#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004922
Jens Axboed7718a92020-02-14 22:23:12 -07004923struct io_poll_table {
4924 struct poll_table_struct pt;
4925 struct io_kiocb *req;
Pavel Begunkov9eef9022021-07-20 10:50:43 +01004926 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07004927 int error;
4928};
4929
Jens Axboed7718a92020-02-14 22:23:12 -07004930static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4931 __poll_t mask, task_work_func_t func)
4932{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004933 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004934 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004935
4936 /* for instances that support it check for an event match first: */
4937 if (mask && !(mask & poll->events))
4938 return 0;
4939
4940 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4941
4942 list_del_init(&poll->wait.entry);
4943
Jens Axboed7718a92020-02-14 22:23:12 -07004944 req->result = mask;
4945 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004946 percpu_ref_get(&req->ctx->refs);
4947
Jens Axboed7718a92020-02-14 22:23:12 -07004948 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004949 * If we using the signalfd wait_queue_head for this wakeup, then
4950 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4951 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4952 * either, as the normal wakeup will suffice.
4953 */
4954 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4955
4956 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004957 * If this fails, then the task is exiting. When a task exits, the
4958 * work gets canceled, so just cancel this request as well instead
4959 * of executing it. We can't safely execute it anyway, as we may not
4960 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004961 */
Jens Axboe87c43112020-09-30 21:00:14 -06004962 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004963 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004964 struct task_struct *tsk;
4965
Jens Axboee3aabf92020-05-18 11:04:17 -06004966 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004967 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06004968 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06004969 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004970 }
Jens Axboed7718a92020-02-14 22:23:12 -07004971 return 1;
4972}
4973
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004974static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4975 __acquires(&req->ctx->completion_lock)
4976{
4977 struct io_ring_ctx *ctx = req->ctx;
4978
4979 if (!req->result && !READ_ONCE(poll->canceled)) {
4980 struct poll_table_struct pt = { ._key = poll->events };
4981
4982 req->result = vfs_poll(req->file, &pt) & poll->events;
4983 }
4984
4985 spin_lock_irq(&ctx->completion_lock);
4986 if (!req->result && !READ_ONCE(poll->canceled)) {
4987 add_wait_queue(poll->head, &poll->wait);
4988 return true;
4989 }
4990
4991 return false;
4992}
4993
Jens Axboed4e7cd32020-08-15 11:44:50 -07004994static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004995{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004996 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004997 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004998 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004999 return req->apoll->double_poll;
5000}
5001
5002static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5003{
5004 if (req->opcode == IORING_OP_POLL_ADD)
5005 return &req->poll;
5006 return &req->apoll->poll;
5007}
5008
5009static void io_poll_remove_double(struct io_kiocb *req)
5010{
5011 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005012
5013 lockdep_assert_held(&req->ctx->completion_lock);
5014
5015 if (poll && poll->head) {
5016 struct wait_queue_head *head = poll->head;
5017
5018 spin_lock(&head->lock);
5019 list_del_init(&poll->wait.entry);
5020 if (poll->wait.private)
5021 refcount_dec(&req->refs);
5022 poll->head = NULL;
5023 spin_unlock(&head->lock);
5024 }
5025}
5026
5027static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5028{
5029 struct io_ring_ctx *ctx = req->ctx;
5030
Jens Axboed4e7cd32020-08-15 11:44:50 -07005031 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005032 req->poll.done = true;
5033 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5034 io_commit_cqring(ctx);
5035}
5036
Jens Axboe18bceab2020-05-15 11:56:54 -06005037static void io_poll_task_func(struct callback_head *cb)
5038{
5039 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005040 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005041 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005042
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005043 if (io_poll_rewait(req, &req->poll)) {
5044 spin_unlock_irq(&ctx->completion_lock);
5045 } else {
5046 hash_del(&req->hash_node);
5047 io_poll_complete(req, req->result, 0);
5048 spin_unlock_irq(&ctx->completion_lock);
5049
5050 nxt = io_put_req_find_next(req);
5051 io_cqring_ev_posted(ctx);
5052 if (nxt)
5053 __io_req_task_submit(nxt);
5054 }
5055
Jens Axboe6d816e02020-08-11 08:04:14 -06005056 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005057}
5058
5059static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5060 int sync, void *key)
5061{
5062 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005063 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005064 __poll_t mask = key_to_poll(key);
5065
5066 /* for instances that support it check for an event match first: */
5067 if (mask && !(mask & poll->events))
5068 return 0;
5069
Jens Axboe8706e042020-09-28 08:38:54 -06005070 list_del_init(&wait->entry);
5071
Jens Axboe807abcb2020-07-17 17:09:27 -06005072 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005073 bool done;
5074
Jens Axboe807abcb2020-07-17 17:09:27 -06005075 spin_lock(&poll->head->lock);
5076 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005077 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005078 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005079 /* make sure double remove sees this as being gone */
5080 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005081 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005082 if (!done) {
5083 /* use wait func handler, so it matches the rq type */
5084 poll->wait.func(&poll->wait, mode, sync, key);
5085 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005086 }
5087 refcount_dec(&req->refs);
5088 return 1;
5089}
5090
5091static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5092 wait_queue_func_t wake_func)
5093{
5094 poll->head = NULL;
5095 poll->done = false;
5096 poll->canceled = false;
5097 poll->events = events;
5098 INIT_LIST_HEAD(&poll->wait.entry);
5099 init_waitqueue_func_entry(&poll->wait, wake_func);
5100}
5101
5102static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005103 struct wait_queue_head *head,
5104 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005105{
5106 struct io_kiocb *req = pt->req;
5107
5108 /*
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005109 * The file being polled uses multiple waitqueues for poll handling
5110 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5111 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005112 */
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005113 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005114 struct io_poll_iocb *poll_one = poll;
5115
Jens Axboe18bceab2020-05-15 11:56:54 -06005116 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005117 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005118 pt->error = -EINVAL;
5119 return;
5120 }
Jens Axboea2501d82021-02-28 16:07:30 -07005121 /* double add on the same waitqueue head, ignore */
5122 if (poll->head == head)
5123 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005124 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5125 if (!poll) {
5126 pt->error = -ENOMEM;
5127 return;
5128 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005129 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005130 refcount_inc(&req->refs);
5131 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005132 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005133 }
5134
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005135 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005136 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005137
5138 if (poll->events & EPOLLEXCLUSIVE)
5139 add_wait_queue_exclusive(head, &poll->wait);
5140 else
5141 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005142}
5143
5144static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5145 struct poll_table_struct *p)
5146{
5147 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005148 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005149
Jens Axboe807abcb2020-07-17 17:09:27 -06005150 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005151}
5152
Jens Axboed7718a92020-02-14 22:23:12 -07005153static void io_async_task_func(struct callback_head *cb)
5154{
5155 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5156 struct async_poll *apoll = req->apoll;
5157 struct io_ring_ctx *ctx = req->ctx;
5158
5159 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5160
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005161 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005162 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005163 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005164 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005165 }
5166
Jens Axboe31067252020-05-17 17:43:31 -06005167 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005168 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005169 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005170
Jens Axboed4e7cd32020-08-15 11:44:50 -07005171 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005172 spin_unlock_irq(&ctx->completion_lock);
5173
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005174 if (!READ_ONCE(apoll->poll.canceled))
5175 __io_req_task_submit(req);
5176 else
5177 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005178
Jens Axboe6d816e02020-08-11 08:04:14 -06005179 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005180 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005181 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005182}
5183
5184static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5185 void *key)
5186{
5187 struct io_kiocb *req = wait->private;
5188 struct io_poll_iocb *poll = &req->apoll->poll;
5189
5190 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5191 key_to_poll(key));
5192
5193 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5194}
5195
5196static void io_poll_req_insert(struct io_kiocb *req)
5197{
5198 struct io_ring_ctx *ctx = req->ctx;
5199 struct hlist_head *list;
5200
5201 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5202 hlist_add_head(&req->hash_node, list);
5203}
5204
5205static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5206 struct io_poll_iocb *poll,
5207 struct io_poll_table *ipt, __poll_t mask,
5208 wait_queue_func_t wake_func)
5209 __acquires(&ctx->completion_lock)
5210{
5211 struct io_ring_ctx *ctx = req->ctx;
5212 bool cancel = false;
5213
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005214 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005215 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005216 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005217 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005218
5219 ipt->pt._key = mask;
5220 ipt->req = req;
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005221 ipt->error = 0;
5222 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005223
Jens Axboed7718a92020-02-14 22:23:12 -07005224 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005225 if (unlikely(!ipt->nr_entries) && !ipt->error)
5226 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005227
5228 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkovfca53432021-07-20 10:50:44 +01005229 if (ipt->error)
5230 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005231 if (likely(poll->head)) {
5232 spin_lock(&poll->head->lock);
5233 if (unlikely(list_empty(&poll->wait.entry))) {
5234 if (ipt->error)
5235 cancel = true;
5236 ipt->error = 0;
5237 mask = 0;
5238 }
5239 if (mask || ipt->error)
5240 list_del_init(&poll->wait.entry);
5241 else if (cancel)
5242 WRITE_ONCE(poll->canceled, true);
5243 else if (!poll->done) /* actually waiting for an event */
5244 io_poll_req_insert(req);
5245 spin_unlock(&poll->head->lock);
5246 }
5247
5248 return mask;
5249}
5250
5251static bool io_arm_poll_handler(struct io_kiocb *req)
5252{
5253 const struct io_op_def *def = &io_op_defs[req->opcode];
5254 struct io_ring_ctx *ctx = req->ctx;
5255 struct async_poll *apoll;
5256 struct io_poll_table ipt;
5257 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005258 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005259
5260 if (!req->file || !file_can_poll(req->file))
5261 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005262 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005263 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005264 if (def->pollin)
5265 rw = READ;
5266 else if (def->pollout)
5267 rw = WRITE;
5268 else
5269 return false;
5270 /* if we can't nonblock try, then no point in arming a poll handler */
5271 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005272 return false;
5273
5274 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5275 if (unlikely(!apoll))
5276 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005277 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005278
5279 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005280 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005281
Nathan Chancellor8755d972020-03-02 16:01:19 -07005282 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005283 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005284 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005285 if (def->pollout)
5286 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005287
5288 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5289 if ((req->opcode == IORING_OP_RECVMSG) &&
5290 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5291 mask &= ~POLLIN;
5292
Jens Axboed7718a92020-02-14 22:23:12 -07005293 mask |= POLLERR | POLLPRI;
5294
5295 ipt.pt._qproc = io_async_queue_proc;
5296
5297 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5298 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005299 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005300 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005301 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005302 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005303 kfree(apoll);
5304 return false;
5305 }
5306 spin_unlock_irq(&ctx->completion_lock);
5307 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5308 apoll->poll.events);
5309 return true;
5310}
5311
5312static bool __io_poll_remove_one(struct io_kiocb *req,
5313 struct io_poll_iocb *poll)
5314{
Jens Axboeb41e9852020-02-17 09:52:41 -07005315 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005316
5317 spin_lock(&poll->head->lock);
5318 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005319 if (!list_empty(&poll->wait.entry)) {
5320 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005321 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005322 }
5323 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005324 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005325 return do_complete;
5326}
5327
5328static bool io_poll_remove_one(struct io_kiocb *req)
5329{
5330 bool do_complete;
5331
Jens Axboed4e7cd32020-08-15 11:44:50 -07005332 io_poll_remove_double(req);
5333
Jens Axboed7718a92020-02-14 22:23:12 -07005334 if (req->opcode == IORING_OP_POLL_ADD) {
5335 do_complete = __io_poll_remove_one(req, &req->poll);
5336 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005337 struct async_poll *apoll = req->apoll;
5338
Jens Axboed7718a92020-02-14 22:23:12 -07005339 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005340 do_complete = __io_poll_remove_one(req, &apoll->poll);
5341 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005342 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005343 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005344 kfree(apoll);
5345 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005346 }
5347
Jens Axboeb41e9852020-02-17 09:52:41 -07005348 if (do_complete) {
5349 io_cqring_fill_event(req, -ECANCELED);
5350 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005351 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005352 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005353 }
5354
5355 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005356}
5357
Jens Axboe76e1b642020-09-26 15:05:03 -06005358/*
5359 * Returns true if we found and killed one or more poll requests
5360 */
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00005361static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5362 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005363{
Jens Axboe78076bb2019-12-04 19:56:40 -07005364 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005365 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005366 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005367
5368 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005369 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5370 struct hlist_head *list;
5371
5372 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005373 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00005374 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005375 posted += io_poll_remove_one(req);
5376 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005377 }
5378 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005379
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005380 if (posted)
5381 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005382
5383 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005384}
5385
Jens Axboe47f46762019-11-09 17:43:02 -07005386static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5387{
Jens Axboe78076bb2019-12-04 19:56:40 -07005388 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005389 struct io_kiocb *req;
5390
Jens Axboe78076bb2019-12-04 19:56:40 -07005391 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5392 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005393 if (sqe_addr != req->user_data)
5394 continue;
5395 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005396 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005397 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005398 }
5399
5400 return -ENOENT;
5401}
5402
Jens Axboe3529d8c2019-12-19 18:24:38 -07005403static int io_poll_remove_prep(struct io_kiocb *req,
5404 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005405{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005406 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5407 return -EINVAL;
5408 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5409 sqe->poll_events)
5410 return -EINVAL;
5411
Jens Axboe0969e782019-12-17 18:40:57 -07005412 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005413 return 0;
5414}
5415
5416/*
5417 * Find a running poll command that matches one specified in sqe->addr,
5418 * and remove it if found.
5419 */
5420static int io_poll_remove(struct io_kiocb *req)
5421{
5422 struct io_ring_ctx *ctx = req->ctx;
5423 u64 addr;
5424 int ret;
5425
Jens Axboe0969e782019-12-17 18:40:57 -07005426 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005427 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005428 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005429 spin_unlock_irq(&ctx->completion_lock);
5430
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005431 if (ret < 0)
5432 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005433 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005434 return 0;
5435}
5436
Jens Axboe221c5eb2019-01-17 09:41:58 -07005437static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5438 void *key)
5439{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005440 struct io_kiocb *req = wait->private;
5441 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005442
Jens Axboed7718a92020-02-14 22:23:12 -07005443 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005444}
5445
Jens Axboe221c5eb2019-01-17 09:41:58 -07005446static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5447 struct poll_table_struct *p)
5448{
5449 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5450
Jens Axboee8c2bc12020-08-15 18:44:09 -07005451 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005452}
5453
Jens Axboe3529d8c2019-12-19 18:24:38 -07005454static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005455{
5456 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005457 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005458
5459 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5460 return -EINVAL;
5461 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5462 return -EINVAL;
5463
Jiufei Xue5769a352020-06-17 17:53:55 +08005464 events = READ_ONCE(sqe->poll32_events);
5465#ifdef __BIG_ENDIAN
5466 events = swahw32(events);
5467#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005468 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5469 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005470 return 0;
5471}
5472
Pavel Begunkov014db002020-03-03 21:33:12 +03005473static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005474{
5475 struct io_poll_iocb *poll = &req->poll;
5476 struct io_ring_ctx *ctx = req->ctx;
5477 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005478 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005479
Jens Axboed7718a92020-02-14 22:23:12 -07005480 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005481
Jens Axboed7718a92020-02-14 22:23:12 -07005482 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5483 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005484
Jens Axboe8c838782019-03-12 15:48:16 -06005485 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005486 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005487 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005488 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005489 spin_unlock_irq(&ctx->completion_lock);
5490
Jens Axboe8c838782019-03-12 15:48:16 -06005491 if (mask) {
5492 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005493 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005494 }
Jens Axboe8c838782019-03-12 15:48:16 -06005495 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005496}
5497
Jens Axboe5262f562019-09-17 12:26:57 -06005498static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5499{
Jens Axboead8a48a2019-11-15 08:49:11 -07005500 struct io_timeout_data *data = container_of(timer,
5501 struct io_timeout_data, timer);
5502 struct io_kiocb *req = data->req;
5503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005504 unsigned long flags;
5505
Jens Axboe5262f562019-09-17 12:26:57 -06005506 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005507 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005508 atomic_set(&req->ctx->cq_timeouts,
5509 atomic_read(&req->ctx->cq_timeouts) + 1);
5510
Jens Axboe78e19bb2019-11-06 15:21:34 -07005511 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005512 io_commit_cqring(ctx);
5513 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5514
5515 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005516 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005517 io_put_req(req);
5518 return HRTIMER_NORESTART;
5519}
5520
Jens Axboef254ac02020-08-12 17:33:30 -06005521static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005522{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005523 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005524 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005525
Jens Axboee8c2bc12020-08-15 18:44:09 -07005526 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005527 if (ret == -1)
5528 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005529 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005530
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005531 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005532 io_cqring_fill_event(req, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005533 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07005534 return 0;
5535}
5536
Jens Axboef254ac02020-08-12 17:33:30 -06005537static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5538{
5539 struct io_kiocb *req;
5540 int ret = -ENOENT;
5541
5542 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5543 if (user_data == req->user_data) {
5544 ret = 0;
5545 break;
5546 }
5547 }
5548
5549 if (ret == -ENOENT)
5550 return ret;
5551
5552 return __io_timeout_cancel(req);
5553}
5554
Jens Axboe3529d8c2019-12-19 18:24:38 -07005555static int io_timeout_remove_prep(struct io_kiocb *req,
5556 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005557{
Jens Axboeb29472e2019-12-17 18:50:29 -07005558 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5559 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005560 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5561 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06005562 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags |
5563 sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005564 return -EINVAL;
5565
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005566 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005567 return 0;
5568}
5569
Jens Axboe11365042019-10-16 09:08:32 -06005570/*
5571 * Remove or update an existing timeout command
5572 */
Jens Axboefc4df992019-12-10 14:38:45 -07005573static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005574{
5575 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005576 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005577
Jens Axboe11365042019-10-16 09:08:32 -06005578 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005579 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005580
Jens Axboe47f46762019-11-09 17:43:02 -07005581 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005582 io_commit_cqring(ctx);
5583 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005584 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005585 if (ret < 0)
5586 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005587 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005588 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005589}
5590
Jens Axboe3529d8c2019-12-19 18:24:38 -07005591static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005592 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005593{
Jens Axboead8a48a2019-11-15 08:49:11 -07005594 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005595 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005596 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005597
Jens Axboead8a48a2019-11-15 08:49:11 -07005598 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005599 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06005600 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
5601 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06005602 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005603 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005604 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005605 flags = READ_ONCE(sqe->timeout_flags);
5606 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005607 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005608
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005609 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005610
Jens Axboee8c2bc12020-08-15 18:44:09 -07005611 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005612 return -ENOMEM;
5613
Jens Axboee8c2bc12020-08-15 18:44:09 -07005614 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005615 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005616
5617 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005618 return -EFAULT;
5619
Jens Axboe11365042019-10-16 09:08:32 -06005620 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005621 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005622 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005623 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005624
Jens Axboead8a48a2019-11-15 08:49:11 -07005625 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5626 return 0;
5627}
5628
Jens Axboefc4df992019-12-10 14:38:45 -07005629static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005630{
Jens Axboead8a48a2019-11-15 08:49:11 -07005631 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005632 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005633 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005634 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005635
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005636 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005637
Jens Axboe5262f562019-09-17 12:26:57 -06005638 /*
5639 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005640 * timeout event to be satisfied. If it isn't set, then this is
5641 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005642 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005643 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005644 entry = ctx->timeout_list.prev;
5645 goto add;
5646 }
Jens Axboe5262f562019-09-17 12:26:57 -06005647
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005648 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5649 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005650
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05005651 /* Update the last seq here in case io_flush_timeouts() hasn't.
5652 * This is safe because ->completion_lock is held, and submissions
5653 * and completions are never mixed in the same ->completion_lock section.
5654 */
5655 ctx->cq_last_tm_flush = tail;
5656
Jens Axboe5262f562019-09-17 12:26:57 -06005657 /*
5658 * Insertion sort, ensuring the first entry in the list is always
5659 * the one we need first.
5660 */
Jens Axboe5262f562019-09-17 12:26:57 -06005661 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005662 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5663 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005664
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005665 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005666 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005667 /* nxt.seq is behind @tail, otherwise would've been completed */
5668 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005669 break;
5670 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005671add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005672 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005673 data->timer.function = io_timeout_fn;
5674 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005675 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005676 return 0;
5677}
5678
Jens Axboe62755e32019-10-28 21:49:21 -06005679static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005680{
Jens Axboe62755e32019-10-28 21:49:21 -06005681 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005682
Jens Axboe62755e32019-10-28 21:49:21 -06005683 return req->user_data == (unsigned long) data;
5684}
5685
Jens Axboee977d6d2019-11-05 12:39:45 -07005686static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005687{
Jens Axboe62755e32019-10-28 21:49:21 -06005688 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005689 int ret = 0;
5690
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005691 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005692 switch (cancel_ret) {
5693 case IO_WQ_CANCEL_OK:
5694 ret = 0;
5695 break;
5696 case IO_WQ_CANCEL_RUNNING:
5697 ret = -EALREADY;
5698 break;
5699 case IO_WQ_CANCEL_NOTFOUND:
5700 ret = -ENOENT;
5701 break;
5702 }
5703
Jens Axboee977d6d2019-11-05 12:39:45 -07005704 return ret;
5705}
5706
Jens Axboe47f46762019-11-09 17:43:02 -07005707static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5708 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005709 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005710{
5711 unsigned long flags;
5712 int ret;
5713
5714 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5715 if (ret != -ENOENT) {
5716 spin_lock_irqsave(&ctx->completion_lock, flags);
5717 goto done;
5718 }
5719
5720 spin_lock_irqsave(&ctx->completion_lock, flags);
5721 ret = io_timeout_cancel(ctx, sqe_addr);
5722 if (ret != -ENOENT)
5723 goto done;
5724 ret = io_poll_cancel(ctx, sqe_addr);
5725done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005726 if (!ret)
5727 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005728 io_cqring_fill_event(req, ret);
5729 io_commit_cqring(ctx);
5730 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5731 io_cqring_ev_posted(ctx);
5732
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005733 if (ret < 0)
5734 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005735 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005736}
5737
Jens Axboe3529d8c2019-12-19 18:24:38 -07005738static int io_async_cancel_prep(struct io_kiocb *req,
5739 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005740{
Jens Axboefbf23842019-12-17 18:45:56 -07005741 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005742 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005743 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5744 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06005745 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
5746 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07005747 return -EINVAL;
5748
Jens Axboefbf23842019-12-17 18:45:56 -07005749 req->cancel.addr = READ_ONCE(sqe->addr);
5750 return 0;
5751}
5752
Pavel Begunkov014db002020-03-03 21:33:12 +03005753static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005754{
5755 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005756
Pavel Begunkov014db002020-03-03 21:33:12 +03005757 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005758 return 0;
5759}
5760
Jens Axboe05f3fb32019-12-09 11:22:50 -07005761static int io_files_update_prep(struct io_kiocb *req,
5762 const struct io_uring_sqe *sqe)
5763{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005764 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5765 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005766 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5767 return -EINVAL;
5768 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005769 return -EINVAL;
5770
5771 req->files_update.offset = READ_ONCE(sqe->off);
5772 req->files_update.nr_args = READ_ONCE(sqe->len);
5773 if (!req->files_update.nr_args)
5774 return -EINVAL;
5775 req->files_update.arg = READ_ONCE(sqe->addr);
5776 return 0;
5777}
5778
Jens Axboe229a7b62020-06-22 10:13:11 -06005779static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5780 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005781{
5782 struct io_ring_ctx *ctx = req->ctx;
5783 struct io_uring_files_update up;
5784 int ret;
5785
Jens Axboef86cd202020-01-29 13:46:44 -07005786 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005787 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005788
5789 up.offset = req->files_update.offset;
5790 up.fds = req->files_update.arg;
5791
5792 mutex_lock(&ctx->uring_lock);
5793 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5794 mutex_unlock(&ctx->uring_lock);
5795
5796 if (ret < 0)
5797 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005798 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005799 return 0;
5800}
5801
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005802static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005803{
Jens Axboed625c6e2019-12-17 19:53:05 -07005804 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005805 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005806 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005807 case IORING_OP_READV:
5808 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005809 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005810 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005811 case IORING_OP_WRITEV:
5812 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005813 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005814 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005815 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005816 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005817 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005818 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005819 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005820 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005821 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005822 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005823 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005824 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005825 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005826 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005827 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005828 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005829 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005830 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005831 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005832 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005833 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005834 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005835 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005836 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005837 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005838 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005839 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005840 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005841 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005842 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005843 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005844 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005845 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005846 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005847 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005848 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005849 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005850 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005851 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005852 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005853 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005854 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005855 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005856 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005857 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005858 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005859 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005860 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005861 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005862 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005863 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005864 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005865 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005866 return io_tee_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005867 }
5868
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005869 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5870 req->opcode);
5871 return-EINVAL;
5872}
5873
Jens Axboedef596e2019-01-09 08:59:42 -07005874static int io_req_defer_prep(struct io_kiocb *req,
5875 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005876{
Jens Axboedef596e2019-01-09 08:59:42 -07005877 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005878 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005879 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005880 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005881 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005882}
5883
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005884static u32 io_get_sequence(struct io_kiocb *req)
5885{
5886 struct io_kiocb *pos;
5887 struct io_ring_ctx *ctx = req->ctx;
5888 u32 total_submitted, nr_reqs = 1;
5889
5890 if (req->flags & REQ_F_LINK_HEAD)
5891 list_for_each_entry(pos, &req->link_list, link_list)
5892 nr_reqs++;
5893
5894 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5895 return total_submitted - nr_reqs;
5896}
5897
Jens Axboe3529d8c2019-12-19 18:24:38 -07005898static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005899{
5900 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005901 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005902 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005903 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005904
5905 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005906 if (likely(list_empty_careful(&ctx->defer_list) &&
5907 !(req->flags & REQ_F_IO_DRAIN)))
5908 return 0;
5909
5910 seq = io_get_sequence(req);
5911 /* Still a chance to pass the sequence check */
5912 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005913 return 0;
5914
Jens Axboee8c2bc12020-08-15 18:44:09 -07005915 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005916 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005917 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005918 return ret;
5919 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005920 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005921 de = kmalloc(sizeof(*de), GFP_KERNEL);
5922 if (!de)
5923 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005924
5925 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005926 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005927 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005928 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005929 io_queue_async_work(req);
5930 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005931 }
5932
5933 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005934 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005935 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005936 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005937 spin_unlock_irq(&ctx->completion_lock);
5938 return -EIOCBQUEUED;
5939}
Jens Axboeedafcce2019-01-09 09:16:05 -07005940
Jens Axboef573d382020-09-22 10:19:24 -06005941static void io_req_drop_files(struct io_kiocb *req)
5942{
5943 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov52382df82021-02-09 04:47:44 +00005944 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06005945 unsigned long flags;
5946
Jens Axboed16692a2021-02-09 04:47:41 +00005947 if (req->work.flags & IO_WQ_WORK_FILES) {
5948 put_files_struct(req->work.identity->files);
5949 put_nsproxy(req->work.identity->nsproxy);
5950 }
Pavel Begunkov52504a62020-12-18 13:12:21 +00005951 spin_lock_irqsave(&ctx->inflight_lock, flags);
5952 list_del(&req->inflight_entry);
5953 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5954 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06005955 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkov52382df82021-02-09 04:47:44 +00005956 if (atomic_read(&tctx->in_idle))
5957 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06005958}
5959
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005960static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005961{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005962 if (req->flags & REQ_F_BUFFER_SELECTED) {
5963 switch (req->opcode) {
5964 case IORING_OP_READV:
5965 case IORING_OP_READ_FIXED:
5966 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005967 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005968 break;
5969 case IORING_OP_RECVMSG:
5970 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005971 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005972 break;
5973 }
5974 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005975 }
5976
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005977 if (req->flags & REQ_F_NEED_CLEANUP) {
5978 switch (req->opcode) {
5979 case IORING_OP_READV:
5980 case IORING_OP_READ_FIXED:
5981 case IORING_OP_READ:
5982 case IORING_OP_WRITEV:
5983 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005984 case IORING_OP_WRITE: {
5985 struct io_async_rw *io = req->async_data;
5986 if (io->free_iovec)
5987 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005988 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005989 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005990 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005991 case IORING_OP_SENDMSG: {
5992 struct io_async_msghdr *io = req->async_data;
5993 if (io->iov != io->fast_iov)
5994 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005995 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005996 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005997 case IORING_OP_SPLICE:
5998 case IORING_OP_TEE:
5999 io_put_file(req, req->splice.file_in,
6000 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6001 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006002 case IORING_OP_OPENAT:
6003 case IORING_OP_OPENAT2:
6004 if (req->open.filename)
6005 putname(req->open.filename);
6006 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006007 }
6008 req->flags &= ~REQ_F_NEED_CLEANUP;
6009 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006010}
6011
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006012static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6013 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006014{
Jens Axboeedafcce2019-01-09 09:16:05 -07006015 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006016 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006017
Jens Axboed625c6e2019-12-17 19:53:05 -07006018 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006019 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006020 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006021 break;
6022 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006023 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006024 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006025 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006026 break;
6027 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006028 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006029 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006030 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031 break;
6032 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006033 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034 break;
6035 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006036 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006037 break;
6038 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006039 ret = io_poll_remove(req);
6040 break;
6041 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006042 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006043 break;
6044 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006045 ret = io_sendmsg(req, force_nonblock, cs);
6046 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006047 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006048 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006049 break;
6050 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006051 ret = io_recvmsg(req, force_nonblock, cs);
6052 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006053 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006054 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006055 break;
6056 case IORING_OP_TIMEOUT:
6057 ret = io_timeout(req);
6058 break;
6059 case IORING_OP_TIMEOUT_REMOVE:
6060 ret = io_timeout_remove(req);
6061 break;
6062 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006063 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006064 break;
6065 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006066 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006067 break;
6068 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006069 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006071 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006072 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006073 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006074 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006075 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006076 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006077 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006078 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006079 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006080 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006081 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006082 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006083 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006084 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006085 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006086 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006087 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006088 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006089 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006090 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006091 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006092 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006093 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006094 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006095 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006096 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006097 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006098 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006099 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006100 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006101 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006102 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006103 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006104 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006105 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006106 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006107 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006108 ret = io_tee(req, force_nonblock);
6109 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110 default:
6111 ret = -EINVAL;
6112 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006113 }
6114
6115 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006116 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117
Jens Axboeb5325762020-05-19 21:20:27 -06006118 /* If the op doesn't have a file, we're not polling for it */
6119 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006120 const bool in_async = io_wq_current_is_worker();
6121
Jens Axboe11ba8202020-01-15 21:51:17 -07006122 /* workqueue context doesn't hold uring_lock, grab it now */
6123 if (in_async)
6124 mutex_lock(&ctx->uring_lock);
6125
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006127
6128 if (in_async)
6129 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006130 }
6131
6132 return 0;
6133}
6134
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006135static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006136{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006137 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006138 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006139 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006140
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006141 timeout = io_prep_linked_timeout(req);
6142 if (timeout)
6143 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006144
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006145 /* if NO_CANCEL is set, we must still run the work */
6146 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6147 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006148 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006149 }
Jens Axboe31b51512019-01-18 22:56:34 -07006150
Jens Axboe561fb042019-10-24 07:25:42 -06006151 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006152 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006153 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006154 /*
6155 * We can get EAGAIN for polled IO even though we're
6156 * forcing a sync submission from here, since we can't
6157 * wait for request slots on the block side.
6158 */
6159 if (ret != -EAGAIN)
6160 break;
6161 cond_resched();
6162 } while (1);
6163 }
Jens Axboe31b51512019-01-18 22:56:34 -07006164
Jens Axboe561fb042019-10-24 07:25:42 -06006165 if (ret) {
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006166 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangcd13f1d2020-12-06 22:22:42 +00006167
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006168 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6169 lock_ctx = req->ctx;
6170
6171 /*
6172 * io_iopoll_complete() does not hold completion_lock to
6173 * complete polled io, so here for polled io, we can not call
6174 * io_req_complete() directly, otherwise there maybe concurrent
6175 * access to cqring, defer_list, etc, which is not safe. Given
6176 * that io_iopoll_complete() is always called under uring_lock,
6177 * so here for polled io, we also get uring_lock to complete
6178 * it.
6179 */
6180 if (lock_ctx)
6181 mutex_lock(&lock_ctx->uring_lock);
6182
6183 req_set_fail_links(req);
6184 io_req_complete(req, ret);
6185
6186 if (lock_ctx)
6187 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006188 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006189
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006190 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006191}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006192
Jens Axboe65e19f52019-10-26 07:20:21 -06006193static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6194 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006195{
Jens Axboe65e19f52019-10-26 07:20:21 -06006196 struct fixed_file_table *table;
6197
Jens Axboe05f3fb32019-12-09 11:22:50 -07006198 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006199 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006200}
6201
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006202static struct file *io_file_get(struct io_submit_state *state,
6203 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006204{
6205 struct io_ring_ctx *ctx = req->ctx;
6206 struct file *file;
6207
6208 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006209 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006210 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006211 fd = array_index_nospec(fd, ctx->nr_user_files);
6212 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006213 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01006214 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06006215 percpu_ref_get(req->fixed_file_refs);
6216 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006217 } else {
6218 trace_io_uring_file_get(ctx, fd);
6219 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006220 }
6221
Pavel Begunkov8c7febf2021-02-09 04:47:47 +00006222 if (file && file->f_op == &io_uring_fops &&
6223 !(req->flags & REQ_F_INFLIGHT)) {
Jens Axboed16692a2021-02-09 04:47:41 +00006224 io_req_init_async(req);
6225 req->flags |= REQ_F_INFLIGHT;
6226
6227 spin_lock_irq(&ctx->inflight_lock);
6228 list_add(&req->inflight_entry, &ctx->inflight_list);
6229 spin_unlock_irq(&ctx->inflight_lock);
6230 }
6231
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006232 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006233}
6234
Jens Axboe3529d8c2019-12-19 18:24:38 -07006235static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006236 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006237{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006238 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006239
Jens Axboe63ff8222020-05-07 14:56:15 -06006240 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006241 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006242 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006243
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006244 req->file = io_file_get(state, req, fd, fixed);
6245 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07006246 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006247 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006248}
6249
Jens Axboe2665abf2019-11-05 12:40:47 -07006250static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6251{
Jens Axboead8a48a2019-11-15 08:49:11 -07006252 struct io_timeout_data *data = container_of(timer,
6253 struct io_timeout_data, timer);
6254 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006255 struct io_ring_ctx *ctx = req->ctx;
6256 struct io_kiocb *prev = NULL;
6257 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006258
6259 spin_lock_irqsave(&ctx->completion_lock, flags);
6260
6261 /*
6262 * We don't expect the list to be empty, that will only happen if we
6263 * race with the completion of the linked work.
6264 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006265 if (!list_empty(&req->link_list)) {
6266 prev = list_entry(req->link_list.prev, struct io_kiocb,
6267 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006268 if (refcount_inc_not_zero(&prev->refs))
Pavel Begunkov44932332019-12-05 16:16:35 +03006269 list_del_init(&req->link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006270 else
Jens Axboe76a46e02019-11-10 23:34:16 -07006271 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006272 }
6273
6274 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6275
6276 if (prev) {
Pavel Begunkov014db002020-03-03 21:33:12 +03006277 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov1c20e902021-03-10 11:30:37 +00006278 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006279 } else {
Pavel Begunkov1c20e902021-03-10 11:30:37 +00006280 io_cqring_add_event(req, -ETIME, 0);
6281 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006282 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006283 return HRTIMER_NORESTART;
6284}
6285
Jens Axboe7271ef32020-08-10 09:55:22 -06006286static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006287{
Jens Axboe76a46e02019-11-10 23:34:16 -07006288 /*
6289 * If the list is now empty, then our linked request finished before
6290 * we got a chance to setup the timer
6291 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006292 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006293 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006294
Jens Axboead8a48a2019-11-15 08:49:11 -07006295 data->timer.function = io_link_timeout_fn;
6296 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6297 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006298 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006299}
6300
6301static void io_queue_linked_timeout(struct io_kiocb *req)
6302{
6303 struct io_ring_ctx *ctx = req->ctx;
6304
6305 spin_lock_irq(&ctx->completion_lock);
6306 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006307 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006308
Jens Axboe2665abf2019-11-05 12:40:47 -07006309 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006310 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006311}
6312
Jens Axboead8a48a2019-11-15 08:49:11 -07006313static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006314{
6315 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006317 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006318 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006319 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006320 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006321
Pavel Begunkov44932332019-12-05 16:16:35 +03006322 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6323 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006324 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006325 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006326
Pavel Begunkov900fad42020-10-19 16:39:16 +01006327 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006328 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006329 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006330}
6331
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006332static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006333{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006334 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006335 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006336 int ret;
6337
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006338again:
6339 linked_timeout = io_prep_linked_timeout(req);
6340
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006341 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6342 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006343 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006344 if (old_creds)
6345 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006346 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006347 old_creds = NULL; /* restored original creds */
6348 else
Jens Axboe98447d62020-10-14 10:48:51 -06006349 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006350 }
6351
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006352 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006353
6354 /*
6355 * We async punt it if the file wasn't marked NOWAIT, or if the file
6356 * doesn't support non-blocking read/write attempts
6357 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006358 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006359 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006360 /*
6361 * Queued up for async execution, worker will release
6362 * submit reference when the iocb is actually submitted.
6363 */
6364 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006366
Pavel Begunkovf063c542020-07-25 14:41:59 +03006367 if (linked_timeout)
6368 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006369 } else if (likely(!ret)) {
6370 /* drop submission reference */
6371 req = io_put_req_find_next(req);
6372 if (linked_timeout)
6373 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006374
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006375 if (req) {
6376 if (!(req->flags & REQ_F_FORCE_ASYNC))
6377 goto again;
6378 io_queue_async_work(req);
6379 }
6380 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006381 /* un-prep timeout, so it'll be killed as any other linked */
6382 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006383 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006384 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006385 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006386 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006387
Jens Axboe193155c2020-02-22 23:22:19 -07006388 if (old_creds)
6389 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390}
6391
Jens Axboef13fad72020-06-22 09:34:30 -06006392static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6393 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006394{
6395 int ret;
6396
Jens Axboe3529d8c2019-12-19 18:24:38 -07006397 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006398 if (ret) {
6399 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006400fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006401 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006402 io_put_req(req);
6403 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006404 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006405 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006406 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006407 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006408 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006409 goto fail_req;
6410 }
Jens Axboece35a472019-12-17 08:04:44 -07006411 io_queue_async_work(req);
6412 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006413 if (sqe) {
6414 ret = io_req_prep(req, sqe);
6415 if (unlikely(ret))
6416 goto fail_req;
6417 }
6418 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006419 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006420}
6421
Jens Axboef13fad72020-06-22 09:34:30 -06006422static inline void io_queue_link_head(struct io_kiocb *req,
6423 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006424{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006425 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006426 io_put_req(req);
6427 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006428 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006429 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006430}
6431
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006432static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006433 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006434{
Jackie Liua197f662019-11-08 08:09:12 -07006435 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006436 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006437
Jens Axboe9e645e112019-05-10 16:07:28 -06006438 /*
6439 * If we already have a head request, queue this one for async
6440 * submittal once the head completes. If we don't have a head but
6441 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6442 * submitted sync once the chain is complete. If none of those
6443 * conditions are true (normal request), then just queue it.
6444 */
6445 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006446 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006447
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006448 /*
6449 * Taking sequential execution of a link, draining both sides
6450 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6451 * requests in the link. So, it drains the head and the
6452 * next after the link request. The last one is done via
6453 * drain_next flag to persist the effect across calls.
6454 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006455 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006456 head->flags |= REQ_F_IO_DRAIN;
6457 ctx->drain_next = 1;
6458 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006459 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006460 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006461 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006462 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006463 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006464 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006465 trace_io_uring_link(ctx, req, head);
6466 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006467
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006468 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006469 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006470 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006471 *link = NULL;
6472 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006473 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006474 if (unlikely(ctx->drain_next)) {
6475 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006476 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006477 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006478 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006479 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006480 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006481
Pavel Begunkov711be032020-01-17 03:57:59 +03006482 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006483 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006484 req->flags |= REQ_F_FAIL_LINK;
6485 *link = req;
6486 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006487 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006488 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006489 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006490
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006491 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006492}
6493
Jens Axboe9a56a232019-01-09 09:06:50 -07006494/*
6495 * Batched submission is done, ensure local IO is flushed out.
6496 */
6497static void io_submit_state_end(struct io_submit_state *state)
6498{
Jens Axboef13fad72020-06-22 09:34:30 -06006499 if (!list_empty(&state->comp.list))
6500 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006501 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006502 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006503 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006504 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006505}
6506
6507/*
6508 * Start submission side cache.
6509 */
6510static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006511 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006512{
6513 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006514 state->comp.nr = 0;
6515 INIT_LIST_HEAD(&state->comp.list);
6516 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006517 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006518 state->file = NULL;
6519 state->ios_left = max_ios;
6520}
6521
Jens Axboe2b188cc2019-01-07 10:46:33 -07006522static void io_commit_sqring(struct io_ring_ctx *ctx)
6523{
Hristo Venev75b28af2019-08-26 17:23:46 +00006524 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006525
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006526 /*
6527 * Ensure any loads from the SQEs are done at this point,
6528 * since once we write the new head, the application could
6529 * write new data to them.
6530 */
6531 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006532}
6533
6534/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006535 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006536 * that is mapped by userspace. This means that care needs to be taken to
6537 * ensure that reads are stable, as we cannot rely on userspace always
6538 * being a good citizen. If members of the sqe are validated and then later
6539 * used, it's important that those reads are done through READ_ONCE() to
6540 * prevent a re-load down the line.
6541 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006542static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006543{
Hristo Venev75b28af2019-08-26 17:23:46 +00006544 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006545 unsigned head;
6546
6547 /*
6548 * The cached sq head (or cq tail) serves two purposes:
6549 *
6550 * 1) allows us to batch the cost of updating the user visible
6551 * head updates.
6552 * 2) allows the kernel side to track the head on its own, even
6553 * though the application is the one updating it.
6554 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006555 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006556 if (likely(head < ctx->sq_entries))
6557 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006558
6559 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006560 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006561 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006562 return NULL;
6563}
6564
6565static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6566{
6567 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006568}
6569
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006570/*
6571 * Check SQE restrictions (opcode and flags).
6572 *
6573 * Returns 'true' if SQE is allowed, 'false' otherwise.
6574 */
6575static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6576 struct io_kiocb *req,
6577 unsigned int sqe_flags)
6578{
6579 if (!ctx->restricted)
6580 return true;
6581
6582 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6583 return false;
6584
6585 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6586 ctx->restrictions.sqe_flags_required)
6587 return false;
6588
6589 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6590 ctx->restrictions.sqe_flags_required))
6591 return false;
6592
6593 return true;
6594}
6595
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006596#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6597 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6598 IOSQE_BUFFER_SELECT)
6599
6600static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6601 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006602 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006603{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006604 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006605 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006606
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006607 req->opcode = READ_ONCE(sqe->opcode);
6608 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006609 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006610 req->file = NULL;
6611 req->ctx = ctx;
6612 req->flags = 0;
6613 /* one is dropped after submission, the other at completion */
6614 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006615 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006616 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006617
6618 if (unlikely(req->opcode >= IORING_OP_LAST))
6619 return -EINVAL;
6620
Jens Axboe9d8426a2020-06-16 18:42:49 -06006621 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6622 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006623
6624 sqe_flags = READ_ONCE(sqe->flags);
6625 /* enforce forwards compatibility on users */
6626 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6627 return -EINVAL;
6628
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006629 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6630 return -EACCES;
6631
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006632 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6633 !io_op_defs[req->opcode].buffer_select)
6634 return -EOPNOTSUPP;
6635
6636 id = READ_ONCE(sqe->personality);
6637 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006638 struct io_identity *iod;
6639
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08006640 iod = xa_load(&ctx->personalities, id);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006641 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006642 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006643 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006644
6645 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006646 get_cred(iod->creds);
6647 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006648 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006649 }
6650
6651 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006652 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006653
Jens Axboe63ff8222020-05-07 14:56:15 -06006654 if (!io_op_defs[req->opcode].needs_file)
6655 return 0;
6656
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006657 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6658 state->ios_left--;
6659 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006660}
6661
Jens Axboe0f212202020-09-13 13:09:39 -06006662static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006663{
Jens Axboeac8691c2020-06-01 08:30:41 -06006664 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006665 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006666 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006667
Jens Axboec4a2ed72019-11-21 21:01:26 -07006668 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006669 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006670 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006671 return -EBUSY;
6672 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006673
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006674 /* make sure SQ entry isn't read before tail */
6675 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006676
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006677 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6678 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006679
Jens Axboed8a6df12020-10-15 16:24:45 -06006680 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006681 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006682
Jens Axboe6c271ce2019-01-10 11:22:30 -07006683 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006684
Jens Axboe6c271ce2019-01-10 11:22:30 -07006685 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006686 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006687 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006688 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006689
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006690 sqe = io_get_sqe(ctx);
6691 if (unlikely(!sqe)) {
6692 io_consume_sqe(ctx);
6693 break;
6694 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006695 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006696 if (unlikely(!req)) {
6697 if (!submitted)
6698 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006699 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006700 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006701 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006702 /* will complete beyond this point, count as submitted */
6703 submitted++;
6704
Pavel Begunkov692d8362020-10-10 18:34:13 +01006705 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006706 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006707fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006708 io_put_req(req);
6709 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006710 break;
6711 }
6712
Jens Axboe354420f2020-01-08 18:55:15 -07006713 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006714 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006715 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006716 if (err)
6717 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006718 }
6719
Pavel Begunkov9466f432020-01-25 22:34:01 +03006720 if (unlikely(submitted != nr)) {
6721 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006722 struct io_uring_task *tctx = current->io_uring;
6723 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006724
Jens Axboed8a6df12020-10-15 16:24:45 -06006725 percpu_ref_put_many(&ctx->refs, unused);
6726 percpu_counter_sub(&tctx->inflight, unused);
6727 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006728 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006729 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006730 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006731 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006732
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006733 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6734 io_commit_sqring(ctx);
6735
Jens Axboe6c271ce2019-01-10 11:22:30 -07006736 return submitted;
6737}
6738
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006739static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6740{
6741 /* Tell userspace we may need a wakeup call */
6742 spin_lock_irq(&ctx->completion_lock);
6743 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6744 spin_unlock_irq(&ctx->completion_lock);
6745}
6746
6747static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6748{
6749 spin_lock_irq(&ctx->completion_lock);
6750 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6751 spin_unlock_irq(&ctx->completion_lock);
6752}
6753
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006754static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6755 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006756{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006757 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6758 int ret;
6759
6760 ret = autoremove_wake_function(wqe, mode, sync, key);
6761 if (ret) {
6762 unsigned long flags;
6763
6764 spin_lock_irqsave(&ctx->completion_lock, flags);
6765 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6766 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6767 }
6768 return ret;
6769}
6770
Jens Axboec8d1ba52020-09-14 11:07:26 -06006771enum sq_ret {
6772 SQT_IDLE = 1,
6773 SQT_SPIN = 2,
6774 SQT_DID_WORK = 4,
6775};
6776
6777static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006778 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006779{
6780 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006781 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006782 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006783 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006784
Jens Axboec8d1ba52020-09-14 11:07:26 -06006785again:
6786 if (!list_empty(&ctx->iopoll_list)) {
6787 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006788
Jens Axboec8d1ba52020-09-14 11:07:26 -06006789 mutex_lock(&ctx->uring_lock);
6790 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6791 io_do_iopoll(ctx, &nr_events, 0);
6792 mutex_unlock(&ctx->uring_lock);
6793 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006794
Jens Axboec8d1ba52020-09-14 11:07:26 -06006795 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006796
Jens Axboec8d1ba52020-09-14 11:07:26 -06006797 /*
6798 * If submit got -EBUSY, flag us as needing the application
6799 * to enter the kernel to reap and flush events.
6800 */
6801 if (!to_submit || ret == -EBUSY || need_resched()) {
6802 /*
6803 * Drop cur_mm before scheduling, we can't hold it for
6804 * long periods (or over schedule()). Do this before
6805 * adding ourselves to the waitqueue, as the unuse/drop
6806 * may sleep.
6807 */
6808 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006809
Jens Axboec8d1ba52020-09-14 11:07:26 -06006810 /*
6811 * We're polling. If we're within the defined idle
6812 * period, then let us spin without work before going
6813 * to sleep. The exception is if we got EBUSY doing
6814 * more IO, we should wait for the application to
6815 * reap events and wake us up.
6816 */
6817 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6818 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6819 !percpu_ref_is_dying(&ctx->refs)))
6820 return SQT_SPIN;
6821
Jens Axboe534ca6d2020-09-02 13:52:19 -06006822 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006823 TASK_INTERRUPTIBLE);
6824
6825 /*
6826 * While doing polled IO, before going to sleep, we need
6827 * to check if there are new reqs added to iopoll_list,
6828 * it is because reqs may have been punted to io worker
6829 * and will be added to iopoll_list later, hence check
6830 * the iopoll_list again.
6831 */
6832 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6833 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006834 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006835 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006836 }
6837
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006838 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006839 if (!to_submit || ret == -EBUSY)
6840 return SQT_IDLE;
6841 }
6842
Jens Axboe534ca6d2020-09-02 13:52:19 -06006843 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006844 io_ring_clear_wakeup_flag(ctx);
6845
Jens Axboee95eee22020-09-08 09:11:32 -06006846 /* if we're handling multiple rings, cap submit size for fairness */
6847 if (cap_entries && to_submit > 8)
6848 to_submit = 8;
6849
Jens Axboec8d1ba52020-09-14 11:07:26 -06006850 mutex_lock(&ctx->uring_lock);
Pavel Begunkova63d9152021-01-26 11:17:03 +00006851 if (likely(!percpu_ref_is_dying(&ctx->refs) && !ctx->sqo_dead))
Jens Axboec8d1ba52020-09-14 11:07:26 -06006852 ret = io_submit_sqes(ctx, to_submit);
6853 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006854
6855 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6856 wake_up(&ctx->sqo_sq_wait);
6857
Jens Axboec8d1ba52020-09-14 11:07:26 -06006858 return SQT_DID_WORK;
6859}
6860
Jens Axboe69fb2132020-09-14 11:16:23 -06006861static void io_sqd_init_new(struct io_sq_data *sqd)
6862{
6863 struct io_ring_ctx *ctx;
6864
6865 while (!list_empty(&sqd->ctx_new_list)) {
6866 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6867 init_wait(&ctx->sqo_wait_entry);
6868 ctx->sqo_wait_entry.func = io_sq_wake_function;
6869 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6870 complete(&ctx->sq_thread_comp);
6871 }
6872}
6873
Jens Axboe6c271ce2019-01-10 11:22:30 -07006874static int io_sq_thread(void *data)
6875{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006876 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006877 const struct cred *old_cred = NULL;
6878 struct io_sq_data *sqd = data;
6879 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006880 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006881
Jens Axboec8d1ba52020-09-14 11:07:26 -06006882 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006883 while (!kthread_should_stop()) {
6884 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006885 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006886
6887 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006888 * Any changes to the sqd lists are synchronized through the
6889 * kthread parking. This synchronizes the thread vs users,
6890 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006891 */
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006892 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006893 kthread_parkme();
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006894 /*
6895 * When sq thread is unparked, in case the previous park operation
6896 * comes from io_put_sq_data(), which means that sq thread is going
6897 * to be stopped, so here needs to have a check.
6898 */
6899 if (kthread_should_stop())
6900 break;
6901 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006902
6903 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6904 io_sqd_init_new(sqd);
6905
Jens Axboee95eee22020-09-08 09:11:32 -06006906 cap_entries = !list_is_singular(&sqd->ctx_list);
6907
Jens Axboe69fb2132020-09-14 11:16:23 -06006908 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6909 if (current->cred != ctx->creds) {
6910 if (old_cred)
6911 revert_creds(old_cred);
6912 old_cred = override_creds(ctx->creds);
6913 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006914 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006915#ifdef CONFIG_AUDIT
6916 current->loginuid = ctx->loginuid;
6917 current->sessionid = ctx->sessionid;
6918#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06006919
Jens Axboee95eee22020-09-08 09:11:32 -06006920 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006921
Jens Axboe4349f302020-07-09 15:07:01 -06006922 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006923 }
6924
Jens Axboe69fb2132020-09-14 11:16:23 -06006925 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006926 io_run_task_work();
Pavel Begunkovf7f32822021-01-11 04:00:30 +00006927 io_sq_thread_drop_mm();
Jens Axboec8d1ba52020-09-14 11:07:26 -06006928 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006929 } else if (ret == SQT_IDLE) {
6930 if (kthread_should_park())
6931 continue;
6932 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6933 io_ring_set_wakeup_flag(ctx);
6934 schedule();
6935 start_jiffies = jiffies;
6936 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6937 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006938 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006939 }
6940
Jens Axboe4c6e2772020-07-01 11:29:10 -06006941 io_run_task_work();
Pavel Begunkovf7f32822021-01-11 04:00:30 +00006942 io_sq_thread_drop_mm();
Jens Axboeb41e9852020-02-17 09:52:41 -07006943
Dennis Zhou91d8f512020-09-16 13:41:05 -07006944 if (cur_css)
6945 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006946 if (old_cred)
6947 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006948
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006949 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006950
Jens Axboe6c271ce2019-01-10 11:22:30 -07006951 return 0;
6952}
6953
Jens Axboebda52162019-09-24 13:47:15 -06006954struct io_wait_queue {
6955 struct wait_queue_entry wq;
6956 struct io_ring_ctx *ctx;
6957 unsigned to_wait;
6958 unsigned nr_timeouts;
6959};
6960
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006961static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006962{
6963 struct io_ring_ctx *ctx = iowq->ctx;
6964
6965 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006966 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006967 * started waiting. For timeouts, we always want to return to userspace,
6968 * regardless of event count.
6969 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006970 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006971 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6972}
6973
6974static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6975 int wake_flags, void *key)
6976{
6977 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6978 wq);
6979
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006980 /*
6981 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6982 * the task, and the next invocation will do it.
6983 */
6984 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6985 return autoremove_wake_function(curr, mode, wake_flags, key);
6986 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006987}
6988
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006989static int io_run_task_work_sig(void)
6990{
6991 if (io_run_task_work())
6992 return 1;
6993 if (!signal_pending(current))
6994 return 0;
6995 if (current->jobctl & JOBCTL_TASK_WORK) {
6996 spin_lock_irq(&current->sighand->siglock);
6997 current->jobctl &= ~JOBCTL_TASK_WORK;
6998 recalc_sigpending();
6999 spin_unlock_irq(&current->sighand->siglock);
7000 return 1;
7001 }
7002 return -EINTR;
7003}
7004
Jens Axboe2b188cc2019-01-07 10:46:33 -07007005/*
7006 * Wait until events become available, if we don't already have some. The
7007 * application must reap them itself, as they reside on the shared cq ring.
7008 */
7009static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
7010 const sigset_t __user *sig, size_t sigsz)
7011{
Jens Axboebda52162019-09-24 13:47:15 -06007012 struct io_wait_queue iowq = {
7013 .wq = {
7014 .private = current,
7015 .func = io_wake_function,
7016 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7017 },
7018 .ctx = ctx,
7019 .to_wait = min_events,
7020 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007021 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08007022 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007023
Jens Axboeb41e9852020-02-17 09:52:41 -07007024 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007025 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7026 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007027 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007028 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007029 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007030 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007031
7032 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007033#ifdef CONFIG_COMPAT
7034 if (in_compat_syscall())
7035 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007036 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007037 else
7038#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007039 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007040
Jens Axboe2b188cc2019-01-07 10:46:33 -07007041 if (ret)
7042 return ret;
7043 }
7044
Jens Axboebda52162019-09-24 13:47:15 -06007045 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007046 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007047 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007048 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007049 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7050 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007051 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007052 ret = io_run_task_work_sig();
Hao Xu7250f332021-02-09 04:47:46 +00007053 if (ret > 0) {
7054 finish_wait(&ctx->wait, &iowq.wq);
Jens Axboe4c6e2772020-07-01 11:29:10 -06007055 continue;
Hao Xu7250f332021-02-09 04:47:46 +00007056 }
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007057 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007058 break;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007059 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007060 break;
Hao Xu7250f332021-02-09 04:47:46 +00007061 if (test_bit(0, &ctx->cq_check_overflow)) {
7062 finish_wait(&ctx->wait, &iowq.wq);
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007063 continue;
Hao Xu7250f332021-02-09 04:47:46 +00007064 }
Jens Axboebda52162019-09-24 13:47:15 -06007065 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06007066 } while (1);
7067 finish_wait(&ctx->wait, &iowq.wq);
7068
Jens Axboeb7db41c2020-07-04 08:55:50 -06007069 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007070
Hristo Venev75b28af2019-08-26 17:23:46 +00007071 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007072}
7073
Jens Axboe6b063142019-01-10 22:13:58 -07007074static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7075{
7076#if defined(CONFIG_UNIX)
7077 if (ctx->ring_sock) {
7078 struct sock *sock = ctx->ring_sock->sk;
7079 struct sk_buff *skb;
7080
7081 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7082 kfree_skb(skb);
7083 }
7084#else
7085 int i;
7086
Jens Axboe65e19f52019-10-26 07:20:21 -06007087 for (i = 0; i < ctx->nr_user_files; i++) {
7088 struct file *file;
7089
7090 file = io_file_from_index(ctx, i);
7091 if (file)
7092 fput(file);
7093 }
Jens Axboe6b063142019-01-10 22:13:58 -07007094#endif
7095}
7096
Jens Axboe05f3fb32019-12-09 11:22:50 -07007097static void io_file_ref_kill(struct percpu_ref *ref)
7098{
7099 struct fixed_file_data *data;
7100
7101 data = container_of(ref, struct fixed_file_data, refs);
7102 complete(&data->done);
7103}
7104
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007105static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7106 struct fixed_file_ref_node *ref_node)
7107{
7108 spin_lock_bh(&file_data->lock);
7109 file_data->node = ref_node;
7110 list_add_tail(&ref_node->node, &file_data->ref_list);
7111 spin_unlock_bh(&file_data->lock);
7112 percpu_ref_get(&file_data->refs);
7113}
7114
Jens Axboe6b063142019-01-10 22:13:58 -07007115static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7116{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007117 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007118 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007119 unsigned nr_tables, i;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007120 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007121
Jens Axboe05f3fb32019-12-09 11:22:50 -07007122 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007123 return -ENXIO;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007124 backup_node = alloc_fixed_file_ref_node(ctx);
7125 if (!backup_node)
7126 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007127
Jens Axboe25a2de62020-11-23 09:37:51 -07007128 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007129 ref_node = data->node;
Jens Axboe25a2de62020-11-23 09:37:51 -07007130 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007131 if (ref_node)
7132 percpu_ref_kill(&ref_node->refs);
7133
7134 percpu_ref_kill(&data->refs);
7135
7136 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007137 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007138 do {
7139 ret = wait_for_completion_interruptible(&data->done);
7140 if (!ret)
7141 break;
7142 ret = io_run_task_work_sig();
7143 if (ret < 0) {
7144 percpu_ref_resurrect(&data->refs);
7145 reinit_completion(&data->done);
7146 io_sqe_files_set_node(data, backup_node);
7147 return ret;
7148 }
7149 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007150
Jens Axboe6b063142019-01-10 22:13:58 -07007151 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007152 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7153 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007154 kfree(data->table[i].files);
7155 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007156 percpu_ref_exit(&data->refs);
7157 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007158 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007159 ctx->nr_user_files = 0;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007160 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007161 return 0;
7162}
7163
Jens Axboe534ca6d2020-09-02 13:52:19 -06007164static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007165{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007166 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007167 /*
7168 * The park is a bit of a work-around, without it we get
7169 * warning spews on shutdown with SQPOLL set and affinity
7170 * set to a single CPU.
7171 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007172 if (sqd->thread) {
7173 kthread_park(sqd->thread);
7174 kthread_stop(sqd->thread);
7175 }
7176
7177 kfree(sqd);
7178 }
7179}
7180
Jens Axboeaa061652020-09-02 14:50:27 -06007181static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7182{
7183 struct io_ring_ctx *ctx_attach;
7184 struct io_sq_data *sqd;
7185 struct fd f;
7186
7187 f = fdget(p->wq_fd);
7188 if (!f.file)
7189 return ERR_PTR(-ENXIO);
7190 if (f.file->f_op != &io_uring_fops) {
7191 fdput(f);
7192 return ERR_PTR(-EINVAL);
7193 }
7194
7195 ctx_attach = f.file->private_data;
7196 sqd = ctx_attach->sq_data;
7197 if (!sqd) {
7198 fdput(f);
7199 return ERR_PTR(-EINVAL);
7200 }
7201
7202 refcount_inc(&sqd->refs);
7203 fdput(f);
7204 return sqd;
7205}
7206
Jens Axboe534ca6d2020-09-02 13:52:19 -06007207static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7208{
7209 struct io_sq_data *sqd;
7210
Jens Axboeaa061652020-09-02 14:50:27 -06007211 if (p->flags & IORING_SETUP_ATTACH_WQ)
7212 return io_attach_sq_data(p);
7213
Jens Axboe534ca6d2020-09-02 13:52:19 -06007214 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7215 if (!sqd)
7216 return ERR_PTR(-ENOMEM);
7217
7218 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007219 INIT_LIST_HEAD(&sqd->ctx_list);
7220 INIT_LIST_HEAD(&sqd->ctx_new_list);
7221 mutex_init(&sqd->ctx_lock);
7222 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007223 init_waitqueue_head(&sqd->wait);
7224 return sqd;
7225}
7226
Jens Axboe69fb2132020-09-14 11:16:23 -06007227static void io_sq_thread_unpark(struct io_sq_data *sqd)
7228 __releases(&sqd->lock)
7229{
7230 if (!sqd->thread)
7231 return;
7232 kthread_unpark(sqd->thread);
7233 mutex_unlock(&sqd->lock);
7234}
7235
7236static void io_sq_thread_park(struct io_sq_data *sqd)
7237 __acquires(&sqd->lock)
7238{
7239 if (!sqd->thread)
7240 return;
7241 mutex_lock(&sqd->lock);
7242 kthread_park(sqd->thread);
7243}
7244
Jens Axboe534ca6d2020-09-02 13:52:19 -06007245static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7246{
7247 struct io_sq_data *sqd = ctx->sq_data;
7248
7249 if (sqd) {
7250 if (sqd->thread) {
7251 /*
7252 * We may arrive here from the error branch in
7253 * io_sq_offload_create() where the kthread is created
7254 * without being waked up, thus wake it up now to make
7255 * sure the wait will complete.
7256 */
7257 wake_up_process(sqd->thread);
7258 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007259
7260 io_sq_thread_park(sqd);
7261 }
7262
7263 mutex_lock(&sqd->ctx_lock);
7264 list_del(&ctx->sqd_list);
7265 mutex_unlock(&sqd->ctx_lock);
7266
7267 if (sqd->thread) {
7268 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7269 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007270 }
7271
7272 io_put_sq_data(sqd);
7273 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007274 }
7275}
7276
Jens Axboe6b063142019-01-10 22:13:58 -07007277static void io_finish_async(struct io_ring_ctx *ctx)
7278{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007279 io_sq_thread_stop(ctx);
7280
Jens Axboe561fb042019-10-24 07:25:42 -06007281 if (ctx->io_wq) {
7282 io_wq_destroy(ctx->io_wq);
7283 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007284 }
7285}
7286
7287#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007288/*
7289 * Ensure the UNIX gc is aware of our file set, so we are certain that
7290 * the io_uring can be safely unregistered on process exit, even if we have
7291 * loops in the file referencing.
7292 */
7293static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7294{
7295 struct sock *sk = ctx->ring_sock->sk;
7296 struct scm_fp_list *fpl;
7297 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007298 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007299
Jens Axboe6b063142019-01-10 22:13:58 -07007300 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7301 if (!fpl)
7302 return -ENOMEM;
7303
7304 skb = alloc_skb(0, GFP_KERNEL);
7305 if (!skb) {
7306 kfree(fpl);
7307 return -ENOMEM;
7308 }
7309
7310 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007311
Jens Axboe08a45172019-10-03 08:11:03 -06007312 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007313 fpl->user = get_uid(ctx->user);
7314 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007315 struct file *file = io_file_from_index(ctx, i + offset);
7316
7317 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007318 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007319 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007320 unix_inflight(fpl->user, fpl->fp[nr_files]);
7321 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007322 }
7323
Jens Axboe08a45172019-10-03 08:11:03 -06007324 if (nr_files) {
7325 fpl->max = SCM_MAX_FD;
7326 fpl->count = nr_files;
7327 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007328 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007329 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7330 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007331
Jens Axboe08a45172019-10-03 08:11:03 -06007332 for (i = 0; i < nr_files; i++)
7333 fput(fpl->fp[i]);
7334 } else {
7335 kfree_skb(skb);
7336 kfree(fpl);
7337 }
Jens Axboe6b063142019-01-10 22:13:58 -07007338
7339 return 0;
7340}
7341
7342/*
7343 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7344 * causes regular reference counting to break down. We rely on the UNIX
7345 * garbage collection to take care of this problem for us.
7346 */
7347static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7348{
7349 unsigned left, total;
7350 int ret = 0;
7351
7352 total = 0;
7353 left = ctx->nr_user_files;
7354 while (left) {
7355 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007356
7357 ret = __io_sqe_files_scm(ctx, this_files, total);
7358 if (ret)
7359 break;
7360 left -= this_files;
7361 total += this_files;
7362 }
7363
7364 if (!ret)
7365 return 0;
7366
7367 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007368 struct file *file = io_file_from_index(ctx, total);
7369
7370 if (file)
7371 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007372 total++;
7373 }
7374
7375 return ret;
7376}
7377#else
7378static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7379{
7380 return 0;
7381}
7382#endif
7383
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007384static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7385 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007386{
7387 int i;
7388
7389 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007390 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007391 unsigned this_files;
7392
7393 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7394 table->files = kcalloc(this_files, sizeof(struct file *),
Pavel Begunkova3ed34b2021-09-13 09:37:00 -06007395 GFP_KERNEL_ACCOUNT);
Jens Axboe65e19f52019-10-26 07:20:21 -06007396 if (!table->files)
7397 break;
7398 nr_files -= this_files;
7399 }
7400
7401 if (i == nr_tables)
7402 return 0;
7403
7404 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007405 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007406 kfree(table->files);
7407 }
7408 return 1;
7409}
7410
Jens Axboe05f3fb32019-12-09 11:22:50 -07007411static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007412{
7413#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007414 struct sock *sock = ctx->ring_sock->sk;
7415 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7416 struct sk_buff *skb;
7417 int i;
7418
7419 __skb_queue_head_init(&list);
7420
7421 /*
7422 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7423 * remove this entry and rearrange the file array.
7424 */
7425 skb = skb_dequeue(head);
7426 while (skb) {
7427 struct scm_fp_list *fp;
7428
7429 fp = UNIXCB(skb).fp;
7430 for (i = 0; i < fp->count; i++) {
7431 int left;
7432
7433 if (fp->fp[i] != file)
7434 continue;
7435
7436 unix_notinflight(fp->user, fp->fp[i]);
7437 left = fp->count - 1 - i;
7438 if (left) {
7439 memmove(&fp->fp[i], &fp->fp[i + 1],
7440 left * sizeof(struct file *));
7441 }
7442 fp->count--;
7443 if (!fp->count) {
7444 kfree_skb(skb);
7445 skb = NULL;
7446 } else {
7447 __skb_queue_tail(&list, skb);
7448 }
7449 fput(file);
7450 file = NULL;
7451 break;
7452 }
7453
7454 if (!file)
7455 break;
7456
7457 __skb_queue_tail(&list, skb);
7458
7459 skb = skb_dequeue(head);
7460 }
7461
7462 if (skb_peek(&list)) {
7463 spin_lock_irq(&head->lock);
7464 while ((skb = __skb_dequeue(&list)) != NULL)
7465 __skb_queue_tail(head, skb);
7466 spin_unlock_irq(&head->lock);
7467 }
7468#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007469 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007470#endif
7471}
7472
Jens Axboe05f3fb32019-12-09 11:22:50 -07007473struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007474 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007475 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007476};
7477
Jens Axboe4a38aed22020-05-14 17:21:15 -06007478static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007479{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007480 struct fixed_file_data *file_data = ref_node->file_data;
7481 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007482 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007483
7484 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007485 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007486 io_ring_file_put(ctx, pfile->file);
7487 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007488 }
7489
Xiaoguang Wang05589552020-03-31 14:05:18 +08007490 percpu_ref_exit(&ref_node->refs);
7491 kfree(ref_node);
7492 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007493}
7494
Jens Axboe4a38aed22020-05-14 17:21:15 -06007495static void io_file_put_work(struct work_struct *work)
7496{
7497 struct io_ring_ctx *ctx;
7498 struct llist_node *node;
7499
7500 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7501 node = llist_del_all(&ctx->file_put_llist);
7502
7503 while (node) {
7504 struct fixed_file_ref_node *ref_node;
7505 struct llist_node *next = node->next;
7506
7507 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7508 __io_file_put_work(ref_node);
7509 node = next;
7510 }
7511}
7512
Jens Axboe05f3fb32019-12-09 11:22:50 -07007513static void io_file_data_ref_zero(struct percpu_ref *ref)
7514{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007515 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007516 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007517 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007518 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007519 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007520
Xiaoguang Wang05589552020-03-31 14:05:18 +08007521 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007522 data = ref_node->file_data;
7523 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007524
Jens Axboe25a2de62020-11-23 09:37:51 -07007525 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007526 ref_node->done = true;
7527
7528 while (!list_empty(&data->ref_list)) {
7529 ref_node = list_first_entry(&data->ref_list,
7530 struct fixed_file_ref_node, node);
7531 /* recycle ref nodes in order */
7532 if (!ref_node->done)
7533 break;
7534 list_del(&ref_node->node);
7535 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7536 }
Jens Axboe25a2de62020-11-23 09:37:51 -07007537 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007538
7539 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007540 delay = 0;
7541
Jens Axboe4a38aed22020-05-14 17:21:15 -06007542 if (!delay)
7543 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7544 else if (first_add)
7545 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007546}
7547
7548static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7549 struct io_ring_ctx *ctx)
7550{
7551 struct fixed_file_ref_node *ref_node;
7552
7553 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7554 if (!ref_node)
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007555 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007556
7557 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7558 0, GFP_KERNEL)) {
7559 kfree(ref_node);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007560 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007561 }
7562 INIT_LIST_HEAD(&ref_node->node);
7563 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007564 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007565 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007566 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007567}
7568
7569static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7570{
7571 percpu_ref_exit(&ref_node->refs);
7572 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007573}
7574
7575static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7576 unsigned nr_args)
7577{
7578 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007579 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007580 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007581 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007582 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007583 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007584
7585 if (ctx->file_data)
7586 return -EBUSY;
7587 if (!nr_args)
7588 return -EINVAL;
7589 if (nr_args > IORING_MAX_FIXED_FILES)
7590 return -EMFILE;
Pavel Begunkov5103b732021-09-13 09:35:35 -06007591 if (nr_args > rlimit(RLIMIT_NOFILE))
7592 return -EMFILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007593
Pavel Begunkova3ed34b2021-09-13 09:37:00 -06007594 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL_ACCOUNT);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007595 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007596 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007597 file_data->ctx = ctx;
7598 init_completion(&file_data->done);
7599 INIT_LIST_HEAD(&file_data->ref_list);
7600 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007601
7602 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007603 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkova3ed34b2021-09-13 09:37:00 -06007604 GFP_KERNEL_ACCOUNT);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007605 if (!file_data->table)
7606 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007607
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007608 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007609 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7610 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007611
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007612 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7613 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007614 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007615
7616 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7617 struct fixed_file_table *table;
7618 unsigned index;
7619
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007620 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7621 ret = -EFAULT;
7622 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007623 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007624 /* allow sparse sets */
7625 if (fd == -1)
7626 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007627
Jens Axboe05f3fb32019-12-09 11:22:50 -07007628 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007629 ret = -EBADF;
7630 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007631 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007632
7633 /*
7634 * Don't allow io_uring instances to be registered. If UNIX
7635 * isn't enabled, then this causes a reference cycle and this
7636 * instance can never get freed. If UNIX is enabled we'll
7637 * handle it just fine, but there's still no point in allowing
7638 * a ring fd as it doesn't support regular read/write anyway.
7639 */
7640 if (file->f_op == &io_uring_fops) {
7641 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007642 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007643 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007644 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7645 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007646 table->files[index] = file;
7647 }
7648
Jens Axboe05f3fb32019-12-09 11:22:50 -07007649 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007650 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007651 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007652 return ret;
7653 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007654
Xiaoguang Wang05589552020-03-31 14:05:18 +08007655 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007656 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007657 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007658 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007659 }
7660
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007661 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007662 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007663out_fput:
7664 for (i = 0; i < ctx->nr_user_files; i++) {
7665 file = io_file_from_index(ctx, i);
7666 if (file)
7667 fput(file);
7668 }
7669 for (i = 0; i < nr_tables; i++)
7670 kfree(file_data->table[i].files);
7671 ctx->nr_user_files = 0;
7672out_ref:
7673 percpu_ref_exit(&file_data->refs);
7674out_free:
7675 kfree(file_data->table);
7676 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007677 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007678 return ret;
7679}
7680
Jens Axboec3a31e62019-10-03 13:59:56 -06007681static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7682 int index)
7683{
7684#if defined(CONFIG_UNIX)
7685 struct sock *sock = ctx->ring_sock->sk;
7686 struct sk_buff_head *head = &sock->sk_receive_queue;
7687 struct sk_buff *skb;
7688
7689 /*
7690 * See if we can merge this file into an existing skb SCM_RIGHTS
7691 * file set. If there's no room, fall back to allocating a new skb
7692 * and filling it in.
7693 */
7694 spin_lock_irq(&head->lock);
7695 skb = skb_peek(head);
7696 if (skb) {
7697 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7698
7699 if (fpl->count < SCM_MAX_FD) {
7700 __skb_unlink(skb, head);
7701 spin_unlock_irq(&head->lock);
7702 fpl->fp[fpl->count] = get_file(file);
7703 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7704 fpl->count++;
7705 spin_lock_irq(&head->lock);
7706 __skb_queue_head(head, skb);
7707 } else {
7708 skb = NULL;
7709 }
7710 }
7711 spin_unlock_irq(&head->lock);
7712
7713 if (skb) {
7714 fput(file);
7715 return 0;
7716 }
7717
7718 return __io_sqe_files_scm(ctx, 1, index);
7719#else
7720 return 0;
7721#endif
7722}
7723
Hillf Dantona5318d32020-03-23 17:47:15 +08007724static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007725 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007726{
Hillf Dantona5318d32020-03-23 17:47:15 +08007727 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007728 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007729
Jens Axboe05f3fb32019-12-09 11:22:50 -07007730 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007731 if (!pfile)
7732 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733
7734 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007735 list_add(&pfile->list, &ref_node->file_list);
7736
Hillf Dantona5318d32020-03-23 17:47:15 +08007737 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007738}
7739
7740static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7741 struct io_uring_files_update *up,
7742 unsigned nr_args)
7743{
7744 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007745 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007746 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007747 __s32 __user *fds;
7748 int fd, i, err;
7749 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007750 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007751
Jens Axboe05f3fb32019-12-09 11:22:50 -07007752 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007753 return -EOVERFLOW;
7754 if (done > ctx->nr_user_files)
7755 return -EINVAL;
7756
Xiaoguang Wang05589552020-03-31 14:05:18 +08007757 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007758 if (!ref_node)
7759 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007760
Jens Axboec3a31e62019-10-03 13:59:56 -06007761 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007762 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007763 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007764 struct fixed_file_table *table;
7765 unsigned index;
7766
Jens Axboec3a31e62019-10-03 13:59:56 -06007767 err = 0;
7768 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7769 err = -EFAULT;
7770 break;
7771 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007772 i = array_index_nospec(up->offset, ctx->nr_user_files);
7773 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007774 index = i & IORING_FILE_TABLE_MASK;
7775 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007776 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007777 err = io_queue_file_removal(data, file);
7778 if (err)
7779 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007780 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007781 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007782 }
7783 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007784 file = fget(fd);
7785 if (!file) {
7786 err = -EBADF;
7787 break;
7788 }
7789 /*
7790 * Don't allow io_uring instances to be registered. If
7791 * UNIX isn't enabled, then this causes a reference
7792 * cycle and this instance can never get freed. If UNIX
7793 * is enabled we'll handle it just fine, but there's
7794 * still no point in allowing a ring fd as it doesn't
7795 * support regular read/write anyway.
7796 */
7797 if (file->f_op == &io_uring_fops) {
7798 fput(file);
7799 err = -EBADF;
7800 break;
7801 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007802 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007803 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007804 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007805 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007806 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007807 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007808 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007809 }
7810 nr_args--;
7811 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007812 up->offset++;
7813 }
7814
Xiaoguang Wang05589552020-03-31 14:05:18 +08007815 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007816 percpu_ref_kill(&data->node->refs);
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007817 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007818 } else
7819 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007820
7821 return done ? done : err;
7822}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007823
Jens Axboe05f3fb32019-12-09 11:22:50 -07007824static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7825 unsigned nr_args)
7826{
7827 struct io_uring_files_update up;
7828
7829 if (!ctx->file_data)
7830 return -ENXIO;
7831 if (!nr_args)
7832 return -EINVAL;
7833 if (copy_from_user(&up, arg, sizeof(up)))
7834 return -EFAULT;
7835 if (up.resv)
7836 return -EINVAL;
7837
7838 return __io_sqe_files_update(ctx, &up, nr_args);
7839}
Jens Axboec3a31e62019-10-03 13:59:56 -06007840
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007841static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007842{
7843 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7844
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007845 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007846 io_put_req(req);
7847}
7848
Pavel Begunkov24369c22020-01-28 03:15:48 +03007849static int io_init_wq_offload(struct io_ring_ctx *ctx,
7850 struct io_uring_params *p)
7851{
7852 struct io_wq_data data;
7853 struct fd f;
7854 struct io_ring_ctx *ctx_attach;
7855 unsigned int concurrency;
7856 int ret = 0;
7857
7858 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007859 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007860 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007861
7862 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7863 /* Do QD, or 4 * CPUS, whatever is smallest */
7864 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7865
7866 ctx->io_wq = io_wq_create(concurrency, &data);
7867 if (IS_ERR(ctx->io_wq)) {
7868 ret = PTR_ERR(ctx->io_wq);
7869 ctx->io_wq = NULL;
7870 }
7871 return ret;
7872 }
7873
7874 f = fdget(p->wq_fd);
7875 if (!f.file)
7876 return -EBADF;
7877
7878 if (f.file->f_op != &io_uring_fops) {
7879 ret = -EINVAL;
7880 goto out_fput;
7881 }
7882
7883 ctx_attach = f.file->private_data;
7884 /* @io_wq is protected by holding the fd */
7885 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7886 ret = -EINVAL;
7887 goto out_fput;
7888 }
7889
7890 ctx->io_wq = ctx_attach->io_wq;
7891out_fput:
7892 fdput(f);
7893 return ret;
7894}
7895
Jens Axboe0f212202020-09-13 13:09:39 -06007896static int io_uring_alloc_task_context(struct task_struct *task)
7897{
7898 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007899 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007900
7901 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7902 if (unlikely(!tctx))
7903 return -ENOMEM;
7904
Jens Axboed8a6df12020-10-15 16:24:45 -06007905 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7906 if (unlikely(ret)) {
7907 kfree(tctx);
7908 return ret;
7909 }
7910
Jens Axboe0f212202020-09-13 13:09:39 -06007911 xa_init(&tctx->xa);
7912 init_waitqueue_head(&tctx->wait);
7913 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007914 atomic_set(&tctx->in_idle, 0);
7915 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06007916 io_init_identity(&tctx->__identity);
7917 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06007918 task->io_uring = tctx;
7919 return 0;
7920}
7921
7922void __io_uring_free(struct task_struct *tsk)
7923{
7924 struct io_uring_task *tctx = tsk->io_uring;
7925
7926 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06007927 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
7928 if (tctx->identity != &tctx->__identity)
7929 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06007930 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007931 kfree(tctx);
7932 tsk->io_uring = NULL;
7933}
7934
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007935static int io_sq_offload_create(struct io_ring_ctx *ctx,
7936 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007937{
7938 int ret;
7939
Jens Axboe6c271ce2019-01-10 11:22:30 -07007940 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007941 struct io_sq_data *sqd;
7942
Jens Axboe3ec482d2019-04-08 10:51:01 -06007943 ret = -EPERM;
7944 if (!capable(CAP_SYS_ADMIN))
7945 goto err;
7946
Jens Axboe534ca6d2020-09-02 13:52:19 -06007947 sqd = io_get_sq_data(p);
7948 if (IS_ERR(sqd)) {
7949 ret = PTR_ERR(sqd);
7950 goto err;
7951 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007952
Jens Axboe534ca6d2020-09-02 13:52:19 -06007953 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007954 io_sq_thread_park(sqd);
7955 mutex_lock(&sqd->ctx_lock);
7956 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7957 mutex_unlock(&sqd->ctx_lock);
7958 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007959
Jens Axboe917257d2019-04-13 09:28:55 -06007960 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7961 if (!ctx->sq_thread_idle)
7962 ctx->sq_thread_idle = HZ;
7963
Jens Axboeaa061652020-09-02 14:50:27 -06007964 if (sqd->thread)
7965 goto done;
7966
Jens Axboe6c271ce2019-01-10 11:22:30 -07007967 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007968 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007969
Jens Axboe917257d2019-04-13 09:28:55 -06007970 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007971 if (cpu >= nr_cpu_ids)
7972 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007973 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007974 goto err;
7975
Jens Axboe69fb2132020-09-14 11:16:23 -06007976 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007977 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007978 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007979 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007980 "io_uring-sq");
7981 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007982 if (IS_ERR(sqd->thread)) {
7983 ret = PTR_ERR(sqd->thread);
7984 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007985 goto err;
7986 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007987 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007988 if (ret)
7989 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007990 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7991 /* Can't have SQ_AFF without SQPOLL */
7992 ret = -EINVAL;
7993 goto err;
7994 }
7995
Jens Axboeaa061652020-09-02 14:50:27 -06007996done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007997 ret = io_init_wq_offload(ctx, p);
7998 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007999 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008000
8001 return 0;
8002err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008003 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008004 return ret;
8005}
8006
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008007static void io_sq_offload_start(struct io_ring_ctx *ctx)
8008{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008009 struct io_sq_data *sqd = ctx->sq_data;
8010
Yang Yingliang09058802021-07-15 21:18:25 +08008011 ctx->flags &= ~IORING_SETUP_R_DISABLED;
Yang Yingliang65b26582021-07-29 22:23:38 +08008012 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd && sqd->thread)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008013 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008014}
8015
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008016static inline void __io_unaccount_mem(struct user_struct *user,
8017 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008018{
8019 atomic_long_sub(nr_pages, &user->locked_vm);
8020}
8021
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008022static inline int __io_account_mem(struct user_struct *user,
8023 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008024{
8025 unsigned long page_limit, cur_pages, new_pages;
8026
8027 /* Don't allow more pages than we can safely lock */
8028 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8029
8030 do {
8031 cur_pages = atomic_long_read(&user->locked_vm);
8032 new_pages = cur_pages + nr_pages;
8033 if (new_pages > page_limit)
8034 return -ENOMEM;
8035 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8036 new_pages) != cur_pages);
8037
8038 return 0;
8039}
8040
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008041static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8042 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008043{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008044 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008045 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008046
Jens Axboe2aede0e2020-09-14 10:45:53 -06008047 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008048 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008049 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008050 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008051 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008052 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008053}
8054
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008055static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8056 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008057{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008058 int ret;
8059
8060 if (ctx->limit_mem) {
8061 ret = __io_account_mem(ctx->user, nr_pages);
8062 if (ret)
8063 return ret;
8064 }
8065
Jens Axboe2aede0e2020-09-14 10:45:53 -06008066 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008067 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008068 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008069 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008070 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008071 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008072
8073 return 0;
8074}
8075
Jens Axboe2b188cc2019-01-07 10:46:33 -07008076static void io_mem_free(void *ptr)
8077{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008078 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008079
Mark Rutland52e04ef2019-04-30 17:30:21 +01008080 if (!ptr)
8081 return;
8082
8083 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008084 if (put_page_testzero(page))
8085 free_compound_page(page);
8086}
8087
8088static void *io_mem_alloc(size_t size)
8089{
8090 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8091 __GFP_NORETRY;
8092
8093 return (void *) __get_free_pages(gfp_flags, get_order(size));
8094}
8095
Hristo Venev75b28af2019-08-26 17:23:46 +00008096static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8097 size_t *sq_offset)
8098{
8099 struct io_rings *rings;
8100 size_t off, sq_array_size;
8101
8102 off = struct_size(rings, cqes, cq_entries);
8103 if (off == SIZE_MAX)
8104 return SIZE_MAX;
8105
8106#ifdef CONFIG_SMP
8107 off = ALIGN(off, SMP_CACHE_BYTES);
8108 if (off == 0)
8109 return SIZE_MAX;
8110#endif
8111
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008112 if (sq_offset)
8113 *sq_offset = off;
8114
Hristo Venev75b28af2019-08-26 17:23:46 +00008115 sq_array_size = array_size(sizeof(u32), sq_entries);
8116 if (sq_array_size == SIZE_MAX)
8117 return SIZE_MAX;
8118
8119 if (check_add_overflow(off, sq_array_size, &off))
8120 return SIZE_MAX;
8121
Hristo Venev75b28af2019-08-26 17:23:46 +00008122 return off;
8123}
8124
Jens Axboe2b188cc2019-01-07 10:46:33 -07008125static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8126{
Hristo Venev75b28af2019-08-26 17:23:46 +00008127 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008128
Hristo Venev75b28af2019-08-26 17:23:46 +00008129 pages = (size_t)1 << get_order(
8130 rings_size(sq_entries, cq_entries, NULL));
8131 pages += (size_t)1 << get_order(
8132 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008133
Hristo Venev75b28af2019-08-26 17:23:46 +00008134 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008135}
8136
Jens Axboeedafcce2019-01-09 09:16:05 -07008137static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8138{
8139 int i, j;
8140
8141 if (!ctx->user_bufs)
8142 return -ENXIO;
8143
8144 for (i = 0; i < ctx->nr_user_bufs; i++) {
8145 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8146
8147 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008148 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008149
Jens Axboede293932020-09-17 16:19:16 -06008150 if (imu->acct_pages)
8151 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008152 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008153 imu->nr_bvecs = 0;
8154 }
8155
8156 kfree(ctx->user_bufs);
8157 ctx->user_bufs = NULL;
8158 ctx->nr_user_bufs = 0;
8159 return 0;
8160}
8161
8162static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8163 void __user *arg, unsigned index)
8164{
8165 struct iovec __user *src;
8166
8167#ifdef CONFIG_COMPAT
8168 if (ctx->compat) {
8169 struct compat_iovec __user *ciovs;
8170 struct compat_iovec ciov;
8171
8172 ciovs = (struct compat_iovec __user *) arg;
8173 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8174 return -EFAULT;
8175
Jens Axboed55e5f52019-12-11 16:12:15 -07008176 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008177 dst->iov_len = ciov.iov_len;
8178 return 0;
8179 }
8180#endif
8181 src = (struct iovec __user *) arg;
8182 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8183 return -EFAULT;
8184 return 0;
8185}
8186
Jens Axboede293932020-09-17 16:19:16 -06008187/*
8188 * Not super efficient, but this is just a registration time. And we do cache
8189 * the last compound head, so generally we'll only do a full search if we don't
8190 * match that one.
8191 *
8192 * We check if the given compound head page has already been accounted, to
8193 * avoid double accounting it. This allows us to account the full size of the
8194 * page, not just the constituent pages of a huge page.
8195 */
8196static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8197 int nr_pages, struct page *hpage)
8198{
8199 int i, j;
8200
8201 /* check current page array */
8202 for (i = 0; i < nr_pages; i++) {
8203 if (!PageCompound(pages[i]))
8204 continue;
8205 if (compound_head(pages[i]) == hpage)
8206 return true;
8207 }
8208
8209 /* check previously registered pages */
8210 for (i = 0; i < ctx->nr_user_bufs; i++) {
8211 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8212
8213 for (j = 0; j < imu->nr_bvecs; j++) {
8214 if (!PageCompound(imu->bvec[j].bv_page))
8215 continue;
8216 if (compound_head(imu->bvec[j].bv_page) == hpage)
8217 return true;
8218 }
8219 }
8220
8221 return false;
8222}
8223
8224static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8225 int nr_pages, struct io_mapped_ubuf *imu,
8226 struct page **last_hpage)
8227{
8228 int i, ret;
8229
8230 for (i = 0; i < nr_pages; i++) {
8231 if (!PageCompound(pages[i])) {
8232 imu->acct_pages++;
8233 } else {
8234 struct page *hpage;
8235
8236 hpage = compound_head(pages[i]);
8237 if (hpage == *last_hpage)
8238 continue;
8239 *last_hpage = hpage;
8240 if (headpage_already_acct(ctx, pages, i, hpage))
8241 continue;
8242 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8243 }
8244 }
8245
8246 if (!imu->acct_pages)
8247 return 0;
8248
8249 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8250 if (ret)
8251 imu->acct_pages = 0;
8252 return ret;
8253}
8254
Jens Axboeedafcce2019-01-09 09:16:05 -07008255static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8256 unsigned nr_args)
8257{
8258 struct vm_area_struct **vmas = NULL;
8259 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008260 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008261 int i, j, got_pages = 0;
8262 int ret = -EINVAL;
8263
8264 if (ctx->user_bufs)
8265 return -EBUSY;
8266 if (!nr_args || nr_args > UIO_MAXIOV)
8267 return -EINVAL;
8268
8269 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8270 GFP_KERNEL);
8271 if (!ctx->user_bufs)
8272 return -ENOMEM;
8273
8274 for (i = 0; i < nr_args; i++) {
8275 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8276 unsigned long off, start, end, ubuf;
8277 int pret, nr_pages;
8278 struct iovec iov;
8279 size_t size;
8280
8281 ret = io_copy_iov(ctx, &iov, arg, i);
8282 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008283 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008284
8285 /*
8286 * Don't impose further limits on the size and buffer
8287 * constraints here, we'll -EINVAL later when IO is
8288 * submitted if they are wrong.
8289 */
8290 ret = -EFAULT;
8291 if (!iov.iov_base || !iov.iov_len)
8292 goto err;
8293
8294 /* arbitrary limit, but we need something */
8295 if (iov.iov_len > SZ_1G)
8296 goto err;
8297
8298 ubuf = (unsigned long) iov.iov_base;
8299 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8300 start = ubuf >> PAGE_SHIFT;
8301 nr_pages = end - start;
8302
Jens Axboeedafcce2019-01-09 09:16:05 -07008303 ret = 0;
8304 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008305 kvfree(vmas);
8306 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008307 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008308 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008309 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008310 sizeof(struct vm_area_struct *),
8311 GFP_KERNEL);
8312 if (!pages || !vmas) {
8313 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008314 goto err;
8315 }
8316 got_pages = nr_pages;
8317 }
8318
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008319 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008320 GFP_KERNEL);
8321 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008322 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008323 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008324
8325 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008326 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008327 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008328 FOLL_WRITE | FOLL_LONGTERM,
8329 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008330 if (pret == nr_pages) {
8331 /* don't support file backed memory */
8332 for (j = 0; j < nr_pages; j++) {
8333 struct vm_area_struct *vma = vmas[j];
8334
8335 if (vma->vm_file &&
8336 !is_file_hugepages(vma->vm_file)) {
8337 ret = -EOPNOTSUPP;
8338 break;
8339 }
8340 }
8341 } else {
8342 ret = pret < 0 ? pret : -EFAULT;
8343 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008344 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008345 if (ret) {
8346 /*
8347 * if we did partial map, or found file backed vmas,
8348 * release any pages we did get
8349 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008350 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008351 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008352 kvfree(imu->bvec);
8353 goto err;
8354 }
8355
8356 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8357 if (ret) {
8358 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008359 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008360 goto err;
8361 }
8362
8363 off = ubuf & ~PAGE_MASK;
8364 size = iov.iov_len;
8365 for (j = 0; j < nr_pages; j++) {
8366 size_t vec_len;
8367
8368 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8369 imu->bvec[j].bv_page = pages[j];
8370 imu->bvec[j].bv_len = vec_len;
8371 imu->bvec[j].bv_offset = off;
8372 off = 0;
8373 size -= vec_len;
8374 }
8375 /* store original address for later verification */
8376 imu->ubuf = ubuf;
8377 imu->len = iov.iov_len;
8378 imu->nr_bvecs = nr_pages;
8379
8380 ctx->nr_user_bufs++;
8381 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008382 kvfree(pages);
8383 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008384 return 0;
8385err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008386 kvfree(pages);
8387 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008388 io_sqe_buffer_unregister(ctx);
8389 return ret;
8390}
8391
Jens Axboe9b402842019-04-11 11:45:41 -06008392static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8393{
8394 __s32 __user *fds = arg;
8395 int fd;
8396
8397 if (ctx->cq_ev_fd)
8398 return -EBUSY;
8399
8400 if (copy_from_user(&fd, fds, sizeof(*fds)))
8401 return -EFAULT;
8402
8403 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8404 if (IS_ERR(ctx->cq_ev_fd)) {
8405 int ret = PTR_ERR(ctx->cq_ev_fd);
8406 ctx->cq_ev_fd = NULL;
8407 return ret;
8408 }
8409
8410 return 0;
8411}
8412
8413static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8414{
8415 if (ctx->cq_ev_fd) {
8416 eventfd_ctx_put(ctx->cq_ev_fd);
8417 ctx->cq_ev_fd = NULL;
8418 return 0;
8419 }
8420
8421 return -ENXIO;
8422}
8423
Jens Axboe5a2e7452020-02-23 16:23:11 -07008424static void io_destroy_buffers(struct io_ring_ctx *ctx)
8425{
Jens Axboe90731882021-07-13 17:18:35 +08008426 struct io_buffer *buf;
8427 unsigned long index;
8428
8429 xa_for_each(&ctx->io_buffers, index, buf)
8430 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008431}
8432
Jens Axboe2b188cc2019-01-07 10:46:33 -07008433static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8434{
Jens Axboe6b063142019-01-10 22:13:58 -07008435 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008436 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008437
8438 if (ctx->sqo_task) {
8439 put_task_struct(ctx->sqo_task);
8440 ctx->sqo_task = NULL;
8441 mmdrop(ctx->mm_account);
8442 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008443 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008444
Dennis Zhou91d8f512020-09-16 13:41:05 -07008445#ifdef CONFIG_BLK_CGROUP
8446 if (ctx->sqo_blkcg_css)
8447 css_put(ctx->sqo_blkcg_css);
8448#endif
8449
Jens Axboe6b063142019-01-10 22:13:58 -07008450 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008451 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008452 io_destroy_buffers(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07008453
Jens Axboe2b188cc2019-01-07 10:46:33 -07008454#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008455 if (ctx->ring_sock) {
8456 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008457 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008458 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008459#endif
8460
Hristo Venev75b28af2019-08-26 17:23:46 +00008461 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008462 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008463
8464 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008465 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008466 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008467 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008468 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008469 kfree(ctx);
8470}
8471
8472static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8473{
8474 struct io_ring_ctx *ctx = file->private_data;
8475 __poll_t mask = 0;
8476
8477 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008478 /*
8479 * synchronizes with barrier from wq_has_sleeper call in
8480 * io_commit_cqring
8481 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008482 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008483 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008484 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xu81dfee42021-02-05 16:34:21 +08008485
8486 /*
8487 * Don't flush cqring overflow list here, just do a simple check.
8488 * Otherwise there could possible be ABBA deadlock:
8489 * CPU0 CPU1
8490 * ---- ----
8491 * lock(&ctx->uring_lock);
8492 * lock(&ep->mtx);
8493 * lock(&ctx->uring_lock);
8494 * lock(&ep->mtx);
8495 *
8496 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8497 * pushs them to do the flush.
8498 */
8499 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008500 mask |= EPOLLIN | EPOLLRDNORM;
8501
8502 return mask;
8503}
8504
8505static int io_uring_fasync(int fd, struct file *file, int on)
8506{
8507 struct io_ring_ctx *ctx = file->private_data;
8508
8509 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8510}
8511
Yejune Dengcb2985f2021-07-13 17:18:33 +08008512static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008513{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008514 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008515
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08008516 iod = xa_erase(&ctx->personalities, id);
Jens Axboe1e6fa522020-10-15 08:46:24 -06008517 if (iod) {
8518 put_cred(iod->creds);
8519 if (refcount_dec_and_test(&iod->count))
8520 kfree(iod);
Yejune Dengcb2985f2021-07-13 17:18:33 +08008521 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008522 }
Yejune Dengcb2985f2021-07-13 17:18:33 +08008523
8524 return -EINVAL;
8525}
8526
Jens Axboe85faa7b2020-04-09 18:14:00 -06008527static void io_ring_exit_work(struct work_struct *work)
8528{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008529 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8530 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008531
Jens Axboe56952e92020-06-17 15:00:04 -06008532 /*
8533 * If we're doing polled IO and end up having requests being
8534 * submitted async (out-of-line), then completions can come in while
8535 * we're waiting for refs to drop. We need to reap these manually,
8536 * as nobody else will be looking for them.
8537 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008538 do {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008539 io_iopoll_try_reap_events(ctx);
8540 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008541 io_ring_ctx_free(ctx);
8542}
8543
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008544static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8545{
8546 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8547
8548 return req->ctx == data;
8549}
8550
Jens Axboe2b188cc2019-01-07 10:46:33 -07008551static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8552{
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08008553 unsigned long index;
8554 struct io_identify *iod;
8555
Jens Axboe2b188cc2019-01-07 10:46:33 -07008556 mutex_lock(&ctx->uring_lock);
8557 percpu_ref_kill(&ctx->refs);
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008558 /* if force is set, the ring is going away. always drop after that */
Pavel Begunkova63d9152021-01-26 11:17:03 +00008559
8560 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8561 ctx->sqo_dead = 1;
8562
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008563 ctx->cq_overflow_flushed = 1;
Pavel Begunkovc0fd45a2020-12-06 22:22:44 +00008564 if (ctx->rings)
Pavel Begunkov85e25e22021-01-12 21:17:26 +00008565 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008566 mutex_unlock(&ctx->uring_lock);
8567
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00008568 io_kill_timeouts(ctx, NULL, NULL);
8569 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008570
8571 if (ctx->io_wq)
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008572 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008573
Jens Axboe15dff282019-11-13 09:09:23 -07008574 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008575 io_iopoll_try_reap_events(ctx);
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08008576 xa_for_each(&ctx->personalities, index, iod)
8577 io_unregister_personality(ctx, index);
Jens Axboe309fc032020-07-10 09:13:34 -06008578
8579 /*
8580 * Do this upfront, so we won't have a grace period where the ring
8581 * is closed but resources aren't reaped yet. This can cause
8582 * spurious failure in setting up a new ring.
8583 */
Jens Axboe760618f2020-07-24 12:53:31 -06008584 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8585 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008586
Jens Axboe85faa7b2020-04-09 18:14:00 -06008587 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008588 /*
8589 * Use system_unbound_wq to avoid spawning tons of event kworkers
8590 * if we're exiting a ton of rings at the same time. It just adds
8591 * noise and overhead, there's no discernable change in runtime
8592 * over using system_wq.
8593 */
8594 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008595}
8596
8597static int io_uring_release(struct inode *inode, struct file *file)
8598{
8599 struct io_ring_ctx *ctx = file->private_data;
8600
8601 file->private_data = NULL;
8602 io_ring_ctx_wait_and_kill(ctx);
8603 return 0;
8604}
8605
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008606struct io_task_cancel {
8607 struct task_struct *task;
8608 struct files_struct *files;
8609};
Jens Axboef254ac02020-08-12 17:33:30 -06008610
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008611static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008612{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008613 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008614 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008615 bool ret;
8616
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008617 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008618 unsigned long flags;
8619 struct io_ring_ctx *ctx = req->ctx;
8620
8621 /* protect against races with linked timeouts */
8622 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008623 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008624 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8625 } else {
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008626 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008627 }
8628 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008629}
8630
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008631static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008632 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008633 struct files_struct *files)
8634{
8635 struct io_defer_entry *de = NULL;
8636 LIST_HEAD(list);
8637
8638 spin_lock_irq(&ctx->completion_lock);
8639 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00008640 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008641 list_cut_position(&list, &ctx->defer_list, &de->list);
8642 break;
8643 }
8644 }
8645 spin_unlock_irq(&ctx->completion_lock);
8646
8647 while (!list_empty(&list)) {
8648 de = list_first_entry(&list, struct io_defer_entry, list);
8649 list_del_init(&de->list);
8650 req_set_fail_links(de->req);
8651 io_put_req(de->req);
8652 io_req_complete(de->req, -ECANCELED);
8653 kfree(de);
8654 }
8655}
8656
Pavel Begunkovd300d032021-02-09 04:47:45 +00008657static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8658 struct task_struct *task,
8659 struct files_struct *files)
8660{
8661 struct io_kiocb *req;
8662 int cnt = 0;
8663
8664 spin_lock_irq(&ctx->inflight_lock);
8665 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8666 cnt += io_match_task(req, task, files);
8667 spin_unlock_irq(&ctx->inflight_lock);
8668 return cnt;
8669}
8670
Pavel Begunkov49250f32021-02-09 04:47:37 +00008671static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkova773dea2020-11-06 13:00:23 +00008672 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008673 struct files_struct *files)
8674{
Jens Axboefcb323c2019-10-24 12:39:47 -06008675 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkov1e7eb062021-02-09 04:47:40 +00008676 struct io_task_cancel cancel = { .task = task, .files = files };
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008677 DEFINE_WAIT(wait);
Pavel Begunkovd300d032021-02-09 04:47:45 +00008678 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008679
Pavel Begunkovd300d032021-02-09 04:47:45 +00008680 inflight = io_uring_count_inflight(ctx, task, files);
8681 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008682 break;
Pavel Begunkovd92d0082021-01-26 11:17:10 +00008683
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008684 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8685 io_poll_remove_all(ctx, task, files);
8686 io_kill_timeouts(ctx, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008687 /* cancellations _may_ trigger task work */
8688 io_run_task_work();
Pavel Begunkovd300d032021-02-09 04:47:45 +00008689
8690 prepare_to_wait(&task->io_uring->wait, &wait,
8691 TASK_UNINTERRUPTIBLE);
8692 if (inflight == io_uring_count_inflight(ctx, task, files))
8693 schedule();
Pavel Begunkov52382df82021-02-09 04:47:44 +00008694 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008695 }
8696}
8697
Pavel Begunkov49250f32021-02-09 04:47:37 +00008698static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8699 struct task_struct *task)
Jens Axboe0f212202020-09-13 13:09:39 -06008700{
Pavel Begunkov49250f32021-02-09 04:47:37 +00008701 while (1) {
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008702 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06008703 enum io_wq_cancel cret;
Pavel Begunkov49250f32021-02-09 04:47:37 +00008704 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008705
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008706 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
Jens Axboe0f212202020-09-13 13:09:39 -06008707 if (cret != IO_WQ_CANCEL_NOTFOUND)
8708 ret = true;
8709
8710 /* SQPOLL thread does its own polling */
8711 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8712 while (!list_empty_careful(&ctx->iopoll_list)) {
8713 io_iopoll_try_reap_events(ctx);
8714 ret = true;
8715 }
8716 }
8717
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00008718 ret |= io_poll_remove_all(ctx, task, NULL);
8719 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov49250f32021-02-09 04:47:37 +00008720 if (!ret)
8721 break;
8722 io_run_task_work();
8723 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06008724 }
Jens Axboe0f212202020-09-13 13:09:39 -06008725}
8726
Pavel Begunkova63d9152021-01-26 11:17:03 +00008727static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8728{
Pavel Begunkova63d9152021-01-26 11:17:03 +00008729 mutex_lock(&ctx->uring_lock);
8730 ctx->sqo_dead = 1;
Jens Axboe6cae8092021-02-28 15:32:18 -07008731 if (ctx->flags & IORING_SETUP_R_DISABLED)
8732 io_sq_offload_start(ctx);
Pavel Begunkova63d9152021-01-26 11:17:03 +00008733 mutex_unlock(&ctx->uring_lock);
8734
8735 /* make sure callers enter the ring to get error */
Pavel Begunkov0e3562e2021-01-26 11:17:04 +00008736 if (ctx->rings)
8737 io_ring_set_wakeup_flag(ctx);
Pavel Begunkova63d9152021-01-26 11:17:03 +00008738}
8739
Jens Axboe0f212202020-09-13 13:09:39 -06008740/*
8741 * We need to iteratively cancel requests, in case a request has dependent
8742 * hard links. These persist even for failure of cancelations, hence keep
8743 * looping until none are found.
8744 */
8745static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8746 struct files_struct *files)
8747{
8748 struct task_struct *task = current;
8749
Jens Axboefdaf0832020-10-30 09:37:30 -06008750 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkova63d9152021-01-26 11:17:03 +00008751 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008752 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008753 atomic_inc(&task->io_uring->in_idle);
8754 io_sq_thread_park(ctx->sq_data);
8755 }
Jens Axboe0f212202020-09-13 13:09:39 -06008756
Pavel Begunkova773dea2020-11-06 13:00:23 +00008757 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008758 io_cqring_overflow_flush(ctx, true, task, files);
8759
Pavel Begunkov88dbd082021-02-09 04:47:49 +00008760 io_uring_cancel_files(ctx, task, files);
Pavel Begunkov49250f32021-02-09 04:47:37 +00008761 if (!files)
8762 __io_uring_cancel_task_requests(ctx, task);
Jens Axboefdaf0832020-10-30 09:37:30 -06008763
8764 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8765 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008766 io_sq_thread_unpark(ctx->sq_data);
8767 }
Jens Axboe0f212202020-09-13 13:09:39 -06008768}
8769
8770/*
8771 * Note that this task has used io_uring. We use it for cancelation purposes.
8772 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008773static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008774{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008775 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008776 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008777
8778 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008779 ret = io_uring_alloc_task_context(current);
8780 if (unlikely(ret))
8781 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008782 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008783 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008784 if (tctx->last != file) {
8785 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008786
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008787 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008788 get_file(file);
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008789 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8790 file, GFP_KERNEL));
8791 if (ret) {
8792 fput(file);
8793 return ret;
8794 }
Jens Axboe0f212202020-09-13 13:09:39 -06008795 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008796 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008797 }
8798
Jens Axboefdaf0832020-10-30 09:37:30 -06008799 /*
8800 * This is race safe in that the task itself is doing this, hence it
8801 * cannot be going through the exit/cancel paths at the same time.
8802 * This cannot be modified while exit/cancel is running.
8803 */
8804 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8805 tctx->sqpoll = true;
8806
Jens Axboe0f212202020-09-13 13:09:39 -06008807 return 0;
8808}
8809
8810/*
8811 * Remove this io_uring_file -> task mapping.
8812 */
8813static void io_uring_del_task_file(struct file *file)
8814{
8815 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008816
8817 if (tctx->last == file)
8818 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008819 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008820 if (file)
8821 fput(file);
8822}
8823
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008824static void io_uring_remove_task_files(struct io_uring_task *tctx)
8825{
8826 struct file *file;
8827 unsigned long index;
8828
8829 xa_for_each(&tctx->xa, index, file)
8830 io_uring_del_task_file(file);
8831}
8832
Jens Axboe0f212202020-09-13 13:09:39 -06008833void __io_uring_files_cancel(struct files_struct *files)
8834{
8835 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008836 struct file *file;
8837 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008838
8839 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008840 atomic_inc(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008841 xa_for_each(&tctx->xa, index, file)
8842 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008843 atomic_dec(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008844
8845 if (files)
8846 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008847}
8848
8849static s64 tctx_inflight(struct io_uring_task *tctx)
8850{
8851 unsigned long index;
8852 struct file *file;
8853 s64 inflight;
8854
8855 inflight = percpu_counter_sum(&tctx->inflight);
8856 if (!tctx->sqpoll)
8857 return inflight;
8858
8859 /*
8860 * If we have SQPOLL rings, then we need to iterate and find them, and
8861 * add the pending count for those.
8862 */
8863 xa_for_each(&tctx->xa, index, file) {
8864 struct io_ring_ctx *ctx = file->private_data;
8865
8866 if (ctx->flags & IORING_SETUP_SQPOLL) {
8867 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
8868
8869 inflight += percpu_counter_sum(&__tctx->inflight);
8870 }
8871 }
8872
8873 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008874}
8875
Jens Axboe0f212202020-09-13 13:09:39 -06008876/*
8877 * Find any io_uring fd that this task has registered or done IO on, and cancel
8878 * requests.
8879 */
8880void __io_uring_task_cancel(void)
8881{
8882 struct io_uring_task *tctx = current->io_uring;
8883 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008884 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008885
8886 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008887 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008888
Pavel Begunkov186725a2021-01-26 11:17:08 +00008889 /* trigger io_disable_sqo_submit() */
8890 if (tctx->sqpoll)
8891 __io_uring_files_cancel(NULL);
8892
Jens Axboed8a6df12020-10-15 16:24:45 -06008893 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008894 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008895 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008896 if (!inflight)
8897 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008898 __io_uring_files_cancel(NULL);
8899
8900 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8901
8902 /*
Pavel Begunkovb462a7b2021-02-09 04:47:43 +00008903 * If we've seen completions, retry without waiting. This
8904 * avoids a race where a completion comes in before we did
8905 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008906 */
Pavel Begunkovb462a7b2021-02-09 04:47:43 +00008907 if (inflight == tctx_inflight(tctx))
8908 schedule();
8909 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008910 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008911
Jens Axboefdaf0832020-10-30 09:37:30 -06008912 atomic_dec(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008913
8914 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008915}
8916
Jens Axboefcb323c2019-10-24 12:39:47 -06008917static int io_uring_flush(struct file *file, void *data)
8918{
Pavel Begunkovda676312021-01-26 11:17:02 +00008919 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova63d9152021-01-26 11:17:03 +00008920 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkovda676312021-01-26 11:17:02 +00008921
Jens Axboef0ff1a92021-02-09 04:47:42 +00008922 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8923 io_uring_cancel_task_requests(ctx, NULL);
8924
Pavel Begunkovda676312021-01-26 11:17:02 +00008925 if (!tctx)
Pavel Begunkov18f31592021-01-26 11:17:01 +00008926 return 0;
8927
Pavel Begunkovda676312021-01-26 11:17:02 +00008928 /* we should have cancelled and erased it before PF_EXITING */
8929 WARN_ON_ONCE((current->flags & PF_EXITING) &&
8930 xa_load(&tctx->xa, (unsigned long)file));
8931
Pavel Begunkov18f31592021-01-26 11:17:01 +00008932 /*
8933 * fput() is pending, will be 2 if the only other ref is our potential
8934 * task file note. If the task is exiting, drop regardless of count.
8935 */
Pavel Begunkovda676312021-01-26 11:17:02 +00008936 if (atomic_long_read(&file->f_count) != 2)
8937 return 0;
Pavel Begunkov18f31592021-01-26 11:17:01 +00008938
Pavel Begunkova63d9152021-01-26 11:17:03 +00008939 if (ctx->flags & IORING_SETUP_SQPOLL) {
8940 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov54b4c4f2021-01-26 11:17:07 +00008941 WARN_ON_ONCE(ctx->sqo_task != current &&
8942 xa_load(&tctx->xa, (unsigned long)file));
8943 /* sqo_dead check is for when this happens after cancellation */
8944 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkova63d9152021-01-26 11:17:03 +00008945 !xa_load(&tctx->xa, (unsigned long)file));
8946
8947 io_disable_sqo_submit(ctx);
8948 }
8949
8950 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
8951 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008952 return 0;
8953}
8954
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008955static void *io_uring_validate_mmap_request(struct file *file,
8956 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008957{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008958 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008959 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008960 struct page *page;
8961 void *ptr;
8962
8963 switch (offset) {
8964 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008965 case IORING_OFF_CQ_RING:
8966 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008967 break;
8968 case IORING_OFF_SQES:
8969 ptr = ctx->sq_sqes;
8970 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008971 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008972 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008973 }
8974
8975 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008976 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008977 return ERR_PTR(-EINVAL);
8978
8979 return ptr;
8980}
8981
8982#ifdef CONFIG_MMU
8983
8984static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8985{
8986 size_t sz = vma->vm_end - vma->vm_start;
8987 unsigned long pfn;
8988 void *ptr;
8989
8990 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8991 if (IS_ERR(ptr))
8992 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008993
8994 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8995 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8996}
8997
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008998#else /* !CONFIG_MMU */
8999
9000static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9001{
9002 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9003}
9004
9005static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9006{
9007 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9008}
9009
9010static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9011 unsigned long addr, unsigned long len,
9012 unsigned long pgoff, unsigned long flags)
9013{
9014 void *ptr;
9015
9016 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9017 if (IS_ERR(ptr))
9018 return PTR_ERR(ptr);
9019
9020 return (unsigned long) ptr;
9021}
9022
9023#endif /* !CONFIG_MMU */
9024
Pavel Begunkova63d9152021-01-26 11:17:03 +00009025static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009026{
Pavel Begunkova63d9152021-01-26 11:17:03 +00009027 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009028 DEFINE_WAIT(wait);
9029
9030 do {
9031 if (!io_sqring_full(ctx))
9032 break;
9033
9034 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9035
Pavel Begunkova63d9152021-01-26 11:17:03 +00009036 if (unlikely(ctx->sqo_dead)) {
9037 ret = -EOWNERDEAD;
9038 goto out;
9039 }
9040
Jens Axboe90554202020-09-03 12:12:41 -06009041 if (!io_sqring_full(ctx))
9042 break;
9043
9044 schedule();
9045 } while (!signal_pending(current));
9046
9047 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkova63d9152021-01-26 11:17:03 +00009048out:
9049 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009050}
9051
Jens Axboe2b188cc2019-01-07 10:46:33 -07009052SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9053 u32, min_complete, u32, flags, const sigset_t __user *, sig,
9054 size_t, sigsz)
9055{
9056 struct io_ring_ctx *ctx;
9057 long ret = -EBADF;
9058 int submitted = 0;
9059 struct fd f;
9060
Jens Axboe4c6e2772020-07-01 11:29:10 -06009061 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009062
Jens Axboe90554202020-09-03 12:12:41 -06009063 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9064 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009065 return -EINVAL;
9066
9067 f = fdget(fd);
9068 if (!f.file)
9069 return -EBADF;
9070
9071 ret = -EOPNOTSUPP;
9072 if (f.file->f_op != &io_uring_fops)
9073 goto out_fput;
9074
9075 ret = -ENXIO;
9076 ctx = f.file->private_data;
9077 if (!percpu_ref_tryget(&ctx->refs))
9078 goto out_fput;
9079
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009080 ret = -EBADFD;
9081 if (ctx->flags & IORING_SETUP_R_DISABLED)
9082 goto out;
9083
Jens Axboe6c271ce2019-01-10 11:22:30 -07009084 /*
9085 * For SQ polling, the thread will do all submissions and completions.
9086 * Just return the requested submit count, and wake the thread if
9087 * we were asked to.
9088 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009089 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009090 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00009091 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkovbc924dd2021-01-12 21:17:25 +00009092
Jens Axboef15e64262021-08-24 13:15:31 +01009093 if (unlikely(ctx->sqo_dead)) {
9094 ret = -EOWNERDEAD;
Pavel Begunkova63d9152021-01-26 11:17:03 +00009095 goto out;
Jens Axboef15e64262021-08-24 13:15:31 +01009096 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009097 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009098 wake_up(&ctx->sq_data->wait);
Pavel Begunkova63d9152021-01-26 11:17:03 +00009099 if (flags & IORING_ENTER_SQ_WAIT) {
9100 ret = io_sqpoll_wait_sq(ctx);
9101 if (ret)
9102 goto out;
9103 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009104 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009105 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009106 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009107 if (unlikely(ret))
9108 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009109 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009110 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009111 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009112
9113 if (submitted != to_submit)
9114 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009115 }
9116 if (flags & IORING_ENTER_GETEVENTS) {
9117 min_complete = min(min_complete, ctx->cq_entries);
9118
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009119 /*
9120 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9121 * space applications don't need to do io completion events
9122 * polling again, they can rely on io_sq_thread to do polling
9123 * work, which can reduce cpu usage and uring_lock contention.
9124 */
9125 if (ctx->flags & IORING_SETUP_IOPOLL &&
9126 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009127 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009128 } else {
9129 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9130 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009131 }
9132
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009133out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009134 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009135out_fput:
9136 fdput(f);
9137 return submitted ? submitted : ret;
9138}
9139
Tobias Klauserbebdb652020-02-26 18:38:32 +01009140#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009141static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9142 const struct io_identity *iod)
Jens Axboe87ce9552020-01-30 08:25:34 -07009143{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009144 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009145 struct user_namespace *uns = seq_user_ns(m);
9146 struct group_info *gi;
9147 kernel_cap_t cap;
9148 unsigned __capi;
9149 int g;
9150
9151 seq_printf(m, "%5d\n", id);
9152 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9153 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9154 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9155 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9156 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9157 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9158 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9159 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9160 seq_puts(m, "\n\tGroups:\t");
9161 gi = cred->group_info;
9162 for (g = 0; g < gi->ngroups; g++) {
9163 seq_put_decimal_ull(m, g ? " " : "",
9164 from_kgid_munged(uns, gi->gid[g]));
9165 }
9166 seq_puts(m, "\n\tCapEff:\t");
9167 cap = cred->cap_effective;
9168 CAP_FOR_EACH_U32(__capi)
9169 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9170 seq_putc(m, '\n');
9171 return 0;
9172}
9173
9174static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9175{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009176 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009177 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009178 int i;
9179
Jens Axboefad8e0d2020-09-28 08:57:48 -06009180 /*
9181 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9182 * since fdinfo case grabs it in the opposite direction of normal use
9183 * cases. If we fail to get the lock, we just don't iterate any
9184 * structures that could be going away outside the io_uring mutex.
9185 */
9186 has_lock = mutex_trylock(&ctx->uring_lock);
9187
Joseph Qidbbe9c62020-09-29 09:01:22 -06009188 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9189 sq = ctx->sq_data;
9190
9191 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9192 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009193 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009194 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009195 struct fixed_file_table *table;
9196 struct file *f;
9197
9198 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9199 f = table->files[i & IORING_FILE_TABLE_MASK];
9200 if (f)
9201 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9202 else
9203 seq_printf(m, "%5u: <none>\n", i);
9204 }
9205 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009206 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009207 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9208
9209 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9210 (unsigned int) buf->len);
9211 }
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009212 if (has_lock && !xa_empty(&ctx->personalities)) {
9213 unsigned long index;
9214 const struct io_identity *iod;
9215
Jens Axboe87ce9552020-01-30 08:25:34 -07009216 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009217 xa_for_each(&ctx->personalities, index, iod)
9218 io_uring_show_cred(m, index, iod);
Jens Axboe87ce9552020-01-30 08:25:34 -07009219 }
Jens Axboed7718a92020-02-14 22:23:12 -07009220 seq_printf(m, "PollList:\n");
9221 spin_lock_irq(&ctx->completion_lock);
9222 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9223 struct hlist_head *list = &ctx->cancel_hash[i];
9224 struct io_kiocb *req;
9225
9226 hlist_for_each_entry(req, list, hash_node)
9227 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9228 req->task->task_works != NULL);
9229 }
9230 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009231 if (has_lock)
9232 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009233}
9234
9235static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9236{
9237 struct io_ring_ctx *ctx = f->private_data;
9238
9239 if (percpu_ref_tryget(&ctx->refs)) {
9240 __io_uring_show_fdinfo(ctx, m);
9241 percpu_ref_put(&ctx->refs);
9242 }
9243}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009244#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009245
Jens Axboe2b188cc2019-01-07 10:46:33 -07009246static const struct file_operations io_uring_fops = {
9247 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009248 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009249 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009250#ifndef CONFIG_MMU
9251 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9252 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9253#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009254 .poll = io_uring_poll,
9255 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009256#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009257 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009258#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009259};
9260
9261static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9262 struct io_uring_params *p)
9263{
Hristo Venev75b28af2019-08-26 17:23:46 +00009264 struct io_rings *rings;
9265 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009266
Jens Axboebd740482020-08-05 12:58:23 -06009267 /* make sure these are sane, as we already accounted them */
9268 ctx->sq_entries = p->sq_entries;
9269 ctx->cq_entries = p->cq_entries;
9270
Hristo Venev75b28af2019-08-26 17:23:46 +00009271 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9272 if (size == SIZE_MAX)
9273 return -EOVERFLOW;
9274
9275 rings = io_mem_alloc(size);
9276 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009277 return -ENOMEM;
9278
Hristo Venev75b28af2019-08-26 17:23:46 +00009279 ctx->rings = rings;
9280 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9281 rings->sq_ring_mask = p->sq_entries - 1;
9282 rings->cq_ring_mask = p->cq_entries - 1;
9283 rings->sq_ring_entries = p->sq_entries;
9284 rings->cq_ring_entries = p->cq_entries;
9285 ctx->sq_mask = rings->sq_ring_mask;
9286 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009287
9288 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009289 if (size == SIZE_MAX) {
9290 io_mem_free(ctx->rings);
9291 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009292 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009293 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009294
9295 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009296 if (!ctx->sq_sqes) {
9297 io_mem_free(ctx->rings);
9298 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009299 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009300 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009301
Jens Axboe2b188cc2019-01-07 10:46:33 -07009302 return 0;
9303}
9304
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009305static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9306{
9307 int ret, fd;
9308
9309 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9310 if (fd < 0)
9311 return fd;
9312
9313 ret = io_uring_add_task_file(ctx, file);
9314 if (ret) {
9315 put_unused_fd(fd);
9316 return ret;
9317 }
9318 fd_install(fd, file);
9319 return fd;
9320}
9321
Jens Axboe2b188cc2019-01-07 10:46:33 -07009322/*
9323 * Allocate an anonymous fd, this is what constitutes the application
9324 * visible backing of an io_uring instance. The application mmaps this
9325 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9326 * we have to tie this fd to a socket for file garbage collection purposes.
9327 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009328static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009329{
9330 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331#if defined(CONFIG_UNIX)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009332 int ret;
9333
Jens Axboe2b188cc2019-01-07 10:46:33 -07009334 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9335 &ctx->ring_sock);
9336 if (ret)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009337 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009338#endif
9339
Jens Axboe2b188cc2019-01-07 10:46:33 -07009340 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9341 O_RDWR | O_CLOEXEC);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009342#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009343 if (IS_ERR(file)) {
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009344 sock_release(ctx->ring_sock);
9345 ctx->ring_sock = NULL;
9346 } else {
9347 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009348 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009349#endif
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009350 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009351}
9352
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009353static int io_uring_create(unsigned entries, struct io_uring_params *p,
9354 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009355{
9356 struct user_struct *user = NULL;
9357 struct io_ring_ctx *ctx;
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009358 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009359 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009360 int ret;
9361
Jens Axboe8110c1a2019-12-28 15:39:54 -07009362 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009363 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009364 if (entries > IORING_MAX_ENTRIES) {
9365 if (!(p->flags & IORING_SETUP_CLAMP))
9366 return -EINVAL;
9367 entries = IORING_MAX_ENTRIES;
9368 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009369
9370 /*
9371 * Use twice as many entries for the CQ ring. It's possible for the
9372 * application to drive a higher depth than the size of the SQ ring,
9373 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009374 * some flexibility in overcommitting a bit. If the application has
9375 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9376 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009377 */
9378 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009379 if (p->flags & IORING_SETUP_CQSIZE) {
9380 /*
9381 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9382 * to a power-of-two, if it isn't already. We do NOT impose
9383 * any cq vs sq ring sizing.
9384 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009385 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009386 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009387 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9388 if (!(p->flags & IORING_SETUP_CLAMP))
9389 return -EINVAL;
9390 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9391 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009392 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9393 if (p->cq_entries < p->sq_entries)
9394 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009395 } else {
9396 p->cq_entries = 2 * p->sq_entries;
9397 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009398
9399 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009400 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009401
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009402 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009403 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404 ring_pages(p->sq_entries, p->cq_entries));
9405 if (ret) {
9406 free_uid(user);
9407 return ret;
9408 }
9409 }
9410
9411 ctx = io_ring_ctx_alloc(p);
9412 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009413 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009414 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009415 p->cq_entries));
9416 free_uid(user);
9417 return -ENOMEM;
9418 }
9419 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009420 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009421 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009422#ifdef CONFIG_AUDIT
9423 ctx->loginuid = current->loginuid;
9424 ctx->sessionid = current->sessionid;
9425#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009426 ctx->sqo_task = get_task_struct(current);
9427
9428 /*
9429 * This is just grabbed for accounting purposes. When a process exits,
9430 * the mm is exited and dropped before the files, hence we need to hang
9431 * on to this mm purely for the purposes of being able to unaccount
9432 * memory (locked/pinned vm). It's not used for anything else.
9433 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009434 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009435 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009436
Dennis Zhou91d8f512020-09-16 13:41:05 -07009437#ifdef CONFIG_BLK_CGROUP
9438 /*
9439 * The sq thread will belong to the original cgroup it was inited in.
9440 * If the cgroup goes offline (e.g. disabling the io controller), then
9441 * issued bios will be associated with the closest cgroup later in the
9442 * block layer.
9443 */
9444 rcu_read_lock();
9445 ctx->sqo_blkcg_css = blkcg_css();
9446 ret = css_tryget_online(ctx->sqo_blkcg_css);
9447 rcu_read_unlock();
9448 if (!ret) {
9449 /* don't init against a dying cgroup, have the user try again */
9450 ctx->sqo_blkcg_css = NULL;
9451 ret = -ENODEV;
9452 goto err;
9453 }
9454#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009455
Jens Axboe2b188cc2019-01-07 10:46:33 -07009456 /*
9457 * Account memory _before_ installing the file descriptor. Once
9458 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009459 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009460 * will un-account as well.
9461 */
9462 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9463 ACCT_LOCKED);
9464 ctx->limit_mem = limit_mem;
9465
9466 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009467 if (ret)
9468 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009469
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009470 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009471 if (ret)
9472 goto err;
9473
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009474 if (!(p->flags & IORING_SETUP_R_DISABLED))
9475 io_sq_offload_start(ctx);
9476
Jens Axboe2b188cc2019-01-07 10:46:33 -07009477 memset(&p->sq_off, 0, sizeof(p->sq_off));
9478 p->sq_off.head = offsetof(struct io_rings, sq.head);
9479 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9480 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9481 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9482 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9483 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9484 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9485
9486 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009487 p->cq_off.head = offsetof(struct io_rings, cq.head);
9488 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9489 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9490 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9491 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9492 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009493 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009494
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009495 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9496 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009497 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9498 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009499
9500 if (copy_to_user(params, p, sizeof(*p))) {
9501 ret = -EFAULT;
9502 goto err;
9503 }
Jens Axboed1719f72020-07-30 13:43:53 -06009504
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009505 file = io_uring_get_file(ctx);
9506 if (IS_ERR(file)) {
9507 ret = PTR_ERR(file);
9508 goto err;
9509 }
9510
Jens Axboed1719f72020-07-30 13:43:53 -06009511 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009512 * Install ring fd as the very last thing, so we don't risk someone
9513 * having closed it before we finish setup
9514 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009515 ret = io_uring_install_fd(ctx, file);
9516 if (ret < 0) {
Pavel Begunkov8cb6f4d2021-01-26 11:17:05 +00009517 io_disable_sqo_submit(ctx);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009518 /* fput will clean it up */
9519 fput(file);
9520 return ret;
9521 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009522
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009523 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009524 return ret;
9525err:
Pavel Begunkova63d9152021-01-26 11:17:03 +00009526 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009527 io_ring_ctx_wait_and_kill(ctx);
9528 return ret;
9529}
9530
9531/*
9532 * Sets up an aio uring context, and returns the fd. Applications asks for a
9533 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9534 * params structure passed in.
9535 */
9536static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9537{
9538 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009539 int i;
9540
9541 if (copy_from_user(&p, params, sizeof(p)))
9542 return -EFAULT;
9543 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9544 if (p.resv[i])
9545 return -EINVAL;
9546 }
9547
Jens Axboe6c271ce2019-01-10 11:22:30 -07009548 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009549 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009550 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9551 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009552 return -EINVAL;
9553
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009554 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009555}
9556
9557SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9558 struct io_uring_params __user *, params)
9559{
9560 return io_uring_setup(entries, params);
9561}
9562
Jens Axboe66f4af92020-01-16 15:36:52 -07009563static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9564{
9565 struct io_uring_probe *p;
9566 size_t size;
9567 int i, ret;
9568
9569 size = struct_size(p, ops, nr_args);
9570 if (size == SIZE_MAX)
9571 return -EOVERFLOW;
9572 p = kzalloc(size, GFP_KERNEL);
9573 if (!p)
9574 return -ENOMEM;
9575
9576 ret = -EFAULT;
9577 if (copy_from_user(p, arg, size))
9578 goto out;
9579 ret = -EINVAL;
9580 if (memchr_inv(p, 0, size))
9581 goto out;
9582
9583 p->last_op = IORING_OP_LAST - 1;
9584 if (nr_args > IORING_OP_LAST)
9585 nr_args = IORING_OP_LAST;
9586
9587 for (i = 0; i < nr_args; i++) {
9588 p->ops[i].op = i;
9589 if (!io_op_defs[i].not_supported)
9590 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9591 }
9592 p->ops_len = i;
9593
9594 ret = 0;
9595 if (copy_to_user(arg, p, size))
9596 ret = -EFAULT;
9597out:
9598 kfree(p);
9599 return ret;
9600}
9601
Jens Axboe071698e2020-01-28 10:04:42 -07009602static int io_register_personality(struct io_ring_ctx *ctx)
9603{
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009604 struct io_identity *iod;
9605 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009606 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009607
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009608 iod = kmalloc(sizeof(*iod), GFP_KERNEL);
9609 if (unlikely(!iod))
Jens Axboe1e6fa522020-10-15 08:46:24 -06009610 return -ENOMEM;
9611
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009612 io_init_identity(iod);
9613 iod->creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009614
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009615 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)iod,
9616 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboe695ab282021-08-24 13:15:01 +01009617 if (ret < 0) {
9618 put_cred(iod->creds);
9619 kfree(iod);
9620 return ret;
9621 }
9622 return id;
Jens Axboe071698e2020-01-28 10:04:42 -07009623}
9624
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009625static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9626 unsigned int nr_args)
9627{
9628 struct io_uring_restriction *res;
9629 size_t size;
9630 int i, ret;
9631
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009632 /* Restrictions allowed only if rings started disabled */
9633 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9634 return -EBADFD;
9635
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009636 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009637 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009638 return -EBUSY;
9639
9640 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9641 return -EINVAL;
9642
9643 size = array_size(nr_args, sizeof(*res));
9644 if (size == SIZE_MAX)
9645 return -EOVERFLOW;
9646
9647 res = memdup_user(arg, size);
9648 if (IS_ERR(res))
9649 return PTR_ERR(res);
9650
9651 ret = 0;
9652
9653 for (i = 0; i < nr_args; i++) {
9654 switch (res[i].opcode) {
9655 case IORING_RESTRICTION_REGISTER_OP:
9656 if (res[i].register_op >= IORING_REGISTER_LAST) {
9657 ret = -EINVAL;
9658 goto out;
9659 }
9660
9661 __set_bit(res[i].register_op,
9662 ctx->restrictions.register_op);
9663 break;
9664 case IORING_RESTRICTION_SQE_OP:
9665 if (res[i].sqe_op >= IORING_OP_LAST) {
9666 ret = -EINVAL;
9667 goto out;
9668 }
9669
9670 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9671 break;
9672 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9673 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9674 break;
9675 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9676 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9677 break;
9678 default:
9679 ret = -EINVAL;
9680 goto out;
9681 }
9682 }
9683
9684out:
9685 /* Reset all restrictions if an error happened */
9686 if (ret != 0)
9687 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9688 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009689 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009690
9691 kfree(res);
9692 return ret;
9693}
9694
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009695static int io_register_enable_rings(struct io_ring_ctx *ctx)
9696{
9697 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9698 return -EBADFD;
9699
9700 if (ctx->restrictions.registered)
9701 ctx->restricted = 1;
9702
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009703 io_sq_offload_start(ctx);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009704 return 0;
9705}
9706
Jens Axboe071698e2020-01-28 10:04:42 -07009707static bool io_register_op_must_quiesce(int op)
9708{
9709 switch (op) {
9710 case IORING_UNREGISTER_FILES:
9711 case IORING_REGISTER_FILES_UPDATE:
9712 case IORING_REGISTER_PROBE:
9713 case IORING_REGISTER_PERSONALITY:
9714 case IORING_UNREGISTER_PERSONALITY:
9715 return false;
9716 default:
9717 return true;
9718 }
9719}
9720
Jens Axboeedafcce2019-01-09 09:16:05 -07009721static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9722 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009723 __releases(ctx->uring_lock)
9724 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009725{
9726 int ret;
9727
Jens Axboe35fa71a2019-04-22 10:23:23 -06009728 /*
9729 * We're inside the ring mutex, if the ref is already dying, then
9730 * someone else killed the ctx or is already going through
9731 * io_uring_register().
9732 */
9733 if (percpu_ref_is_dying(&ctx->refs))
9734 return -ENXIO;
9735
Jens Axboe071698e2020-01-28 10:04:42 -07009736 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009737 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009738
Jens Axboe05f3fb32019-12-09 11:22:50 -07009739 /*
9740 * Drop uring mutex before waiting for references to exit. If
9741 * another thread is currently inside io_uring_enter() it might
9742 * need to grab the uring_lock to make progress. If we hold it
9743 * here across the drain wait, then we can deadlock. It's safe
9744 * to drop the mutex here, since no new references will come in
9745 * after we've killed the percpu ref.
9746 */
9747 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009748 do {
9749 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9750 if (!ret)
9751 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009752 ret = io_run_task_work_sig();
9753 if (ret < 0)
9754 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009755 } while (1);
9756
Jens Axboe05f3fb32019-12-09 11:22:50 -07009757 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009758
Jens Axboec1503682020-01-08 08:26:07 -07009759 if (ret) {
9760 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009761 goto out_quiesce;
9762 }
9763 }
9764
9765 if (ctx->restricted) {
9766 if (opcode >= IORING_REGISTER_LAST) {
9767 ret = -EINVAL;
9768 goto out;
9769 }
9770
9771 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9772 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009773 goto out;
9774 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009775 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009776
9777 switch (opcode) {
9778 case IORING_REGISTER_BUFFERS:
9779 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9780 break;
9781 case IORING_UNREGISTER_BUFFERS:
9782 ret = -EINVAL;
9783 if (arg || nr_args)
9784 break;
9785 ret = io_sqe_buffer_unregister(ctx);
9786 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009787 case IORING_REGISTER_FILES:
9788 ret = io_sqe_files_register(ctx, arg, nr_args);
9789 break;
9790 case IORING_UNREGISTER_FILES:
9791 ret = -EINVAL;
9792 if (arg || nr_args)
9793 break;
9794 ret = io_sqe_files_unregister(ctx);
9795 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009796 case IORING_REGISTER_FILES_UPDATE:
9797 ret = io_sqe_files_update(ctx, arg, nr_args);
9798 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009799 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009800 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009801 ret = -EINVAL;
9802 if (nr_args != 1)
9803 break;
9804 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009805 if (ret)
9806 break;
9807 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9808 ctx->eventfd_async = 1;
9809 else
9810 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009811 break;
9812 case IORING_UNREGISTER_EVENTFD:
9813 ret = -EINVAL;
9814 if (arg || nr_args)
9815 break;
9816 ret = io_eventfd_unregister(ctx);
9817 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009818 case IORING_REGISTER_PROBE:
9819 ret = -EINVAL;
9820 if (!arg || nr_args > 256)
9821 break;
9822 ret = io_probe(ctx, arg, nr_args);
9823 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009824 case IORING_REGISTER_PERSONALITY:
9825 ret = -EINVAL;
9826 if (arg || nr_args)
9827 break;
9828 ret = io_register_personality(ctx);
9829 break;
9830 case IORING_UNREGISTER_PERSONALITY:
9831 ret = -EINVAL;
9832 if (arg)
9833 break;
9834 ret = io_unregister_personality(ctx, nr_args);
9835 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009836 case IORING_REGISTER_ENABLE_RINGS:
9837 ret = -EINVAL;
9838 if (arg || nr_args)
9839 break;
9840 ret = io_register_enable_rings(ctx);
9841 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009842 case IORING_REGISTER_RESTRICTIONS:
9843 ret = io_register_restrictions(ctx, arg, nr_args);
9844 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009845 default:
9846 ret = -EINVAL;
9847 break;
9848 }
9849
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009850out:
Jens Axboe071698e2020-01-28 10:04:42 -07009851 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009852 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009853 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009854out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009855 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009856 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009857 return ret;
9858}
9859
9860SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9861 void __user *, arg, unsigned int, nr_args)
9862{
9863 struct io_ring_ctx *ctx;
9864 long ret = -EBADF;
9865 struct fd f;
9866
9867 f = fdget(fd);
9868 if (!f.file)
9869 return -EBADF;
9870
9871 ret = -EOPNOTSUPP;
9872 if (f.file->f_op != &io_uring_fops)
9873 goto out_fput;
9874
9875 ctx = f.file->private_data;
9876
9877 mutex_lock(&ctx->uring_lock);
9878 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9879 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009880 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9881 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009882out_fput:
9883 fdput(f);
9884 return ret;
9885}
9886
Jens Axboe2b188cc2019-01-07 10:46:33 -07009887static int __init io_uring_init(void)
9888{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009889#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9890 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9891 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9892} while (0)
9893
9894#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9895 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9896 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9897 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9898 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9899 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9900 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9901 BUILD_BUG_SQE_ELEM(8, __u64, off);
9902 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9903 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009904 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009905 BUILD_BUG_SQE_ELEM(24, __u32, len);
9906 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9907 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9908 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9909 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009910 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9911 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009912 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9913 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9914 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9915 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9916 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9917 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9918 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9919 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009920 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009921 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9922 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9923 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009924 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009925
Jens Axboed3656342019-12-18 09:50:26 -07009926 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009927 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009928 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9929 return 0;
9930};
9931__initcall(io_uring_init);