blob: 019cbde8c3d67dc269b49d14bfb0f95fb5e751d1 [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
Pavel Begunkov3f2c12e2021-02-18 22:32:51 +00002078 mutex_lock(&ctx->uring_lock);
Jens Axboec40f6372020-06-25 15:39:59 -06002079 __io_req_task_cancel(req, -ECANCELED);
Pavel Begunkov3f2c12e2021-02-18 22:32:51 +00002080 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002081 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002082}
2083
2084static void __io_req_task_submit(struct io_kiocb *req)
2085{
2086 struct io_ring_ctx *ctx = req->ctx;
2087
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002088 mutex_lock(&ctx->uring_lock);
Pavel Begunkova63d9152021-01-26 11:17:03 +00002089 if (!ctx->sqo_dead && !__io_sq_thread_acquire_mm(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002090 __io_queue_sqe(req, NULL);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002091 else
Jens Axboec40f6372020-06-25 15:39:59 -06002092 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002093 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov5592eae2021-02-09 04:47:50 +00002094
2095 if (ctx->flags & IORING_SETUP_SQPOLL)
2096 io_sq_thread_drop_mm();
Jens Axboe9e645e112019-05-10 16:07:28 -06002097}
2098
Jens Axboec40f6372020-06-25 15:39:59 -06002099static void io_req_task_submit(struct callback_head *cb)
2100{
2101 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002102 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002103
2104 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002105 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002106}
2107
2108static void io_req_task_queue(struct io_kiocb *req)
2109{
Jens Axboec40f6372020-06-25 15:39:59 -06002110 int ret;
2111
2112 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002113 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002114
Jens Axboe87c43112020-09-30 21:00:14 -06002115 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06002116 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002117 struct task_struct *tsk;
2118
Jens Axboec40f6372020-06-25 15:39:59 -06002119 init_task_work(&req->task_work, io_req_task_cancel);
2120 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002121 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002122 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002123 }
Jens Axboec40f6372020-06-25 15:39:59 -06002124}
2125
Pavel Begunkovc3524382020-06-28 12:52:32 +03002126static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002127{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002128 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002129
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002130 if (nxt)
2131 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002132}
2133
Jens Axboe9e645e112019-05-10 16:07:28 -06002134static void io_free_req(struct io_kiocb *req)
2135{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002136 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002137 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002138}
2139
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002140struct req_batch {
2141 void *reqs[IO_IOPOLL_BATCH];
2142 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002143
2144 struct task_struct *task;
2145 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002146};
2147
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002148static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002149{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002150 rb->to_free = 0;
2151 rb->task_refs = 0;
2152 rb->task = NULL;
2153}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002154
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002155static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2156 struct req_batch *rb)
2157{
2158 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2159 percpu_ref_put_many(&ctx->refs, rb->to_free);
2160 rb->to_free = 0;
2161}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002162
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002163static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2164 struct req_batch *rb)
2165{
2166 if (rb->to_free)
2167 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002168 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002169 struct io_uring_task *tctx = rb->task->io_uring;
2170
2171 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboeca758722021-01-16 11:52:11 -07002172 if (atomic_read(&tctx->in_idle))
2173 wake_up(&tctx->wait);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002174 put_task_struct_many(rb->task, rb->task_refs);
2175 rb->task = NULL;
2176 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002177}
2178
2179static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2180{
2181 if (unlikely(io_is_fallback_req(req))) {
2182 io_free_req(req);
2183 return;
2184 }
2185 if (req->flags & REQ_F_LINK_HEAD)
2186 io_queue_next(req);
2187
Jens Axboee3bc8e92020-09-24 08:45:57 -06002188 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002189 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002190 struct io_uring_task *tctx = rb->task->io_uring;
2191
2192 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboeca758722021-01-16 11:52:11 -07002193 if (atomic_read(&tctx->in_idle))
2194 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002195 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002196 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002197 rb->task = req->task;
2198 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002199 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002200 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002201
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002202 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002203 rb->reqs[rb->to_free++] = req;
2204 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2205 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002206}
2207
Jens Axboeba816ad2019-09-28 11:36:45 -06002208/*
2209 * Drop reference to request, return next in chain (if there is one) if this
2210 * was the last reference to this request.
2211 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002212static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002213{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002214 struct io_kiocb *nxt = NULL;
2215
Jens Axboe2a44f462020-02-25 13:25:41 -07002216 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002217 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002218 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002219 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002220 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002221}
2222
Jens Axboe2b188cc2019-01-07 10:46:33 -07002223static void io_put_req(struct io_kiocb *req)
2224{
Jens Axboedef596e2019-01-09 08:59:42 -07002225 if (refcount_dec_and_test(&req->refs))
2226 io_free_req(req);
2227}
2228
Pavel Begunkov216578e2020-10-13 09:44:00 +01002229static void io_put_req_deferred_cb(struct callback_head *cb)
2230{
2231 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2232
2233 io_free_req(req);
2234}
2235
2236static void io_free_req_deferred(struct io_kiocb *req)
2237{
2238 int ret;
2239
2240 init_task_work(&req->task_work, io_put_req_deferred_cb);
2241 ret = io_req_task_work_add(req, true);
2242 if (unlikely(ret)) {
2243 struct task_struct *tsk;
2244
2245 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002246 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002247 wake_up_process(tsk);
2248 }
2249}
2250
2251static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2252{
2253 if (refcount_sub_and_test(refs, &req->refs))
2254 io_free_req_deferred(req);
2255}
2256
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002257static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002258{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002259 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002260
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002261 /*
2262 * A ref is owned by io-wq in which context we're. So, if that's the
2263 * last one, it's safe to steal next work. False negatives are Ok,
2264 * it just will be re-punted async in io_put_work()
2265 */
2266 if (refcount_read(&req->refs) != 1)
2267 return NULL;
2268
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002269 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002270 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002271}
2272
Jens Axboe978db572019-11-14 22:39:04 -07002273static void io_double_put_req(struct io_kiocb *req)
2274{
2275 /* drop both submit and complete references */
2276 if (refcount_sub_and_test(2, &req->refs))
2277 io_free_req(req);
2278}
2279
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002280static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002281{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002282 struct io_rings *rings = ctx->rings;
2283
Jens Axboea3a0e432019-08-20 11:03:11 -06002284 /* See comment at the top of this file */
2285 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002286 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002287}
2288
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002289static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2290{
2291 struct io_rings *rings = ctx->rings;
2292
2293 /* make sure SQ entry isn't read before tail */
2294 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2295}
2296
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002297static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002298{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002299 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002300
Jens Axboebcda7ba2020-02-23 16:42:51 -07002301 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2302 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002303 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002304 kfree(kbuf);
2305 return cflags;
2306}
2307
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002308static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2309{
2310 struct io_buffer *kbuf;
2311
2312 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2313 return io_put_kbuf(req, kbuf);
2314}
2315
Jens Axboe4c6e2772020-07-01 11:29:10 -06002316static inline bool io_run_task_work(void)
2317{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002318 /*
2319 * Not safe to run on exiting task, and the task_work handling will
2320 * not add work to such a task.
2321 */
2322 if (unlikely(current->flags & PF_EXITING))
2323 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002324 if (current->task_works) {
2325 __set_current_state(TASK_RUNNING);
2326 task_work_run();
2327 return true;
2328 }
2329
2330 return false;
2331}
2332
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002333static void io_iopoll_queue(struct list_head *again)
2334{
2335 struct io_kiocb *req;
2336
2337 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002338 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2339 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002340 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002341 } while (!list_empty(again));
2342}
2343
Jens Axboedef596e2019-01-09 08:59:42 -07002344/*
2345 * Find and free completed poll iocbs
2346 */
2347static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2348 struct list_head *done)
2349{
Jens Axboe8237e042019-12-28 10:48:22 -07002350 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002351 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002352 LIST_HEAD(again);
2353
2354 /* order with ->result store in io_complete_rw_iopoll() */
2355 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002356
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002357 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002358 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002359 int cflags = 0;
2360
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002361 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002362 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002363 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002364 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002365 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002366 continue;
2367 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002368 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002369
Jens Axboebcda7ba2020-02-23 16:42:51 -07002370 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002371 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002372
2373 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002374 (*nr_events)++;
2375
Pavel Begunkovc3524382020-06-28 12:52:32 +03002376 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002377 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002378 }
Jens Axboedef596e2019-01-09 08:59:42 -07002379
Jens Axboe09bb8392019-03-13 12:39:28 -06002380 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002381 if (ctx->flags & IORING_SETUP_SQPOLL)
2382 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002383 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002384
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002385 if (!list_empty(&again))
2386 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002387}
2388
Jens Axboedef596e2019-01-09 08:59:42 -07002389static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2390 long min)
2391{
2392 struct io_kiocb *req, *tmp;
2393 LIST_HEAD(done);
2394 bool spin;
2395 int ret;
2396
2397 /*
2398 * Only spin for completions if we don't have multiple devices hanging
2399 * off our complete list, and we're under the requested amount.
2400 */
2401 spin = !ctx->poll_multi_file && *nr_events < min;
2402
2403 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002404 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002405 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002406
2407 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002408 * Move completed and retryable entries to our local lists.
2409 * If we find a request that requires polling, break out
2410 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002411 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002412 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002413 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002414 continue;
2415 }
2416 if (!list_empty(&done))
2417 break;
2418
2419 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2420 if (ret < 0)
2421 break;
2422
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002423 /* iopoll may have completed current req */
2424 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002425 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002426
Jens Axboedef596e2019-01-09 08:59:42 -07002427 if (ret && spin)
2428 spin = false;
2429 ret = 0;
2430 }
2431
2432 if (!list_empty(&done))
2433 io_iopoll_complete(ctx, nr_events, &done);
2434
2435 return ret;
2436}
2437
2438/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002439 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002440 * non-spinning poll check - we'll still enter the driver poll loop, but only
2441 * as a non-spinning completion check.
2442 */
2443static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2444 long min)
2445{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002446 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002447 int ret;
2448
2449 ret = io_do_iopoll(ctx, nr_events, min);
2450 if (ret < 0)
2451 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002452 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002453 return 0;
2454 }
2455
2456 return 1;
2457}
2458
2459/*
2460 * We can't just wait for polled events to come to us, we have to actively
2461 * find and complete them.
2462 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002463static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002464{
2465 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2466 return;
2467
2468 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002469 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002470 unsigned int nr_events = 0;
2471
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002472 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002473
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002474 /* let it sleep and repeat later if can't complete a request */
2475 if (nr_events == 0)
2476 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002477 /*
2478 * Ensure we allow local-to-the-cpu processing to take place,
2479 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002480 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002481 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002482 if (need_resched()) {
2483 mutex_unlock(&ctx->uring_lock);
2484 cond_resched();
2485 mutex_lock(&ctx->uring_lock);
2486 }
Jens Axboedef596e2019-01-09 08:59:42 -07002487 }
2488 mutex_unlock(&ctx->uring_lock);
2489}
2490
Pavel Begunkov7668b922020-07-07 16:36:21 +03002491static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002492{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002493 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002494 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002495
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002496 /*
2497 * We disallow the app entering submit/complete with polling, but we
2498 * still need to lock the ring to prevent racing with polled issue
2499 * that got punted to a workqueue.
2500 */
2501 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002502 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002503 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002504 * Don't enter poll loop if we already have events pending.
2505 * If we do, we can potentially be spinning for commands that
2506 * already triggered a CQE (eg in error).
2507 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002508 if (test_bit(0, &ctx->cq_check_overflow))
2509 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2510 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002511 break;
2512
2513 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002514 * If a submit got punted to a workqueue, we can have the
2515 * application entering polling for a command before it gets
2516 * issued. That app will hold the uring_lock for the duration
2517 * of the poll right here, so we need to take a breather every
2518 * now and then to ensure that the issue has a chance to add
2519 * the poll to the issued list. Otherwise we can spin here
2520 * forever, while the workqueue is stuck trying to acquire the
2521 * very same mutex.
2522 */
2523 if (!(++iters & 7)) {
2524 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002525 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002526 mutex_lock(&ctx->uring_lock);
2527 }
2528
Pavel Begunkov7668b922020-07-07 16:36:21 +03002529 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002530 if (ret <= 0)
2531 break;
2532 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002533 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002534
Jens Axboe500f9fb2019-08-19 12:15:59 -06002535 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002536 return ret;
2537}
2538
Jens Axboe491381ce2019-10-17 09:20:46 -06002539static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540{
Jens Axboe491381ce2019-10-17 09:20:46 -06002541 /*
2542 * Tell lockdep we inherited freeze protection from submission
2543 * thread.
2544 */
2545 if (req->flags & REQ_F_ISREG) {
2546 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547
Jens Axboe491381ce2019-10-17 09:20:46 -06002548 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002550 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002551}
2552
Jens Axboea1d7c392020-06-22 11:09:46 -06002553static void io_complete_rw_common(struct kiocb *kiocb, long res,
2554 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555{
Jens Axboe9adbd452019-12-20 08:45:55 -07002556 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002557 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002558
Jens Axboe491381ce2019-10-17 09:20:46 -06002559 if (kiocb->ki_flags & IOCB_WRITE)
2560 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002561
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002562 if (res != req->result)
2563 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002564 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002565 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002566 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002567}
2568
Jens Axboeb63534c2020-06-04 11:28:00 -06002569#ifdef CONFIG_BLOCK
2570static bool io_resubmit_prep(struct io_kiocb *req, int error)
2571{
2572 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2573 ssize_t ret = -ECANCELED;
2574 struct iov_iter iter;
2575 int rw;
2576
2577 if (error) {
2578 ret = error;
2579 goto end_req;
2580 }
2581
2582 switch (req->opcode) {
2583 case IORING_OP_READV:
2584 case IORING_OP_READ_FIXED:
2585 case IORING_OP_READ:
2586 rw = READ;
2587 break;
2588 case IORING_OP_WRITEV:
2589 case IORING_OP_WRITE_FIXED:
2590 case IORING_OP_WRITE:
2591 rw = WRITE;
2592 break;
2593 default:
2594 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2595 req->opcode);
2596 goto end_req;
2597 }
2598
Jens Axboee8c2bc12020-08-15 18:44:09 -07002599 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002600 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2601 if (ret < 0)
2602 goto end_req;
2603 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2604 if (!ret)
2605 return true;
2606 kfree(iovec);
2607 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002608 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002609 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002610end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002611 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002612 return false;
2613}
Jens Axboeb63534c2020-06-04 11:28:00 -06002614#endif
2615
2616static bool io_rw_reissue(struct io_kiocb *req, long res)
2617{
2618#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002619 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002620 int ret;
2621
Jens Axboe355afae2020-09-02 09:30:31 -06002622 if (!S_ISBLK(mode) && !S_ISREG(mode))
2623 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002624 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2625 return false;
Jens Axboe3c08f772021-02-23 19:17:35 -07002626 /*
2627 * If ref is dying, we might be running poll reap from the exit work.
2628 * Don't attempt to reissue from that path, just let it fail with
2629 * -EAGAIN.
2630 */
2631 if (percpu_ref_is_dying(&req->ctx->refs))
2632 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002633
Jens Axboefdee9462020-08-27 16:46:24 -06002634 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002635
Jens Axboefdee9462020-08-27 16:46:24 -06002636 if (io_resubmit_prep(req, ret)) {
2637 refcount_inc(&req->refs);
2638 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002639 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002640 }
2641
Jens Axboeb63534c2020-06-04 11:28:00 -06002642#endif
2643 return false;
2644}
2645
Jens Axboea1d7c392020-06-22 11:09:46 -06002646static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2647 struct io_comp_state *cs)
2648{
2649 if (!io_rw_reissue(req, res))
2650 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002651}
2652
2653static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2654{
Jens Axboe9adbd452019-12-20 08:45:55 -07002655 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002656
Jens Axboea1d7c392020-06-22 11:09:46 -06002657 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002658}
2659
Jens Axboedef596e2019-01-09 08:59:42 -07002660static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2661{
Jens Axboe9adbd452019-12-20 08:45:55 -07002662 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002663
Jens Axboe491381ce2019-10-17 09:20:46 -06002664 if (kiocb->ki_flags & IOCB_WRITE)
2665 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002666
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002667 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002668 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002669
2670 WRITE_ONCE(req->result, res);
2671 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002672 smp_wmb();
2673 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002674}
2675
2676/*
2677 * After the iocb has been issued, it's safe to be found on the poll list.
2678 * Adding the kiocb to the list AFTER submission ensures that we don't
2679 * find it from a io_iopoll_getevents() thread before the issuer is done
2680 * accessing the kiocb cookie.
2681 */
2682static void io_iopoll_req_issued(struct io_kiocb *req)
2683{
2684 struct io_ring_ctx *ctx = req->ctx;
2685
2686 /*
2687 * Track whether we have multiple files in our lists. This will impact
2688 * how we do polling eventually, not spinning if we're on potentially
2689 * different devices.
2690 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002691 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002692 ctx->poll_multi_file = false;
2693 } else if (!ctx->poll_multi_file) {
2694 struct io_kiocb *list_req;
2695
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002696 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002697 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002698 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002699 ctx->poll_multi_file = true;
2700 }
2701
2702 /*
2703 * For fast devices, IO may have already completed. If it has, add
2704 * it to the front so we find it first.
2705 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002706 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002707 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002708 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002709 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002710
2711 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002712 wq_has_sleeper(&ctx->sq_data->wait))
2713 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002714}
2715
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002716static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002717{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002718 if (state->has_refs)
2719 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002720 state->file = NULL;
2721}
2722
2723static inline void io_state_file_put(struct io_submit_state *state)
2724{
2725 if (state->file)
2726 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002727}
2728
2729/*
2730 * Get as many references to a file as we have IOs left in this submission,
2731 * assuming most submissions are for one file, or at least that each file
2732 * has more than one submission.
2733 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002734static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002735{
2736 if (!state)
2737 return fget(fd);
2738
2739 if (state->file) {
2740 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002741 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002742 return state->file;
2743 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002744 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002745 }
2746 state->file = fget_many(fd, state->ios_left);
2747 if (!state->file)
2748 return NULL;
2749
2750 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002751 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002752 return state->file;
2753}
2754
Jens Axboe4503b762020-06-01 10:00:27 -06002755static bool io_bdev_nowait(struct block_device *bdev)
2756{
2757#ifdef CONFIG_BLOCK
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002758 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002759#else
2760 return true;
2761#endif
2762}
2763
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764/*
2765 * If we tracked the file through the SCM inflight mechanism, we could support
2766 * any file. For now, just ensure that anything potentially problematic is done
2767 * inline.
2768 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002769static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002770{
2771 umode_t mode = file_inode(file)->i_mode;
2772
Jens Axboe4503b762020-06-01 10:00:27 -06002773 if (S_ISBLK(mode)) {
2774 if (io_bdev_nowait(file->f_inode->i_bdev))
2775 return true;
2776 return false;
2777 }
Pavel Begunkova75457f2021-06-09 12:07:25 +01002778 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002780 if (S_ISREG(mode)) {
2781 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2782 file->f_op != &io_uring_fops)
2783 return true;
2784 return false;
2785 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002786
Jens Axboec5b85622020-06-09 19:23:05 -06002787 /* any ->read/write should understand O_NONBLOCK */
2788 if (file->f_flags & O_NONBLOCK)
2789 return true;
2790
Jens Axboeaf197f52020-04-28 13:15:06 -06002791 if (!(file->f_mode & FMODE_NOWAIT))
2792 return false;
2793
2794 if (rw == READ)
2795 return file->f_op->read_iter != NULL;
2796
2797 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798}
2799
Pavel Begunkova88fc402020-09-30 22:57:53 +03002800static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801{
Jens Axboedef596e2019-01-09 08:59:42 -07002802 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002803 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002804 unsigned ioprio;
2805 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806
Jens Axboe491381ce2019-10-17 09:20:46 -06002807 if (S_ISREG(file_inode(req->file)->i_mode))
2808 req->flags |= REQ_F_ISREG;
2809
Jens Axboe2b188cc2019-01-07 10:46:33 -07002810 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002811 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2812 req->flags |= REQ_F_CUR_POS;
2813 kiocb->ki_pos = req->file->f_pos;
2814 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002816 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2817 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2818 if (unlikely(ret))
2819 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002820
2821 ioprio = READ_ONCE(sqe->ioprio);
2822 if (ioprio) {
2823 ret = ioprio_check_cap(ioprio);
2824 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002825 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002826
2827 kiocb->ki_ioprio = ioprio;
2828 } else
2829 kiocb->ki_ioprio = get_current_ioprio();
2830
Stefan Bühler8449eed2019-04-27 20:34:19 +02002831 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002832 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002833 req->flags |= REQ_F_NOWAIT;
2834
Jens Axboedef596e2019-01-09 08:59:42 -07002835 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002836 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2837 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002838 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002839
Jens Axboedef596e2019-01-09 08:59:42 -07002840 kiocb->ki_flags |= IOCB_HIPRI;
2841 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002842 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002843 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002844 if (kiocb->ki_flags & IOCB_HIPRI)
2845 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002846 kiocb->ki_complete = io_complete_rw;
2847 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002848
Jens Axboe3529d8c2019-12-19 18:24:38 -07002849 req->rw.addr = READ_ONCE(sqe->addr);
2850 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002851 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853}
2854
2855static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2856{
2857 switch (ret) {
2858 case -EIOCBQUEUED:
2859 break;
2860 case -ERESTARTSYS:
2861 case -ERESTARTNOINTR:
2862 case -ERESTARTNOHAND:
2863 case -ERESTART_RESTARTBLOCK:
2864 /*
2865 * We can't just restart the syscall, since previously
2866 * submitted sqes may already be in progress. Just fail this
2867 * IO with EINTR.
2868 */
2869 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002870 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871 default:
2872 kiocb->ki_complete(kiocb, ret, 0);
2873 }
2874}
2875
Jens Axboea1d7c392020-06-22 11:09:46 -06002876static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2877 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002878{
Jens Axboeba042912019-12-25 16:33:42 -07002879 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002880 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002881
Jens Axboe227c0c92020-08-13 11:51:40 -06002882 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002883 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002884 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002885 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002886 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002887 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002888 }
2889
Jens Axboeba042912019-12-25 16:33:42 -07002890 if (req->flags & REQ_F_CUR_POS)
2891 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002892 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002893 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002894 else
2895 io_rw_done(kiocb, ret);
2896}
2897
Jens Axboe9adbd452019-12-20 08:45:55 -07002898static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002899 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002900{
Jens Axboe9adbd452019-12-20 08:45:55 -07002901 struct io_ring_ctx *ctx = req->ctx;
2902 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002903 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002904 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002905 size_t offset;
2906 u64 buf_addr;
2907
Jens Axboeedafcce2019-01-09 09:16:05 -07002908 if (unlikely(buf_index >= ctx->nr_user_bufs))
2909 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002910 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2911 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002912 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002913
2914 /* overflow */
2915 if (buf_addr + len < buf_addr)
2916 return -EFAULT;
2917 /* not inside the mapped region */
2918 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2919 return -EFAULT;
2920
2921 /*
2922 * May not be a start of buffer, set size appropriately
2923 * and advance us to the beginning.
2924 */
2925 offset = buf_addr - imu->ubuf;
2926 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002927
2928 if (offset) {
2929 /*
2930 * Don't use iov_iter_advance() here, as it's really slow for
2931 * using the latter parts of a big fixed buffer - it iterates
2932 * over each segment manually. We can cheat a bit here, because
2933 * we know that:
2934 *
2935 * 1) it's a BVEC iter, we set it up
2936 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2937 * first and last bvec
2938 *
2939 * So just find our index, and adjust the iterator afterwards.
2940 * If the offset is within the first bvec (or the whole first
2941 * bvec, just use iov_iter_advance(). This makes it easier
2942 * since we can just skip the first segment, which may not
2943 * be PAGE_SIZE aligned.
2944 */
2945 const struct bio_vec *bvec = imu->bvec;
2946
2947 if (offset <= bvec->bv_len) {
2948 iov_iter_advance(iter, offset);
2949 } else {
2950 unsigned long seg_skip;
2951
2952 /* skip first vec */
2953 offset -= bvec->bv_len;
2954 seg_skip = 1 + (offset >> PAGE_SHIFT);
2955
2956 iter->bvec = bvec + seg_skip;
2957 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002958 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002959 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002960 }
2961 }
2962
Jens Axboe5e559562019-11-13 16:12:46 -07002963 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002964}
2965
Jens Axboebcda7ba2020-02-23 16:42:51 -07002966static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2967{
2968 if (needs_lock)
2969 mutex_unlock(&ctx->uring_lock);
2970}
2971
2972static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2973{
2974 /*
2975 * "Normal" inline submissions always hold the uring_lock, since we
2976 * grab it from the system call. Same is true for the SQPOLL offload.
2977 * The only exception is when we've detached the request and issue it
2978 * from an async worker thread, grab the lock for that case.
2979 */
2980 if (needs_lock)
2981 mutex_lock(&ctx->uring_lock);
2982}
2983
2984static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2985 int bgid, struct io_buffer *kbuf,
2986 bool needs_lock)
2987{
2988 struct io_buffer *head;
2989
2990 if (req->flags & REQ_F_BUFFER_SELECTED)
2991 return kbuf;
2992
2993 io_ring_submit_lock(req->ctx, needs_lock);
2994
2995 lockdep_assert_held(&req->ctx->uring_lock);
2996
Jens Axboe90731882021-07-13 17:18:35 +08002997 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002998 if (head) {
2999 if (!list_empty(&head->list)) {
3000 kbuf = list_last_entry(&head->list, struct io_buffer,
3001 list);
3002 list_del(&kbuf->list);
3003 } else {
3004 kbuf = head;
Jens Axboe90731882021-07-13 17:18:35 +08003005 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003006 }
3007 if (*len > kbuf->len)
3008 *len = kbuf->len;
3009 } else {
3010 kbuf = ERR_PTR(-ENOBUFS);
3011 }
3012
3013 io_ring_submit_unlock(req->ctx, needs_lock);
3014
3015 return kbuf;
3016}
3017
Jens Axboe4d954c22020-02-27 07:31:19 -07003018static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3019 bool needs_lock)
3020{
3021 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003022 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003023
3024 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003025 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003026 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3027 if (IS_ERR(kbuf))
3028 return kbuf;
3029 req->rw.addr = (u64) (unsigned long) kbuf;
3030 req->flags |= REQ_F_BUFFER_SELECTED;
3031 return u64_to_user_ptr(kbuf->addr);
3032}
3033
3034#ifdef CONFIG_COMPAT
3035static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3036 bool needs_lock)
3037{
3038 struct compat_iovec __user *uiov;
3039 compat_ssize_t clen;
3040 void __user *buf;
3041 ssize_t len;
3042
3043 uiov = u64_to_user_ptr(req->rw.addr);
3044 if (!access_ok(uiov, sizeof(*uiov)))
3045 return -EFAULT;
3046 if (__get_user(clen, &uiov->iov_len))
3047 return -EFAULT;
3048 if (clen < 0)
3049 return -EINVAL;
3050
3051 len = clen;
3052 buf = io_rw_buffer_select(req, &len, needs_lock);
3053 if (IS_ERR(buf))
3054 return PTR_ERR(buf);
3055 iov[0].iov_base = buf;
3056 iov[0].iov_len = (compat_size_t) len;
3057 return 0;
3058}
3059#endif
3060
3061static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3062 bool needs_lock)
3063{
3064 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3065 void __user *buf;
3066 ssize_t len;
3067
3068 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3069 return -EFAULT;
3070
3071 len = iov[0].iov_len;
3072 if (len < 0)
3073 return -EINVAL;
3074 buf = io_rw_buffer_select(req, &len, needs_lock);
3075 if (IS_ERR(buf))
3076 return PTR_ERR(buf);
3077 iov[0].iov_base = buf;
3078 iov[0].iov_len = len;
3079 return 0;
3080}
3081
3082static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3083 bool needs_lock)
3084{
Jens Axboedddb3e22020-06-04 11:27:01 -06003085 if (req->flags & REQ_F_BUFFER_SELECTED) {
3086 struct io_buffer *kbuf;
3087
3088 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3089 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3090 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003091 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003092 }
Pavel Begunkov72a016d2020-12-19 03:15:43 +00003093 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003094 return -EINVAL;
3095
3096#ifdef CONFIG_COMPAT
3097 if (req->ctx->compat)
3098 return io_compat_import(req, iov, needs_lock);
3099#endif
3100
3101 return __io_iov_buffer_select(req, iov, needs_lock);
3102}
3103
Jens Axboe8452fd02020-08-18 13:58:33 -07003104static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
3105 struct iovec **iovec, struct iov_iter *iter,
3106 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003107{
Jens Axboe9adbd452019-12-20 08:45:55 -07003108 void __user *buf = u64_to_user_ptr(req->rw.addr);
3109 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003110 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003111 u8 opcode;
3112
Jens Axboed625c6e2019-12-17 19:53:05 -07003113 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003114 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003115 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003116 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003117 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003118
Jens Axboebcda7ba2020-02-23 16:42:51 -07003119 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003120 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003121 return -EINVAL;
3122
Jens Axboe3a6820f2019-12-22 15:19:35 -07003123 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003124 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003125 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003126 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003127 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003128 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003129 }
3130
Jens Axboe3a6820f2019-12-22 15:19:35 -07003131 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3132 *iovec = NULL;
David Laight6930a2a2020-11-07 13:16:25 +00003133 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003134 }
3135
Jens Axboe4d954c22020-02-27 07:31:19 -07003136 if (req->flags & REQ_F_BUFFER_SELECT) {
3137 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003138 if (!ret) {
3139 ret = (*iovec)->iov_len;
3140 iov_iter_init(iter, rw, *iovec, 1, ret);
3141 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003142 *iovec = NULL;
3143 return ret;
3144 }
3145
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003146 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3147 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148}
3149
Jens Axboe8452fd02020-08-18 13:58:33 -07003150static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3151 struct iovec **iovec, struct iov_iter *iter,
3152 bool needs_lock)
3153{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003154 struct io_async_rw *iorw = req->async_data;
3155
3156 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003157 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3158 *iovec = NULL;
David Laight6930a2a2020-11-07 13:16:25 +00003159 return 0;
Jens Axboe8452fd02020-08-18 13:58:33 -07003160}
3161
Jens Axboe0fef9482020-08-26 10:36:20 -06003162static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3163{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003164 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003165}
3166
Jens Axboe32960612019-09-23 11:05:34 -06003167/*
3168 * For files that don't have ->read_iter() and ->write_iter(), handle them
3169 * by looping over ->read() or ->write() manually.
3170 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003171static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003172{
Jens Axboe4017eb92020-10-22 14:14:12 -06003173 struct kiocb *kiocb = &req->rw.kiocb;
3174 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003175 ssize_t ret = 0;
3176
3177 /*
3178 * Don't support polled IO through this interface, and we can't
3179 * support non-blocking either. For the latter, this just causes
3180 * the kiocb to be handled from an async context.
3181 */
3182 if (kiocb->ki_flags & IOCB_HIPRI)
3183 return -EOPNOTSUPP;
3184 if (kiocb->ki_flags & IOCB_NOWAIT)
3185 return -EAGAIN;
3186
3187 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003188 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003189 ssize_t nr;
3190
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003191 if (!iov_iter_is_bvec(iter)) {
3192 iovec = iov_iter_iovec(iter);
3193 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003194 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3195 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003196 }
3197
Jens Axboe32960612019-09-23 11:05:34 -06003198 if (rw == READ) {
3199 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003200 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003201 } else {
3202 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003203 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003204 }
3205
3206 if (nr < 0) {
3207 if (!ret)
3208 ret = nr;
3209 break;
3210 }
Jens Axboece8f81b2021-09-12 06:45:07 -06003211 if (!iov_iter_is_bvec(iter)) {
3212 iov_iter_advance(iter, nr);
3213 } else {
3214 req->rw.len -= nr;
3215 req->rw.addr += nr;
3216 }
Jens Axboe32960612019-09-23 11:05:34 -06003217 ret += nr;
3218 if (nr != iovec.iov_len)
3219 break;
Jens Axboe32960612019-09-23 11:05:34 -06003220 }
3221
3222 return ret;
3223}
3224
Jens Axboeff6165b2020-08-13 09:47:43 -06003225static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3226 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003227{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003228 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003229
Jens Axboeff6165b2020-08-13 09:47:43 -06003230 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003231 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003232 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003233 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003234 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003235 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003236 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003237 unsigned iov_off = 0;
3238
3239 rw->iter.iov = rw->fast_iov;
3240 if (iter->iov != fast_iov) {
3241 iov_off = iter->iov - fast_iov;
3242 rw->iter.iov += iov_off;
3243 }
3244 if (rw->fast_iov != fast_iov)
3245 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003246 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003247 } else {
3248 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003249 }
3250}
3251
Jens Axboee8c2bc12020-08-15 18:44:09 -07003252static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003253{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003254 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3255 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3256 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003257}
3258
Jens Axboee8c2bc12020-08-15 18:44:09 -07003259static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003260{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003261 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003262 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003263
Jens Axboee8c2bc12020-08-15 18:44:09 -07003264 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003265}
3266
Jens Axboeff6165b2020-08-13 09:47:43 -06003267static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3268 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003269 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003270{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003271 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003272 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003273 if (!req->async_data) {
3274 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003275 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003276
Jens Axboeff6165b2020-08-13 09:47:43 -06003277 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003278 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003279 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003280}
3281
Pavel Begunkov73debe62020-09-30 22:57:54 +03003282static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003283{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003284 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003285 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003286 ssize_t ret;
3287
Pavel Begunkov73debe62020-09-30 22:57:54 +03003288 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003289 if (unlikely(ret < 0))
3290 return ret;
3291
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003292 iorw->bytes_done = 0;
3293 iorw->free_iovec = iov;
3294 if (iov)
3295 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003296 return 0;
3297}
3298
Pavel Begunkov73debe62020-09-30 22:57:54 +03003299static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003300{
3301 ssize_t ret;
3302
Pavel Begunkova88fc402020-09-30 22:57:53 +03003303 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003304 if (ret)
3305 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003306
Jens Axboe3529d8c2019-12-19 18:24:38 -07003307 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3308 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003309
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003310 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003311 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003312 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003313 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003314}
3315
Jens Axboec1dd91d2020-08-03 16:43:59 -06003316/*
3317 * This is our waitqueue callback handler, registered through lock_page_async()
3318 * when we initially tried to do the IO with the iocb armed our waitqueue.
3319 * This gets called when the page is unlocked, and we generally expect that to
3320 * happen when the page IO is completed and the page is now uptodate. This will
3321 * queue a task_work based retry of the operation, attempting to copy the data
3322 * again. If the latter fails because the page was NOT uptodate, then we will
3323 * do a thread based blocking retry of the operation. That's the unexpected
3324 * slow path.
3325 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003326static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3327 int sync, void *arg)
3328{
3329 struct wait_page_queue *wpq;
3330 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003331 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003332 int ret;
3333
3334 wpq = container_of(wait, struct wait_page_queue, wait);
3335
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003336 if (!wake_page_match(wpq, key))
3337 return 0;
3338
Hao Xuc8d317a2020-09-29 20:00:45 +08003339 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003340 list_del_init(&wait->entry);
3341
Pavel Begunkove7375122020-07-12 20:42:04 +03003342 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003343 percpu_ref_get(&req->ctx->refs);
3344
Jens Axboebcf5a062020-05-22 09:24:42 -06003345 /* submit ref gets dropped, acquire a new one */
3346 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003347 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003348 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003349 struct task_struct *tsk;
3350
Jens Axboebcf5a062020-05-22 09:24:42 -06003351 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003352 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003353 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003354 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003355 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003356 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003357 return 1;
3358}
3359
Jens Axboec1dd91d2020-08-03 16:43:59 -06003360/*
3361 * This controls whether a given IO request should be armed for async page
3362 * based retry. If we return false here, the request is handed to the async
3363 * worker threads for retry. If we're doing buffered reads on a regular file,
3364 * we prepare a private wait_page_queue entry and retry the operation. This
3365 * will either succeed because the page is now uptodate and unlocked, or it
3366 * will register a callback when the page is unlocked at IO completion. Through
3367 * that callback, io_uring uses task_work to setup a retry of the operation.
3368 * That retry will attempt the buffered read again. The retry will generally
3369 * succeed, or in rare cases where it fails, we then fall back to using the
3370 * async worker threads for a blocking retry.
3371 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003372static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003373{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003374 struct io_async_rw *rw = req->async_data;
3375 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003376 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003377
3378 /* never retry for NOWAIT, we just complete with -EAGAIN */
3379 if (req->flags & REQ_F_NOWAIT)
3380 return false;
3381
Jens Axboe227c0c92020-08-13 11:51:40 -06003382 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003383 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003384 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003385
Jens Axboebcf5a062020-05-22 09:24:42 -06003386 /*
3387 * just use poll if we can, and don't attempt if the fs doesn't
3388 * support callback based unlocks
3389 */
3390 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3391 return false;
3392
Jens Axboe3b2a4432020-08-16 10:58:43 -07003393 wait->wait.func = io_async_buf_func;
3394 wait->wait.private = req;
3395 wait->wait.flags = 0;
3396 INIT_LIST_HEAD(&wait->wait.entry);
3397 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003398 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003399 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003400 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003401}
3402
3403static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3404{
3405 if (req->file->f_op->read_iter)
3406 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003407 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003408 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003409 else
3410 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003411}
3412
Jens Axboea1d7c392020-06-22 11:09:46 -06003413static int io_read(struct io_kiocb *req, bool force_nonblock,
3414 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003415{
3416 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003417 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003418 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003419 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003420 ssize_t io_size, ret, ret2;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003421 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003422
Jens Axboee8c2bc12020-08-15 18:44:09 -07003423 if (rw)
3424 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003425
3426 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003427 if (ret < 0)
3428 return ret;
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003429 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003430 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003431 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003432
Jens Axboefd6c2e42019-12-18 12:19:41 -07003433 /* Ensure we clear previously set non-block flag */
3434 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003435 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003436 else
3437 kiocb->ki_flags |= IOCB_NOWAIT;
3438
Jens Axboefd6c2e42019-12-18 12:19:41 -07003439
Pavel Begunkov24c74672020-06-21 13:09:51 +03003440 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003441 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3442 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003443 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003444
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003445 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003446 if (unlikely(ret))
3447 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003448
Jens Axboe227c0c92020-08-13 11:51:40 -06003449 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003450
Jens Axboe227c0c92020-08-13 11:51:40 -06003451 if (!ret) {
3452 goto done;
3453 } else if (ret == -EIOCBQUEUED) {
3454 ret = 0;
3455 goto out_free;
3456 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003457 /* IOPOLL retry should happen for io-wq threads */
3458 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003459 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003460 /* no retry on NONBLOCK marked file */
3461 if (req->file->f_flags & O_NONBLOCK)
3462 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003463 /* some cases will consume bytes even on error returns */
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003464 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003465 ret = 0;
3466 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003467 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003468 /* make sure -ERESTARTSYS -> -EINTR is done */
3469 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003470 }
3471
3472 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003473 if (!iov_iter_count(iter) || !force_nonblock ||
Pavel Begunkov2df15ef2021-01-21 12:01:08 +00003474 (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG))
Jens Axboe227c0c92020-08-13 11:51:40 -06003475 goto done;
3476
3477 io_size -= ret;
3478copy_iov:
3479 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3480 if (ret2) {
3481 ret = ret2;
3482 goto out_free;
3483 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003484 if (no_async)
3485 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003486 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003487 /* it's copied and will be cleaned with ->io */
3488 iovec = NULL;
3489 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003490 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003491retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003492 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003493 /* if we can retry, do so with the callbacks armed */
3494 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003495 kiocb->ki_flags &= ~IOCB_WAITQ;
3496 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003497 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003498
3499 /*
3500 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3501 * get -EIOCBQUEUED, then we'll get a notification when the desired
3502 * page gets unlocked. We can also get a partial read here, and if we
3503 * do, then just retry at the new offset.
3504 */
3505 ret = io_iter_do_read(req, iter);
3506 if (ret == -EIOCBQUEUED) {
3507 ret = 0;
3508 goto out_free;
3509 } else if (ret > 0 && ret < io_size) {
3510 /* we got some bytes, but not all. retry. */
Jens Axboe76f49662021-03-04 21:02:58 -07003511 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboe227c0c92020-08-13 11:51:40 -06003512 goto retry;
3513 }
3514done:
3515 kiocb_done(kiocb, ret, cs);
3516 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003517out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003518 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003519 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003520 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003521 return ret;
3522}
3523
Pavel Begunkov73debe62020-09-30 22:57:54 +03003524static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003525{
3526 ssize_t ret;
3527
Pavel Begunkova88fc402020-09-30 22:57:53 +03003528 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003529 if (ret)
3530 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003531
Jens Axboe3529d8c2019-12-19 18:24:38 -07003532 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3533 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003534
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003535 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003536 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003537 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003538 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003539}
3540
Jens Axboea1d7c392020-06-22 11:09:46 -06003541static int io_write(struct io_kiocb *req, bool force_nonblock,
3542 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003543{
3544 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003545 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003546 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003547 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003548 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003549
Jens Axboee8c2bc12020-08-15 18:44:09 -07003550 if (rw)
3551 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003552
3553 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003554 if (ret < 0)
3555 return ret;
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003556 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003557 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003558
Jens Axboefd6c2e42019-12-18 12:19:41 -07003559 /* Ensure we clear previously set non-block flag */
3560 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003561 kiocb->ki_flags &= ~IOCB_NOWAIT;
3562 else
3563 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003564
Pavel Begunkov24c74672020-06-21 13:09:51 +03003565 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003566 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003567 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003568
Jens Axboe10d59342019-12-09 20:16:22 -07003569 /* file path doesn't support NOWAIT for non-direct_IO */
3570 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3571 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003572 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003573
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003574 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003575 if (unlikely(ret))
3576 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003577
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003578 /*
3579 * Open-code file_start_write here to grab freeze protection,
3580 * which will be released by another thread in
3581 * io_complete_rw(). Fool lockdep by telling it the lock got
3582 * released so that it doesn't complain about the held lock when
3583 * we return to userspace.
3584 */
3585 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003586 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003587 __sb_writers_release(file_inode(req->file)->i_sb,
3588 SB_FREEZE_WRITE);
3589 }
3590 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003591
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003592 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003593 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003594 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003595 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003596 else
3597 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003598
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003599 /*
3600 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3601 * retry them without IOCB_NOWAIT.
3602 */
3603 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3604 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003605 /* no retry on NONBLOCK marked file */
3606 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3607 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003608 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003609 /* IOPOLL retry should happen for io-wq threads */
3610 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3611 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003612done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003613 kiocb_done(kiocb, ret2, cs);
3614 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003615copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003616 /* some cases will consume bytes even on error returns */
Pavel Begunkov9a4e7f92020-11-07 13:16:26 +00003617 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003618 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003619 if (!ret)
3620 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003621 }
Jens Axboe31b51512019-01-18 22:56:34 -07003622out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003623 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003624 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003625 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003626 return ret;
3627}
3628
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003629static int __io_splice_prep(struct io_kiocb *req,
3630 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003631{
3632 struct io_splice* sp = &req->splice;
3633 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003634
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003635 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3636 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003637
3638 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003639 sp->len = READ_ONCE(sqe->len);
3640 sp->flags = READ_ONCE(sqe->splice_flags);
3641
3642 if (unlikely(sp->flags & ~valid_flags))
3643 return -EINVAL;
3644
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003645 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3646 (sp->flags & SPLICE_F_FD_IN_FIXED));
3647 if (!sp->file_in)
3648 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003649 req->flags |= REQ_F_NEED_CLEANUP;
3650
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003651 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3652 /*
3653 * Splice operation will be punted aync, and here need to
3654 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3655 */
3656 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003657 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003658 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003659
3660 return 0;
3661}
3662
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003663static int io_tee_prep(struct io_kiocb *req,
3664 const struct io_uring_sqe *sqe)
3665{
3666 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3667 return -EINVAL;
3668 return __io_splice_prep(req, sqe);
3669}
3670
3671static int io_tee(struct io_kiocb *req, bool force_nonblock)
3672{
3673 struct io_splice *sp = &req->splice;
3674 struct file *in = sp->file_in;
3675 struct file *out = sp->file_out;
3676 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3677 long ret = 0;
3678
3679 if (force_nonblock)
3680 return -EAGAIN;
3681 if (sp->len)
3682 ret = do_tee(in, out, sp->len, flags);
3683
3684 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3685 req->flags &= ~REQ_F_NEED_CLEANUP;
3686
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003687 if (ret != sp->len)
3688 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003689 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003690 return 0;
3691}
3692
3693static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3694{
3695 struct io_splice* sp = &req->splice;
3696
3697 sp->off_in = READ_ONCE(sqe->splice_off_in);
3698 sp->off_out = READ_ONCE(sqe->off);
3699 return __io_splice_prep(req, sqe);
3700}
3701
Pavel Begunkov014db002020-03-03 21:33:12 +03003702static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003703{
3704 struct io_splice *sp = &req->splice;
3705 struct file *in = sp->file_in;
3706 struct file *out = sp->file_out;
3707 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3708 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003709 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003710
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003711 if (force_nonblock)
3712 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003713
3714 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3715 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003716
Jens Axboe948a7742020-05-17 14:21:38 -06003717 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003718 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003719
3720 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3721 req->flags &= ~REQ_F_NEED_CLEANUP;
3722
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003723 if (ret != sp->len)
3724 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003725 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003726 return 0;
3727}
3728
Jens Axboe2b188cc2019-01-07 10:46:33 -07003729/*
3730 * IORING_OP_NOP just posts a completion event, nothing else.
3731 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003732static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003733{
3734 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003735
Jens Axboedef596e2019-01-09 08:59:42 -07003736 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3737 return -EINVAL;
3738
Jens Axboe229a7b62020-06-22 10:13:11 -06003739 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003740 return 0;
3741}
3742
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003744{
Jens Axboe6b063142019-01-10 22:13:58 -07003745 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003746
Jens Axboe09bb8392019-03-13 12:39:28 -06003747 if (!req->file)
3748 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003749
Jens Axboe6b063142019-01-10 22:13:58 -07003750 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003751 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003752 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
3753 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003754 return -EINVAL;
3755
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003756 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3757 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3758 return -EINVAL;
3759
3760 req->sync.off = READ_ONCE(sqe->off);
3761 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003762 return 0;
3763}
3764
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003765static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003766{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003767 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003768 int ret;
3769
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003770 /* fsync always requires a blocking context */
3771 if (force_nonblock)
3772 return -EAGAIN;
3773
Jens Axboe9adbd452019-12-20 08:45:55 -07003774 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003775 end > 0 ? end : LLONG_MAX,
3776 req->sync.flags & IORING_FSYNC_DATASYNC);
3777 if (ret < 0)
3778 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003779 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003780 return 0;
3781}
3782
Jens Axboed63d1b52019-12-10 10:38:56 -07003783static int io_fallocate_prep(struct io_kiocb *req,
3784 const struct io_uring_sqe *sqe)
3785{
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003786 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
3787 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07003788 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003789 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3790 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003791
3792 req->sync.off = READ_ONCE(sqe->off);
3793 req->sync.len = READ_ONCE(sqe->addr);
3794 req->sync.mode = READ_ONCE(sqe->len);
3795 return 0;
3796}
3797
Pavel Begunkov014db002020-03-03 21:33:12 +03003798static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003799{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003800 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003801
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003802 /* fallocate always requiring blocking context */
3803 if (force_nonblock)
3804 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003805 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3806 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003807 if (ret < 0)
3808 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003809 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003810 return 0;
3811}
3812
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003813static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003814{
Jens Axboef8748882020-01-08 17:47:02 -07003815 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003816 int ret;
3817
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003818 if (unlikely(sqe->ioprio || sqe->buf_index || sqe->splice_fd_in))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003819 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003820 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003821 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003822
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003823 /* open.how should be already initialised */
3824 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003825 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003826
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003827 req->open.dfd = READ_ONCE(sqe->fd);
3828 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003829 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003830 if (IS_ERR(req->open.filename)) {
3831 ret = PTR_ERR(req->open.filename);
3832 req->open.filename = NULL;
3833 return ret;
3834 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003835 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07003836 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003837 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003838 return 0;
3839}
3840
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003841static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3842{
3843 u64 flags, mode;
3844
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003845 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3846 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003847 mode = READ_ONCE(sqe->len);
3848 flags = READ_ONCE(sqe->open_flags);
3849 req->open.how = build_open_how(flags, mode);
3850 return __io_openat_prep(req, sqe);
3851}
3852
Jens Axboecebdb982020-01-08 17:59:24 -07003853static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3854{
3855 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003856 size_t len;
3857 int ret;
3858
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003859 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3860 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003861 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3862 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003863 if (len < OPEN_HOW_SIZE_VER0)
3864 return -EINVAL;
3865
3866 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3867 len);
3868 if (ret)
3869 return ret;
3870
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003871 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003872}
3873
Pavel Begunkov014db002020-03-03 21:33:12 +03003874static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003875{
3876 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003877 struct file *file;
3878 int ret;
3879
Jens Axboe944d1442020-11-13 16:48:44 -07003880 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003881 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003882
Jens Axboecebdb982020-01-08 17:59:24 -07003883 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003884 if (ret)
3885 goto err;
3886
Jens Axboe4022e7a2020-03-19 19:23:18 -06003887 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003888 if (ret < 0)
3889 goto err;
3890
3891 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3892 if (IS_ERR(file)) {
3893 put_unused_fd(ret);
3894 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07003895 /*
3896 * A work-around to ensure that /proc/self works that way
3897 * that it should - if we get -EOPNOTSUPP back, then assume
3898 * that proc_self_get_link() failed us because we're in async
3899 * context. We should be safe to retry this from the task
3900 * itself with force_nonblock == false set, as it should not
3901 * block on lookup. Would be nice to know this upfront and
3902 * avoid the async dance, but doesn't seem feasible.
3903 */
3904 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
3905 req->open.ignore_nonblock = true;
3906 refcount_inc(&req->refs);
3907 io_req_task_queue(req);
3908 return 0;
3909 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003910 } else {
3911 fsnotify_open(file);
3912 fd_install(ret, file);
3913 }
3914err:
3915 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003916 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003917 if (ret < 0)
3918 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003919 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003920 return 0;
3921}
3922
Pavel Begunkov014db002020-03-03 21:33:12 +03003923static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003924{
Pavel Begunkov014db002020-03-03 21:33:12 +03003925 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003926}
3927
Jens Axboe067524e2020-03-02 16:32:28 -07003928static int io_remove_buffers_prep(struct io_kiocb *req,
3929 const struct io_uring_sqe *sqe)
3930{
3931 struct io_provide_buf *p = &req->pbuf;
3932 u64 tmp;
3933
Pavel Begunkov54eb6212021-09-13 09:42:47 -06003934 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
3935 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07003936 return -EINVAL;
3937
3938 tmp = READ_ONCE(sqe->fd);
3939 if (!tmp || tmp > USHRT_MAX)
3940 return -EINVAL;
3941
3942 memset(p, 0, sizeof(*p));
3943 p->nbufs = tmp;
3944 p->bgid = READ_ONCE(sqe->buf_group);
3945 return 0;
3946}
3947
3948static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3949 int bgid, unsigned nbufs)
3950{
3951 unsigned i = 0;
3952
3953 /* shouldn't happen */
3954 if (!nbufs)
3955 return 0;
3956
3957 /* the head kbuf is the list itself */
3958 while (!list_empty(&buf->list)) {
3959 struct io_buffer *nxt;
3960
3961 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3962 list_del(&nxt->list);
3963 kfree(nxt);
3964 if (++i == nbufs)
3965 return i;
3966 }
3967 i++;
3968 kfree(buf);
Jens Axboe90731882021-07-13 17:18:35 +08003969 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07003970
3971 return i;
3972}
3973
Jens Axboe229a7b62020-06-22 10:13:11 -06003974static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3975 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003976{
3977 struct io_provide_buf *p = &req->pbuf;
3978 struct io_ring_ctx *ctx = req->ctx;
3979 struct io_buffer *head;
3980 int ret = 0;
3981
3982 io_ring_submit_lock(ctx, !force_nonblock);
3983
3984 lockdep_assert_held(&ctx->uring_lock);
3985
3986 ret = -ENOENT;
Jens Axboe90731882021-07-13 17:18:35 +08003987 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07003988 if (head)
3989 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003990 if (ret < 0)
3991 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00003992
3993 /* need to hold the lock to complete IOPOLL requests */
3994 if (ctx->flags & IORING_SETUP_IOPOLL) {
3995 __io_req_complete(req, ret, 0, cs);
3996 io_ring_submit_unlock(ctx, !force_nonblock);
3997 } else {
3998 io_ring_submit_unlock(ctx, !force_nonblock);
3999 __io_req_complete(req, ret, 0, cs);
4000 }
Jens Axboe067524e2020-03-02 16:32:28 -07004001 return 0;
4002}
4003
Jens Axboeddf0322d2020-02-23 16:41:33 -07004004static int io_provide_buffers_prep(struct io_kiocb *req,
4005 const struct io_uring_sqe *sqe)
4006{
Pavel Begunkovcbbc13b2021-04-15 13:07:39 +01004007 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004008 struct io_provide_buf *p = &req->pbuf;
4009 u64 tmp;
4010
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004011 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004012 return -EINVAL;
4013
4014 tmp = READ_ONCE(sqe->fd);
4015 if (!tmp || tmp > USHRT_MAX)
4016 return -E2BIG;
4017 p->nbufs = tmp;
4018 p->addr = READ_ONCE(sqe->addr);
4019 p->len = READ_ONCE(sqe->len);
4020
Pavel Begunkovcbbc13b2021-04-15 13:07:39 +01004021 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4022 &size))
4023 return -EOVERFLOW;
4024 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4025 return -EOVERFLOW;
4026
Pavel Begunkovdcf2dfc2021-03-19 10:21:19 +00004027 size = (unsigned long)p->len * p->nbufs;
4028 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004029 return -EFAULT;
4030
4031 p->bgid = READ_ONCE(sqe->buf_group);
4032 tmp = READ_ONCE(sqe->off);
4033 if (tmp > USHRT_MAX)
4034 return -E2BIG;
4035 p->bid = tmp;
4036 return 0;
4037}
4038
4039static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4040{
4041 struct io_buffer *buf;
4042 u64 addr = pbuf->addr;
4043 int i, bid = pbuf->bid;
4044
4045 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboece092352021-09-24 07:39:08 -06004046 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004047 if (!buf)
4048 break;
4049
4050 buf->addr = addr;
Thadeu Lima de Souza Cascardo7e916d02021-05-05 09:47:06 -03004051 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004052 buf->bid = bid;
4053 addr += pbuf->len;
4054 bid++;
4055 if (!*head) {
4056 INIT_LIST_HEAD(&buf->list);
4057 *head = buf;
4058 } else {
4059 list_add_tail(&buf->list, &(*head)->list);
4060 }
Eric Dumazet4a93c652022-02-14 20:10:03 -08004061 cond_resched();
Jens Axboeddf0322d2020-02-23 16:41:33 -07004062 }
4063
4064 return i ? i : -ENOMEM;
4065}
4066
Jens Axboe229a7b62020-06-22 10:13:11 -06004067static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4068 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004069{
4070 struct io_provide_buf *p = &req->pbuf;
4071 struct io_ring_ctx *ctx = req->ctx;
4072 struct io_buffer *head, *list;
4073 int ret = 0;
4074
4075 io_ring_submit_lock(ctx, !force_nonblock);
4076
4077 lockdep_assert_held(&ctx->uring_lock);
4078
Jens Axboe90731882021-07-13 17:18:35 +08004079 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004080
4081 ret = io_add_buffers(p, &head);
Jens Axboe90731882021-07-13 17:18:35 +08004082 if (ret >= 0 && !list) {
4083 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4084 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004085 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004086 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004087 if (ret < 0)
4088 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00004089
4090 /* need to hold the lock to complete IOPOLL requests */
4091 if (ctx->flags & IORING_SETUP_IOPOLL) {
4092 __io_req_complete(req, ret, 0, cs);
4093 io_ring_submit_unlock(ctx, !force_nonblock);
4094 } else {
4095 io_ring_submit_unlock(ctx, !force_nonblock);
4096 __io_req_complete(req, ret, 0, cs);
4097 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004098 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004099}
4100
Jens Axboe3e4827b2020-01-08 15:18:09 -07004101static int io_epoll_ctl_prep(struct io_kiocb *req,
4102 const struct io_uring_sqe *sqe)
4103{
4104#if defined(CONFIG_EPOLL)
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004105 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004106 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004107 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004108 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004109
4110 req->epoll.epfd = READ_ONCE(sqe->fd);
4111 req->epoll.op = READ_ONCE(sqe->len);
4112 req->epoll.fd = READ_ONCE(sqe->off);
4113
4114 if (ep_op_has_event(req->epoll.op)) {
4115 struct epoll_event __user *ev;
4116
4117 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4118 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4119 return -EFAULT;
4120 }
4121
4122 return 0;
4123#else
4124 return -EOPNOTSUPP;
4125#endif
4126}
4127
Jens Axboe229a7b62020-06-22 10:13:11 -06004128static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4129 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004130{
4131#if defined(CONFIG_EPOLL)
4132 struct io_epoll *ie = &req->epoll;
4133 int ret;
4134
4135 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4136 if (force_nonblock && ret == -EAGAIN)
4137 return -EAGAIN;
4138
4139 if (ret < 0)
4140 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004141 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004142 return 0;
4143#else
4144 return -EOPNOTSUPP;
4145#endif
4146}
4147
Jens Axboec1ca7572019-12-25 22:18:28 -07004148static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4149{
4150#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004151 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004152 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004153 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4154 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004155
4156 req->madvise.addr = READ_ONCE(sqe->addr);
4157 req->madvise.len = READ_ONCE(sqe->len);
4158 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4159 return 0;
4160#else
4161 return -EOPNOTSUPP;
4162#endif
4163}
4164
Pavel Begunkov014db002020-03-03 21:33:12 +03004165static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004166{
4167#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4168 struct io_madvise *ma = &req->madvise;
4169 int ret;
4170
4171 if (force_nonblock)
4172 return -EAGAIN;
4173
Minchan Kim0726b012020-10-17 16:14:50 -07004174 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004175 if (ret < 0)
4176 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004177 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004178 return 0;
4179#else
4180 return -EOPNOTSUPP;
4181#endif
4182}
4183
Jens Axboe4840e412019-12-25 22:03:45 -07004184static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4185{
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004186 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004187 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004188 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4189 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004190
4191 req->fadvise.offset = READ_ONCE(sqe->off);
4192 req->fadvise.len = READ_ONCE(sqe->len);
4193 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4194 return 0;
4195}
4196
Pavel Begunkov014db002020-03-03 21:33:12 +03004197static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004198{
4199 struct io_fadvise *fa = &req->fadvise;
4200 int ret;
4201
Jens Axboe3e694262020-02-01 09:22:49 -07004202 if (force_nonblock) {
4203 switch (fa->advice) {
4204 case POSIX_FADV_NORMAL:
4205 case POSIX_FADV_RANDOM:
4206 case POSIX_FADV_SEQUENTIAL:
4207 break;
4208 default:
4209 return -EAGAIN;
4210 }
4211 }
Jens Axboe4840e412019-12-25 22:03:45 -07004212
4213 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4214 if (ret < 0)
4215 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004216 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004217 return 0;
4218}
4219
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004220static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4221{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004222 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004223 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004224 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004225 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004226 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004227 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004228
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004229 req->statx.dfd = READ_ONCE(sqe->fd);
4230 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004231 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004232 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4233 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004234
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004235 return 0;
4236}
4237
Pavel Begunkov014db002020-03-03 21:33:12 +03004238static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004239{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004240 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004241 int ret;
4242
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004243 if (force_nonblock) {
4244 /* only need file table for an actual valid fd */
4245 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4246 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004247 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004248 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004249
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004250 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4251 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004252
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004253 if (ret < 0)
4254 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004255 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004256 return 0;
4257}
4258
Jens Axboeb5dba592019-12-11 14:02:38 -07004259static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4260{
4261 /*
4262 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004263 * leave the 'file' in an undeterminate state, and here need to modify
4264 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004265 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004266 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004267
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004268 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4269 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004270 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004271 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004272 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004273 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004274 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004275
4276 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004277 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004278 return -EBADF;
4279
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004280 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004281 return 0;
4282}
4283
Jens Axboe229a7b62020-06-22 10:13:11 -06004284static int io_close(struct io_kiocb *req, bool force_nonblock,
4285 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004286{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004287 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004288 int ret;
4289
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004290 /* might be already done during nonblock submission */
4291 if (!close->put_file) {
4292 ret = __close_fd_get_file(close->fd, &close->put_file);
4293 if (ret < 0)
4294 return (ret == -ENOENT) ? -EBADF : ret;
4295 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004296
4297 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004298 if (close->put_file->f_op->flush && force_nonblock) {
Jens Axboef3ac7a52021-01-19 10:10:54 -07004299 /* not safe to cancel at this point */
4300 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004301 /* was never set, but play safe */
4302 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004303 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004304 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004305 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004306 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004307
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004308 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004309 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004310 if (ret < 0)
4311 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004312 fput(close->put_file);
4313 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004314 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004315 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004316}
4317
Jens Axboe3529d8c2019-12-19 18:24:38 -07004318static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004319{
4320 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004321
4322 if (!req->file)
4323 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004324
4325 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4326 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004327 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4328 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004329 return -EINVAL;
4330
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004331 req->sync.off = READ_ONCE(sqe->off);
4332 req->sync.len = READ_ONCE(sqe->len);
4333 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004334 return 0;
4335}
4336
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004337static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004338{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004339 int ret;
4340
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004341 /* sync_file_range always requires a blocking context */
4342 if (force_nonblock)
4343 return -EAGAIN;
4344
Jens Axboe9adbd452019-12-20 08:45:55 -07004345 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004346 req->sync.flags);
4347 if (ret < 0)
4348 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004349 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004350 return 0;
4351}
4352
YueHaibing469956e2020-03-04 15:53:52 +08004353#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004354static int io_setup_async_msg(struct io_kiocb *req,
4355 struct io_async_msghdr *kmsg)
4356{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004357 struct io_async_msghdr *async_msg = req->async_data;
4358
4359 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004360 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004361 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004362 if (kmsg->iov != kmsg->fast_iov)
4363 kfree(kmsg->iov);
4364 return -ENOMEM;
4365 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004366 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004367 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004368 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004369 return -EAGAIN;
4370}
4371
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004372static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4373 struct io_async_msghdr *iomsg)
4374{
4375 iomsg->iov = iomsg->fast_iov;
4376 iomsg->msg.msg_name = &iomsg->addr;
4377 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4378 req->sr_msg.msg_flags, &iomsg->iov);
4379}
4380
Jens Axboe3529d8c2019-12-19 18:24:38 -07004381static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004382{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004383 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004384 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004385 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004386
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004387 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4388 return -EINVAL;
4389
Jens Axboee47293f2019-12-20 08:58:21 -07004390 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004391 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004392 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004393
Jens Axboed8768362020-02-27 14:17:49 -07004394#ifdef CONFIG_COMPAT
4395 if (req->ctx->compat)
4396 sr->msg_flags |= MSG_CMSG_COMPAT;
4397#endif
4398
Jens Axboee8c2bc12020-08-15 18:44:09 -07004399 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004400 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004401 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004402 if (!ret)
4403 req->flags |= REQ_F_NEED_CLEANUP;
4404 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004405}
4406
Jens Axboe229a7b62020-06-22 10:13:11 -06004407static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4408 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004409{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004410 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004411 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004412 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004413 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004414 int ret;
4415
Jens Axboe03b12302019-12-02 18:50:25 -07004416 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004417 if (unlikely(!sock))
4418 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004419
Jens Axboee8c2bc12020-08-15 18:44:09 -07004420 if (req->async_data) {
4421 kmsg = req->async_data;
4422 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004423 /* if iov is set, it's allocated already */
4424 if (!kmsg->iov)
4425 kmsg->iov = kmsg->fast_iov;
4426 kmsg->msg.msg_iter.iov = kmsg->iov;
4427 } else {
4428 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004429 if (ret)
4430 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004431 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004432 }
4433
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004434 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004435 if (flags & MSG_DONTWAIT)
4436 req->flags |= REQ_F_NOWAIT;
4437 else if (force_nonblock)
4438 flags |= MSG_DONTWAIT;
4439
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004440 if (flags & MSG_WAITALL)
4441 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4442
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004443 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4444 if (force_nonblock && ret == -EAGAIN)
4445 return io_setup_async_msg(req, kmsg);
4446 if (ret == -ERESTARTSYS)
4447 ret = -EINTR;
4448
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004449 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004450 kfree(kmsg->iov);
4451 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004452 if (ret < min_ret)
Jens Axboefddafac2020-01-04 20:19:44 -07004453 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004454 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004455 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004456}
4457
Jens Axboe229a7b62020-06-22 10:13:11 -06004458static int io_send(struct io_kiocb *req, bool force_nonblock,
4459 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004460{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004461 struct io_sr_msg *sr = &req->sr_msg;
4462 struct msghdr msg;
4463 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004464 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004465 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004466 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004467 int ret;
4468
4469 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004470 if (unlikely(!sock))
4471 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004472
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004473 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4474 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004475 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004476
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004477 msg.msg_name = NULL;
4478 msg.msg_control = NULL;
4479 msg.msg_controllen = 0;
4480 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004481
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004482 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004483 if (flags & MSG_DONTWAIT)
4484 req->flags |= REQ_F_NOWAIT;
4485 else if (force_nonblock)
4486 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004487
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004488 if (flags & MSG_WAITALL)
4489 min_ret = iov_iter_count(&msg.msg_iter);
4490
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004491 msg.msg_flags = flags;
4492 ret = sock_sendmsg(sock, &msg);
4493 if (force_nonblock && ret == -EAGAIN)
4494 return -EAGAIN;
4495 if (ret == -ERESTARTSYS)
4496 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004497
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004498 if (ret < min_ret)
Jens Axboe03b12302019-12-02 18:50:25 -07004499 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004500 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004501 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004502}
4503
Pavel Begunkov1400e692020-07-12 20:41:05 +03004504static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4505 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004506{
4507 struct io_sr_msg *sr = &req->sr_msg;
4508 struct iovec __user *uiov;
4509 size_t iov_len;
4510 int ret;
4511
Pavel Begunkov1400e692020-07-12 20:41:05 +03004512 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4513 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004514 if (ret)
4515 return ret;
4516
4517 if (req->flags & REQ_F_BUFFER_SELECT) {
4518 if (iov_len > 1)
4519 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004520 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004521 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004522 sr->len = iomsg->iov[0].iov_len;
4523 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004524 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004525 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004526 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004527 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4528 &iomsg->iov, &iomsg->msg.msg_iter,
4529 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004530 if (ret > 0)
4531 ret = 0;
4532 }
4533
4534 return ret;
4535}
4536
4537#ifdef CONFIG_COMPAT
4538static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004539 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004540{
4541 struct compat_msghdr __user *msg_compat;
4542 struct io_sr_msg *sr = &req->sr_msg;
4543 struct compat_iovec __user *uiov;
4544 compat_uptr_t ptr;
4545 compat_size_t len;
4546 int ret;
4547
Pavel Begunkov270a5942020-07-12 20:41:04 +03004548 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004549 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004550 &ptr, &len);
4551 if (ret)
4552 return ret;
4553
4554 uiov = compat_ptr(ptr);
4555 if (req->flags & REQ_F_BUFFER_SELECT) {
4556 compat_ssize_t clen;
4557
4558 if (len > 1)
4559 return -EINVAL;
4560 if (!access_ok(uiov, sizeof(*uiov)))
4561 return -EFAULT;
4562 if (__get_user(clen, &uiov->iov_len))
4563 return -EFAULT;
4564 if (clen < 0)
4565 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004566 sr->len = clen;
4567 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004568 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004569 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004570 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4571 UIO_FASTIOV, &iomsg->iov,
4572 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004573 if (ret < 0)
4574 return ret;
4575 }
4576
4577 return 0;
4578}
Jens Axboe03b12302019-12-02 18:50:25 -07004579#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004580
Pavel Begunkov1400e692020-07-12 20:41:05 +03004581static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4582 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004583{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004584 iomsg->msg.msg_name = &iomsg->addr;
4585 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004586
4587#ifdef CONFIG_COMPAT
4588 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004589 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004590#endif
4591
Pavel Begunkov1400e692020-07-12 20:41:05 +03004592 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004593}
4594
Jens Axboebcda7ba2020-02-23 16:42:51 -07004595static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004596 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004597{
4598 struct io_sr_msg *sr = &req->sr_msg;
4599 struct io_buffer *kbuf;
4600
Jens Axboebcda7ba2020-02-23 16:42:51 -07004601 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4602 if (IS_ERR(kbuf))
4603 return kbuf;
4604
4605 sr->kbuf = kbuf;
4606 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004607 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004608}
4609
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004610static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4611{
4612 return io_put_kbuf(req, req->sr_msg.kbuf);
4613}
4614
Jens Axboe3529d8c2019-12-19 18:24:38 -07004615static int io_recvmsg_prep(struct io_kiocb *req,
4616 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004617{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004618 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004619 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004620 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004621
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004622 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4623 return -EINVAL;
4624
Jens Axboe3529d8c2019-12-19 18:24:38 -07004625 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004626 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004627 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004628 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004629
Jens Axboed8768362020-02-27 14:17:49 -07004630#ifdef CONFIG_COMPAT
4631 if (req->ctx->compat)
4632 sr->msg_flags |= MSG_CMSG_COMPAT;
4633#endif
4634
Jens Axboee8c2bc12020-08-15 18:44:09 -07004635 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004636 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004637 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004638 if (!ret)
4639 req->flags |= REQ_F_NEED_CLEANUP;
4640 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004641}
4642
Jens Axboe229a7b62020-06-22 10:13:11 -06004643static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4644 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004645{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004646 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004647 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004648 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004649 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004650 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004651 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004652
Jens Axboe0fa03c62019-04-19 13:34:07 -06004653 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004654 if (unlikely(!sock))
4655 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004656
Jens Axboee8c2bc12020-08-15 18:44:09 -07004657 if (req->async_data) {
4658 kmsg = req->async_data;
4659 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004660 /* if iov is set, it's allocated already */
4661 if (!kmsg->iov)
4662 kmsg->iov = kmsg->fast_iov;
4663 kmsg->msg.msg_iter.iov = kmsg->iov;
4664 } else {
4665 ret = io_recvmsg_copy_hdr(req, &iomsg);
4666 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004667 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004668 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004669 }
4670
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004671 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004672 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004673 if (IS_ERR(kbuf))
4674 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004675 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4676 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4677 1, req->sr_msg.len);
4678 }
4679
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004680 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004681 if (flags & MSG_DONTWAIT)
4682 req->flags |= REQ_F_NOWAIT;
4683 else if (force_nonblock)
4684 flags |= MSG_DONTWAIT;
4685
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004686 if (flags & MSG_WAITALL)
4687 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4688
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004689 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4690 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004691 if (force_nonblock && ret == -EAGAIN)
4692 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004693 if (ret == -ERESTARTSYS)
4694 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004695
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004696 if (req->flags & REQ_F_BUFFER_SELECTED)
4697 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004698 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004699 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004700 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004701 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004702 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004703 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004704 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004705}
4706
Jens Axboe229a7b62020-06-22 10:13:11 -06004707static int io_recv(struct io_kiocb *req, bool force_nonblock,
4708 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004709{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004710 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004711 struct io_sr_msg *sr = &req->sr_msg;
4712 struct msghdr msg;
4713 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004714 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004715 struct iovec iov;
4716 unsigned flags;
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004717 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004718 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004719
Jens Axboefddafac2020-01-04 20:19:44 -07004720 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004721 if (unlikely(!sock))
4722 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004723
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004724 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004725 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004726 if (IS_ERR(kbuf))
4727 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004728 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004729 }
4730
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004731 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004732 if (unlikely(ret))
4733 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004734
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004735 msg.msg_name = NULL;
4736 msg.msg_control = NULL;
4737 msg.msg_controllen = 0;
4738 msg.msg_namelen = 0;
4739 msg.msg_iocb = NULL;
4740 msg.msg_flags = 0;
4741
Stefan Metzmacher21c2bbc2021-03-16 16:33:27 +01004742 flags = req->sr_msg.msg_flags | MSG_NOSIGNAL;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004743 if (flags & MSG_DONTWAIT)
4744 req->flags |= REQ_F_NOWAIT;
4745 else if (force_nonblock)
4746 flags |= MSG_DONTWAIT;
4747
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004748 if (flags & MSG_WAITALL)
4749 min_ret = iov_iter_count(&msg.msg_iter);
4750
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004751 ret = sock_recvmsg(sock, &msg, flags);
4752 if (force_nonblock && ret == -EAGAIN)
4753 return -EAGAIN;
4754 if (ret == -ERESTARTSYS)
4755 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004756out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004757 if (req->flags & REQ_F_BUFFER_SELECTED)
4758 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher44c816c2021-03-20 20:33:36 +01004759 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Jens Axboefddafac2020-01-04 20:19:44 -07004760 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004761 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004762 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004763}
4764
Jens Axboe3529d8c2019-12-19 18:24:38 -07004765static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004766{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004767 struct io_accept *accept = &req->accept;
4768
Jens Axboe17f2fe32019-10-17 14:42:58 -06004769 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4770 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004771 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004772 return -EINVAL;
4773
Jens Axboed55e5f52019-12-11 16:12:15 -07004774 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4775 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004776 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004777 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004778 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004779}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004780
Jens Axboe229a7b62020-06-22 10:13:11 -06004781static int io_accept(struct io_kiocb *req, bool force_nonblock,
4782 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004783{
4784 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004785 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004786 int ret;
4787
Jiufei Xuee697dee2020-06-10 13:41:59 +08004788 if (req->file->f_flags & O_NONBLOCK)
4789 req->flags |= REQ_F_NOWAIT;
4790
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004791 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004792 accept->addr_len, accept->flags,
4793 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004794 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004795 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004796 if (ret < 0) {
4797 if (ret == -ERESTARTSYS)
4798 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004799 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004800 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004801 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004802 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004803}
4804
Jens Axboe3529d8c2019-12-19 18:24:38 -07004805static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004806{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004807 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004808 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004809
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004810 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4811 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06004812 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
4813 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004814 return -EINVAL;
4815
Jens Axboe3529d8c2019-12-19 18:24:38 -07004816 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4817 conn->addr_len = READ_ONCE(sqe->addr2);
4818
4819 if (!io)
4820 return 0;
4821
4822 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004823 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004824}
4825
Jens Axboe229a7b62020-06-22 10:13:11 -06004826static int io_connect(struct io_kiocb *req, bool force_nonblock,
4827 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004828{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004829 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004830 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004831 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004832
Jens Axboee8c2bc12020-08-15 18:44:09 -07004833 if (req->async_data) {
4834 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004835 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004836 ret = move_addr_to_kernel(req->connect.addr,
4837 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004838 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004839 if (ret)
4840 goto out;
4841 io = &__io;
4842 }
4843
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004844 file_flags = force_nonblock ? O_NONBLOCK : 0;
4845
Jens Axboee8c2bc12020-08-15 18:44:09 -07004846 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004847 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004848 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004849 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004850 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004851 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004852 ret = -ENOMEM;
4853 goto out;
4854 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004855 io = req->async_data;
4856 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004857 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004858 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004859 if (ret == -ERESTARTSYS)
4860 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004861out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004862 if (ret < 0)
4863 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004864 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004865 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004866}
YueHaibing469956e2020-03-04 15:53:52 +08004867#else /* !CONFIG_NET */
4868static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4869{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004870 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004871}
4872
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004873static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4874 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004875{
YueHaibing469956e2020-03-04 15:53:52 +08004876 return -EOPNOTSUPP;
4877}
4878
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004879static int io_send(struct io_kiocb *req, bool force_nonblock,
4880 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004881{
4882 return -EOPNOTSUPP;
4883}
4884
4885static int io_recvmsg_prep(struct io_kiocb *req,
4886 const struct io_uring_sqe *sqe)
4887{
4888 return -EOPNOTSUPP;
4889}
4890
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004891static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4892 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004893{
4894 return -EOPNOTSUPP;
4895}
4896
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004897static int io_recv(struct io_kiocb *req, bool force_nonblock,
4898 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004899{
4900 return -EOPNOTSUPP;
4901}
4902
4903static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4904{
4905 return -EOPNOTSUPP;
4906}
4907
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004908static int io_accept(struct io_kiocb *req, bool force_nonblock,
4909 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004910{
4911 return -EOPNOTSUPP;
4912}
4913
4914static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4915{
4916 return -EOPNOTSUPP;
4917}
4918
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004919static int io_connect(struct io_kiocb *req, bool force_nonblock,
4920 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004921{
4922 return -EOPNOTSUPP;
4923}
4924#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004925
Jens Axboed7718a92020-02-14 22:23:12 -07004926struct io_poll_table {
4927 struct poll_table_struct pt;
4928 struct io_kiocb *req;
Pavel Begunkov9eef9022021-07-20 10:50:43 +01004929 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07004930 int error;
4931};
4932
Jens Axboed7718a92020-02-14 22:23:12 -07004933static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4934 __poll_t mask, task_work_func_t func)
4935{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004936 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004937 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004938
4939 /* for instances that support it check for an event match first: */
4940 if (mask && !(mask & poll->events))
4941 return 0;
4942
4943 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4944
4945 list_del_init(&poll->wait.entry);
4946
Jens Axboed7718a92020-02-14 22:23:12 -07004947 req->result = mask;
4948 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004949 percpu_ref_get(&req->ctx->refs);
4950
Jens Axboed7718a92020-02-14 22:23:12 -07004951 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004952 * If we using the signalfd wait_queue_head for this wakeup, then
4953 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4954 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4955 * either, as the normal wakeup will suffice.
4956 */
4957 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4958
4959 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004960 * If this fails, then the task is exiting. When a task exits, the
4961 * work gets canceled, so just cancel this request as well instead
4962 * of executing it. We can't safely execute it anyway, as we may not
4963 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004964 */
Jens Axboe87c43112020-09-30 21:00:14 -06004965 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004966 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004967 struct task_struct *tsk;
4968
Jens Axboee3aabf92020-05-18 11:04:17 -06004969 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004970 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06004971 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06004972 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004973 }
Jens Axboed7718a92020-02-14 22:23:12 -07004974 return 1;
4975}
4976
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004977static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4978 __acquires(&req->ctx->completion_lock)
4979{
4980 struct io_ring_ctx *ctx = req->ctx;
4981
4982 if (!req->result && !READ_ONCE(poll->canceled)) {
4983 struct poll_table_struct pt = { ._key = poll->events };
4984
4985 req->result = vfs_poll(req->file, &pt) & poll->events;
4986 }
4987
4988 spin_lock_irq(&ctx->completion_lock);
4989 if (!req->result && !READ_ONCE(poll->canceled)) {
4990 add_wait_queue(poll->head, &poll->wait);
4991 return true;
4992 }
4993
4994 return false;
4995}
4996
Jens Axboed4e7cd32020-08-15 11:44:50 -07004997static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004998{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004999 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005000 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005001 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005002 return req->apoll->double_poll;
5003}
5004
5005static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5006{
5007 if (req->opcode == IORING_OP_POLL_ADD)
5008 return &req->poll;
5009 return &req->apoll->poll;
5010}
5011
5012static void io_poll_remove_double(struct io_kiocb *req)
5013{
5014 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005015
5016 lockdep_assert_held(&req->ctx->completion_lock);
5017
5018 if (poll && poll->head) {
5019 struct wait_queue_head *head = poll->head;
5020
5021 spin_lock(&head->lock);
5022 list_del_init(&poll->wait.entry);
5023 if (poll->wait.private)
5024 refcount_dec(&req->refs);
5025 poll->head = NULL;
5026 spin_unlock(&head->lock);
5027 }
5028}
5029
5030static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5031{
5032 struct io_ring_ctx *ctx = req->ctx;
5033
Jens Axboed4e7cd32020-08-15 11:44:50 -07005034 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005035 req->poll.done = true;
5036 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5037 io_commit_cqring(ctx);
5038}
5039
Jens Axboe18bceab2020-05-15 11:56:54 -06005040static void io_poll_task_func(struct callback_head *cb)
5041{
5042 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005043 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005044 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005045
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005046 if (io_poll_rewait(req, &req->poll)) {
5047 spin_unlock_irq(&ctx->completion_lock);
5048 } else {
5049 hash_del(&req->hash_node);
5050 io_poll_complete(req, req->result, 0);
5051 spin_unlock_irq(&ctx->completion_lock);
5052
5053 nxt = io_put_req_find_next(req);
5054 io_cqring_ev_posted(ctx);
5055 if (nxt)
5056 __io_req_task_submit(nxt);
5057 }
5058
Jens Axboe6d816e02020-08-11 08:04:14 -06005059 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005060}
5061
5062static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5063 int sync, void *key)
5064{
5065 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005066 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005067 __poll_t mask = key_to_poll(key);
5068
5069 /* for instances that support it check for an event match first: */
5070 if (mask && !(mask & poll->events))
5071 return 0;
5072
Jens Axboe8706e042020-09-28 08:38:54 -06005073 list_del_init(&wait->entry);
5074
Jens Axboe807abcb2020-07-17 17:09:27 -06005075 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005076 bool done;
5077
Jens Axboe807abcb2020-07-17 17:09:27 -06005078 spin_lock(&poll->head->lock);
5079 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005080 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005081 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005082 /* make sure double remove sees this as being gone */
5083 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005084 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005085 if (!done) {
5086 /* use wait func handler, so it matches the rq type */
5087 poll->wait.func(&poll->wait, mode, sync, key);
5088 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005089 }
5090 refcount_dec(&req->refs);
5091 return 1;
5092}
5093
5094static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5095 wait_queue_func_t wake_func)
5096{
5097 poll->head = NULL;
5098 poll->done = false;
5099 poll->canceled = false;
5100 poll->events = events;
5101 INIT_LIST_HEAD(&poll->wait.entry);
5102 init_waitqueue_func_entry(&poll->wait, wake_func);
5103}
5104
5105static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005106 struct wait_queue_head *head,
5107 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005108{
5109 struct io_kiocb *req = pt->req;
5110
5111 /*
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005112 * The file being polled uses multiple waitqueues for poll handling
5113 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5114 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005115 */
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005116 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005117 struct io_poll_iocb *poll_one = poll;
5118
Jens Axboe18bceab2020-05-15 11:56:54 -06005119 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005120 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005121 pt->error = -EINVAL;
5122 return;
5123 }
Jens Axboea2501d82021-02-28 16:07:30 -07005124 /* double add on the same waitqueue head, ignore */
5125 if (poll->head == head)
5126 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005127 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5128 if (!poll) {
5129 pt->error = -ENOMEM;
5130 return;
5131 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005132 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005133 refcount_inc(&req->refs);
5134 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005135 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005136 }
5137
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005138 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005139 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005140
5141 if (poll->events & EPOLLEXCLUSIVE)
5142 add_wait_queue_exclusive(head, &poll->wait);
5143 else
5144 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005145}
5146
5147static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5148 struct poll_table_struct *p)
5149{
5150 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005151 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005152
Jens Axboe807abcb2020-07-17 17:09:27 -06005153 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005154}
5155
Jens Axboed7718a92020-02-14 22:23:12 -07005156static void io_async_task_func(struct callback_head *cb)
5157{
5158 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5159 struct async_poll *apoll = req->apoll;
5160 struct io_ring_ctx *ctx = req->ctx;
5161
5162 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5163
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005164 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005165 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005166 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005167 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005168 }
5169
Jens Axboe31067252020-05-17 17:43:31 -06005170 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005171 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005172 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005173
Jens Axboed4e7cd32020-08-15 11:44:50 -07005174 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005175 spin_unlock_irq(&ctx->completion_lock);
5176
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005177 if (!READ_ONCE(apoll->poll.canceled))
5178 __io_req_task_submit(req);
5179 else
5180 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005181
Jens Axboe6d816e02020-08-11 08:04:14 -06005182 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005183 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005184 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005185}
5186
5187static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5188 void *key)
5189{
5190 struct io_kiocb *req = wait->private;
5191 struct io_poll_iocb *poll = &req->apoll->poll;
5192
5193 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5194 key_to_poll(key));
5195
5196 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5197}
5198
5199static void io_poll_req_insert(struct io_kiocb *req)
5200{
5201 struct io_ring_ctx *ctx = req->ctx;
5202 struct hlist_head *list;
5203
5204 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5205 hlist_add_head(&req->hash_node, list);
5206}
5207
5208static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5209 struct io_poll_iocb *poll,
5210 struct io_poll_table *ipt, __poll_t mask,
5211 wait_queue_func_t wake_func)
5212 __acquires(&ctx->completion_lock)
5213{
5214 struct io_ring_ctx *ctx = req->ctx;
5215 bool cancel = false;
5216
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005217 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005218 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005219 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005220 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005221
5222 ipt->pt._key = mask;
5223 ipt->req = req;
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005224 ipt->error = 0;
5225 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005226
Jens Axboed7718a92020-02-14 22:23:12 -07005227 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov9eef9022021-07-20 10:50:43 +01005228 if (unlikely(!ipt->nr_entries) && !ipt->error)
5229 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005230
5231 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkovfca53432021-07-20 10:50:44 +01005232 if (ipt->error)
5233 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005234 if (likely(poll->head)) {
5235 spin_lock(&poll->head->lock);
5236 if (unlikely(list_empty(&poll->wait.entry))) {
5237 if (ipt->error)
5238 cancel = true;
5239 ipt->error = 0;
5240 mask = 0;
5241 }
5242 if (mask || ipt->error)
5243 list_del_init(&poll->wait.entry);
5244 else if (cancel)
5245 WRITE_ONCE(poll->canceled, true);
5246 else if (!poll->done) /* actually waiting for an event */
5247 io_poll_req_insert(req);
5248 spin_unlock(&poll->head->lock);
5249 }
5250
5251 return mask;
5252}
5253
5254static bool io_arm_poll_handler(struct io_kiocb *req)
5255{
5256 const struct io_op_def *def = &io_op_defs[req->opcode];
5257 struct io_ring_ctx *ctx = req->ctx;
5258 struct async_poll *apoll;
5259 struct io_poll_table ipt;
5260 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005261 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005262
5263 if (!req->file || !file_can_poll(req->file))
5264 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005265 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005266 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005267 if (def->pollin)
5268 rw = READ;
5269 else if (def->pollout)
5270 rw = WRITE;
5271 else
5272 return false;
5273 /* if we can't nonblock try, then no point in arming a poll handler */
5274 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005275 return false;
5276
5277 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5278 if (unlikely(!apoll))
5279 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005280 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005281
5282 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005283 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005284
Nathan Chancellor8755d972020-03-02 16:01:19 -07005285 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005286 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005287 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005288 if (def->pollout)
5289 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005290
5291 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5292 if ((req->opcode == IORING_OP_RECVMSG) &&
5293 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5294 mask &= ~POLLIN;
5295
Jens Axboed7718a92020-02-14 22:23:12 -07005296 mask |= POLLERR | POLLPRI;
5297
5298 ipt.pt._qproc = io_async_queue_proc;
5299
5300 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5301 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005302 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005303 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005304 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005305 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005306 kfree(apoll);
5307 return false;
5308 }
5309 spin_unlock_irq(&ctx->completion_lock);
5310 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5311 apoll->poll.events);
5312 return true;
5313}
5314
5315static bool __io_poll_remove_one(struct io_kiocb *req,
5316 struct io_poll_iocb *poll)
5317{
Jens Axboeb41e9852020-02-17 09:52:41 -07005318 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005319
5320 spin_lock(&poll->head->lock);
5321 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005322 if (!list_empty(&poll->wait.entry)) {
5323 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005324 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005325 }
5326 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005327 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005328 return do_complete;
5329}
5330
5331static bool io_poll_remove_one(struct io_kiocb *req)
5332{
5333 bool do_complete;
5334
Jens Axboed4e7cd32020-08-15 11:44:50 -07005335 io_poll_remove_double(req);
5336
Jens Axboed7718a92020-02-14 22:23:12 -07005337 if (req->opcode == IORING_OP_POLL_ADD) {
5338 do_complete = __io_poll_remove_one(req, &req->poll);
5339 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005340 struct async_poll *apoll = req->apoll;
5341
Jens Axboed7718a92020-02-14 22:23:12 -07005342 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005343 do_complete = __io_poll_remove_one(req, &apoll->poll);
5344 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005345 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005346 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005347 kfree(apoll);
5348 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005349 }
5350
Jens Axboeb41e9852020-02-17 09:52:41 -07005351 if (do_complete) {
5352 io_cqring_fill_event(req, -ECANCELED);
5353 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005354 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005355 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005356 }
5357
5358 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005359}
5360
Jens Axboe76e1b642020-09-26 15:05:03 -06005361/*
5362 * Returns true if we found and killed one or more poll requests
5363 */
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00005364static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5365 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005366{
Jens Axboe78076bb2019-12-04 19:56:40 -07005367 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005368 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005369 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005370
5371 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005372 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5373 struct hlist_head *list;
5374
5375 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005376 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00005377 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005378 posted += io_poll_remove_one(req);
5379 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005380 }
5381 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005382
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005383 if (posted)
5384 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005385
5386 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005387}
5388
Jens Axboe47f46762019-11-09 17:43:02 -07005389static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5390{
Jens Axboe78076bb2019-12-04 19:56:40 -07005391 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005392 struct io_kiocb *req;
5393
Jens Axboe78076bb2019-12-04 19:56:40 -07005394 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5395 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005396 if (sqe_addr != req->user_data)
5397 continue;
5398 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005399 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005400 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005401 }
5402
5403 return -ENOENT;
5404}
5405
Jens Axboe3529d8c2019-12-19 18:24:38 -07005406static int io_poll_remove_prep(struct io_kiocb *req,
5407 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005408{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005409 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5410 return -EINVAL;
5411 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5412 sqe->poll_events)
5413 return -EINVAL;
5414
Jens Axboe0969e782019-12-17 18:40:57 -07005415 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005416 return 0;
5417}
5418
5419/*
5420 * Find a running poll command that matches one specified in sqe->addr,
5421 * and remove it if found.
5422 */
5423static int io_poll_remove(struct io_kiocb *req)
5424{
5425 struct io_ring_ctx *ctx = req->ctx;
5426 u64 addr;
5427 int ret;
5428
Jens Axboe0969e782019-12-17 18:40:57 -07005429 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005430 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005431 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005432 spin_unlock_irq(&ctx->completion_lock);
5433
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005434 if (ret < 0)
5435 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005436 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005437 return 0;
5438}
5439
Jens Axboe221c5eb2019-01-17 09:41:58 -07005440static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5441 void *key)
5442{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005443 struct io_kiocb *req = wait->private;
5444 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005445
Jens Axboed7718a92020-02-14 22:23:12 -07005446 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005447}
5448
Jens Axboe221c5eb2019-01-17 09:41:58 -07005449static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5450 struct poll_table_struct *p)
5451{
5452 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5453
Jens Axboee8c2bc12020-08-15 18:44:09 -07005454 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005455}
5456
Jens Axboe3529d8c2019-12-19 18:24:38 -07005457static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005458{
5459 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005460 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005461
5462 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5463 return -EINVAL;
5464 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5465 return -EINVAL;
5466
Jiufei Xue5769a352020-06-17 17:53:55 +08005467 events = READ_ONCE(sqe->poll32_events);
5468#ifdef __BIG_ENDIAN
5469 events = swahw32(events);
5470#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005471 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5472 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005473 return 0;
5474}
5475
Pavel Begunkov014db002020-03-03 21:33:12 +03005476static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005477{
5478 struct io_poll_iocb *poll = &req->poll;
5479 struct io_ring_ctx *ctx = req->ctx;
5480 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005481 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005482
Jens Axboed7718a92020-02-14 22:23:12 -07005483 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005484
Jens Axboed7718a92020-02-14 22:23:12 -07005485 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5486 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005487
Jens Axboe8c838782019-03-12 15:48:16 -06005488 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005489 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005490 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005491 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005492 spin_unlock_irq(&ctx->completion_lock);
5493
Jens Axboe8c838782019-03-12 15:48:16 -06005494 if (mask) {
5495 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005496 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005497 }
Jens Axboe8c838782019-03-12 15:48:16 -06005498 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005499}
5500
Jens Axboe5262f562019-09-17 12:26:57 -06005501static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5502{
Jens Axboead8a48a2019-11-15 08:49:11 -07005503 struct io_timeout_data *data = container_of(timer,
5504 struct io_timeout_data, timer);
5505 struct io_kiocb *req = data->req;
5506 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005507 unsigned long flags;
5508
Jens Axboe5262f562019-09-17 12:26:57 -06005509 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005510 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005511 atomic_set(&req->ctx->cq_timeouts,
5512 atomic_read(&req->ctx->cq_timeouts) + 1);
5513
Jens Axboe78e19bb2019-11-06 15:21:34 -07005514 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005515 io_commit_cqring(ctx);
5516 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5517
5518 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005519 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005520 io_put_req(req);
5521 return HRTIMER_NORESTART;
5522}
5523
Jens Axboef254ac02020-08-12 17:33:30 -06005524static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005525{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005526 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005527 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005528
Jens Axboee8c2bc12020-08-15 18:44:09 -07005529 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005530 if (ret == -1)
5531 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005532 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005533
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005534 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005535 io_cqring_fill_event(req, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005536 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07005537 return 0;
5538}
5539
Jens Axboef254ac02020-08-12 17:33:30 -06005540static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5541{
5542 struct io_kiocb *req;
5543 int ret = -ENOENT;
5544
5545 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5546 if (user_data == req->user_data) {
5547 ret = 0;
5548 break;
5549 }
5550 }
5551
5552 if (ret == -ENOENT)
5553 return ret;
5554
5555 return __io_timeout_cancel(req);
5556}
5557
Jens Axboe3529d8c2019-12-19 18:24:38 -07005558static int io_timeout_remove_prep(struct io_kiocb *req,
5559 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005560{
Jens Axboeb29472e2019-12-17 18:50:29 -07005561 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5562 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005563 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5564 return -EINVAL;
Kamal Mostafaf59da9f2021-10-18 10:18:08 -07005565 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags ||
Pavel Begunkov54eb6212021-09-13 09:42:47 -06005566 sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005567 return -EINVAL;
5568
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005569 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005570 return 0;
5571}
5572
Jens Axboe11365042019-10-16 09:08:32 -06005573/*
5574 * Remove or update an existing timeout command
5575 */
Jens Axboefc4df992019-12-10 14:38:45 -07005576static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005577{
5578 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005579 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005580
Jens Axboe11365042019-10-16 09:08:32 -06005581 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005582 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005583
Jens Axboe47f46762019-11-09 17:43:02 -07005584 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005585 io_commit_cqring(ctx);
5586 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005587 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005588 if (ret < 0)
5589 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005590 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005591 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005592}
5593
Jens Axboe3529d8c2019-12-19 18:24:38 -07005594static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005595 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005596{
Jens Axboead8a48a2019-11-15 08:49:11 -07005597 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005598 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005599 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005600
Jens Axboead8a48a2019-11-15 08:49:11 -07005601 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005602 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06005603 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
5604 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06005605 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005606 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005607 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005608 flags = READ_ONCE(sqe->timeout_flags);
5609 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005610 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005611
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005612 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005613
Jens Axboee8c2bc12020-08-15 18:44:09 -07005614 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005615 return -ENOMEM;
5616
Jens Axboee8c2bc12020-08-15 18:44:09 -07005617 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005618 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005619
5620 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005621 return -EFAULT;
5622
Jens Axboe11365042019-10-16 09:08:32 -06005623 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005624 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005625 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005626 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005627
Jens Axboead8a48a2019-11-15 08:49:11 -07005628 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5629 return 0;
5630}
5631
Jens Axboefc4df992019-12-10 14:38:45 -07005632static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005633{
Jens Axboead8a48a2019-11-15 08:49:11 -07005634 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005635 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005636 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005637 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005638
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005639 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005640
Jens Axboe5262f562019-09-17 12:26:57 -06005641 /*
5642 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005643 * timeout event to be satisfied. If it isn't set, then this is
5644 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005645 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005646 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005647 entry = ctx->timeout_list.prev;
5648 goto add;
5649 }
Jens Axboe5262f562019-09-17 12:26:57 -06005650
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005651 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5652 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005653
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05005654 /* Update the last seq here in case io_flush_timeouts() hasn't.
5655 * This is safe because ->completion_lock is held, and submissions
5656 * and completions are never mixed in the same ->completion_lock section.
5657 */
5658 ctx->cq_last_tm_flush = tail;
5659
Jens Axboe5262f562019-09-17 12:26:57 -06005660 /*
5661 * Insertion sort, ensuring the first entry in the list is always
5662 * the one we need first.
5663 */
Jens Axboe5262f562019-09-17 12:26:57 -06005664 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005665 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5666 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005667
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005668 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005669 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005670 /* nxt.seq is behind @tail, otherwise would've been completed */
5671 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005672 break;
5673 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005674add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005675 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005676 data->timer.function = io_timeout_fn;
5677 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005678 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005679 return 0;
5680}
5681
Jens Axboe62755e32019-10-28 21:49:21 -06005682static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005683{
Jens Axboe62755e32019-10-28 21:49:21 -06005684 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005685
Jens Axboe62755e32019-10-28 21:49:21 -06005686 return req->user_data == (unsigned long) data;
5687}
5688
Jens Axboee977d6d2019-11-05 12:39:45 -07005689static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005690{
Jens Axboe62755e32019-10-28 21:49:21 -06005691 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005692 int ret = 0;
5693
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005694 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005695 switch (cancel_ret) {
5696 case IO_WQ_CANCEL_OK:
5697 ret = 0;
5698 break;
5699 case IO_WQ_CANCEL_RUNNING:
5700 ret = -EALREADY;
5701 break;
5702 case IO_WQ_CANCEL_NOTFOUND:
5703 ret = -ENOENT;
5704 break;
5705 }
5706
Jens Axboee977d6d2019-11-05 12:39:45 -07005707 return ret;
5708}
5709
Jens Axboe47f46762019-11-09 17:43:02 -07005710static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5711 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005712 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005713{
5714 unsigned long flags;
5715 int ret;
5716
5717 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5718 if (ret != -ENOENT) {
5719 spin_lock_irqsave(&ctx->completion_lock, flags);
5720 goto done;
5721 }
5722
5723 spin_lock_irqsave(&ctx->completion_lock, flags);
5724 ret = io_timeout_cancel(ctx, sqe_addr);
5725 if (ret != -ENOENT)
5726 goto done;
5727 ret = io_poll_cancel(ctx, sqe_addr);
5728done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005729 if (!ret)
5730 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005731 io_cqring_fill_event(req, ret);
5732 io_commit_cqring(ctx);
5733 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5734 io_cqring_ev_posted(ctx);
5735
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005736 if (ret < 0)
5737 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005738 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005739}
5740
Jens Axboe3529d8c2019-12-19 18:24:38 -07005741static int io_async_cancel_prep(struct io_kiocb *req,
5742 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005743{
Jens Axboefbf23842019-12-17 18:45:56 -07005744 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005745 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005746 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5747 return -EINVAL;
Pavel Begunkov54eb6212021-09-13 09:42:47 -06005748 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
5749 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07005750 return -EINVAL;
5751
Jens Axboefbf23842019-12-17 18:45:56 -07005752 req->cancel.addr = READ_ONCE(sqe->addr);
5753 return 0;
5754}
5755
Pavel Begunkov014db002020-03-03 21:33:12 +03005756static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005757{
5758 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005759
Pavel Begunkov014db002020-03-03 21:33:12 +03005760 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005761 return 0;
5762}
5763
Jens Axboe05f3fb32019-12-09 11:22:50 -07005764static int io_files_update_prep(struct io_kiocb *req,
5765 const struct io_uring_sqe *sqe)
5766{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005767 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5768 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005769 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5770 return -EINVAL;
5771 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005772 return -EINVAL;
5773
5774 req->files_update.offset = READ_ONCE(sqe->off);
5775 req->files_update.nr_args = READ_ONCE(sqe->len);
5776 if (!req->files_update.nr_args)
5777 return -EINVAL;
5778 req->files_update.arg = READ_ONCE(sqe->addr);
5779 return 0;
5780}
5781
Jens Axboe229a7b62020-06-22 10:13:11 -06005782static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5783 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005784{
5785 struct io_ring_ctx *ctx = req->ctx;
5786 struct io_uring_files_update up;
5787 int ret;
5788
Jens Axboef86cd202020-01-29 13:46:44 -07005789 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005790 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005791
5792 up.offset = req->files_update.offset;
5793 up.fds = req->files_update.arg;
5794
5795 mutex_lock(&ctx->uring_lock);
5796 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5797 mutex_unlock(&ctx->uring_lock);
5798
5799 if (ret < 0)
5800 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005801 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 return 0;
5803}
5804
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005805static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005806{
Jens Axboed625c6e2019-12-17 19:53:05 -07005807 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005808 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005809 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005810 case IORING_OP_READV:
5811 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005812 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005813 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005814 case IORING_OP_WRITEV:
5815 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005816 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005817 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005818 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005819 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005820 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005821 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005822 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005823 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005824 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005825 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005826 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005827 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005828 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005829 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005830 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005831 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005832 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005833 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005834 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005835 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005836 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005837 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005838 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005839 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005840 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005841 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005842 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005843 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005844 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005845 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005846 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005847 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005848 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005849 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005850 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005851 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005852 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005853 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005854 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005855 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005856 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005857 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005858 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005859 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005860 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005861 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005862 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005863 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005864 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005865 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005866 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005867 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005868 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005869 return io_tee_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005870 }
5871
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005872 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5873 req->opcode);
5874 return-EINVAL;
5875}
5876
Jens Axboedef596e2019-01-09 08:59:42 -07005877static int io_req_defer_prep(struct io_kiocb *req,
5878 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005879{
Jens Axboedef596e2019-01-09 08:59:42 -07005880 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005881 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005882 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005883 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005884 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005885}
5886
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005887static u32 io_get_sequence(struct io_kiocb *req)
5888{
5889 struct io_kiocb *pos;
5890 struct io_ring_ctx *ctx = req->ctx;
5891 u32 total_submitted, nr_reqs = 1;
5892
5893 if (req->flags & REQ_F_LINK_HEAD)
5894 list_for_each_entry(pos, &req->link_list, link_list)
5895 nr_reqs++;
5896
5897 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5898 return total_submitted - nr_reqs;
5899}
5900
Jens Axboe3529d8c2019-12-19 18:24:38 -07005901static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005902{
5903 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005904 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005905 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005906 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005907
5908 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005909 if (likely(list_empty_careful(&ctx->defer_list) &&
5910 !(req->flags & REQ_F_IO_DRAIN)))
5911 return 0;
5912
5913 seq = io_get_sequence(req);
5914 /* Still a chance to pass the sequence check */
5915 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005916 return 0;
5917
Jens Axboee8c2bc12020-08-15 18:44:09 -07005918 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005919 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005920 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005921 return ret;
5922 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005923 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005924 de = kmalloc(sizeof(*de), GFP_KERNEL);
5925 if (!de)
5926 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005927
5928 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005929 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005930 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005931 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005932 io_queue_async_work(req);
5933 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005934 }
5935
5936 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005937 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005938 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005939 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005940 spin_unlock_irq(&ctx->completion_lock);
5941 return -EIOCBQUEUED;
5942}
Jens Axboeedafcce2019-01-09 09:16:05 -07005943
Jens Axboef573d382020-09-22 10:19:24 -06005944static void io_req_drop_files(struct io_kiocb *req)
5945{
5946 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov52382df82021-02-09 04:47:44 +00005947 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06005948 unsigned long flags;
5949
Jens Axboed16692a2021-02-09 04:47:41 +00005950 if (req->work.flags & IO_WQ_WORK_FILES) {
5951 put_files_struct(req->work.identity->files);
5952 put_nsproxy(req->work.identity->nsproxy);
5953 }
Pavel Begunkov52504a62020-12-18 13:12:21 +00005954 spin_lock_irqsave(&ctx->inflight_lock, flags);
5955 list_del(&req->inflight_entry);
5956 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5957 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06005958 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkov52382df82021-02-09 04:47:44 +00005959 if (atomic_read(&tctx->in_idle))
5960 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06005961}
5962
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005963static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005964{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005965 if (req->flags & REQ_F_BUFFER_SELECTED) {
5966 switch (req->opcode) {
5967 case IORING_OP_READV:
5968 case IORING_OP_READ_FIXED:
5969 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005970 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005971 break;
5972 case IORING_OP_RECVMSG:
5973 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005974 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005975 break;
5976 }
5977 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005978 }
5979
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005980 if (req->flags & REQ_F_NEED_CLEANUP) {
5981 switch (req->opcode) {
5982 case IORING_OP_READV:
5983 case IORING_OP_READ_FIXED:
5984 case IORING_OP_READ:
5985 case IORING_OP_WRITEV:
5986 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005987 case IORING_OP_WRITE: {
5988 struct io_async_rw *io = req->async_data;
5989 if (io->free_iovec)
5990 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005991 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005992 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005993 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005994 case IORING_OP_SENDMSG: {
5995 struct io_async_msghdr *io = req->async_data;
5996 if (io->iov != io->fast_iov)
5997 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005998 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005999 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006000 case IORING_OP_SPLICE:
6001 case IORING_OP_TEE:
6002 io_put_file(req, req->splice.file_in,
6003 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6004 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006005 case IORING_OP_OPENAT:
6006 case IORING_OP_OPENAT2:
6007 if (req->open.filename)
6008 putname(req->open.filename);
6009 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006010 }
6011 req->flags &= ~REQ_F_NEED_CLEANUP;
6012 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006013}
6014
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006015static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6016 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006017{
Jens Axboeedafcce2019-01-09 09:16:05 -07006018 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006019 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006020
Jens Axboed625c6e2019-12-17 19:53:05 -07006021 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006022 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006023 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006024 break;
6025 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006026 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006027 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006028 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006029 break;
6030 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006032 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006033 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034 break;
6035 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006036 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006037 break;
6038 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006039 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006040 break;
6041 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006042 ret = io_poll_remove(req);
6043 break;
6044 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006045 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006046 break;
6047 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006048 ret = io_sendmsg(req, force_nonblock, cs);
6049 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006050 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006051 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006052 break;
6053 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006054 ret = io_recvmsg(req, force_nonblock, cs);
6055 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006056 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006057 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058 break;
6059 case IORING_OP_TIMEOUT:
6060 ret = io_timeout(req);
6061 break;
6062 case IORING_OP_TIMEOUT_REMOVE:
6063 ret = io_timeout_remove(req);
6064 break;
6065 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006066 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006067 break;
6068 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006069 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070 break;
6071 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006072 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006073 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006074 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006075 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006076 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006077 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006078 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006079 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006080 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006081 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006082 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006083 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006084 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006085 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006086 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006087 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006088 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006089 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006090 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006091 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006092 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006093 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006094 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006095 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006096 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006097 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006098 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006099 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006100 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006101 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006102 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006103 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006104 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006105 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006106 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006107 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006108 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006109 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006110 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006111 ret = io_tee(req, force_nonblock);
6112 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113 default:
6114 ret = -EINVAL;
6115 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006116 }
6117
6118 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006119 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006120
Jens Axboeb5325762020-05-19 21:20:27 -06006121 /* If the op doesn't have a file, we're not polling for it */
6122 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006123 const bool in_async = io_wq_current_is_worker();
6124
Jens Axboe11ba8202020-01-15 21:51:17 -07006125 /* workqueue context doesn't hold uring_lock, grab it now */
6126 if (in_async)
6127 mutex_lock(&ctx->uring_lock);
6128
Jens Axboe2b188cc2019-01-07 10:46:33 -07006129 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006130
6131 if (in_async)
6132 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006133 }
6134
6135 return 0;
6136}
6137
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006138static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006139{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006140 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006141 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006142 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006143
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006144 timeout = io_prep_linked_timeout(req);
6145 if (timeout)
6146 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006147
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006148 /* if NO_CANCEL is set, we must still run the work */
6149 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6150 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006151 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006152 }
Jens Axboe31b51512019-01-18 22:56:34 -07006153
Jens Axboe561fb042019-10-24 07:25:42 -06006154 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006155 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006156 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006157 /*
6158 * We can get EAGAIN for polled IO even though we're
6159 * forcing a sync submission from here, since we can't
6160 * wait for request slots on the block side.
6161 */
6162 if (ret != -EAGAIN)
6163 break;
6164 cond_resched();
6165 } while (1);
6166 }
Jens Axboe31b51512019-01-18 22:56:34 -07006167
Jens Axboe561fb042019-10-24 07:25:42 -06006168 if (ret) {
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006169 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangcd13f1d2020-12-06 22:22:42 +00006170
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006171 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6172 lock_ctx = req->ctx;
6173
6174 /*
6175 * io_iopoll_complete() does not hold completion_lock to
6176 * complete polled io, so here for polled io, we can not call
6177 * io_req_complete() directly, otherwise there maybe concurrent
6178 * access to cqring, defer_list, etc, which is not safe. Given
6179 * that io_iopoll_complete() is always called under uring_lock,
6180 * so here for polled io, we also get uring_lock to complete
6181 * it.
6182 */
6183 if (lock_ctx)
6184 mutex_lock(&lock_ctx->uring_lock);
6185
6186 req_set_fail_links(req);
6187 io_req_complete(req, ret);
6188
6189 if (lock_ctx)
6190 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006191 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006192
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006193 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006194}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006195
Jens Axboe65e19f52019-10-26 07:20:21 -06006196static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6197 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006198{
Jens Axboe65e19f52019-10-26 07:20:21 -06006199 struct fixed_file_table *table;
6200
Jens Axboe05f3fb32019-12-09 11:22:50 -07006201 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006202 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006203}
6204
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006205static struct file *io_file_get(struct io_submit_state *state,
6206 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006207{
6208 struct io_ring_ctx *ctx = req->ctx;
6209 struct file *file;
6210
6211 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006212 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006213 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006214 fd = array_index_nospec(fd, ctx->nr_user_files);
6215 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006216 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01006217 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06006218 percpu_ref_get(req->fixed_file_refs);
6219 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006220 } else {
6221 trace_io_uring_file_get(ctx, fd);
6222 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006223 }
6224
Pavel Begunkov8c7febf2021-02-09 04:47:47 +00006225 if (file && file->f_op == &io_uring_fops &&
6226 !(req->flags & REQ_F_INFLIGHT)) {
Jens Axboed16692a2021-02-09 04:47:41 +00006227 io_req_init_async(req);
6228 req->flags |= REQ_F_INFLIGHT;
6229
6230 spin_lock_irq(&ctx->inflight_lock);
6231 list_add(&req->inflight_entry, &ctx->inflight_list);
6232 spin_unlock_irq(&ctx->inflight_lock);
6233 }
6234
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006235 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006236}
6237
Jens Axboe3529d8c2019-12-19 18:24:38 -07006238static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006239 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006240{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006241 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006242
Jens Axboe63ff8222020-05-07 14:56:15 -06006243 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006244 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006245 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006246
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006247 req->file = io_file_get(state, req, fd, fixed);
6248 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07006249 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006250 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006251}
6252
Jens Axboe2665abf2019-11-05 12:40:47 -07006253static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6254{
Jens Axboead8a48a2019-11-15 08:49:11 -07006255 struct io_timeout_data *data = container_of(timer,
6256 struct io_timeout_data, timer);
6257 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006258 struct io_ring_ctx *ctx = req->ctx;
6259 struct io_kiocb *prev = NULL;
6260 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006261
6262 spin_lock_irqsave(&ctx->completion_lock, flags);
6263
6264 /*
6265 * We don't expect the list to be empty, that will only happen if we
6266 * race with the completion of the linked work.
6267 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006268 if (!list_empty(&req->link_list)) {
6269 prev = list_entry(req->link_list.prev, struct io_kiocb,
6270 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006271 if (refcount_inc_not_zero(&prev->refs))
Pavel Begunkov44932332019-12-05 16:16:35 +03006272 list_del_init(&req->link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006273 else
Jens Axboe76a46e02019-11-10 23:34:16 -07006274 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006275 }
6276
6277 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6278
6279 if (prev) {
Pavel Begunkov014db002020-03-03 21:33:12 +03006280 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov1c20e902021-03-10 11:30:37 +00006281 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006282 } else {
Pavel Begunkov1c20e902021-03-10 11:30:37 +00006283 io_cqring_add_event(req, -ETIME, 0);
6284 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006285 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006286 return HRTIMER_NORESTART;
6287}
6288
Jens Axboe7271ef32020-08-10 09:55:22 -06006289static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006290{
Jens Axboe76a46e02019-11-10 23:34:16 -07006291 /*
6292 * If the list is now empty, then our linked request finished before
6293 * we got a chance to setup the timer
6294 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006295 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006296 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006297
Jens Axboead8a48a2019-11-15 08:49:11 -07006298 data->timer.function = io_link_timeout_fn;
6299 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6300 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006301 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006302}
6303
6304static void io_queue_linked_timeout(struct io_kiocb *req)
6305{
6306 struct io_ring_ctx *ctx = req->ctx;
6307
6308 spin_lock_irq(&ctx->completion_lock);
6309 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006310 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006311
Jens Axboe2665abf2019-11-05 12:40:47 -07006312 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006313 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006314}
6315
Jens Axboead8a48a2019-11-15 08:49:11 -07006316static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006317{
6318 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006320 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006321 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006322 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006323 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006324
Pavel Begunkov44932332019-12-05 16:16:35 +03006325 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6326 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006327 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006328 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006329
Pavel Begunkov900fad42020-10-19 16:39:16 +01006330 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006331 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006332 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006333}
6334
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006335static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006336{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006337 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006338 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339 int ret;
6340
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006341again:
6342 linked_timeout = io_prep_linked_timeout(req);
6343
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006344 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6345 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006346 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006347 if (old_creds)
6348 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006349 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006350 old_creds = NULL; /* restored original creds */
6351 else
Jens Axboe98447d62020-10-14 10:48:51 -06006352 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006353 }
6354
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006355 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006356
6357 /*
6358 * We async punt it if the file wasn't marked NOWAIT, or if the file
6359 * doesn't support non-blocking read/write attempts
6360 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006361 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006362 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006363 /*
6364 * Queued up for async execution, worker will release
6365 * submit reference when the iocb is actually submitted.
6366 */
6367 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006369
Pavel Begunkovf063c542020-07-25 14:41:59 +03006370 if (linked_timeout)
6371 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006372 } else if (likely(!ret)) {
6373 /* drop submission reference */
6374 req = io_put_req_find_next(req);
6375 if (linked_timeout)
6376 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006377
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006378 if (req) {
6379 if (!(req->flags & REQ_F_FORCE_ASYNC))
6380 goto again;
6381 io_queue_async_work(req);
6382 }
6383 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006384 /* un-prep timeout, so it'll be killed as any other linked */
6385 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006386 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006387 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006388 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006389 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006390
Jens Axboe193155c2020-02-22 23:22:19 -07006391 if (old_creds)
6392 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006393}
6394
Jens Axboef13fad72020-06-22 09:34:30 -06006395static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6396 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006397{
6398 int ret;
6399
Jens Axboe3529d8c2019-12-19 18:24:38 -07006400 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006401 if (ret) {
6402 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006403fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006404 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006405 io_put_req(req);
6406 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006407 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006408 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006409 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006410 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006411 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006412 goto fail_req;
6413 }
Jens Axboece35a472019-12-17 08:04:44 -07006414 io_queue_async_work(req);
6415 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006416 if (sqe) {
6417 ret = io_req_prep(req, sqe);
6418 if (unlikely(ret))
6419 goto fail_req;
6420 }
6421 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006422 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006423}
6424
Jens Axboef13fad72020-06-22 09:34:30 -06006425static inline void io_queue_link_head(struct io_kiocb *req,
6426 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006427{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006428 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006429 io_put_req(req);
6430 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006431 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006432 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006433}
6434
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006435static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006436 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006437{
Jackie Liua197f662019-11-08 08:09:12 -07006438 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006439 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006440
Jens Axboe9e645e112019-05-10 16:07:28 -06006441 /*
6442 * If we already have a head request, queue this one for async
6443 * submittal once the head completes. If we don't have a head but
6444 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6445 * submitted sync once the chain is complete. If none of those
6446 * conditions are true (normal request), then just queue it.
6447 */
6448 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006449 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006450
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006451 /*
6452 * Taking sequential execution of a link, draining both sides
6453 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6454 * requests in the link. So, it drains the head and the
6455 * next after the link request. The last one is done via
6456 * drain_next flag to persist the effect across calls.
6457 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006458 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006459 head->flags |= REQ_F_IO_DRAIN;
6460 ctx->drain_next = 1;
6461 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006462 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006463 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006464 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006465 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006466 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006467 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006468 trace_io_uring_link(ctx, req, head);
6469 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006470
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006471 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006472 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006473 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006474 *link = NULL;
6475 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006476 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006477 if (unlikely(ctx->drain_next)) {
6478 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006479 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006480 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006481 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006482 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006483 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006484
Pavel Begunkov711be032020-01-17 03:57:59 +03006485 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006486 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006487 req->flags |= REQ_F_FAIL_LINK;
6488 *link = req;
6489 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006490 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006491 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006492 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006493
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006494 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006495}
6496
Jens Axboe9a56a232019-01-09 09:06:50 -07006497/*
6498 * Batched submission is done, ensure local IO is flushed out.
6499 */
6500static void io_submit_state_end(struct io_submit_state *state)
6501{
Jens Axboef13fad72020-06-22 09:34:30 -06006502 if (!list_empty(&state->comp.list))
6503 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006504 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006505 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006506 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006507 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006508}
6509
6510/*
6511 * Start submission side cache.
6512 */
6513static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006514 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006515{
6516 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006517 state->comp.nr = 0;
6518 INIT_LIST_HEAD(&state->comp.list);
6519 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006520 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006521 state->file = NULL;
6522 state->ios_left = max_ios;
6523}
6524
Jens Axboe2b188cc2019-01-07 10:46:33 -07006525static void io_commit_sqring(struct io_ring_ctx *ctx)
6526{
Hristo Venev75b28af2019-08-26 17:23:46 +00006527 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006528
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006529 /*
6530 * Ensure any loads from the SQEs are done at this point,
6531 * since once we write the new head, the application could
6532 * write new data to them.
6533 */
6534 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006535}
6536
6537/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006538 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006539 * that is mapped by userspace. This means that care needs to be taken to
6540 * ensure that reads are stable, as we cannot rely on userspace always
6541 * being a good citizen. If members of the sqe are validated and then later
6542 * used, it's important that those reads are done through READ_ONCE() to
6543 * prevent a re-load down the line.
6544 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006545static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006546{
Hristo Venev75b28af2019-08-26 17:23:46 +00006547 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006548 unsigned head;
6549
6550 /*
6551 * The cached sq head (or cq tail) serves two purposes:
6552 *
6553 * 1) allows us to batch the cost of updating the user visible
6554 * head updates.
6555 * 2) allows the kernel side to track the head on its own, even
6556 * though the application is the one updating it.
6557 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006558 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006559 if (likely(head < ctx->sq_entries))
6560 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006561
6562 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006563 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006564 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006565 return NULL;
6566}
6567
6568static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6569{
6570 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006571}
6572
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006573/*
6574 * Check SQE restrictions (opcode and flags).
6575 *
6576 * Returns 'true' if SQE is allowed, 'false' otherwise.
6577 */
6578static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6579 struct io_kiocb *req,
6580 unsigned int sqe_flags)
6581{
6582 if (!ctx->restricted)
6583 return true;
6584
6585 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6586 return false;
6587
6588 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6589 ctx->restrictions.sqe_flags_required)
6590 return false;
6591
6592 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6593 ctx->restrictions.sqe_flags_required))
6594 return false;
6595
6596 return true;
6597}
6598
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006599#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6600 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6601 IOSQE_BUFFER_SELECT)
6602
6603static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6604 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006605 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006606{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006607 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006608 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006609
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006610 req->opcode = READ_ONCE(sqe->opcode);
6611 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006612 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006613 req->file = NULL;
6614 req->ctx = ctx;
6615 req->flags = 0;
6616 /* one is dropped after submission, the other at completion */
6617 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006618 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006619 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006620
6621 if (unlikely(req->opcode >= IORING_OP_LAST))
6622 return -EINVAL;
6623
Jens Axboe9d8426a2020-06-16 18:42:49 -06006624 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6625 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006626
6627 sqe_flags = READ_ONCE(sqe->flags);
6628 /* enforce forwards compatibility on users */
6629 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6630 return -EINVAL;
6631
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006632 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6633 return -EACCES;
6634
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006635 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6636 !io_op_defs[req->opcode].buffer_select)
6637 return -EOPNOTSUPP;
6638
6639 id = READ_ONCE(sqe->personality);
6640 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006641 struct io_identity *iod;
6642
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08006643 iod = xa_load(&ctx->personalities, id);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006644 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006645 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006646 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006647
6648 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006649 get_cred(iod->creds);
6650 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006651 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006652 }
6653
6654 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006655 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006656
Jens Axboe63ff8222020-05-07 14:56:15 -06006657 if (!io_op_defs[req->opcode].needs_file)
6658 return 0;
6659
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006660 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6661 state->ios_left--;
6662 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006663}
6664
Jens Axboe0f212202020-09-13 13:09:39 -06006665static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006666{
Jens Axboeac8691c2020-06-01 08:30:41 -06006667 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006668 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006669 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006670
Jens Axboec4a2ed72019-11-21 21:01:26 -07006671 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006672 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006673 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006674 return -EBUSY;
6675 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006676
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006677 /* make sure SQ entry isn't read before tail */
6678 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006679
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006680 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6681 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006682
Jens Axboed8a6df12020-10-15 16:24:45 -06006683 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006684 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006685
Jens Axboe6c271ce2019-01-10 11:22:30 -07006686 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006687
Jens Axboe6c271ce2019-01-10 11:22:30 -07006688 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006689 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006690 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006691 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006692
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006693 sqe = io_get_sqe(ctx);
6694 if (unlikely(!sqe)) {
6695 io_consume_sqe(ctx);
6696 break;
6697 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006698 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006699 if (unlikely(!req)) {
6700 if (!submitted)
6701 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006702 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006703 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006704 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006705 /* will complete beyond this point, count as submitted */
6706 submitted++;
6707
Pavel Begunkov692d8362020-10-10 18:34:13 +01006708 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006709 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006710fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006711 io_put_req(req);
6712 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006713 break;
6714 }
6715
Jens Axboe354420f2020-01-08 18:55:15 -07006716 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006717 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006718 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006719 if (err)
6720 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006721 }
6722
Pavel Begunkov9466f432020-01-25 22:34:01 +03006723 if (unlikely(submitted != nr)) {
6724 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006725 struct io_uring_task *tctx = current->io_uring;
6726 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006727
Jens Axboed8a6df12020-10-15 16:24:45 -06006728 percpu_ref_put_many(&ctx->refs, unused);
6729 percpu_counter_sub(&tctx->inflight, unused);
6730 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006731 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006732 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006733 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006734 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006735
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006736 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6737 io_commit_sqring(ctx);
6738
Jens Axboe6c271ce2019-01-10 11:22:30 -07006739 return submitted;
6740}
6741
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006742static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6743{
6744 /* Tell userspace we may need a wakeup call */
6745 spin_lock_irq(&ctx->completion_lock);
6746 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6747 spin_unlock_irq(&ctx->completion_lock);
6748}
6749
6750static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6751{
6752 spin_lock_irq(&ctx->completion_lock);
6753 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6754 spin_unlock_irq(&ctx->completion_lock);
6755}
6756
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006757static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6758 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006759{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006760 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6761 int ret;
6762
6763 ret = autoremove_wake_function(wqe, mode, sync, key);
6764 if (ret) {
6765 unsigned long flags;
6766
6767 spin_lock_irqsave(&ctx->completion_lock, flags);
6768 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6769 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6770 }
6771 return ret;
6772}
6773
Jens Axboec8d1ba52020-09-14 11:07:26 -06006774enum sq_ret {
6775 SQT_IDLE = 1,
6776 SQT_SPIN = 2,
6777 SQT_DID_WORK = 4,
6778};
6779
6780static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006781 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006782{
6783 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006784 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006785 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006786 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006787
Jens Axboec8d1ba52020-09-14 11:07:26 -06006788again:
6789 if (!list_empty(&ctx->iopoll_list)) {
6790 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006791
Jens Axboec8d1ba52020-09-14 11:07:26 -06006792 mutex_lock(&ctx->uring_lock);
6793 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6794 io_do_iopoll(ctx, &nr_events, 0);
6795 mutex_unlock(&ctx->uring_lock);
6796 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006797
Jens Axboec8d1ba52020-09-14 11:07:26 -06006798 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006799
Jens Axboec8d1ba52020-09-14 11:07:26 -06006800 /*
6801 * If submit got -EBUSY, flag us as needing the application
6802 * to enter the kernel to reap and flush events.
6803 */
6804 if (!to_submit || ret == -EBUSY || need_resched()) {
6805 /*
6806 * Drop cur_mm before scheduling, we can't hold it for
6807 * long periods (or over schedule()). Do this before
6808 * adding ourselves to the waitqueue, as the unuse/drop
6809 * may sleep.
6810 */
6811 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006812
Jens Axboec8d1ba52020-09-14 11:07:26 -06006813 /*
6814 * We're polling. If we're within the defined idle
6815 * period, then let us spin without work before going
6816 * to sleep. The exception is if we got EBUSY doing
6817 * more IO, we should wait for the application to
6818 * reap events and wake us up.
6819 */
6820 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6821 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6822 !percpu_ref_is_dying(&ctx->refs)))
6823 return SQT_SPIN;
6824
Jens Axboe534ca6d2020-09-02 13:52:19 -06006825 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006826 TASK_INTERRUPTIBLE);
6827
6828 /*
6829 * While doing polled IO, before going to sleep, we need
6830 * to check if there are new reqs added to iopoll_list,
6831 * it is because reqs may have been punted to io worker
6832 * and will be added to iopoll_list later, hence check
6833 * the iopoll_list again.
6834 */
6835 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6836 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006837 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006838 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006839 }
6840
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006841 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006842 if (!to_submit || ret == -EBUSY)
6843 return SQT_IDLE;
6844 }
6845
Jens Axboe534ca6d2020-09-02 13:52:19 -06006846 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006847 io_ring_clear_wakeup_flag(ctx);
6848
Jens Axboee95eee22020-09-08 09:11:32 -06006849 /* if we're handling multiple rings, cap submit size for fairness */
6850 if (cap_entries && to_submit > 8)
6851 to_submit = 8;
6852
Jens Axboec8d1ba52020-09-14 11:07:26 -06006853 mutex_lock(&ctx->uring_lock);
Pavel Begunkova63d9152021-01-26 11:17:03 +00006854 if (likely(!percpu_ref_is_dying(&ctx->refs) && !ctx->sqo_dead))
Jens Axboec8d1ba52020-09-14 11:07:26 -06006855 ret = io_submit_sqes(ctx, to_submit);
6856 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006857
6858 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6859 wake_up(&ctx->sqo_sq_wait);
6860
Jens Axboec8d1ba52020-09-14 11:07:26 -06006861 return SQT_DID_WORK;
6862}
6863
Jens Axboe69fb2132020-09-14 11:16:23 -06006864static void io_sqd_init_new(struct io_sq_data *sqd)
6865{
6866 struct io_ring_ctx *ctx;
6867
6868 while (!list_empty(&sqd->ctx_new_list)) {
6869 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6870 init_wait(&ctx->sqo_wait_entry);
6871 ctx->sqo_wait_entry.func = io_sq_wake_function;
6872 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6873 complete(&ctx->sq_thread_comp);
6874 }
6875}
6876
Jens Axboe6c271ce2019-01-10 11:22:30 -07006877static int io_sq_thread(void *data)
6878{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006879 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006880 const struct cred *old_cred = NULL;
6881 struct io_sq_data *sqd = data;
6882 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006883 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006884
Jens Axboec8d1ba52020-09-14 11:07:26 -06006885 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006886 while (!kthread_should_stop()) {
6887 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006888 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006889
6890 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006891 * Any changes to the sqd lists are synchronized through the
6892 * kthread parking. This synchronizes the thread vs users,
6893 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006894 */
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006895 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006896 kthread_parkme();
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006897 /*
6898 * When sq thread is unparked, in case the previous park operation
6899 * comes from io_put_sq_data(), which means that sq thread is going
6900 * to be stopped, so here needs to have a check.
6901 */
6902 if (kthread_should_stop())
6903 break;
6904 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006905
6906 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6907 io_sqd_init_new(sqd);
6908
Jens Axboee95eee22020-09-08 09:11:32 -06006909 cap_entries = !list_is_singular(&sqd->ctx_list);
6910
Jens Axboe69fb2132020-09-14 11:16:23 -06006911 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6912 if (current->cred != ctx->creds) {
6913 if (old_cred)
6914 revert_creds(old_cred);
6915 old_cred = override_creds(ctx->creds);
6916 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006917 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006918#ifdef CONFIG_AUDIT
6919 current->loginuid = ctx->loginuid;
6920 current->sessionid = ctx->sessionid;
6921#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06006922
Jens Axboee95eee22020-09-08 09:11:32 -06006923 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006924
Jens Axboe4349f302020-07-09 15:07:01 -06006925 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006926 }
6927
Jens Axboe69fb2132020-09-14 11:16:23 -06006928 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006929 io_run_task_work();
Pavel Begunkovf7f32822021-01-11 04:00:30 +00006930 io_sq_thread_drop_mm();
Jens Axboec8d1ba52020-09-14 11:07:26 -06006931 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006932 } else if (ret == SQT_IDLE) {
6933 if (kthread_should_park())
6934 continue;
6935 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6936 io_ring_set_wakeup_flag(ctx);
6937 schedule();
6938 start_jiffies = jiffies;
6939 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6940 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006941 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006942 }
6943
Jens Axboe4c6e2772020-07-01 11:29:10 -06006944 io_run_task_work();
Pavel Begunkovf7f32822021-01-11 04:00:30 +00006945 io_sq_thread_drop_mm();
Jens Axboeb41e9852020-02-17 09:52:41 -07006946
Dennis Zhou91d8f512020-09-16 13:41:05 -07006947 if (cur_css)
6948 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006949 if (old_cred)
6950 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006951
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006952 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006953
Jens Axboe6c271ce2019-01-10 11:22:30 -07006954 return 0;
6955}
6956
Jens Axboebda52162019-09-24 13:47:15 -06006957struct io_wait_queue {
6958 struct wait_queue_entry wq;
6959 struct io_ring_ctx *ctx;
6960 unsigned to_wait;
6961 unsigned nr_timeouts;
6962};
6963
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006964static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006965{
6966 struct io_ring_ctx *ctx = iowq->ctx;
6967
6968 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006969 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006970 * started waiting. For timeouts, we always want to return to userspace,
6971 * regardless of event count.
6972 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006973 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006974 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6975}
6976
6977static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6978 int wake_flags, void *key)
6979{
6980 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6981 wq);
6982
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006983 /*
6984 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6985 * the task, and the next invocation will do it.
6986 */
6987 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6988 return autoremove_wake_function(curr, mode, wake_flags, key);
6989 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006990}
6991
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006992static int io_run_task_work_sig(void)
6993{
6994 if (io_run_task_work())
6995 return 1;
6996 if (!signal_pending(current))
6997 return 0;
6998 if (current->jobctl & JOBCTL_TASK_WORK) {
6999 spin_lock_irq(&current->sighand->siglock);
7000 current->jobctl &= ~JOBCTL_TASK_WORK;
7001 recalc_sigpending();
7002 spin_unlock_irq(&current->sighand->siglock);
7003 return 1;
7004 }
7005 return -EINTR;
7006}
7007
Jens Axboe2b188cc2019-01-07 10:46:33 -07007008/*
7009 * Wait until events become available, if we don't already have some. The
7010 * application must reap them itself, as they reside on the shared cq ring.
7011 */
7012static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
7013 const sigset_t __user *sig, size_t sigsz)
7014{
Jens Axboebda52162019-09-24 13:47:15 -06007015 struct io_wait_queue iowq = {
7016 .wq = {
7017 .private = current,
7018 .func = io_wake_function,
7019 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7020 },
7021 .ctx = ctx,
7022 .to_wait = min_events,
7023 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007024 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08007025 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007026
Jens Axboeb41e9852020-02-17 09:52:41 -07007027 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007028 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7029 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007030 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007031 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007032 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007033 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007034
7035 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007036#ifdef CONFIG_COMPAT
7037 if (in_compat_syscall())
7038 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007039 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007040 else
7041#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007042 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007043
Jens Axboe2b188cc2019-01-07 10:46:33 -07007044 if (ret)
7045 return ret;
7046 }
7047
Jens Axboebda52162019-09-24 13:47:15 -06007048 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007049 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007050 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007051 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007052 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7053 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007054 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007055 ret = io_run_task_work_sig();
Hao Xu7250f332021-02-09 04:47:46 +00007056 if (ret > 0) {
7057 finish_wait(&ctx->wait, &iowq.wq);
Jens Axboe4c6e2772020-07-01 11:29:10 -06007058 continue;
Hao Xu7250f332021-02-09 04:47:46 +00007059 }
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007060 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007061 break;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007062 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007063 break;
Hao Xu7250f332021-02-09 04:47:46 +00007064 if (test_bit(0, &ctx->cq_check_overflow)) {
7065 finish_wait(&ctx->wait, &iowq.wq);
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007066 continue;
Hao Xu7250f332021-02-09 04:47:46 +00007067 }
Jens Axboebda52162019-09-24 13:47:15 -06007068 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06007069 } while (1);
7070 finish_wait(&ctx->wait, &iowq.wq);
7071
Jens Axboeb7db41c2020-07-04 08:55:50 -06007072 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007073
Hristo Venev75b28af2019-08-26 17:23:46 +00007074 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007075}
7076
Jens Axboe6b063142019-01-10 22:13:58 -07007077static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7078{
7079#if defined(CONFIG_UNIX)
7080 if (ctx->ring_sock) {
7081 struct sock *sock = ctx->ring_sock->sk;
7082 struct sk_buff *skb;
7083
7084 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7085 kfree_skb(skb);
7086 }
7087#else
7088 int i;
7089
Jens Axboe65e19f52019-10-26 07:20:21 -06007090 for (i = 0; i < ctx->nr_user_files; i++) {
7091 struct file *file;
7092
7093 file = io_file_from_index(ctx, i);
7094 if (file)
7095 fput(file);
7096 }
Jens Axboe6b063142019-01-10 22:13:58 -07007097#endif
7098}
7099
Jens Axboe05f3fb32019-12-09 11:22:50 -07007100static void io_file_ref_kill(struct percpu_ref *ref)
7101{
7102 struct fixed_file_data *data;
7103
7104 data = container_of(ref, struct fixed_file_data, refs);
7105 complete(&data->done);
7106}
7107
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007108static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7109 struct fixed_file_ref_node *ref_node)
7110{
7111 spin_lock_bh(&file_data->lock);
7112 file_data->node = ref_node;
7113 list_add_tail(&ref_node->node, &file_data->ref_list);
7114 spin_unlock_bh(&file_data->lock);
7115 percpu_ref_get(&file_data->refs);
7116}
7117
Jens Axboe6b063142019-01-10 22:13:58 -07007118static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7119{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007120 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007121 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007122 unsigned nr_tables, i;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007123 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007124
Jens Axboe05f3fb32019-12-09 11:22:50 -07007125 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007126 return -ENXIO;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007127 backup_node = alloc_fixed_file_ref_node(ctx);
7128 if (!backup_node)
7129 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007130
Jens Axboe25a2de62020-11-23 09:37:51 -07007131 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007132 ref_node = data->node;
Jens Axboe25a2de62020-11-23 09:37:51 -07007133 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007134 if (ref_node)
7135 percpu_ref_kill(&ref_node->refs);
7136
7137 percpu_ref_kill(&data->refs);
7138
7139 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007140 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007141 do {
7142 ret = wait_for_completion_interruptible(&data->done);
7143 if (!ret)
7144 break;
7145 ret = io_run_task_work_sig();
7146 if (ret < 0) {
7147 percpu_ref_resurrect(&data->refs);
7148 reinit_completion(&data->done);
7149 io_sqe_files_set_node(data, backup_node);
7150 return ret;
7151 }
7152 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007153
Jens Axboe6b063142019-01-10 22:13:58 -07007154 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007155 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7156 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007157 kfree(data->table[i].files);
7158 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007159 percpu_ref_exit(&data->refs);
7160 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007161 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007162 ctx->nr_user_files = 0;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007163 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007164 return 0;
7165}
7166
Jens Axboe534ca6d2020-09-02 13:52:19 -06007167static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007168{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007169 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007170 /*
7171 * The park is a bit of a work-around, without it we get
7172 * warning spews on shutdown with SQPOLL set and affinity
7173 * set to a single CPU.
7174 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007175 if (sqd->thread) {
7176 kthread_park(sqd->thread);
7177 kthread_stop(sqd->thread);
7178 }
7179
7180 kfree(sqd);
7181 }
7182}
7183
Jens Axboeaa061652020-09-02 14:50:27 -06007184static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7185{
7186 struct io_ring_ctx *ctx_attach;
7187 struct io_sq_data *sqd;
7188 struct fd f;
7189
7190 f = fdget(p->wq_fd);
7191 if (!f.file)
7192 return ERR_PTR(-ENXIO);
7193 if (f.file->f_op != &io_uring_fops) {
7194 fdput(f);
7195 return ERR_PTR(-EINVAL);
7196 }
7197
7198 ctx_attach = f.file->private_data;
7199 sqd = ctx_attach->sq_data;
7200 if (!sqd) {
7201 fdput(f);
7202 return ERR_PTR(-EINVAL);
7203 }
7204
7205 refcount_inc(&sqd->refs);
7206 fdput(f);
7207 return sqd;
7208}
7209
Jens Axboe534ca6d2020-09-02 13:52:19 -06007210static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7211{
7212 struct io_sq_data *sqd;
7213
Jens Axboeaa061652020-09-02 14:50:27 -06007214 if (p->flags & IORING_SETUP_ATTACH_WQ)
7215 return io_attach_sq_data(p);
7216
Jens Axboe534ca6d2020-09-02 13:52:19 -06007217 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7218 if (!sqd)
7219 return ERR_PTR(-ENOMEM);
7220
7221 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007222 INIT_LIST_HEAD(&sqd->ctx_list);
7223 INIT_LIST_HEAD(&sqd->ctx_new_list);
7224 mutex_init(&sqd->ctx_lock);
7225 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007226 init_waitqueue_head(&sqd->wait);
7227 return sqd;
7228}
7229
Jens Axboe69fb2132020-09-14 11:16:23 -06007230static void io_sq_thread_unpark(struct io_sq_data *sqd)
7231 __releases(&sqd->lock)
7232{
7233 if (!sqd->thread)
7234 return;
7235 kthread_unpark(sqd->thread);
7236 mutex_unlock(&sqd->lock);
7237}
7238
7239static void io_sq_thread_park(struct io_sq_data *sqd)
7240 __acquires(&sqd->lock)
7241{
7242 if (!sqd->thread)
7243 return;
7244 mutex_lock(&sqd->lock);
7245 kthread_park(sqd->thread);
7246}
7247
Jens Axboe534ca6d2020-09-02 13:52:19 -06007248static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7249{
7250 struct io_sq_data *sqd = ctx->sq_data;
7251
7252 if (sqd) {
7253 if (sqd->thread) {
7254 /*
7255 * We may arrive here from the error branch in
7256 * io_sq_offload_create() where the kthread is created
7257 * without being waked up, thus wake it up now to make
7258 * sure the wait will complete.
7259 */
7260 wake_up_process(sqd->thread);
7261 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007262
7263 io_sq_thread_park(sqd);
7264 }
7265
7266 mutex_lock(&sqd->ctx_lock);
7267 list_del(&ctx->sqd_list);
7268 mutex_unlock(&sqd->ctx_lock);
7269
7270 if (sqd->thread) {
7271 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7272 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007273 }
7274
7275 io_put_sq_data(sqd);
7276 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007277 }
7278}
7279
Jens Axboe6b063142019-01-10 22:13:58 -07007280static void io_finish_async(struct io_ring_ctx *ctx)
7281{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007282 io_sq_thread_stop(ctx);
7283
Jens Axboe561fb042019-10-24 07:25:42 -06007284 if (ctx->io_wq) {
7285 io_wq_destroy(ctx->io_wq);
7286 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007287 }
7288}
7289
7290#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007291/*
7292 * Ensure the UNIX gc is aware of our file set, so we are certain that
7293 * the io_uring can be safely unregistered on process exit, even if we have
7294 * loops in the file referencing.
7295 */
7296static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7297{
7298 struct sock *sk = ctx->ring_sock->sk;
7299 struct scm_fp_list *fpl;
7300 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007301 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007302
Jens Axboe6b063142019-01-10 22:13:58 -07007303 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7304 if (!fpl)
7305 return -ENOMEM;
7306
7307 skb = alloc_skb(0, GFP_KERNEL);
7308 if (!skb) {
7309 kfree(fpl);
7310 return -ENOMEM;
7311 }
7312
7313 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007314
Jens Axboe08a45172019-10-03 08:11:03 -06007315 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007316 fpl->user = get_uid(ctx->user);
7317 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007318 struct file *file = io_file_from_index(ctx, i + offset);
7319
7320 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007321 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007322 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007323 unix_inflight(fpl->user, fpl->fp[nr_files]);
7324 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007325 }
7326
Jens Axboe08a45172019-10-03 08:11:03 -06007327 if (nr_files) {
7328 fpl->max = SCM_MAX_FD;
7329 fpl->count = nr_files;
7330 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007332 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7333 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007334
Jens Axboe08a45172019-10-03 08:11:03 -06007335 for (i = 0; i < nr_files; i++)
7336 fput(fpl->fp[i]);
7337 } else {
7338 kfree_skb(skb);
7339 kfree(fpl);
7340 }
Jens Axboe6b063142019-01-10 22:13:58 -07007341
7342 return 0;
7343}
7344
7345/*
7346 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7347 * causes regular reference counting to break down. We rely on the UNIX
7348 * garbage collection to take care of this problem for us.
7349 */
7350static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7351{
7352 unsigned left, total;
7353 int ret = 0;
7354
7355 total = 0;
7356 left = ctx->nr_user_files;
7357 while (left) {
7358 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007359
7360 ret = __io_sqe_files_scm(ctx, this_files, total);
7361 if (ret)
7362 break;
7363 left -= this_files;
7364 total += this_files;
7365 }
7366
7367 if (!ret)
7368 return 0;
7369
7370 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007371 struct file *file = io_file_from_index(ctx, total);
7372
7373 if (file)
7374 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007375 total++;
7376 }
7377
7378 return ret;
7379}
7380#else
7381static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7382{
7383 return 0;
7384}
7385#endif
7386
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007387static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7388 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007389{
7390 int i;
7391
7392 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007393 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007394 unsigned this_files;
7395
7396 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7397 table->files = kcalloc(this_files, sizeof(struct file *),
Pavel Begunkova3ed34b2021-09-13 09:37:00 -06007398 GFP_KERNEL_ACCOUNT);
Jens Axboe65e19f52019-10-26 07:20:21 -06007399 if (!table->files)
7400 break;
7401 nr_files -= this_files;
7402 }
7403
7404 if (i == nr_tables)
7405 return 0;
7406
7407 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007408 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007409 kfree(table->files);
7410 }
7411 return 1;
7412}
7413
Jens Axboe05f3fb32019-12-09 11:22:50 -07007414static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007415{
7416#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007417 struct sock *sock = ctx->ring_sock->sk;
7418 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7419 struct sk_buff *skb;
7420 int i;
7421
7422 __skb_queue_head_init(&list);
7423
7424 /*
7425 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7426 * remove this entry and rearrange the file array.
7427 */
7428 skb = skb_dequeue(head);
7429 while (skb) {
7430 struct scm_fp_list *fp;
7431
7432 fp = UNIXCB(skb).fp;
7433 for (i = 0; i < fp->count; i++) {
7434 int left;
7435
7436 if (fp->fp[i] != file)
7437 continue;
7438
7439 unix_notinflight(fp->user, fp->fp[i]);
7440 left = fp->count - 1 - i;
7441 if (left) {
7442 memmove(&fp->fp[i], &fp->fp[i + 1],
7443 left * sizeof(struct file *));
7444 }
7445 fp->count--;
7446 if (!fp->count) {
7447 kfree_skb(skb);
7448 skb = NULL;
7449 } else {
7450 __skb_queue_tail(&list, skb);
7451 }
7452 fput(file);
7453 file = NULL;
7454 break;
7455 }
7456
7457 if (!file)
7458 break;
7459
7460 __skb_queue_tail(&list, skb);
7461
7462 skb = skb_dequeue(head);
7463 }
7464
7465 if (skb_peek(&list)) {
7466 spin_lock_irq(&head->lock);
7467 while ((skb = __skb_dequeue(&list)) != NULL)
7468 __skb_queue_tail(head, skb);
7469 spin_unlock_irq(&head->lock);
7470 }
7471#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007472 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007473#endif
7474}
7475
Jens Axboe05f3fb32019-12-09 11:22:50 -07007476struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007477 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007478 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007479};
7480
Jens Axboe4a38aed22020-05-14 17:21:15 -06007481static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007482{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007483 struct fixed_file_data *file_data = ref_node->file_data;
7484 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007485 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007486
7487 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007488 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007489 io_ring_file_put(ctx, pfile->file);
7490 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007491 }
7492
Xiaoguang Wang05589552020-03-31 14:05:18 +08007493 percpu_ref_exit(&ref_node->refs);
7494 kfree(ref_node);
7495 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007496}
7497
Jens Axboe4a38aed22020-05-14 17:21:15 -06007498static void io_file_put_work(struct work_struct *work)
7499{
7500 struct io_ring_ctx *ctx;
7501 struct llist_node *node;
7502
7503 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7504 node = llist_del_all(&ctx->file_put_llist);
7505
7506 while (node) {
7507 struct fixed_file_ref_node *ref_node;
7508 struct llist_node *next = node->next;
7509
7510 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7511 __io_file_put_work(ref_node);
7512 node = next;
7513 }
7514}
7515
Jens Axboe05f3fb32019-12-09 11:22:50 -07007516static void io_file_data_ref_zero(struct percpu_ref *ref)
7517{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007518 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007519 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007520 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007521 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007522 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007523
Xiaoguang Wang05589552020-03-31 14:05:18 +08007524 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007525 data = ref_node->file_data;
7526 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007527
Jens Axboe25a2de62020-11-23 09:37:51 -07007528 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007529 ref_node->done = true;
7530
7531 while (!list_empty(&data->ref_list)) {
7532 ref_node = list_first_entry(&data->ref_list,
7533 struct fixed_file_ref_node, node);
7534 /* recycle ref nodes in order */
7535 if (!ref_node->done)
7536 break;
7537 list_del(&ref_node->node);
7538 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7539 }
Jens Axboe25a2de62020-11-23 09:37:51 -07007540 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007541
7542 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007543 delay = 0;
7544
Jens Axboe4a38aed22020-05-14 17:21:15 -06007545 if (!delay)
7546 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7547 else if (first_add)
7548 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007549}
7550
7551static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7552 struct io_ring_ctx *ctx)
7553{
7554 struct fixed_file_ref_node *ref_node;
7555
7556 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7557 if (!ref_node)
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007558 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007559
7560 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7561 0, GFP_KERNEL)) {
7562 kfree(ref_node);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007563 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007564 }
7565 INIT_LIST_HEAD(&ref_node->node);
7566 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007567 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007568 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007569 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007570}
7571
7572static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7573{
7574 percpu_ref_exit(&ref_node->refs);
7575 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007576}
7577
7578static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7579 unsigned nr_args)
7580{
7581 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007582 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007583 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007584 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007585 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007586 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007587
7588 if (ctx->file_data)
7589 return -EBUSY;
7590 if (!nr_args)
7591 return -EINVAL;
7592 if (nr_args > IORING_MAX_FIXED_FILES)
7593 return -EMFILE;
Pavel Begunkov5103b732021-09-13 09:35:35 -06007594 if (nr_args > rlimit(RLIMIT_NOFILE))
7595 return -EMFILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007596
Pavel Begunkova3ed34b2021-09-13 09:37:00 -06007597 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL_ACCOUNT);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007598 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007599 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007600 file_data->ctx = ctx;
7601 init_completion(&file_data->done);
7602 INIT_LIST_HEAD(&file_data->ref_list);
7603 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007604
7605 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007606 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkova3ed34b2021-09-13 09:37:00 -06007607 GFP_KERNEL_ACCOUNT);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007608 if (!file_data->table)
7609 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007610
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007611 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007612 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7613 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007614
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007615 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7616 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007617 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007618
7619 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7620 struct fixed_file_table *table;
7621 unsigned index;
7622
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007623 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7624 ret = -EFAULT;
7625 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007626 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007627 /* allow sparse sets */
7628 if (fd == -1)
7629 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007630
Jens Axboe05f3fb32019-12-09 11:22:50 -07007631 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007632 ret = -EBADF;
7633 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007634 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007635
7636 /*
7637 * Don't allow io_uring instances to be registered. If UNIX
7638 * isn't enabled, then this causes a reference cycle and this
7639 * instance can never get freed. If UNIX is enabled we'll
7640 * handle it just fine, but there's still no point in allowing
7641 * a ring fd as it doesn't support regular read/write anyway.
7642 */
7643 if (file->f_op == &io_uring_fops) {
7644 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007645 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007646 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007647 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7648 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007649 table->files[index] = file;
7650 }
7651
Jens Axboe05f3fb32019-12-09 11:22:50 -07007652 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007653 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007654 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007655 return ret;
7656 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007657
Xiaoguang Wang05589552020-03-31 14:05:18 +08007658 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007659 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007660 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007661 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007662 }
7663
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007664 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007665 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007666out_fput:
7667 for (i = 0; i < ctx->nr_user_files; i++) {
7668 file = io_file_from_index(ctx, i);
7669 if (file)
7670 fput(file);
7671 }
7672 for (i = 0; i < nr_tables; i++)
7673 kfree(file_data->table[i].files);
7674 ctx->nr_user_files = 0;
7675out_ref:
7676 percpu_ref_exit(&file_data->refs);
7677out_free:
7678 kfree(file_data->table);
7679 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007680 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007681 return ret;
7682}
7683
Jens Axboec3a31e62019-10-03 13:59:56 -06007684static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7685 int index)
7686{
7687#if defined(CONFIG_UNIX)
7688 struct sock *sock = ctx->ring_sock->sk;
7689 struct sk_buff_head *head = &sock->sk_receive_queue;
7690 struct sk_buff *skb;
7691
7692 /*
7693 * See if we can merge this file into an existing skb SCM_RIGHTS
7694 * file set. If there's no room, fall back to allocating a new skb
7695 * and filling it in.
7696 */
7697 spin_lock_irq(&head->lock);
7698 skb = skb_peek(head);
7699 if (skb) {
7700 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7701
7702 if (fpl->count < SCM_MAX_FD) {
7703 __skb_unlink(skb, head);
7704 spin_unlock_irq(&head->lock);
7705 fpl->fp[fpl->count] = get_file(file);
7706 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7707 fpl->count++;
7708 spin_lock_irq(&head->lock);
7709 __skb_queue_head(head, skb);
7710 } else {
7711 skb = NULL;
7712 }
7713 }
7714 spin_unlock_irq(&head->lock);
7715
7716 if (skb) {
7717 fput(file);
7718 return 0;
7719 }
7720
7721 return __io_sqe_files_scm(ctx, 1, index);
7722#else
7723 return 0;
7724#endif
7725}
7726
Hillf Dantona5318d32020-03-23 17:47:15 +08007727static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007728 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007729{
Hillf Dantona5318d32020-03-23 17:47:15 +08007730 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007731 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007732
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007734 if (!pfile)
7735 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007736
7737 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007738 list_add(&pfile->list, &ref_node->file_list);
7739
Hillf Dantona5318d32020-03-23 17:47:15 +08007740 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007741}
7742
7743static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7744 struct io_uring_files_update *up,
7745 unsigned nr_args)
7746{
7747 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007748 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007749 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007750 __s32 __user *fds;
7751 int fd, i, err;
7752 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007753 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007754
Jens Axboe05f3fb32019-12-09 11:22:50 -07007755 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007756 return -EOVERFLOW;
7757 if (done > ctx->nr_user_files)
7758 return -EINVAL;
7759
Xiaoguang Wang05589552020-03-31 14:05:18 +08007760 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007761 if (!ref_node)
7762 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007763
Jens Axboec3a31e62019-10-03 13:59:56 -06007764 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007765 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007766 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007767 struct fixed_file_table *table;
7768 unsigned index;
7769
Jens Axboec3a31e62019-10-03 13:59:56 -06007770 err = 0;
7771 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7772 err = -EFAULT;
7773 break;
7774 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007775 i = array_index_nospec(up->offset, ctx->nr_user_files);
7776 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007777 index = i & IORING_FILE_TABLE_MASK;
7778 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007779 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007780 err = io_queue_file_removal(data, file);
7781 if (err)
7782 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007783 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007784 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007785 }
7786 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007787 file = fget(fd);
7788 if (!file) {
7789 err = -EBADF;
7790 break;
7791 }
7792 /*
7793 * Don't allow io_uring instances to be registered. If
7794 * UNIX isn't enabled, then this causes a reference
7795 * cycle and this instance can never get freed. If UNIX
7796 * is enabled we'll handle it just fine, but there's
7797 * still no point in allowing a ring fd as it doesn't
7798 * support regular read/write anyway.
7799 */
7800 if (file->f_op == &io_uring_fops) {
7801 fput(file);
7802 err = -EBADF;
7803 break;
7804 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007805 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007806 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007807 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007808 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007809 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007810 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007811 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007812 }
7813 nr_args--;
7814 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007815 up->offset++;
7816 }
7817
Xiaoguang Wang05589552020-03-31 14:05:18 +08007818 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007819 percpu_ref_kill(&data->node->refs);
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007820 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007821 } else
7822 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007823
7824 return done ? done : err;
7825}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007826
Jens Axboe05f3fb32019-12-09 11:22:50 -07007827static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7828 unsigned nr_args)
7829{
7830 struct io_uring_files_update up;
7831
7832 if (!ctx->file_data)
7833 return -ENXIO;
7834 if (!nr_args)
7835 return -EINVAL;
7836 if (copy_from_user(&up, arg, sizeof(up)))
7837 return -EFAULT;
7838 if (up.resv)
7839 return -EINVAL;
7840
7841 return __io_sqe_files_update(ctx, &up, nr_args);
7842}
Jens Axboec3a31e62019-10-03 13:59:56 -06007843
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007844static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007845{
7846 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7847
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007848 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007849 io_put_req(req);
7850}
7851
Pavel Begunkov24369c22020-01-28 03:15:48 +03007852static int io_init_wq_offload(struct io_ring_ctx *ctx,
7853 struct io_uring_params *p)
7854{
7855 struct io_wq_data data;
7856 struct fd f;
7857 struct io_ring_ctx *ctx_attach;
7858 unsigned int concurrency;
7859 int ret = 0;
7860
7861 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007862 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007863 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007864
7865 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7866 /* Do QD, or 4 * CPUS, whatever is smallest */
7867 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7868
7869 ctx->io_wq = io_wq_create(concurrency, &data);
7870 if (IS_ERR(ctx->io_wq)) {
7871 ret = PTR_ERR(ctx->io_wq);
7872 ctx->io_wq = NULL;
7873 }
7874 return ret;
7875 }
7876
7877 f = fdget(p->wq_fd);
7878 if (!f.file)
7879 return -EBADF;
7880
7881 if (f.file->f_op != &io_uring_fops) {
7882 ret = -EINVAL;
7883 goto out_fput;
7884 }
7885
7886 ctx_attach = f.file->private_data;
7887 /* @io_wq is protected by holding the fd */
7888 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7889 ret = -EINVAL;
7890 goto out_fput;
7891 }
7892
7893 ctx->io_wq = ctx_attach->io_wq;
7894out_fput:
7895 fdput(f);
7896 return ret;
7897}
7898
Jens Axboe0f212202020-09-13 13:09:39 -06007899static int io_uring_alloc_task_context(struct task_struct *task)
7900{
7901 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007902 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007903
7904 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7905 if (unlikely(!tctx))
7906 return -ENOMEM;
7907
Jens Axboed8a6df12020-10-15 16:24:45 -06007908 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7909 if (unlikely(ret)) {
7910 kfree(tctx);
7911 return ret;
7912 }
7913
Jens Axboe0f212202020-09-13 13:09:39 -06007914 xa_init(&tctx->xa);
7915 init_waitqueue_head(&tctx->wait);
7916 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007917 atomic_set(&tctx->in_idle, 0);
7918 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06007919 io_init_identity(&tctx->__identity);
7920 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06007921 task->io_uring = tctx;
7922 return 0;
7923}
7924
7925void __io_uring_free(struct task_struct *tsk)
7926{
7927 struct io_uring_task *tctx = tsk->io_uring;
7928
7929 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06007930 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
7931 if (tctx->identity != &tctx->__identity)
7932 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06007933 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007934 kfree(tctx);
7935 tsk->io_uring = NULL;
7936}
7937
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007938static int io_sq_offload_create(struct io_ring_ctx *ctx,
7939 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007940{
7941 int ret;
7942
Jens Axboe6c271ce2019-01-10 11:22:30 -07007943 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007944 struct io_sq_data *sqd;
7945
Jens Axboe3ec482d2019-04-08 10:51:01 -06007946 ret = -EPERM;
7947 if (!capable(CAP_SYS_ADMIN))
7948 goto err;
7949
Jens Axboe534ca6d2020-09-02 13:52:19 -06007950 sqd = io_get_sq_data(p);
7951 if (IS_ERR(sqd)) {
7952 ret = PTR_ERR(sqd);
7953 goto err;
7954 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007955
Jens Axboe534ca6d2020-09-02 13:52:19 -06007956 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007957 io_sq_thread_park(sqd);
7958 mutex_lock(&sqd->ctx_lock);
7959 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7960 mutex_unlock(&sqd->ctx_lock);
7961 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007962
Jens Axboe917257d2019-04-13 09:28:55 -06007963 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7964 if (!ctx->sq_thread_idle)
7965 ctx->sq_thread_idle = HZ;
7966
Jens Axboeaa061652020-09-02 14:50:27 -06007967 if (sqd->thread)
7968 goto done;
7969
Jens Axboe6c271ce2019-01-10 11:22:30 -07007970 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007971 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007972
Jens Axboe917257d2019-04-13 09:28:55 -06007973 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007974 if (cpu >= nr_cpu_ids)
7975 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007976 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007977 goto err;
7978
Jens Axboe69fb2132020-09-14 11:16:23 -06007979 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007980 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007981 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007982 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007983 "io_uring-sq");
7984 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007985 if (IS_ERR(sqd->thread)) {
7986 ret = PTR_ERR(sqd->thread);
7987 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007988 goto err;
7989 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007990 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007991 if (ret)
7992 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007993 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7994 /* Can't have SQ_AFF without SQPOLL */
7995 ret = -EINVAL;
7996 goto err;
7997 }
7998
Jens Axboeaa061652020-09-02 14:50:27 -06007999done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008000 ret = io_init_wq_offload(ctx, p);
8001 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008002 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008003
8004 return 0;
8005err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008006 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008007 return ret;
8008}
8009
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008010static void io_sq_offload_start(struct io_ring_ctx *ctx)
8011{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008012 struct io_sq_data *sqd = ctx->sq_data;
8013
Yang Yingliang09058802021-07-15 21:18:25 +08008014 ctx->flags &= ~IORING_SETUP_R_DISABLED;
Yang Yingliang65b26582021-07-29 22:23:38 +08008015 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd && sqd->thread)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008016 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008017}
8018
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008019static inline void __io_unaccount_mem(struct user_struct *user,
8020 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008021{
8022 atomic_long_sub(nr_pages, &user->locked_vm);
8023}
8024
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008025static inline int __io_account_mem(struct user_struct *user,
8026 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008027{
8028 unsigned long page_limit, cur_pages, new_pages;
8029
8030 /* Don't allow more pages than we can safely lock */
8031 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8032
8033 do {
8034 cur_pages = atomic_long_read(&user->locked_vm);
8035 new_pages = cur_pages + nr_pages;
8036 if (new_pages > page_limit)
8037 return -ENOMEM;
8038 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8039 new_pages) != cur_pages);
8040
8041 return 0;
8042}
8043
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008044static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8045 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008046{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008047 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008048 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008049
Jens Axboe2aede0e2020-09-14 10:45:53 -06008050 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008051 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008052 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008053 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008054 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008055 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008056}
8057
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008058static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8059 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008060{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008061 int ret;
8062
8063 if (ctx->limit_mem) {
8064 ret = __io_account_mem(ctx->user, nr_pages);
8065 if (ret)
8066 return ret;
8067 }
8068
Jens Axboe2aede0e2020-09-14 10:45:53 -06008069 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008070 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008071 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008072 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008073 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008074 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008075
8076 return 0;
8077}
8078
Jens Axboe2b188cc2019-01-07 10:46:33 -07008079static void io_mem_free(void *ptr)
8080{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008081 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008082
Mark Rutland52e04ef2019-04-30 17:30:21 +01008083 if (!ptr)
8084 return;
8085
8086 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008087 if (put_page_testzero(page))
8088 free_compound_page(page);
8089}
8090
8091static void *io_mem_alloc(size_t size)
8092{
8093 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8094 __GFP_NORETRY;
8095
8096 return (void *) __get_free_pages(gfp_flags, get_order(size));
8097}
8098
Hristo Venev75b28af2019-08-26 17:23:46 +00008099static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8100 size_t *sq_offset)
8101{
8102 struct io_rings *rings;
8103 size_t off, sq_array_size;
8104
8105 off = struct_size(rings, cqes, cq_entries);
8106 if (off == SIZE_MAX)
8107 return SIZE_MAX;
8108
8109#ifdef CONFIG_SMP
8110 off = ALIGN(off, SMP_CACHE_BYTES);
8111 if (off == 0)
8112 return SIZE_MAX;
8113#endif
8114
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008115 if (sq_offset)
8116 *sq_offset = off;
8117
Hristo Venev75b28af2019-08-26 17:23:46 +00008118 sq_array_size = array_size(sizeof(u32), sq_entries);
8119 if (sq_array_size == SIZE_MAX)
8120 return SIZE_MAX;
8121
8122 if (check_add_overflow(off, sq_array_size, &off))
8123 return SIZE_MAX;
8124
Hristo Venev75b28af2019-08-26 17:23:46 +00008125 return off;
8126}
8127
Jens Axboe2b188cc2019-01-07 10:46:33 -07008128static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8129{
Hristo Venev75b28af2019-08-26 17:23:46 +00008130 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008131
Hristo Venev75b28af2019-08-26 17:23:46 +00008132 pages = (size_t)1 << get_order(
8133 rings_size(sq_entries, cq_entries, NULL));
8134 pages += (size_t)1 << get_order(
8135 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008136
Hristo Venev75b28af2019-08-26 17:23:46 +00008137 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008138}
8139
Jens Axboeedafcce2019-01-09 09:16:05 -07008140static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8141{
8142 int i, j;
8143
8144 if (!ctx->user_bufs)
8145 return -ENXIO;
8146
8147 for (i = 0; i < ctx->nr_user_bufs; i++) {
8148 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8149
8150 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008151 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008152
Jens Axboede293932020-09-17 16:19:16 -06008153 if (imu->acct_pages)
8154 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008155 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008156 imu->nr_bvecs = 0;
8157 }
8158
8159 kfree(ctx->user_bufs);
8160 ctx->user_bufs = NULL;
8161 ctx->nr_user_bufs = 0;
8162 return 0;
8163}
8164
8165static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8166 void __user *arg, unsigned index)
8167{
8168 struct iovec __user *src;
8169
8170#ifdef CONFIG_COMPAT
8171 if (ctx->compat) {
8172 struct compat_iovec __user *ciovs;
8173 struct compat_iovec ciov;
8174
8175 ciovs = (struct compat_iovec __user *) arg;
8176 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8177 return -EFAULT;
8178
Jens Axboed55e5f52019-12-11 16:12:15 -07008179 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008180 dst->iov_len = ciov.iov_len;
8181 return 0;
8182 }
8183#endif
8184 src = (struct iovec __user *) arg;
8185 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8186 return -EFAULT;
8187 return 0;
8188}
8189
Jens Axboede293932020-09-17 16:19:16 -06008190/*
8191 * Not super efficient, but this is just a registration time. And we do cache
8192 * the last compound head, so generally we'll only do a full search if we don't
8193 * match that one.
8194 *
8195 * We check if the given compound head page has already been accounted, to
8196 * avoid double accounting it. This allows us to account the full size of the
8197 * page, not just the constituent pages of a huge page.
8198 */
8199static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8200 int nr_pages, struct page *hpage)
8201{
8202 int i, j;
8203
8204 /* check current page array */
8205 for (i = 0; i < nr_pages; i++) {
8206 if (!PageCompound(pages[i]))
8207 continue;
8208 if (compound_head(pages[i]) == hpage)
8209 return true;
8210 }
8211
8212 /* check previously registered pages */
8213 for (i = 0; i < ctx->nr_user_bufs; i++) {
8214 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8215
8216 for (j = 0; j < imu->nr_bvecs; j++) {
8217 if (!PageCompound(imu->bvec[j].bv_page))
8218 continue;
8219 if (compound_head(imu->bvec[j].bv_page) == hpage)
8220 return true;
8221 }
8222 }
8223
8224 return false;
8225}
8226
8227static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8228 int nr_pages, struct io_mapped_ubuf *imu,
8229 struct page **last_hpage)
8230{
8231 int i, ret;
8232
8233 for (i = 0; i < nr_pages; i++) {
8234 if (!PageCompound(pages[i])) {
8235 imu->acct_pages++;
8236 } else {
8237 struct page *hpage;
8238
8239 hpage = compound_head(pages[i]);
8240 if (hpage == *last_hpage)
8241 continue;
8242 *last_hpage = hpage;
8243 if (headpage_already_acct(ctx, pages, i, hpage))
8244 continue;
8245 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8246 }
8247 }
8248
8249 if (!imu->acct_pages)
8250 return 0;
8251
8252 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8253 if (ret)
8254 imu->acct_pages = 0;
8255 return ret;
8256}
8257
Jens Axboeedafcce2019-01-09 09:16:05 -07008258static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8259 unsigned nr_args)
8260{
8261 struct vm_area_struct **vmas = NULL;
8262 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008263 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008264 int i, j, got_pages = 0;
8265 int ret = -EINVAL;
8266
8267 if (ctx->user_bufs)
8268 return -EBUSY;
8269 if (!nr_args || nr_args > UIO_MAXIOV)
8270 return -EINVAL;
8271
8272 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8273 GFP_KERNEL);
8274 if (!ctx->user_bufs)
8275 return -ENOMEM;
8276
8277 for (i = 0; i < nr_args; i++) {
8278 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8279 unsigned long off, start, end, ubuf;
8280 int pret, nr_pages;
8281 struct iovec iov;
8282 size_t size;
8283
8284 ret = io_copy_iov(ctx, &iov, arg, i);
8285 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008286 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008287
8288 /*
8289 * Don't impose further limits on the size and buffer
8290 * constraints here, we'll -EINVAL later when IO is
8291 * submitted if they are wrong.
8292 */
8293 ret = -EFAULT;
8294 if (!iov.iov_base || !iov.iov_len)
8295 goto err;
8296
8297 /* arbitrary limit, but we need something */
8298 if (iov.iov_len > SZ_1G)
8299 goto err;
8300
8301 ubuf = (unsigned long) iov.iov_base;
8302 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8303 start = ubuf >> PAGE_SHIFT;
8304 nr_pages = end - start;
8305
Jens Axboeedafcce2019-01-09 09:16:05 -07008306 ret = 0;
8307 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008308 kvfree(vmas);
8309 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008310 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008311 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008312 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008313 sizeof(struct vm_area_struct *),
8314 GFP_KERNEL);
8315 if (!pages || !vmas) {
8316 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008317 goto err;
8318 }
8319 got_pages = nr_pages;
8320 }
8321
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008322 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008323 GFP_KERNEL);
8324 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008325 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008326 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008327
8328 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008329 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008330 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008331 FOLL_WRITE | FOLL_LONGTERM,
8332 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008333 if (pret == nr_pages) {
8334 /* don't support file backed memory */
8335 for (j = 0; j < nr_pages; j++) {
8336 struct vm_area_struct *vma = vmas[j];
8337
8338 if (vma->vm_file &&
8339 !is_file_hugepages(vma->vm_file)) {
8340 ret = -EOPNOTSUPP;
8341 break;
8342 }
8343 }
8344 } else {
8345 ret = pret < 0 ? pret : -EFAULT;
8346 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008347 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008348 if (ret) {
8349 /*
8350 * if we did partial map, or found file backed vmas,
8351 * release any pages we did get
8352 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008353 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008354 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008355 kvfree(imu->bvec);
8356 goto err;
8357 }
8358
8359 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8360 if (ret) {
8361 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008362 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008363 goto err;
8364 }
8365
8366 off = ubuf & ~PAGE_MASK;
8367 size = iov.iov_len;
8368 for (j = 0; j < nr_pages; j++) {
8369 size_t vec_len;
8370
8371 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8372 imu->bvec[j].bv_page = pages[j];
8373 imu->bvec[j].bv_len = vec_len;
8374 imu->bvec[j].bv_offset = off;
8375 off = 0;
8376 size -= vec_len;
8377 }
8378 /* store original address for later verification */
8379 imu->ubuf = ubuf;
8380 imu->len = iov.iov_len;
8381 imu->nr_bvecs = nr_pages;
8382
8383 ctx->nr_user_bufs++;
8384 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008385 kvfree(pages);
8386 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008387 return 0;
8388err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008389 kvfree(pages);
8390 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008391 io_sqe_buffer_unregister(ctx);
8392 return ret;
8393}
8394
Jens Axboe9b402842019-04-11 11:45:41 -06008395static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8396{
8397 __s32 __user *fds = arg;
8398 int fd;
8399
8400 if (ctx->cq_ev_fd)
8401 return -EBUSY;
8402
8403 if (copy_from_user(&fd, fds, sizeof(*fds)))
8404 return -EFAULT;
8405
8406 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8407 if (IS_ERR(ctx->cq_ev_fd)) {
8408 int ret = PTR_ERR(ctx->cq_ev_fd);
8409 ctx->cq_ev_fd = NULL;
8410 return ret;
8411 }
8412
8413 return 0;
8414}
8415
8416static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8417{
8418 if (ctx->cq_ev_fd) {
8419 eventfd_ctx_put(ctx->cq_ev_fd);
8420 ctx->cq_ev_fd = NULL;
8421 return 0;
8422 }
8423
8424 return -ENXIO;
8425}
8426
Jens Axboe5a2e7452020-02-23 16:23:11 -07008427static void io_destroy_buffers(struct io_ring_ctx *ctx)
8428{
Jens Axboe90731882021-07-13 17:18:35 +08008429 struct io_buffer *buf;
8430 unsigned long index;
8431
8432 xa_for_each(&ctx->io_buffers, index, buf)
8433 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008434}
8435
Jens Axboe2b188cc2019-01-07 10:46:33 -07008436static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8437{
Jens Axboe6b063142019-01-10 22:13:58 -07008438 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008439 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008440
8441 if (ctx->sqo_task) {
8442 put_task_struct(ctx->sqo_task);
8443 ctx->sqo_task = NULL;
8444 mmdrop(ctx->mm_account);
8445 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008446 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008447
Dennis Zhou91d8f512020-09-16 13:41:05 -07008448#ifdef CONFIG_BLK_CGROUP
8449 if (ctx->sqo_blkcg_css)
8450 css_put(ctx->sqo_blkcg_css);
8451#endif
8452
Jens Axboe6b063142019-01-10 22:13:58 -07008453 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008454 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008455 io_destroy_buffers(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07008456
Jens Axboe2b188cc2019-01-07 10:46:33 -07008457#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008458 if (ctx->ring_sock) {
8459 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008460 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008461 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008462#endif
8463
Hristo Venev75b28af2019-08-26 17:23:46 +00008464 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008465 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008466
8467 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008468 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008469 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008470 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008471 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008472 kfree(ctx);
8473}
8474
8475static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8476{
8477 struct io_ring_ctx *ctx = file->private_data;
8478 __poll_t mask = 0;
8479
8480 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008481 /*
8482 * synchronizes with barrier from wq_has_sleeper call in
8483 * io_commit_cqring
8484 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008485 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008486 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008487 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xu81dfee42021-02-05 16:34:21 +08008488
8489 /*
8490 * Don't flush cqring overflow list here, just do a simple check.
8491 * Otherwise there could possible be ABBA deadlock:
8492 * CPU0 CPU1
8493 * ---- ----
8494 * lock(&ctx->uring_lock);
8495 * lock(&ep->mtx);
8496 * lock(&ctx->uring_lock);
8497 * lock(&ep->mtx);
8498 *
8499 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8500 * pushs them to do the flush.
8501 */
8502 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008503 mask |= EPOLLIN | EPOLLRDNORM;
8504
8505 return mask;
8506}
8507
8508static int io_uring_fasync(int fd, struct file *file, int on)
8509{
8510 struct io_ring_ctx *ctx = file->private_data;
8511
8512 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8513}
8514
Yejune Dengcb2985f2021-07-13 17:18:33 +08008515static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008516{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008517 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008518
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08008519 iod = xa_erase(&ctx->personalities, id);
Jens Axboe1e6fa522020-10-15 08:46:24 -06008520 if (iod) {
8521 put_cred(iod->creds);
8522 if (refcount_dec_and_test(&iod->count))
8523 kfree(iod);
Yejune Dengcb2985f2021-07-13 17:18:33 +08008524 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008525 }
Yejune Dengcb2985f2021-07-13 17:18:33 +08008526
8527 return -EINVAL;
8528}
8529
Jens Axboe85faa7b2020-04-09 18:14:00 -06008530static void io_ring_exit_work(struct work_struct *work)
8531{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008532 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8533 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008534
Jens Axboe56952e92020-06-17 15:00:04 -06008535 /*
8536 * If we're doing polled IO and end up having requests being
8537 * submitted async (out-of-line), then completions can come in while
8538 * we're waiting for refs to drop. We need to reap these manually,
8539 * as nobody else will be looking for them.
8540 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008541 do {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008542 io_iopoll_try_reap_events(ctx);
8543 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008544 io_ring_ctx_free(ctx);
8545}
8546
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008547static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8548{
8549 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8550
8551 return req->ctx == data;
8552}
8553
Jens Axboe2b188cc2019-01-07 10:46:33 -07008554static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8555{
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08008556 unsigned long index;
8557 struct io_identify *iod;
8558
Jens Axboe2b188cc2019-01-07 10:46:33 -07008559 mutex_lock(&ctx->uring_lock);
8560 percpu_ref_kill(&ctx->refs);
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008561 /* if force is set, the ring is going away. always drop after that */
Pavel Begunkova63d9152021-01-26 11:17:03 +00008562
8563 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8564 ctx->sqo_dead = 1;
8565
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008566 ctx->cq_overflow_flushed = 1;
Pavel Begunkovc0fd45a2020-12-06 22:22:44 +00008567 if (ctx->rings)
Pavel Begunkov85e25e22021-01-12 21:17:26 +00008568 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008569 mutex_unlock(&ctx->uring_lock);
8570
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00008571 io_kill_timeouts(ctx, NULL, NULL);
8572 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008573
8574 if (ctx->io_wq)
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008575 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008576
Jens Axboe15dff282019-11-13 09:09:23 -07008577 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008578 io_iopoll_try_reap_events(ctx);
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08008579 xa_for_each(&ctx->personalities, index, iod)
8580 io_unregister_personality(ctx, index);
Jens Axboe309fc032020-07-10 09:13:34 -06008581
8582 /*
8583 * Do this upfront, so we won't have a grace period where the ring
8584 * is closed but resources aren't reaped yet. This can cause
8585 * spurious failure in setting up a new ring.
8586 */
Jens Axboe760618f2020-07-24 12:53:31 -06008587 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8588 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008589
Jens Axboe85faa7b2020-04-09 18:14:00 -06008590 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008591 /*
8592 * Use system_unbound_wq to avoid spawning tons of event kworkers
8593 * if we're exiting a ton of rings at the same time. It just adds
8594 * noise and overhead, there's no discernable change in runtime
8595 * over using system_wq.
8596 */
8597 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008598}
8599
8600static int io_uring_release(struct inode *inode, struct file *file)
8601{
8602 struct io_ring_ctx *ctx = file->private_data;
8603
8604 file->private_data = NULL;
8605 io_ring_ctx_wait_and_kill(ctx);
8606 return 0;
8607}
8608
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008609struct io_task_cancel {
8610 struct task_struct *task;
8611 struct files_struct *files;
8612};
Jens Axboef254ac02020-08-12 17:33:30 -06008613
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008614static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008615{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008616 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008617 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008618 bool ret;
8619
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008620 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008621 unsigned long flags;
8622 struct io_ring_ctx *ctx = req->ctx;
8623
8624 /* protect against races with linked timeouts */
8625 spin_lock_irqsave(&ctx->completion_lock, flags);
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 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8628 } else {
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008629 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008630 }
8631 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008632}
8633
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008634static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008635 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008636 struct files_struct *files)
8637{
8638 struct io_defer_entry *de = NULL;
8639 LIST_HEAD(list);
8640
8641 spin_lock_irq(&ctx->completion_lock);
8642 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00008643 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008644 list_cut_position(&list, &ctx->defer_list, &de->list);
8645 break;
8646 }
8647 }
8648 spin_unlock_irq(&ctx->completion_lock);
8649
8650 while (!list_empty(&list)) {
8651 de = list_first_entry(&list, struct io_defer_entry, list);
8652 list_del_init(&de->list);
8653 req_set_fail_links(de->req);
8654 io_put_req(de->req);
8655 io_req_complete(de->req, -ECANCELED);
8656 kfree(de);
8657 }
8658}
8659
Pavel Begunkovd300d032021-02-09 04:47:45 +00008660static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8661 struct task_struct *task,
8662 struct files_struct *files)
8663{
8664 struct io_kiocb *req;
8665 int cnt = 0;
8666
8667 spin_lock_irq(&ctx->inflight_lock);
8668 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8669 cnt += io_match_task(req, task, files);
8670 spin_unlock_irq(&ctx->inflight_lock);
8671 return cnt;
8672}
8673
Pavel Begunkov49250f32021-02-09 04:47:37 +00008674static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkova773dea2020-11-06 13:00:23 +00008675 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008676 struct files_struct *files)
8677{
Jens Axboefcb323c2019-10-24 12:39:47 -06008678 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkov1e7eb062021-02-09 04:47:40 +00008679 struct io_task_cancel cancel = { .task = task, .files = files };
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008680 DEFINE_WAIT(wait);
Pavel Begunkovd300d032021-02-09 04:47:45 +00008681 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008682
Pavel Begunkovd300d032021-02-09 04:47:45 +00008683 inflight = io_uring_count_inflight(ctx, task, files);
8684 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008685 break;
Pavel Begunkovd92d0082021-01-26 11:17:10 +00008686
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008687 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8688 io_poll_remove_all(ctx, task, files);
8689 io_kill_timeouts(ctx, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008690 /* cancellations _may_ trigger task work */
8691 io_run_task_work();
Pavel Begunkovd300d032021-02-09 04:47:45 +00008692
8693 prepare_to_wait(&task->io_uring->wait, &wait,
8694 TASK_UNINTERRUPTIBLE);
8695 if (inflight == io_uring_count_inflight(ctx, task, files))
8696 schedule();
Pavel Begunkov52382df82021-02-09 04:47:44 +00008697 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008698 }
8699}
8700
Pavel Begunkov49250f32021-02-09 04:47:37 +00008701static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8702 struct task_struct *task)
Jens Axboe0f212202020-09-13 13:09:39 -06008703{
Pavel Begunkov49250f32021-02-09 04:47:37 +00008704 while (1) {
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008705 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06008706 enum io_wq_cancel cret;
Pavel Begunkov49250f32021-02-09 04:47:37 +00008707 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008708
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008709 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
Jens Axboe0f212202020-09-13 13:09:39 -06008710 if (cret != IO_WQ_CANCEL_NOTFOUND)
8711 ret = true;
8712
8713 /* SQPOLL thread does its own polling */
8714 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8715 while (!list_empty_careful(&ctx->iopoll_list)) {
8716 io_iopoll_try_reap_events(ctx);
8717 ret = true;
8718 }
8719 }
8720
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00008721 ret |= io_poll_remove_all(ctx, task, NULL);
8722 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov49250f32021-02-09 04:47:37 +00008723 if (!ret)
8724 break;
8725 io_run_task_work();
8726 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06008727 }
Jens Axboe0f212202020-09-13 13:09:39 -06008728}
8729
Pavel Begunkova63d9152021-01-26 11:17:03 +00008730static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8731{
Pavel Begunkova63d9152021-01-26 11:17:03 +00008732 mutex_lock(&ctx->uring_lock);
8733 ctx->sqo_dead = 1;
Jens Axboe6cae8092021-02-28 15:32:18 -07008734 if (ctx->flags & IORING_SETUP_R_DISABLED)
8735 io_sq_offload_start(ctx);
Pavel Begunkova63d9152021-01-26 11:17:03 +00008736 mutex_unlock(&ctx->uring_lock);
8737
8738 /* make sure callers enter the ring to get error */
Pavel Begunkov0e3562e2021-01-26 11:17:04 +00008739 if (ctx->rings)
8740 io_ring_set_wakeup_flag(ctx);
Pavel Begunkova63d9152021-01-26 11:17:03 +00008741}
8742
Jens Axboe0f212202020-09-13 13:09:39 -06008743/*
8744 * We need to iteratively cancel requests, in case a request has dependent
8745 * hard links. These persist even for failure of cancelations, hence keep
8746 * looping until none are found.
8747 */
8748static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8749 struct files_struct *files)
8750{
8751 struct task_struct *task = current;
8752
Jens Axboefdaf0832020-10-30 09:37:30 -06008753 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkova63d9152021-01-26 11:17:03 +00008754 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008755 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008756 atomic_inc(&task->io_uring->in_idle);
8757 io_sq_thread_park(ctx->sq_data);
8758 }
Jens Axboe0f212202020-09-13 13:09:39 -06008759
Pavel Begunkova773dea2020-11-06 13:00:23 +00008760 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008761 io_cqring_overflow_flush(ctx, true, task, files);
8762
Pavel Begunkov49250f32021-02-09 04:47:37 +00008763 if (!files)
8764 __io_uring_cancel_task_requests(ctx, task);
Lee Jones74878652021-11-02 15:49:30 +00008765 else
8766 io_uring_cancel_files(ctx, task, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008767
8768 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8769 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008770 io_sq_thread_unpark(ctx->sq_data);
8771 }
Jens Axboe0f212202020-09-13 13:09:39 -06008772}
8773
8774/*
8775 * Note that this task has used io_uring. We use it for cancelation purposes.
8776 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008777static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008778{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008779 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008780 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008781
8782 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008783 ret = io_uring_alloc_task_context(current);
8784 if (unlikely(ret))
8785 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008786 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008787 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008788 if (tctx->last != file) {
8789 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008790
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008791 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008792 get_file(file);
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008793 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8794 file, GFP_KERNEL));
8795 if (ret) {
8796 fput(file);
8797 return ret;
8798 }
Jens Axboe0f212202020-09-13 13:09:39 -06008799 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008800 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008801 }
8802
Jens Axboefdaf0832020-10-30 09:37:30 -06008803 /*
8804 * This is race safe in that the task itself is doing this, hence it
8805 * cannot be going through the exit/cancel paths at the same time.
8806 * This cannot be modified while exit/cancel is running.
8807 */
8808 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8809 tctx->sqpoll = true;
8810
Jens Axboe0f212202020-09-13 13:09:39 -06008811 return 0;
8812}
8813
8814/*
8815 * Remove this io_uring_file -> task mapping.
8816 */
8817static void io_uring_del_task_file(struct file *file)
8818{
8819 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008820
8821 if (tctx->last == file)
8822 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008823 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008824 if (file)
8825 fput(file);
8826}
8827
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008828static void io_uring_remove_task_files(struct io_uring_task *tctx)
8829{
8830 struct file *file;
8831 unsigned long index;
8832
8833 xa_for_each(&tctx->xa, index, file)
8834 io_uring_del_task_file(file);
8835}
8836
Jens Axboe0f212202020-09-13 13:09:39 -06008837void __io_uring_files_cancel(struct files_struct *files)
8838{
8839 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008840 struct file *file;
8841 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008842
8843 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008844 atomic_inc(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008845 xa_for_each(&tctx->xa, index, file)
8846 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008847 atomic_dec(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008848
8849 if (files)
8850 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008851}
8852
8853static s64 tctx_inflight(struct io_uring_task *tctx)
8854{
8855 unsigned long index;
8856 struct file *file;
8857 s64 inflight;
8858
8859 inflight = percpu_counter_sum(&tctx->inflight);
8860 if (!tctx->sqpoll)
8861 return inflight;
8862
8863 /*
8864 * If we have SQPOLL rings, then we need to iterate and find them, and
8865 * add the pending count for those.
8866 */
8867 xa_for_each(&tctx->xa, index, file) {
8868 struct io_ring_ctx *ctx = file->private_data;
8869
8870 if (ctx->flags & IORING_SETUP_SQPOLL) {
8871 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
8872
8873 inflight += percpu_counter_sum(&__tctx->inflight);
8874 }
8875 }
8876
8877 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008878}
8879
Jens Axboe0f212202020-09-13 13:09:39 -06008880/*
8881 * Find any io_uring fd that this task has registered or done IO on, and cancel
8882 * requests.
8883 */
8884void __io_uring_task_cancel(void)
8885{
8886 struct io_uring_task *tctx = current->io_uring;
8887 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008888 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008889
8890 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008891 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008892
Pavel Begunkov186725a2021-01-26 11:17:08 +00008893 /* trigger io_disable_sqo_submit() */
8894 if (tctx->sqpoll)
8895 __io_uring_files_cancel(NULL);
8896
Jens Axboed8a6df12020-10-15 16:24:45 -06008897 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008898 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008899 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008900 if (!inflight)
8901 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008902 __io_uring_files_cancel(NULL);
8903
8904 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8905
8906 /*
Pavel Begunkovb462a7b2021-02-09 04:47:43 +00008907 * If we've seen completions, retry without waiting. This
8908 * avoids a race where a completion comes in before we did
8909 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008910 */
Pavel Begunkovb462a7b2021-02-09 04:47:43 +00008911 if (inflight == tctx_inflight(tctx))
8912 schedule();
8913 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008914 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008915
Jens Axboefdaf0832020-10-30 09:37:30 -06008916 atomic_dec(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008917
8918 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008919}
8920
Jens Axboefcb323c2019-10-24 12:39:47 -06008921static int io_uring_flush(struct file *file, void *data)
8922{
Pavel Begunkovda676312021-01-26 11:17:02 +00008923 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova63d9152021-01-26 11:17:03 +00008924 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkovda676312021-01-26 11:17:02 +00008925
Jens Axboef0ff1a92021-02-09 04:47:42 +00008926 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8927 io_uring_cancel_task_requests(ctx, NULL);
8928
Pavel Begunkovda676312021-01-26 11:17:02 +00008929 if (!tctx)
Pavel Begunkov18f31592021-01-26 11:17:01 +00008930 return 0;
8931
Pavel Begunkovda676312021-01-26 11:17:02 +00008932 /* we should have cancelled and erased it before PF_EXITING */
8933 WARN_ON_ONCE((current->flags & PF_EXITING) &&
8934 xa_load(&tctx->xa, (unsigned long)file));
8935
Pavel Begunkov18f31592021-01-26 11:17:01 +00008936 /*
8937 * fput() is pending, will be 2 if the only other ref is our potential
8938 * task file note. If the task is exiting, drop regardless of count.
8939 */
Pavel Begunkovda676312021-01-26 11:17:02 +00008940 if (atomic_long_read(&file->f_count) != 2)
8941 return 0;
Pavel Begunkov18f31592021-01-26 11:17:01 +00008942
Pavel Begunkova63d9152021-01-26 11:17:03 +00008943 if (ctx->flags & IORING_SETUP_SQPOLL) {
8944 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov54b4c4f2021-01-26 11:17:07 +00008945 WARN_ON_ONCE(ctx->sqo_task != current &&
8946 xa_load(&tctx->xa, (unsigned long)file));
8947 /* sqo_dead check is for when this happens after cancellation */
8948 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkova63d9152021-01-26 11:17:03 +00008949 !xa_load(&tctx->xa, (unsigned long)file));
8950
8951 io_disable_sqo_submit(ctx);
8952 }
8953
8954 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
8955 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008956 return 0;
8957}
8958
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008959static void *io_uring_validate_mmap_request(struct file *file,
8960 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008961{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008962 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008963 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008964 struct page *page;
8965 void *ptr;
8966
8967 switch (offset) {
8968 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008969 case IORING_OFF_CQ_RING:
8970 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008971 break;
8972 case IORING_OFF_SQES:
8973 ptr = ctx->sq_sqes;
8974 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008975 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008976 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008977 }
8978
8979 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008980 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008981 return ERR_PTR(-EINVAL);
8982
8983 return ptr;
8984}
8985
8986#ifdef CONFIG_MMU
8987
8988static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8989{
8990 size_t sz = vma->vm_end - vma->vm_start;
8991 unsigned long pfn;
8992 void *ptr;
8993
8994 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8995 if (IS_ERR(ptr))
8996 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008997
8998 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8999 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9000}
9001
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009002#else /* !CONFIG_MMU */
9003
9004static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9005{
9006 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9007}
9008
9009static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9010{
9011 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9012}
9013
9014static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9015 unsigned long addr, unsigned long len,
9016 unsigned long pgoff, unsigned long flags)
9017{
9018 void *ptr;
9019
9020 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9021 if (IS_ERR(ptr))
9022 return PTR_ERR(ptr);
9023
9024 return (unsigned long) ptr;
9025}
9026
9027#endif /* !CONFIG_MMU */
9028
Pavel Begunkova63d9152021-01-26 11:17:03 +00009029static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009030{
Pavel Begunkova63d9152021-01-26 11:17:03 +00009031 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009032 DEFINE_WAIT(wait);
9033
9034 do {
9035 if (!io_sqring_full(ctx))
9036 break;
9037
9038 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9039
Pavel Begunkova63d9152021-01-26 11:17:03 +00009040 if (unlikely(ctx->sqo_dead)) {
9041 ret = -EOWNERDEAD;
9042 goto out;
9043 }
9044
Jens Axboe90554202020-09-03 12:12:41 -06009045 if (!io_sqring_full(ctx))
9046 break;
9047
9048 schedule();
9049 } while (!signal_pending(current));
9050
9051 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkova63d9152021-01-26 11:17:03 +00009052out:
9053 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009054}
9055
Jens Axboe2b188cc2019-01-07 10:46:33 -07009056SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9057 u32, min_complete, u32, flags, const sigset_t __user *, sig,
9058 size_t, sigsz)
9059{
9060 struct io_ring_ctx *ctx;
9061 long ret = -EBADF;
9062 int submitted = 0;
9063 struct fd f;
9064
Jens Axboe4c6e2772020-07-01 11:29:10 -06009065 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009066
Jens Axboe90554202020-09-03 12:12:41 -06009067 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9068 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009069 return -EINVAL;
9070
9071 f = fdget(fd);
9072 if (!f.file)
9073 return -EBADF;
9074
9075 ret = -EOPNOTSUPP;
9076 if (f.file->f_op != &io_uring_fops)
9077 goto out_fput;
9078
9079 ret = -ENXIO;
9080 ctx = f.file->private_data;
9081 if (!percpu_ref_tryget(&ctx->refs))
9082 goto out_fput;
9083
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009084 ret = -EBADFD;
9085 if (ctx->flags & IORING_SETUP_R_DISABLED)
9086 goto out;
9087
Jens Axboe6c271ce2019-01-10 11:22:30 -07009088 /*
9089 * For SQ polling, the thread will do all submissions and completions.
9090 * Just return the requested submit count, and wake the thread if
9091 * we were asked to.
9092 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009093 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009094 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00009095 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkovbc924dd2021-01-12 21:17:25 +00009096
Jens Axboef15e64262021-08-24 13:15:31 +01009097 if (unlikely(ctx->sqo_dead)) {
9098 ret = -EOWNERDEAD;
Pavel Begunkova63d9152021-01-26 11:17:03 +00009099 goto out;
Jens Axboef15e64262021-08-24 13:15:31 +01009100 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009101 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009102 wake_up(&ctx->sq_data->wait);
Pavel Begunkova63d9152021-01-26 11:17:03 +00009103 if (flags & IORING_ENTER_SQ_WAIT) {
9104 ret = io_sqpoll_wait_sq(ctx);
9105 if (ret)
9106 goto out;
9107 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009108 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009109 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009110 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009111 if (unlikely(ret))
9112 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009113 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009114 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009115 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009116
9117 if (submitted != to_submit)
9118 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009119 }
9120 if (flags & IORING_ENTER_GETEVENTS) {
9121 min_complete = min(min_complete, ctx->cq_entries);
9122
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009123 /*
9124 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9125 * space applications don't need to do io completion events
9126 * polling again, they can rely on io_sq_thread to do polling
9127 * work, which can reduce cpu usage and uring_lock contention.
9128 */
9129 if (ctx->flags & IORING_SETUP_IOPOLL &&
9130 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009131 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009132 } else {
9133 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9134 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009135 }
9136
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009137out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009138 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009139out_fput:
9140 fdput(f);
9141 return submitted ? submitted : ret;
9142}
9143
Tobias Klauserbebdb652020-02-26 18:38:32 +01009144#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009145static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9146 const struct io_identity *iod)
Jens Axboe87ce9552020-01-30 08:25:34 -07009147{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009148 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009149 struct user_namespace *uns = seq_user_ns(m);
9150 struct group_info *gi;
9151 kernel_cap_t cap;
9152 unsigned __capi;
9153 int g;
9154
9155 seq_printf(m, "%5d\n", id);
9156 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9157 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9158 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9159 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9160 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9161 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9162 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9163 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9164 seq_puts(m, "\n\tGroups:\t");
9165 gi = cred->group_info;
9166 for (g = 0; g < gi->ngroups; g++) {
9167 seq_put_decimal_ull(m, g ? " " : "",
9168 from_kgid_munged(uns, gi->gid[g]));
9169 }
9170 seq_puts(m, "\n\tCapEff:\t");
9171 cap = cred->cap_effective;
9172 CAP_FOR_EACH_U32(__capi)
9173 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9174 seq_putc(m, '\n');
9175 return 0;
9176}
9177
9178static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9179{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009180 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009181 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009182 int i;
9183
Jens Axboefad8e0d2020-09-28 08:57:48 -06009184 /*
9185 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9186 * since fdinfo case grabs it in the opposite direction of normal use
9187 * cases. If we fail to get the lock, we just don't iterate any
9188 * structures that could be going away outside the io_uring mutex.
9189 */
9190 has_lock = mutex_trylock(&ctx->uring_lock);
9191
Joseph Qidbbe9c62020-09-29 09:01:22 -06009192 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9193 sq = ctx->sq_data;
9194
9195 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9196 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009197 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009198 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009199 struct fixed_file_table *table;
9200 struct file *f;
9201
9202 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9203 f = table->files[i & IORING_FILE_TABLE_MASK];
9204 if (f)
9205 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9206 else
9207 seq_printf(m, "%5u: <none>\n", i);
9208 }
9209 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009210 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009211 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9212
9213 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9214 (unsigned int) buf->len);
9215 }
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009216 if (has_lock && !xa_empty(&ctx->personalities)) {
9217 unsigned long index;
9218 const struct io_identity *iod;
9219
Jens Axboe87ce9552020-01-30 08:25:34 -07009220 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009221 xa_for_each(&ctx->personalities, index, iod)
9222 io_uring_show_cred(m, index, iod);
Jens Axboe87ce9552020-01-30 08:25:34 -07009223 }
Jens Axboed7718a92020-02-14 22:23:12 -07009224 seq_printf(m, "PollList:\n");
9225 spin_lock_irq(&ctx->completion_lock);
9226 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9227 struct hlist_head *list = &ctx->cancel_hash[i];
9228 struct io_kiocb *req;
9229
9230 hlist_for_each_entry(req, list, hash_node)
9231 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9232 req->task->task_works != NULL);
9233 }
9234 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009235 if (has_lock)
9236 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009237}
9238
9239static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9240{
9241 struct io_ring_ctx *ctx = f->private_data;
9242
9243 if (percpu_ref_tryget(&ctx->refs)) {
9244 __io_uring_show_fdinfo(ctx, m);
9245 percpu_ref_put(&ctx->refs);
9246 }
9247}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009248#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009249
Jens Axboe2b188cc2019-01-07 10:46:33 -07009250static const struct file_operations io_uring_fops = {
9251 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009252 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009253 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009254#ifndef CONFIG_MMU
9255 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9256 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9257#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009258 .poll = io_uring_poll,
9259 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009260#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009261 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009262#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009263};
9264
9265static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9266 struct io_uring_params *p)
9267{
Hristo Venev75b28af2019-08-26 17:23:46 +00009268 struct io_rings *rings;
9269 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009270
Jens Axboebd740482020-08-05 12:58:23 -06009271 /* make sure these are sane, as we already accounted them */
9272 ctx->sq_entries = p->sq_entries;
9273 ctx->cq_entries = p->cq_entries;
9274
Hristo Venev75b28af2019-08-26 17:23:46 +00009275 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9276 if (size == SIZE_MAX)
9277 return -EOVERFLOW;
9278
9279 rings = io_mem_alloc(size);
9280 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009281 return -ENOMEM;
9282
Hristo Venev75b28af2019-08-26 17:23:46 +00009283 ctx->rings = rings;
9284 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9285 rings->sq_ring_mask = p->sq_entries - 1;
9286 rings->cq_ring_mask = p->cq_entries - 1;
9287 rings->sq_ring_entries = p->sq_entries;
9288 rings->cq_ring_entries = p->cq_entries;
9289 ctx->sq_mask = rings->sq_ring_mask;
9290 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009291
9292 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009293 if (size == SIZE_MAX) {
9294 io_mem_free(ctx->rings);
9295 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009296 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009297 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009298
9299 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009300 if (!ctx->sq_sqes) {
9301 io_mem_free(ctx->rings);
9302 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009304 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009305
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306 return 0;
9307}
9308
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009309static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9310{
9311 int ret, fd;
9312
9313 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9314 if (fd < 0)
9315 return fd;
9316
9317 ret = io_uring_add_task_file(ctx, file);
9318 if (ret) {
9319 put_unused_fd(fd);
9320 return ret;
9321 }
9322 fd_install(fd, file);
9323 return fd;
9324}
9325
Jens Axboe2b188cc2019-01-07 10:46:33 -07009326/*
9327 * Allocate an anonymous fd, this is what constitutes the application
9328 * visible backing of an io_uring instance. The application mmaps this
9329 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9330 * we have to tie this fd to a socket for file garbage collection purposes.
9331 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009332static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009333{
9334 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009335#if defined(CONFIG_UNIX)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009336 int ret;
9337
Jens Axboe2b188cc2019-01-07 10:46:33 -07009338 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9339 &ctx->ring_sock);
9340 if (ret)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009341 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342#endif
9343
Jens Axboe2b188cc2019-01-07 10:46:33 -07009344 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9345 O_RDWR | O_CLOEXEC);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009346#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009347 if (IS_ERR(file)) {
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009348 sock_release(ctx->ring_sock);
9349 ctx->ring_sock = NULL;
9350 } else {
9351 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009353#endif
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009354 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009355}
9356
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009357static int io_uring_create(unsigned entries, struct io_uring_params *p,
9358 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009359{
9360 struct user_struct *user = NULL;
9361 struct io_ring_ctx *ctx;
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009362 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009363 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009364 int ret;
9365
Jens Axboe8110c1a2019-12-28 15:39:54 -07009366 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009367 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009368 if (entries > IORING_MAX_ENTRIES) {
9369 if (!(p->flags & IORING_SETUP_CLAMP))
9370 return -EINVAL;
9371 entries = IORING_MAX_ENTRIES;
9372 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009373
9374 /*
9375 * Use twice as many entries for the CQ ring. It's possible for the
9376 * application to drive a higher depth than the size of the SQ ring,
9377 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009378 * some flexibility in overcommitting a bit. If the application has
9379 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9380 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009381 */
9382 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009383 if (p->flags & IORING_SETUP_CQSIZE) {
9384 /*
9385 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9386 * to a power-of-two, if it isn't already. We do NOT impose
9387 * any cq vs sq ring sizing.
9388 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009389 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009390 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009391 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9392 if (!(p->flags & IORING_SETUP_CLAMP))
9393 return -EINVAL;
9394 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9395 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009396 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9397 if (p->cq_entries < p->sq_entries)
9398 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009399 } else {
9400 p->cq_entries = 2 * p->sq_entries;
9401 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009402
9403 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009404 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009406 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009407 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408 ring_pages(p->sq_entries, p->cq_entries));
9409 if (ret) {
9410 free_uid(user);
9411 return ret;
9412 }
9413 }
9414
9415 ctx = io_ring_ctx_alloc(p);
9416 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009417 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009418 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009419 p->cq_entries));
9420 free_uid(user);
9421 return -ENOMEM;
9422 }
9423 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009424 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009425 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009426#ifdef CONFIG_AUDIT
9427 ctx->loginuid = current->loginuid;
9428 ctx->sessionid = current->sessionid;
9429#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009430 ctx->sqo_task = get_task_struct(current);
9431
9432 /*
9433 * This is just grabbed for accounting purposes. When a process exits,
9434 * the mm is exited and dropped before the files, hence we need to hang
9435 * on to this mm purely for the purposes of being able to unaccount
9436 * memory (locked/pinned vm). It's not used for anything else.
9437 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009438 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009439 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009440
Dennis Zhou91d8f512020-09-16 13:41:05 -07009441#ifdef CONFIG_BLK_CGROUP
9442 /*
9443 * The sq thread will belong to the original cgroup it was inited in.
9444 * If the cgroup goes offline (e.g. disabling the io controller), then
9445 * issued bios will be associated with the closest cgroup later in the
9446 * block layer.
9447 */
9448 rcu_read_lock();
9449 ctx->sqo_blkcg_css = blkcg_css();
9450 ret = css_tryget_online(ctx->sqo_blkcg_css);
9451 rcu_read_unlock();
9452 if (!ret) {
9453 /* don't init against a dying cgroup, have the user try again */
9454 ctx->sqo_blkcg_css = NULL;
9455 ret = -ENODEV;
9456 goto err;
9457 }
9458#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009459
Jens Axboe2b188cc2019-01-07 10:46:33 -07009460 /*
9461 * Account memory _before_ installing the file descriptor. Once
9462 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009463 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009464 * will un-account as well.
9465 */
9466 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9467 ACCT_LOCKED);
9468 ctx->limit_mem = limit_mem;
9469
9470 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009471 if (ret)
9472 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009473
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009474 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009475 if (ret)
9476 goto err;
9477
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009478 if (!(p->flags & IORING_SETUP_R_DISABLED))
9479 io_sq_offload_start(ctx);
9480
Jens Axboe2b188cc2019-01-07 10:46:33 -07009481 memset(&p->sq_off, 0, sizeof(p->sq_off));
9482 p->sq_off.head = offsetof(struct io_rings, sq.head);
9483 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9484 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9485 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9486 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9487 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9488 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9489
9490 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009491 p->cq_off.head = offsetof(struct io_rings, cq.head);
9492 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9493 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9494 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9495 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9496 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009497 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009498
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009499 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9500 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009501 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9502 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009503
9504 if (copy_to_user(params, p, sizeof(*p))) {
9505 ret = -EFAULT;
9506 goto err;
9507 }
Jens Axboed1719f72020-07-30 13:43:53 -06009508
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009509 file = io_uring_get_file(ctx);
9510 if (IS_ERR(file)) {
9511 ret = PTR_ERR(file);
9512 goto err;
9513 }
9514
Jens Axboed1719f72020-07-30 13:43:53 -06009515 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009516 * Install ring fd as the very last thing, so we don't risk someone
9517 * having closed it before we finish setup
9518 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009519 ret = io_uring_install_fd(ctx, file);
9520 if (ret < 0) {
Pavel Begunkov8cb6f4d2021-01-26 11:17:05 +00009521 io_disable_sqo_submit(ctx);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009522 /* fput will clean it up */
9523 fput(file);
9524 return ret;
9525 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009526
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009527 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009528 return ret;
9529err:
Pavel Begunkova63d9152021-01-26 11:17:03 +00009530 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009531 io_ring_ctx_wait_and_kill(ctx);
9532 return ret;
9533}
9534
9535/*
9536 * Sets up an aio uring context, and returns the fd. Applications asks for a
9537 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9538 * params structure passed in.
9539 */
9540static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9541{
9542 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009543 int i;
9544
9545 if (copy_from_user(&p, params, sizeof(p)))
9546 return -EFAULT;
9547 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9548 if (p.resv[i])
9549 return -EINVAL;
9550 }
9551
Jens Axboe6c271ce2019-01-10 11:22:30 -07009552 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009553 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009554 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9555 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009556 return -EINVAL;
9557
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009558 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009559}
9560
9561SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9562 struct io_uring_params __user *, params)
9563{
9564 return io_uring_setup(entries, params);
9565}
9566
Jens Axboe66f4af92020-01-16 15:36:52 -07009567static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9568{
9569 struct io_uring_probe *p;
9570 size_t size;
9571 int i, ret;
9572
9573 size = struct_size(p, ops, nr_args);
9574 if (size == SIZE_MAX)
9575 return -EOVERFLOW;
9576 p = kzalloc(size, GFP_KERNEL);
9577 if (!p)
9578 return -ENOMEM;
9579
9580 ret = -EFAULT;
9581 if (copy_from_user(p, arg, size))
9582 goto out;
9583 ret = -EINVAL;
9584 if (memchr_inv(p, 0, size))
9585 goto out;
9586
9587 p->last_op = IORING_OP_LAST - 1;
9588 if (nr_args > IORING_OP_LAST)
9589 nr_args = IORING_OP_LAST;
9590
9591 for (i = 0; i < nr_args; i++) {
9592 p->ops[i].op = i;
9593 if (!io_op_defs[i].not_supported)
9594 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9595 }
9596 p->ops_len = i;
9597
9598 ret = 0;
9599 if (copy_to_user(arg, p, size))
9600 ret = -EFAULT;
9601out:
9602 kfree(p);
9603 return ret;
9604}
9605
Jens Axboe071698e2020-01-28 10:04:42 -07009606static int io_register_personality(struct io_ring_ctx *ctx)
9607{
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009608 struct io_identity *iod;
9609 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009610 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009611
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009612 iod = kmalloc(sizeof(*iod), GFP_KERNEL);
9613 if (unlikely(!iod))
Jens Axboe1e6fa522020-10-15 08:46:24 -06009614 return -ENOMEM;
9615
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009616 io_init_identity(iod);
9617 iod->creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009618
Matthew Wilcox (Oracle)c5a50a22021-07-13 17:18:34 +08009619 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)iod,
9620 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboe695ab282021-08-24 13:15:01 +01009621 if (ret < 0) {
9622 put_cred(iod->creds);
9623 kfree(iod);
9624 return ret;
9625 }
9626 return id;
Jens Axboe071698e2020-01-28 10:04:42 -07009627}
9628
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009629static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9630 unsigned int nr_args)
9631{
9632 struct io_uring_restriction *res;
9633 size_t size;
9634 int i, ret;
9635
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009636 /* Restrictions allowed only if rings started disabled */
9637 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9638 return -EBADFD;
9639
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009640 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009641 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009642 return -EBUSY;
9643
9644 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9645 return -EINVAL;
9646
9647 size = array_size(nr_args, sizeof(*res));
9648 if (size == SIZE_MAX)
9649 return -EOVERFLOW;
9650
9651 res = memdup_user(arg, size);
9652 if (IS_ERR(res))
9653 return PTR_ERR(res);
9654
9655 ret = 0;
9656
9657 for (i = 0; i < nr_args; i++) {
9658 switch (res[i].opcode) {
9659 case IORING_RESTRICTION_REGISTER_OP:
9660 if (res[i].register_op >= IORING_REGISTER_LAST) {
9661 ret = -EINVAL;
9662 goto out;
9663 }
9664
9665 __set_bit(res[i].register_op,
9666 ctx->restrictions.register_op);
9667 break;
9668 case IORING_RESTRICTION_SQE_OP:
9669 if (res[i].sqe_op >= IORING_OP_LAST) {
9670 ret = -EINVAL;
9671 goto out;
9672 }
9673
9674 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9675 break;
9676 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9677 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9678 break;
9679 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9680 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9681 break;
9682 default:
9683 ret = -EINVAL;
9684 goto out;
9685 }
9686 }
9687
9688out:
9689 /* Reset all restrictions if an error happened */
9690 if (ret != 0)
9691 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9692 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009693 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009694
9695 kfree(res);
9696 return ret;
9697}
9698
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009699static int io_register_enable_rings(struct io_ring_ctx *ctx)
9700{
9701 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9702 return -EBADFD;
9703
9704 if (ctx->restrictions.registered)
9705 ctx->restricted = 1;
9706
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009707 io_sq_offload_start(ctx);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009708 return 0;
9709}
9710
Jens Axboe071698e2020-01-28 10:04:42 -07009711static bool io_register_op_must_quiesce(int op)
9712{
9713 switch (op) {
9714 case IORING_UNREGISTER_FILES:
9715 case IORING_REGISTER_FILES_UPDATE:
9716 case IORING_REGISTER_PROBE:
9717 case IORING_REGISTER_PERSONALITY:
9718 case IORING_UNREGISTER_PERSONALITY:
9719 return false;
9720 default:
9721 return true;
9722 }
9723}
9724
Jens Axboeedafcce2019-01-09 09:16:05 -07009725static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9726 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009727 __releases(ctx->uring_lock)
9728 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009729{
9730 int ret;
9731
Jens Axboe35fa71a2019-04-22 10:23:23 -06009732 /*
9733 * We're inside the ring mutex, if the ref is already dying, then
9734 * someone else killed the ctx or is already going through
9735 * io_uring_register().
9736 */
9737 if (percpu_ref_is_dying(&ctx->refs))
9738 return -ENXIO;
9739
Jens Axboe071698e2020-01-28 10:04:42 -07009740 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009741 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009742
Jens Axboe05f3fb32019-12-09 11:22:50 -07009743 /*
9744 * Drop uring mutex before waiting for references to exit. If
9745 * another thread is currently inside io_uring_enter() it might
9746 * need to grab the uring_lock to make progress. If we hold it
9747 * here across the drain wait, then we can deadlock. It's safe
9748 * to drop the mutex here, since no new references will come in
9749 * after we've killed the percpu ref.
9750 */
9751 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009752 do {
9753 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9754 if (!ret)
9755 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009756 ret = io_run_task_work_sig();
9757 if (ret < 0)
9758 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009759 } while (1);
9760
Jens Axboe05f3fb32019-12-09 11:22:50 -07009761 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009762
Jens Axboec1503682020-01-08 08:26:07 -07009763 if (ret) {
9764 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009765 goto out_quiesce;
9766 }
9767 }
9768
9769 if (ctx->restricted) {
9770 if (opcode >= IORING_REGISTER_LAST) {
9771 ret = -EINVAL;
9772 goto out;
9773 }
9774
9775 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9776 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009777 goto out;
9778 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009779 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009780
9781 switch (opcode) {
9782 case IORING_REGISTER_BUFFERS:
9783 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9784 break;
9785 case IORING_UNREGISTER_BUFFERS:
9786 ret = -EINVAL;
9787 if (arg || nr_args)
9788 break;
9789 ret = io_sqe_buffer_unregister(ctx);
9790 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009791 case IORING_REGISTER_FILES:
9792 ret = io_sqe_files_register(ctx, arg, nr_args);
9793 break;
9794 case IORING_UNREGISTER_FILES:
9795 ret = -EINVAL;
9796 if (arg || nr_args)
9797 break;
9798 ret = io_sqe_files_unregister(ctx);
9799 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009800 case IORING_REGISTER_FILES_UPDATE:
9801 ret = io_sqe_files_update(ctx, arg, nr_args);
9802 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009803 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009804 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009805 ret = -EINVAL;
9806 if (nr_args != 1)
9807 break;
9808 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009809 if (ret)
9810 break;
9811 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9812 ctx->eventfd_async = 1;
9813 else
9814 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009815 break;
9816 case IORING_UNREGISTER_EVENTFD:
9817 ret = -EINVAL;
9818 if (arg || nr_args)
9819 break;
9820 ret = io_eventfd_unregister(ctx);
9821 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009822 case IORING_REGISTER_PROBE:
9823 ret = -EINVAL;
9824 if (!arg || nr_args > 256)
9825 break;
9826 ret = io_probe(ctx, arg, nr_args);
9827 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009828 case IORING_REGISTER_PERSONALITY:
9829 ret = -EINVAL;
9830 if (arg || nr_args)
9831 break;
9832 ret = io_register_personality(ctx);
9833 break;
9834 case IORING_UNREGISTER_PERSONALITY:
9835 ret = -EINVAL;
9836 if (arg)
9837 break;
9838 ret = io_unregister_personality(ctx, nr_args);
9839 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009840 case IORING_REGISTER_ENABLE_RINGS:
9841 ret = -EINVAL;
9842 if (arg || nr_args)
9843 break;
9844 ret = io_register_enable_rings(ctx);
9845 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009846 case IORING_REGISTER_RESTRICTIONS:
9847 ret = io_register_restrictions(ctx, arg, nr_args);
9848 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009849 default:
9850 ret = -EINVAL;
9851 break;
9852 }
9853
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009854out:
Jens Axboe071698e2020-01-28 10:04:42 -07009855 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009856 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009857 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009858out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009859 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009860 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009861 return ret;
9862}
9863
9864SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9865 void __user *, arg, unsigned int, nr_args)
9866{
9867 struct io_ring_ctx *ctx;
9868 long ret = -EBADF;
9869 struct fd f;
9870
9871 f = fdget(fd);
9872 if (!f.file)
9873 return -EBADF;
9874
9875 ret = -EOPNOTSUPP;
9876 if (f.file->f_op != &io_uring_fops)
9877 goto out_fput;
9878
9879 ctx = f.file->private_data;
9880
9881 mutex_lock(&ctx->uring_lock);
9882 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9883 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009884 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9885 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009886out_fput:
9887 fdput(f);
9888 return ret;
9889}
9890
Jens Axboe2b188cc2019-01-07 10:46:33 -07009891static int __init io_uring_init(void)
9892{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009893#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9894 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9895 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9896} while (0)
9897
9898#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9899 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9900 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9901 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9902 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9903 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9904 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9905 BUILD_BUG_SQE_ELEM(8, __u64, off);
9906 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9907 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009908 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009909 BUILD_BUG_SQE_ELEM(24, __u32, len);
9910 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9911 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9912 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9913 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009914 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9915 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009916 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9917 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9918 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9919 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9920 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9921 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9922 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9923 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009924 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009925 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9926 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9927 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009928 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009929
Jens Axboed3656342019-12-18 09:50:26 -07009930 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009931 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009932 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9933 return 0;
9934};
9935__initcall(io_uring_init);