blob: 273ebbac065462420f0c648c857eb8a25a217d4d [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
107struct io_uring {
108 u32 head ____cacheline_aligned_in_smp;
109 u32 tail ____cacheline_aligned_in_smp;
110};
111
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * This data is shared with the application through the mmap at offsets
114 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 *
116 * The offsets to the member fields are published through struct
117 * io_sqring_offsets when calling io_uring_setup.
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
121 * Head and tail offsets into the ring; the offsets need to be
122 * masked to get valid indices.
123 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * The kernel controls head of the sq ring and the tail of the cq ring,
125 * and the application controls tail of the sq ring and the head of the
126 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000130 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 * ring_entries - 1)
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_ring_mask, cq_ring_mask;
134 /* Ring sizes (constant, power of 2) */
135 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
137 * Number of invalid entries dropped by the kernel due to
138 * invalid index stored in array
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
142 * cached value).
143 *
144 * After a new SQ head value was read by the application this
145 * counter includes all submissions that were dropped reaching
146 * the new SQ head (and possibly more).
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200150 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 *
152 * Written by the kernel, shouldn't be modified by the
153 * application.
154 *
155 * The application needs a full memory barrier before checking
156 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200160 * Runtime CQ flags
161 *
162 * Written by the application, shouldn't be modified by the
163 * kernel.
164 */
165 u32 cq_flags;
166 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 * Number of completion events lost because the queue was full;
168 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800169 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * the completion queue.
171 *
172 * Written by the kernel, shouldn't be modified by the
173 * application (i.e. get number of "new events" by comparing to
174 * cached value).
175 *
176 * As completion events come in out of order this counter is not
177 * ordered with any other data.
178 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000179 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 /*
181 * Ring buffer of completion events.
182 *
183 * The kernel writes completion events fresh every time they are
184 * produced, so the application is allowed to modify pending
185 * entries.
186 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000187 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700188};
189
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000190enum io_uring_cmd_flags {
191 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000192 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000193};
194
Jens Axboeedafcce2019-01-09 09:16:05 -0700195struct io_mapped_ubuf {
196 u64 ubuf;
197 size_t len;
198 struct bio_vec *bvec;
199 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600200 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700201};
202
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000203struct io_ring_ctx;
204
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000205struct io_rsrc_put {
206 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000207 union {
208 void *rsrc;
209 struct file *file;
210 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000211};
212
213struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600214 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700215};
216
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000217struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct percpu_ref refs;
219 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000220 struct list_head rsrc_list;
221 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000222 void (*rsrc_put)(struct io_ring_ctx *ctx,
223 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600224 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000225 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800226};
227
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000228struct fixed_rsrc_data {
229 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700230 struct io_ring_ctx *ctx;
231
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700233 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700234 struct completion done;
235};
236
Jens Axboe5a2e7452020-02-23 16:23:11 -0700237struct io_buffer {
238 struct list_head list;
239 __u64 addr;
240 __s32 len;
241 __u16 bid;
242};
243
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200244struct io_restriction {
245 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
246 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
247 u8 sqe_flags_allowed;
248 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200249 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200250};
251
Jens Axboe534ca6d2020-09-02 13:52:19 -0600252struct io_sq_data {
253 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600254 struct mutex lock;
255
256 /* ctx's that are using this sqd */
257 struct list_head ctx_list;
258 struct list_head ctx_new_list;
259 struct mutex ctx_lock;
260
Jens Axboe534ca6d2020-09-02 13:52:19 -0600261 struct task_struct *thread;
262 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800263
264 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600265};
266
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267struct io_ring_ctx {
268 struct {
269 struct percpu_ref refs;
270 } ____cacheline_aligned_in_smp;
271
272 struct {
273 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800274 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700275 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800276 unsigned int cq_overflow_flushed: 1;
277 unsigned int drain_next: 1;
278 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200279 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000280 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281
Hristo Venev75b28af2019-08-26 17:23:46 +0000282 /*
283 * Ring buffer of indices into array of io_uring_sqe, which is
284 * mmapped by the application using the IORING_OFF_SQES offset.
285 *
286 * This indirection could e.g. be used to assign fixed
287 * io_uring_sqe entries to operations and only submit them to
288 * the queue when needed.
289 *
290 * The kernel modifies neither the indices array nor the entries
291 * array.
292 */
293 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 unsigned cached_sq_head;
295 unsigned sq_entries;
296 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700297 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600298 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100299 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700300 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600301
302 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600303 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700304 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700305
Jens Axboead3eb2c2019-12-18 17:12:20 -0700306 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307 } ____cacheline_aligned_in_smp;
308
Hristo Venev75b28af2019-08-26 17:23:46 +0000309 struct io_rings *rings;
310
Jens Axboe2b188cc2019-01-07 10:46:33 -0700311 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600312 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600313
314 /*
315 * For SQPOLL usage - we hold a reference to the parent task, so we
316 * have access to the ->files
317 */
318 struct task_struct *sqo_task;
319
320 /* Only used for accounting purposes */
321 struct mm_struct *mm_account;
322
Dennis Zhou91d8f512020-09-16 13:41:05 -0700323#ifdef CONFIG_BLK_CGROUP
324 struct cgroup_subsys_state *sqo_blkcg_css;
325#endif
326
Jens Axboe534ca6d2020-09-02 13:52:19 -0600327 struct io_sq_data *sq_data; /* if using sq thread polling */
328
Jens Axboe90554202020-09-03 12:12:41 -0600329 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600330 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331
Jens Axboe6b063142019-01-10 22:13:58 -0700332 /*
333 * If used, fixed file set. Writers must ensure that ->refs is dead,
334 * readers must ensure that ->refs is alive as long as the file* is
335 * used. Only updated through io_uring_register(2).
336 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000337 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700338 unsigned nr_user_files;
339
Jens Axboeedafcce2019-01-09 09:16:05 -0700340 /* if used, fixed mapped user buffers */
341 unsigned nr_user_bufs;
342 struct io_mapped_ubuf *user_bufs;
343
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 struct user_struct *user;
345
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700346 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700347
Jens Axboe4ea33a92020-10-15 13:46:44 -0600348#ifdef CONFIG_AUDIT
349 kuid_t loginuid;
350 unsigned int sessionid;
351#endif
352
Jens Axboe0f158b42020-05-14 17:18:39 -0600353 struct completion ref_comp;
354 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700355
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700356 /* if all else fails... */
357 struct io_kiocb *fallback_req;
358
Jens Axboe206aefd2019-11-07 18:27:42 -0700359#if defined(CONFIG_UNIX)
360 struct socket *ring_sock;
361#endif
362
Jens Axboe5a2e7452020-02-23 16:23:11 -0700363 struct idr io_buffer_idr;
364
Jens Axboe071698e2020-01-28 10:04:42 -0700365 struct idr personality_idr;
366
Jens Axboe206aefd2019-11-07 18:27:42 -0700367 struct {
368 unsigned cached_cq_tail;
369 unsigned cq_entries;
370 unsigned cq_mask;
371 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500372 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700373 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700374 struct wait_queue_head cq_wait;
375 struct fasync_struct *cq_fasync;
376 struct eventfd_ctx *cq_ev_fd;
377 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700378
379 struct {
380 struct mutex uring_lock;
381 wait_queue_head_t wait;
382 } ____cacheline_aligned_in_smp;
383
384 struct {
385 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700386
Jens Axboedef596e2019-01-09 08:59:42 -0700387 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300388 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700389 * io_uring instances that don't use IORING_SETUP_SQPOLL.
390 * For SQPOLL, only the single threaded io_sq_thread() will
391 * manipulate the list, hence no extra locking is needed there.
392 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300393 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700394 struct hlist_head *cancel_hash;
395 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700396 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600397
398 spinlock_t inflight_lock;
399 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700400 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600401
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000402 struct delayed_work rsrc_put_work;
403 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000404 struct list_head rsrc_ref_list;
405 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600406
Jens Axboe85faa7b2020-04-09 18:14:00 -0600407 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200408 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409};
410
Jens Axboe09bb8392019-03-13 12:39:28 -0600411/*
412 * First field must be the file pointer in all the
413 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
414 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700415struct io_poll_iocb {
416 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000417 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700418 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600419 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700420 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700421 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700422};
423
Pavel Begunkov018043b2020-10-27 23:17:18 +0000424struct io_poll_remove {
425 struct file *file;
426 u64 addr;
427};
428
Jens Axboeb5dba592019-12-11 14:02:38 -0700429struct io_close {
430 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700431 int fd;
432};
433
Jens Axboead8a48a2019-11-15 08:49:11 -0700434struct io_timeout_data {
435 struct io_kiocb *req;
436 struct hrtimer timer;
437 struct timespec64 ts;
438 enum hrtimer_mode mode;
439};
440
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700441struct io_accept {
442 struct file *file;
443 struct sockaddr __user *addr;
444 int __user *addr_len;
445 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600446 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700447};
448
449struct io_sync {
450 struct file *file;
451 loff_t len;
452 loff_t off;
453 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700454 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700455};
456
Jens Axboefbf23842019-12-17 18:45:56 -0700457struct io_cancel {
458 struct file *file;
459 u64 addr;
460};
461
Jens Axboeb29472e2019-12-17 18:50:29 -0700462struct io_timeout {
463 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300464 u32 off;
465 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300466 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000467 /* head of the link, used by linked timeouts only */
468 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700469};
470
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100471struct io_timeout_rem {
472 struct file *file;
473 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000474
475 /* timeout update */
476 struct timespec64 ts;
477 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100478};
479
Jens Axboe9adbd452019-12-20 08:45:55 -0700480struct io_rw {
481 /* NOTE: kiocb has the file as the first member, so don't do it here */
482 struct kiocb kiocb;
483 u64 addr;
484 u64 len;
485};
486
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700487struct io_connect {
488 struct file *file;
489 struct sockaddr __user *addr;
490 int addr_len;
491};
492
Jens Axboee47293f2019-12-20 08:58:21 -0700493struct io_sr_msg {
494 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700495 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300496 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700497 void __user *buf;
498 };
Jens Axboee47293f2019-12-20 08:58:21 -0700499 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700500 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700501 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700502 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700503};
504
Jens Axboe15b71ab2019-12-11 11:20:36 -0700505struct io_open {
506 struct file *file;
507 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700508 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700509 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600510 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700511};
512
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000513struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700514 struct file *file;
515 u64 arg;
516 u32 nr_args;
517 u32 offset;
518};
519
Jens Axboe4840e412019-12-25 22:03:45 -0700520struct io_fadvise {
521 struct file *file;
522 u64 offset;
523 u32 len;
524 u32 advice;
525};
526
Jens Axboec1ca7572019-12-25 22:18:28 -0700527struct io_madvise {
528 struct file *file;
529 u64 addr;
530 u32 len;
531 u32 advice;
532};
533
Jens Axboe3e4827b2020-01-08 15:18:09 -0700534struct io_epoll {
535 struct file *file;
536 int epfd;
537 int op;
538 int fd;
539 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700540};
541
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300542struct io_splice {
543 struct file *file_out;
544 struct file *file_in;
545 loff_t off_out;
546 loff_t off_in;
547 u64 len;
548 unsigned int flags;
549};
550
Jens Axboeddf0322d2020-02-23 16:41:33 -0700551struct io_provide_buf {
552 struct file *file;
553 __u64 addr;
554 __s32 len;
555 __u32 bgid;
556 __u16 nbufs;
557 __u16 bid;
558};
559
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700560struct io_statx {
561 struct file *file;
562 int dfd;
563 unsigned int mask;
564 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700565 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700566 struct statx __user *buffer;
567};
568
Jens Axboe36f4fa62020-09-05 11:14:22 -0600569struct io_shutdown {
570 struct file *file;
571 int how;
572};
573
Jens Axboe80a261f2020-09-28 14:23:58 -0600574struct io_rename {
575 struct file *file;
576 int old_dfd;
577 int new_dfd;
578 struct filename *oldpath;
579 struct filename *newpath;
580 int flags;
581};
582
Jens Axboe14a11432020-09-28 14:27:37 -0600583struct io_unlink {
584 struct file *file;
585 int dfd;
586 int flags;
587 struct filename *filename;
588};
589
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300590struct io_completion {
591 struct file *file;
592 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300593 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300594};
595
Jens Axboef499a022019-12-02 16:28:46 -0700596struct io_async_connect {
597 struct sockaddr_storage address;
598};
599
Jens Axboe03b12302019-12-02 18:50:25 -0700600struct io_async_msghdr {
601 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000602 /* points to an allocated iov, if NULL we use fast_iov instead */
603 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700604 struct sockaddr __user *uaddr;
605 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700606 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700607};
608
Jens Axboef67676d2019-12-02 11:03:47 -0700609struct io_async_rw {
610 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600611 const struct iovec *free_iovec;
612 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600613 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600614 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700615};
616
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300617enum {
618 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
619 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
620 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
621 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
622 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700623 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300625 REQ_F_FAIL_LINK_BIT,
626 REQ_F_INFLIGHT_BIT,
627 REQ_F_CUR_POS_BIT,
628 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300629 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300630 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300631 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700632 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700633 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600634 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800635 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100636 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000637 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700638
639 /* not a real bit, just to check we're not overflowing the space */
640 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300641};
642
643enum {
644 /* ctx owns file */
645 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
646 /* drain existing IO first */
647 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
648 /* linked sqes */
649 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
650 /* doesn't sever on completion < 0 */
651 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
652 /* IOSQE_ASYNC */
653 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700654 /* IOSQE_BUFFER_SELECT */
655 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300656
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300657 /* fail rest of links */
658 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
659 /* on inflight list */
660 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
661 /* read/write uses file position */
662 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
663 /* must not punt to workers */
664 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100665 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300666 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300667 /* regular file */
668 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300669 /* needs cleanup */
670 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700671 /* already went through poll handler */
672 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700673 /* buffer already selected */
674 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600675 /* doesn't need file table for this request */
676 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800677 /* io_wq_work is initialized */
678 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100679 /* linked timeout is active, i.e. prepared by link's head */
680 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000681 /* completion is deferred through io_comp_state */
682 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700683};
684
685struct async_poll {
686 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600687 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300688};
689
Jens Axboe09bb8392019-03-13 12:39:28 -0600690/*
691 * NOTE! Each of the iocb union members has the file pointer
692 * as the first entry in their struct definition. So you can
693 * access the file pointer through any of the sub-structs,
694 * or directly as just 'ki_filp' in this struct.
695 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700696struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700697 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600698 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700699 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700700 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000701 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700702 struct io_accept accept;
703 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700704 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700705 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100706 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700707 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700708 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700709 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700710 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000711 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700712 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700713 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700714 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300715 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700716 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700717 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600718 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600719 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600720 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300721 /* use only after cleaning per-op data, see io_clean_op() */
722 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700723 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700724
Jens Axboee8c2bc12020-08-15 18:44:09 -0700725 /* opcode allocated if it needs to store data for async defer */
726 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700727 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800728 /* polled IO has completed */
729 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700730
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700731 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300732 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700733
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300734 struct io_ring_ctx *ctx;
735 unsigned int flags;
736 refcount_t refs;
737 struct task_struct *task;
738 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700739
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000740 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000741 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700742
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300743 /*
744 * 1. used with ctx->iopoll_list with reads/writes
745 * 2. to track reqs with ->files (see io_op_def::file_table)
746 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300747 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300748 struct callback_head task_work;
749 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
750 struct hlist_node hash_node;
751 struct async_poll *apoll;
752 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700753};
754
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300755struct io_defer_entry {
756 struct list_head list;
757 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300758 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300759};
760
Jens Axboedef596e2019-01-09 08:59:42 -0700761#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700762
Jens Axboe013538b2020-06-22 09:29:15 -0600763struct io_comp_state {
764 unsigned int nr;
765 struct list_head list;
766 struct io_ring_ctx *ctx;
767};
768
Jens Axboe9a56a232019-01-09 09:06:50 -0700769struct io_submit_state {
770 struct blk_plug plug;
771
772 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700773 * io_kiocb alloc cache
774 */
775 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300776 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700777
Jens Axboe27926b62020-10-28 09:33:23 -0600778 bool plug_started;
779
Jens Axboe2579f912019-01-09 09:10:43 -0700780 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600781 * Batch completion logic
782 */
783 struct io_comp_state comp;
784
785 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700786 * File reference cache
787 */
788 struct file *file;
789 unsigned int fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +0000790 unsigned int file_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700791 unsigned int ios_left;
792};
793
Jens Axboed3656342019-12-18 09:50:26 -0700794struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700795 /* needs req->file assigned */
796 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700797 /* hash wq insertion if file is a regular file */
798 unsigned hash_reg_file : 1;
799 /* unbound wq insertion if file is a non-regular file */
800 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700801 /* opcode is not supported by this kernel */
802 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700803 /* set if opcode supports polled "wait" */
804 unsigned pollin : 1;
805 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700806 /* op supports buffer selection */
807 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700808 /* must always have async data allocated */
809 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600810 /* should block plug */
811 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700812 /* size of async data needed, if any */
813 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600814 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700815};
816
Jens Axboe09186822020-10-13 15:01:40 -0600817static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_NOP] = {},
819 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700820 .needs_file = 1,
821 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700822 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700823 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600825 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700826 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600827 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700830 .needs_file = 1,
831 .hash_reg_file = 1,
832 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700834 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600835 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700836 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600837 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
838 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600842 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700845 .needs_file = 1,
846 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700847 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600848 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700849 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600850 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700853 .needs_file = 1,
854 .hash_reg_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600857 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700858 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600859 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
860 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
864 .unbound_nonreg_file = 1,
865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_POLL_REMOVE] = {},
867 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600869 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700875 .needs_async_data = 1,
876 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000877 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700880 .needs_file = 1,
881 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700882 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700883 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700884 .needs_async_data = 1,
885 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000886 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700889 .needs_async_data = 1,
890 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600891 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700892 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000893 [IORING_OP_TIMEOUT_REMOVE] = {
894 /* used by timeout updates' prep() */
895 .work_flags = IO_WQ_WORK_MM,
896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700898 .needs_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700900 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600901 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_ASYNC_CANCEL] = {},
904 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600907 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700908 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300909 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700910 .needs_file = 1,
911 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700912 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700913 .needs_async_data = 1,
914 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600915 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700918 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600919 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600922 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600923 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700924 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600926 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600929 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700930 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300931 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600932 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
933 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700939 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600942 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700945 .needs_file = 1,
946 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700947 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600948 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700949 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600950 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
951 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700954 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600955 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600958 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700961 .needs_file = 1,
962 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700963 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600964 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700967 .needs_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700969 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700970 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600971 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600974 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600975 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700976 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700977 [IORING_OP_EPOLL_CTL] = {
978 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600979 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700980 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300981 [IORING_OP_SPLICE] = {
982 .needs_file = 1,
983 .hash_reg_file = 1,
984 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600985 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700986 },
987 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700988 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300989 [IORING_OP_TEE] = {
990 .needs_file = 1,
991 .hash_reg_file = 1,
992 .unbound_nonreg_file = 1,
993 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600994 [IORING_OP_SHUTDOWN] = {
995 .needs_file = 1,
996 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600997 [IORING_OP_RENAMEAT] = {
998 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
999 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1000 },
Jens Axboe14a11432020-09-28 14:27:37 -06001001 [IORING_OP_UNLINKAT] = {
1002 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1003 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1004 },
Jens Axboed3656342019-12-18 09:50:26 -07001005};
1006
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001007enum io_mem_account {
1008 ACCT_LOCKED,
1009 ACCT_PINNED,
1010};
1011
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001012static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1013 struct task_struct *task,
1014 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001015static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001016static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001017 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001018static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1019 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001020
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001021static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001022 unsigned int issue_flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001023static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001024static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001025static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001026static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001027static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001028static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001029static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001030static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001031 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001032 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001033static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001034static struct file *io_file_get(struct io_submit_state *state,
1035 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001036static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001037static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001038
Pavel Begunkov847595d2021-02-04 13:52:06 +00001039static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1040 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001041static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1042 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001043 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001044static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045
1046static struct kmem_cache *req_cachep;
1047
Jens Axboe09186822020-10-13 15:01:40 -06001048static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049
1050struct sock *io_uring_get_socket(struct file *file)
1051{
1052#if defined(CONFIG_UNIX)
1053 if (file->f_op == &io_uring_fops) {
1054 struct io_ring_ctx *ctx = file->private_data;
1055
1056 return ctx->ring_sock->sk;
1057 }
1058#endif
1059 return NULL;
1060}
1061EXPORT_SYMBOL(io_uring_get_socket);
1062
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001063#define io_for_each_link(pos, head) \
1064 for (pos = (head); pos; pos = pos->link)
1065
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001066static inline void io_clean_op(struct io_kiocb *req)
1067{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001068 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001069 __io_clean_op(req);
1070}
1071
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001072static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001073{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001074 struct io_ring_ctx *ctx = req->ctx;
1075
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001076 if (!req->fixed_rsrc_refs) {
1077 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1078 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001079 }
1080}
1081
Pavel Begunkov08d23632020-11-06 13:00:22 +00001082static bool io_match_task(struct io_kiocb *head,
1083 struct task_struct *task,
1084 struct files_struct *files)
1085{
1086 struct io_kiocb *req;
1087
Jens Axboe84965ff2021-01-23 15:51:11 -07001088 if (task && head->task != task) {
1089 /* in terms of cancelation, always match if req task is dead */
1090 if (head->task->flags & PF_EXITING)
1091 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001092 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001093 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001094 if (!files)
1095 return true;
1096
1097 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001098 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1099 continue;
1100 if (req->file && req->file->f_op == &io_uring_fops)
1101 return true;
1102 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001103 req->work.identity->files == files)
1104 return true;
1105 }
1106 return false;
1107}
1108
Jens Axboe28cea78a2020-09-14 10:51:17 -06001109static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001110{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001111 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001112 struct mm_struct *mm = current->mm;
1113
1114 if (mm) {
1115 kthread_unuse_mm(mm);
1116 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001117 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001118 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001119 if (files) {
1120 struct nsproxy *nsproxy = current->nsproxy;
1121
1122 task_lock(current);
1123 current->files = NULL;
1124 current->nsproxy = NULL;
1125 task_unlock(current);
1126 put_files_struct(files);
1127 put_nsproxy(nsproxy);
1128 }
1129}
1130
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001131static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001132{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001133 if (current->flags & PF_EXITING)
1134 return -EFAULT;
1135
Jens Axboe28cea78a2020-09-14 10:51:17 -06001136 if (!current->files) {
1137 struct files_struct *files;
1138 struct nsproxy *nsproxy;
1139
1140 task_lock(ctx->sqo_task);
1141 files = ctx->sqo_task->files;
1142 if (!files) {
1143 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001144 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001145 }
1146 atomic_inc(&files->count);
1147 get_nsproxy(ctx->sqo_task->nsproxy);
1148 nsproxy = ctx->sqo_task->nsproxy;
1149 task_unlock(ctx->sqo_task);
1150
1151 task_lock(current);
1152 current->files = files;
1153 current->nsproxy = nsproxy;
1154 task_unlock(current);
1155 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001156 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001157}
1158
1159static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1160{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001161 struct mm_struct *mm;
1162
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001163 if (current->flags & PF_EXITING)
1164 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001165 if (current->mm)
1166 return 0;
1167
1168 /* Should never happen */
1169 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1170 return -EFAULT;
1171
1172 task_lock(ctx->sqo_task);
1173 mm = ctx->sqo_task->mm;
1174 if (unlikely(!mm || !mmget_not_zero(mm)))
1175 mm = NULL;
1176 task_unlock(ctx->sqo_task);
1177
1178 if (mm) {
1179 kthread_use_mm(mm);
1180 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001181 }
1182
Jens Axboe4b70cf92020-11-02 10:39:05 -07001183 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001184}
1185
Jens Axboe28cea78a2020-09-14 10:51:17 -06001186static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1187 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001188{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001189 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001190 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001191
1192 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001193 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001194 if (unlikely(ret))
1195 return ret;
1196 }
1197
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001198 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1199 ret = __io_sq_thread_acquire_files(ctx);
1200 if (unlikely(ret))
1201 return ret;
1202 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001203
1204 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001205}
1206
Dennis Zhou91d8f512020-09-16 13:41:05 -07001207static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1208 struct cgroup_subsys_state **cur_css)
1209
1210{
1211#ifdef CONFIG_BLK_CGROUP
1212 /* puts the old one when swapping */
1213 if (*cur_css != ctx->sqo_blkcg_css) {
1214 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1215 *cur_css = ctx->sqo_blkcg_css;
1216 }
1217#endif
1218}
1219
1220static void io_sq_thread_unassociate_blkcg(void)
1221{
1222#ifdef CONFIG_BLK_CGROUP
1223 kthread_associate_blkcg(NULL);
1224#endif
1225}
1226
Jens Axboec40f6372020-06-25 15:39:59 -06001227static inline void req_set_fail_links(struct io_kiocb *req)
1228{
1229 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1230 req->flags |= REQ_F_FAIL_LINK;
1231}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001232
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001233/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001234 * None of these are dereferenced, they are simply used to check if any of
1235 * them have changed. If we're under current and check they are still the
1236 * same, we're fine to grab references to them for actual out-of-line use.
1237 */
1238static void io_init_identity(struct io_identity *id)
1239{
1240 id->files = current->files;
1241 id->mm = current->mm;
1242#ifdef CONFIG_BLK_CGROUP
1243 rcu_read_lock();
1244 id->blkcg_css = blkcg_css();
1245 rcu_read_unlock();
1246#endif
1247 id->creds = current_cred();
1248 id->nsproxy = current->nsproxy;
1249 id->fs = current->fs;
1250 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001251#ifdef CONFIG_AUDIT
1252 id->loginuid = current->loginuid;
1253 id->sessionid = current->sessionid;
1254#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001255 refcount_set(&id->count, 1);
1256}
1257
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001258static inline void __io_req_init_async(struct io_kiocb *req)
1259{
1260 memset(&req->work, 0, sizeof(req->work));
1261 req->flags |= REQ_F_WORK_INITIALIZED;
1262}
1263
Jens Axboe1e6fa522020-10-15 08:46:24 -06001264/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001265 * Note: must call io_req_init_async() for the first time you
1266 * touch any members of io_wq_work.
1267 */
1268static inline void io_req_init_async(struct io_kiocb *req)
1269{
Jens Axboe500a3732020-10-15 17:38:03 -06001270 struct io_uring_task *tctx = current->io_uring;
1271
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001272 if (req->flags & REQ_F_WORK_INITIALIZED)
1273 return;
1274
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001275 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001276
1277 /* Grab a ref if this isn't our static identity */
1278 req->work.identity = tctx->identity;
1279 if (tctx->identity != &tctx->__identity)
1280 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001281}
1282
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1284{
1285 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1286
Jens Axboe0f158b42020-05-14 17:18:39 -06001287 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288}
1289
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001290static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1291{
1292 return !req->timeout.off;
1293}
1294
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1296{
1297 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001298 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299
1300 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1301 if (!ctx)
1302 return NULL;
1303
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001304 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1305 if (!ctx->fallback_req)
1306 goto err;
1307
Jens Axboe78076bb2019-12-04 19:56:40 -07001308 /*
1309 * Use 5 bits less than the max cq entries, that should give us around
1310 * 32 entries per hash list if totally full and uniformly spread.
1311 */
1312 hash_bits = ilog2(p->cq_entries);
1313 hash_bits -= 5;
1314 if (hash_bits <= 0)
1315 hash_bits = 1;
1316 ctx->cancel_hash_bits = hash_bits;
1317 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1318 GFP_KERNEL);
1319 if (!ctx->cancel_hash)
1320 goto err;
1321 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1322
Roman Gushchin21482892019-05-07 10:01:48 -07001323 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001324 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1325 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001326
1327 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001328 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001329 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001331 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001332 init_completion(&ctx->ref_comp);
1333 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001334 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001335 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336 mutex_init(&ctx->uring_lock);
1337 init_waitqueue_head(&ctx->wait);
1338 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001339 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001340 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001341 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001342 spin_lock_init(&ctx->inflight_lock);
1343 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001344 spin_lock_init(&ctx->rsrc_ref_lock);
1345 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001346 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1347 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001349err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001350 if (ctx->fallback_req)
1351 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001352 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001353 kfree(ctx);
1354 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355}
1356
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001357static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001358{
Jens Axboe2bc99302020-07-09 09:43:27 -06001359 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1360 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001361
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001362 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001363 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001364 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001365
Bob Liu9d858b22019-11-13 18:06:25 +08001366 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001367}
1368
Jens Axboe5c3462c2020-10-15 09:02:33 -06001369static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001370{
Jens Axboe500a3732020-10-15 17:38:03 -06001371 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001372 return;
1373 if (refcount_dec_and_test(&req->work.identity->count))
1374 kfree(req->work.identity);
1375}
1376
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001377static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001378{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001379 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001380 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001381
Pavel Begunkove86d0042021-02-01 18:59:54 +00001382 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001383 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001384#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001385 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001386 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001387#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001388 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001389 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001390 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001391 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001392
Jens Axboe98447d62020-10-14 10:48:51 -06001393 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001394 if (--fs->users)
1395 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001396 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001397 if (fs)
1398 free_fs_struct(fs);
1399 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001400 if (req->work.flags & IO_WQ_WORK_FILES) {
1401 put_files_struct(req->work.identity->files);
1402 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001403 }
1404 if (req->flags & REQ_F_INFLIGHT) {
1405 struct io_ring_ctx *ctx = req->ctx;
1406 struct io_uring_task *tctx = req->task->io_uring;
1407 unsigned long flags;
1408
1409 spin_lock_irqsave(&ctx->inflight_lock, flags);
1410 list_del(&req->inflight_entry);
1411 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1412 req->flags &= ~REQ_F_INFLIGHT;
1413 if (atomic_read(&tctx->in_idle))
1414 wake_up(&tctx->wait);
1415 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001416
Pavel Begunkove86d0042021-02-01 18:59:54 +00001417 req->flags &= ~REQ_F_WORK_INITIALIZED;
1418 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1419 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001420 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001421}
1422
1423/*
1424 * Create a private copy of io_identity, since some fields don't match
1425 * the current context.
1426 */
1427static bool io_identity_cow(struct io_kiocb *req)
1428{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001429 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001430 const struct cred *creds = NULL;
1431 struct io_identity *id;
1432
1433 if (req->work.flags & IO_WQ_WORK_CREDS)
1434 creds = req->work.identity->creds;
1435
1436 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1437 if (unlikely(!id)) {
1438 req->work.flags |= IO_WQ_WORK_CANCEL;
1439 return false;
1440 }
1441
1442 /*
1443 * We can safely just re-init the creds we copied Either the field
1444 * matches the current one, or we haven't grabbed it yet. The only
1445 * exception is ->creds, through registered personalities, so handle
1446 * that one separately.
1447 */
1448 io_init_identity(id);
1449 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001450 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001451
1452 /* add one for this request */
1453 refcount_inc(&id->count);
1454
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001455 /* drop tctx and req identity references, if needed */
1456 if (tctx->identity != &tctx->__identity &&
1457 refcount_dec_and_test(&tctx->identity->count))
1458 kfree(tctx->identity);
1459 if (req->work.identity != &tctx->__identity &&
1460 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001461 kfree(req->work.identity);
1462
1463 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001464 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001465 return true;
1466}
1467
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001468static void io_req_track_inflight(struct io_kiocb *req)
1469{
1470 struct io_ring_ctx *ctx = req->ctx;
1471
1472 if (!(req->flags & REQ_F_INFLIGHT)) {
1473 io_req_init_async(req);
1474 req->flags |= REQ_F_INFLIGHT;
1475
1476 spin_lock_irq(&ctx->inflight_lock);
1477 list_add(&req->inflight_entry, &ctx->inflight_list);
1478 spin_unlock_irq(&ctx->inflight_lock);
1479 }
1480}
1481
Jens Axboe1e6fa522020-10-15 08:46:24 -06001482static bool io_grab_identity(struct io_kiocb *req)
1483{
1484 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001485 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001486
Jens Axboe69228332020-10-20 14:28:41 -06001487 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1488 if (id->fsize != rlimit(RLIMIT_FSIZE))
1489 return false;
1490 req->work.flags |= IO_WQ_WORK_FSIZE;
1491 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001492#ifdef CONFIG_BLK_CGROUP
1493 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1494 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1495 rcu_read_lock();
1496 if (id->blkcg_css != blkcg_css()) {
1497 rcu_read_unlock();
1498 return false;
1499 }
1500 /*
1501 * This should be rare, either the cgroup is dying or the task
1502 * is moving cgroups. Just punt to root for the handful of ios.
1503 */
1504 if (css_tryget_online(id->blkcg_css))
1505 req->work.flags |= IO_WQ_WORK_BLKCG;
1506 rcu_read_unlock();
1507 }
1508#endif
1509 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1510 if (id->creds != current_cred())
1511 return false;
1512 get_cred(id->creds);
1513 req->work.flags |= IO_WQ_WORK_CREDS;
1514 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001515#ifdef CONFIG_AUDIT
1516 if (!uid_eq(current->loginuid, id->loginuid) ||
1517 current->sessionid != id->sessionid)
1518 return false;
1519#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001520 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1521 (def->work_flags & IO_WQ_WORK_FS)) {
1522 if (current->fs != id->fs)
1523 return false;
1524 spin_lock(&id->fs->lock);
1525 if (!id->fs->in_exec) {
1526 id->fs->users++;
1527 req->work.flags |= IO_WQ_WORK_FS;
1528 } else {
1529 req->work.flags |= IO_WQ_WORK_CANCEL;
1530 }
1531 spin_unlock(&current->fs->lock);
1532 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001533 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1534 (def->work_flags & IO_WQ_WORK_FILES) &&
1535 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1536 if (id->files != current->files ||
1537 id->nsproxy != current->nsproxy)
1538 return false;
1539 atomic_inc(&id->files->count);
1540 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001541 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001542 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001543 }
Jens Axboe77788772020-12-29 10:50:46 -07001544 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1545 (def->work_flags & IO_WQ_WORK_MM)) {
1546 if (id->mm != current->mm)
1547 return false;
1548 mmgrab(id->mm);
1549 req->work.flags |= IO_WQ_WORK_MM;
1550 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001551
1552 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001553}
1554
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001555static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001556{
Jens Axboed3656342019-12-18 09:50:26 -07001557 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001558 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001559
Pavel Begunkov16d59802020-07-12 16:16:47 +03001560 io_req_init_async(req);
1561
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001562 if (req->flags & REQ_F_FORCE_ASYNC)
1563 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1564
Jens Axboed3656342019-12-18 09:50:26 -07001565 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001566 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001567 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001568 } else {
1569 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001570 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001571 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001572
Jens Axboe1e6fa522020-10-15 08:46:24 -06001573 /* if we fail grabbing identity, we must COW, regrab, and retry */
1574 if (io_grab_identity(req))
1575 return;
1576
1577 if (!io_identity_cow(req))
1578 return;
1579
1580 /* can't fail at this point */
1581 if (!io_grab_identity(req))
1582 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001583}
1584
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001585static void io_prep_async_link(struct io_kiocb *req)
1586{
1587 struct io_kiocb *cur;
1588
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001589 io_for_each_link(cur, req)
1590 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001591}
1592
Jens Axboe7271ef32020-08-10 09:55:22 -06001593static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001594{
Jackie Liua197f662019-11-08 08:09:12 -07001595 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001596 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001597
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001598 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1599 &req->work, req->flags);
1600 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001601 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001602}
1603
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001604static void io_queue_async_work(struct io_kiocb *req)
1605{
Jens Axboe7271ef32020-08-10 09:55:22 -06001606 struct io_kiocb *link;
1607
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001608 /* init ->work of the whole link before punting */
1609 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001610 link = __io_queue_async_work(req);
1611
1612 if (link)
1613 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001614}
1615
Jens Axboe5262f562019-09-17 12:26:57 -06001616static void io_kill_timeout(struct io_kiocb *req)
1617{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001618 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001619 int ret;
1620
Jens Axboee8c2bc12020-08-15 18:44:09 -07001621 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001622 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001623 atomic_set(&req->ctx->cq_timeouts,
1624 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001625 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001626 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001627 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001628 }
1629}
1630
Jens Axboe76e1b642020-09-26 15:05:03 -06001631/*
1632 * Returns true if we found and killed one or more timeouts
1633 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001634static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1635 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001636{
1637 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001638 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001639
1640 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001641 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001642 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001643 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001644 canceled++;
1645 }
Jens Axboef3606e32020-09-22 08:18:24 -06001646 }
Jens Axboe5262f562019-09-17 12:26:57 -06001647 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001648 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001649}
1650
Pavel Begunkov04518942020-05-26 20:34:05 +03001651static void __io_queue_deferred(struct io_ring_ctx *ctx)
1652{
1653 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001654 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1655 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001656
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001657 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001658 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001659 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001660 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001661 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001662 } while (!list_empty(&ctx->defer_list));
1663}
1664
Pavel Begunkov360428f2020-05-30 14:54:17 +03001665static void io_flush_timeouts(struct io_ring_ctx *ctx)
1666{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001667 u32 seq;
1668
1669 if (list_empty(&ctx->timeout_list))
1670 return;
1671
1672 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1673
1674 do {
1675 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001676 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001677 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001678
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001679 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001680 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001681
1682 /*
1683 * Since seq can easily wrap around over time, subtract
1684 * the last seq at which timeouts were flushed before comparing.
1685 * Assuming not more than 2^31-1 events have happened since,
1686 * these subtractions won't have wrapped, so we can check if
1687 * target is in [last_seq, current_seq] by comparing the two.
1688 */
1689 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1690 events_got = seq - ctx->cq_last_tm_flush;
1691 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001692 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001693
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001694 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001695 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001696 } while (!list_empty(&ctx->timeout_list));
1697
1698 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001699}
1700
Jens Axboede0617e2019-04-06 21:51:27 -06001701static void io_commit_cqring(struct io_ring_ctx *ctx)
1702{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001703 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001704
1705 /* order cqe stores with ring update */
1706 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001707
Pavel Begunkov04518942020-05-26 20:34:05 +03001708 if (unlikely(!list_empty(&ctx->defer_list)))
1709 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001710}
1711
Jens Axboe90554202020-09-03 12:12:41 -06001712static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1713{
1714 struct io_rings *r = ctx->rings;
1715
1716 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1717}
1718
Pavel Begunkov888aae22021-01-19 13:32:39 +00001719static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1720{
1721 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1722}
1723
Jens Axboe2b188cc2019-01-07 10:46:33 -07001724static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1725{
Hristo Venev75b28af2019-08-26 17:23:46 +00001726 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727 unsigned tail;
1728
Stefan Bühler115e12e2019-04-24 23:54:18 +02001729 /*
1730 * writes to the cq entry need to come after reading head; the
1731 * control dependency is enough as we're using WRITE_ONCE to
1732 * fill the cq entry
1733 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001734 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735 return NULL;
1736
Pavel Begunkov888aae22021-01-19 13:32:39 +00001737 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001738 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739}
1740
Jens Axboef2842ab2020-01-08 11:04:00 -07001741static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1742{
Jens Axboef0b493e2020-02-01 21:30:11 -07001743 if (!ctx->cq_ev_fd)
1744 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001745 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1746 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001747 if (!ctx->eventfd_async)
1748 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001749 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001750}
1751
Jens Axboeb41e9852020-02-17 09:52:41 -07001752static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001753{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001754 /* see waitqueue_active() comment */
1755 smp_mb();
1756
Jens Axboe8c838782019-03-12 15:48:16 -06001757 if (waitqueue_active(&ctx->wait))
1758 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001759 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1760 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001761 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001762 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001763 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001764 wake_up_interruptible(&ctx->cq_wait);
1765 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1766 }
Jens Axboe8c838782019-03-12 15:48:16 -06001767}
1768
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001769static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1770{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001771 /* see waitqueue_active() comment */
1772 smp_mb();
1773
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001774 if (ctx->flags & IORING_SETUP_SQPOLL) {
1775 if (waitqueue_active(&ctx->wait))
1776 wake_up(&ctx->wait);
1777 }
1778 if (io_should_trigger_evfd(ctx))
1779 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001780 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001781 wake_up_interruptible(&ctx->cq_wait);
1782 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1783 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001784}
1785
Jens Axboec4a2ed72019-11-21 21:01:26 -07001786/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001787static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1788 struct task_struct *tsk,
1789 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001791 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001792 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001793 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001794 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001795 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001796 LIST_HEAD(list);
1797
Pavel Begunkove23de152020-12-17 00:24:37 +00001798 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1799 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001800
Jens Axboeb18032b2021-01-24 16:58:56 -07001801 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001802 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001803 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001804 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001805 continue;
1806
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001807 cqe = io_get_cqring(ctx);
1808 if (!cqe && !force)
1809 break;
1810
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001811 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001812 if (cqe) {
1813 WRITE_ONCE(cqe->user_data, req->user_data);
1814 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001815 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001816 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001817 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001818 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001819 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001820 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001821 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001822 }
1823
Pavel Begunkov09e88402020-12-17 00:24:38 +00001824 all_flushed = list_empty(&ctx->cq_overflow_list);
1825 if (all_flushed) {
1826 clear_bit(0, &ctx->sq_check_overflow);
1827 clear_bit(0, &ctx->cq_check_overflow);
1828 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1829 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001830
Jens Axboeb18032b2021-01-24 16:58:56 -07001831 if (posted)
1832 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001833 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001834 if (posted)
1835 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001836
1837 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001838 req = list_first_entry(&list, struct io_kiocb, compl.list);
1839 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001840 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001841 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001842
Pavel Begunkov09e88402020-12-17 00:24:38 +00001843 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001844}
1845
Pavel Begunkov6c503152021-01-04 20:36:36 +00001846static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1847 struct task_struct *tsk,
1848 struct files_struct *files)
1849{
1850 if (test_bit(0, &ctx->cq_check_overflow)) {
1851 /* iopoll syncs against uring_lock, not completion_lock */
1852 if (ctx->flags & IORING_SETUP_IOPOLL)
1853 mutex_lock(&ctx->uring_lock);
1854 __io_cqring_overflow_flush(ctx, force, tsk, files);
1855 if (ctx->flags & IORING_SETUP_IOPOLL)
1856 mutex_unlock(&ctx->uring_lock);
1857 }
1858}
1859
Jens Axboebcda7ba2020-02-23 16:42:51 -07001860static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001862 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863 struct io_uring_cqe *cqe;
1864
Jens Axboe78e19bb2019-11-06 15:21:34 -07001865 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001866
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867 /*
1868 * If we can't get a cq entry, userspace overflowed the
1869 * submission (by quite a lot). Increment the overflow count in
1870 * the ring.
1871 */
1872 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001873 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001874 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001876 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001877 } else if (ctx->cq_overflow_flushed ||
1878 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001879 /*
1880 * If we're in ring overflow flush mode, or in task cancel mode,
1881 * then we cannot store the request for later flushing, we need
1882 * to drop it on the floor.
1883 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001884 ctx->cached_cq_overflow++;
1885 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001886 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001887 if (list_empty(&ctx->cq_overflow_list)) {
1888 set_bit(0, &ctx->sq_check_overflow);
1889 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001890 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001891 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001892 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001893 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001894 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001895 refcount_inc(&req->refs);
1896 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897 }
1898}
1899
Jens Axboebcda7ba2020-02-23 16:42:51 -07001900static void io_cqring_fill_event(struct io_kiocb *req, long res)
1901{
1902 __io_cqring_fill_event(req, res, 0);
1903}
1904
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001905static void io_req_complete_post(struct io_kiocb *req, long res,
1906 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001908 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909 unsigned long flags;
1910
1911 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001912 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913 io_commit_cqring(ctx);
1914 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1915
Jens Axboe8c838782019-03-12 15:48:16 -06001916 io_cqring_ev_posted(ctx);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001917}
1918
1919static inline void io_req_complete_nostate(struct io_kiocb *req, long res,
1920 unsigned int cflags)
1921{
1922 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001923 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001924}
1925
Jens Axboe229a7b62020-06-22 10:13:11 -06001926static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001927{
Jens Axboe229a7b62020-06-22 10:13:11 -06001928 struct io_ring_ctx *ctx = cs->ctx;
1929
1930 spin_lock_irq(&ctx->completion_lock);
1931 while (!list_empty(&cs->list)) {
1932 struct io_kiocb *req;
1933
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001934 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1935 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001936 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001937
1938 /*
1939 * io_free_req() doesn't care about completion_lock unless one
1940 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1941 * because of a potential deadlock with req->work.fs->lock
Pavel Begunkove342c802021-01-19 13:32:47 +00001942 * We defer both, completion and submission refs.
Pavel Begunkov216578e2020-10-13 09:44:00 +01001943 */
1944 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1945 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001946 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkove342c802021-01-19 13:32:47 +00001947 io_double_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001948 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001949 } else {
Pavel Begunkove342c802021-01-19 13:32:47 +00001950 io_double_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001951 }
1952 }
1953 io_commit_cqring(ctx);
1954 spin_unlock_irq(&ctx->completion_lock);
1955
1956 io_cqring_ev_posted(ctx);
1957 cs->nr = 0;
1958}
1959
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001960static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001961 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001962{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001963 io_clean_op(req);
1964 req->result = res;
1965 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001966 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001967}
1968
Pavel Begunkov889fca72021-02-10 00:03:09 +00001969static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1970 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001971{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001972 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1973 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001974 else
Pavel Begunkov889fca72021-02-10 00:03:09 +00001975 io_req_complete_nostate(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001976}
1977
1978static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001979{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001980 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001981}
1982
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001983static inline bool io_is_fallback_req(struct io_kiocb *req)
1984{
1985 return req == (struct io_kiocb *)
1986 ((unsigned long) req->ctx->fallback_req & ~1UL);
1987}
1988
1989static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1990{
1991 struct io_kiocb *req;
1992
1993 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001994 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001995 return req;
1996
1997 return NULL;
1998}
1999
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03002000static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
2001 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002003 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002004 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002005 size_t sz;
2006 int ret;
2007
2008 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06002009 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
2010
2011 /*
2012 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2013 * retry single alloc to be on the safe side.
2014 */
2015 if (unlikely(ret <= 0)) {
2016 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2017 if (!state->reqs[0])
Pavel Begunkov85bcb6c2021-01-19 13:32:40 +00002018 return io_get_fallback_req(ctx);
Jens Axboefd6fab22019-03-14 16:30:06 -06002019 ret = 1;
2020 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002021 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002022 }
2023
Pavel Begunkov291b2822020-09-30 22:57:01 +03002024 state->free_reqs--;
2025 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002026}
2027
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002028static inline void io_put_file(struct io_kiocb *req, struct file *file,
2029 bool fixed)
2030{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002031 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002032 fput(file);
2033}
2034
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002035static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002036{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002037 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002038
Jens Axboee8c2bc12020-08-15 18:44:09 -07002039 if (req->async_data)
2040 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002041 if (req->file)
2042 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002043 if (req->fixed_rsrc_refs)
2044 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002045 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002046}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002047
Pavel Begunkov7c660732021-01-25 11:42:21 +00002048static inline void io_put_task(struct task_struct *task, int nr)
2049{
2050 struct io_uring_task *tctx = task->io_uring;
2051
2052 percpu_counter_sub(&tctx->inflight, nr);
2053 if (unlikely(atomic_read(&tctx->in_idle)))
2054 wake_up(&tctx->wait);
2055 put_task_struct_many(task, nr);
2056}
2057
Pavel Begunkov216578e2020-10-13 09:44:00 +01002058static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002059{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002060 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002061
Pavel Begunkov216578e2020-10-13 09:44:00 +01002062 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002063 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002064
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03002065 if (likely(!io_is_fallback_req(req)))
2066 kmem_cache_free(req_cachep, req);
2067 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002068 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
2069 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002070}
2071
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002072static inline void io_remove_next_linked(struct io_kiocb *req)
2073{
2074 struct io_kiocb *nxt = req->link;
2075
2076 req->link = nxt->link;
2077 nxt->link = NULL;
2078}
2079
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002080static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002081{
Jackie Liua197f662019-11-08 08:09:12 -07002082 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002083 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002084 bool cancelled = false;
2085 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002086
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002087 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002088 link = req->link;
2089
Pavel Begunkov900fad42020-10-19 16:39:16 +01002090 /*
2091 * Can happen if a linked timeout fired and link had been like
2092 * req -> link t-out -> link t-out [-> ...]
2093 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002094 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2095 struct io_timeout_data *io = link->async_data;
2096 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002097
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002098 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002099 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002100 ret = hrtimer_try_to_cancel(&io->timer);
2101 if (ret != -1) {
2102 io_cqring_fill_event(link, -ECANCELED);
2103 io_commit_cqring(ctx);
2104 cancelled = true;
2105 }
2106 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002107 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002108 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002109
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002110 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002111 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002112 io_put_req(link);
2113 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002114}
2115
Jens Axboe4d7dd462019-11-20 13:03:52 -07002116
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002117static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002118{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002119 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002120 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002121 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002122
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002123 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002124 link = req->link;
2125 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002126
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002127 while (link) {
2128 nxt = link->link;
2129 link->link = NULL;
2130
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002131 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002132 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002133
2134 /*
2135 * It's ok to free under spinlock as they're not linked anymore,
2136 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2137 * work.fs->lock.
2138 */
2139 if (link->flags & REQ_F_WORK_INITIALIZED)
2140 io_put_req_deferred(link, 2);
2141 else
2142 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002143 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002144 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002145 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002146 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002147
Jens Axboe2665abf2019-11-05 12:40:47 -07002148 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002149}
2150
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002151static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002152{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002153 if (req->flags & REQ_F_LINK_TIMEOUT)
2154 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002155
Jens Axboe9e645e112019-05-10 16:07:28 -06002156 /*
2157 * If LINK is set, we have dependent requests in this chain. If we
2158 * didn't fail this request, queue the first one up, moving any other
2159 * dependencies to the next request. In case of failure, fail the rest
2160 * of the chain.
2161 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002162 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2163 struct io_kiocb *nxt = req->link;
2164
2165 req->link = NULL;
2166 return nxt;
2167 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002168 io_fail_links(req);
2169 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002170}
Jens Axboe2665abf2019-11-05 12:40:47 -07002171
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002172static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002173{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002174 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002175 return NULL;
2176 return __io_req_find_next(req);
2177}
2178
Jens Axboe355fb9e2020-10-22 20:19:35 -06002179static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002180{
2181 struct task_struct *tsk = req->task;
2182 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002183 enum task_work_notify_mode notify;
2184 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002185
Jens Axboe6200b0a2020-09-13 14:38:30 -06002186 if (tsk->flags & PF_EXITING)
2187 return -ESRCH;
2188
Jens Axboec2c4c832020-07-01 15:37:11 -06002189 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002190 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2191 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2192 * processing task_work. There's no reliable way to tell if TWA_RESUME
2193 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002194 */
Jens Axboe91989c72020-10-16 09:02:26 -06002195 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002196 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002197 notify = TWA_SIGNAL;
2198
Jens Axboe87c43112020-09-30 21:00:14 -06002199 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002200 if (!ret)
2201 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002202
Jens Axboec2c4c832020-07-01 15:37:11 -06002203 return ret;
2204}
2205
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002206static void io_req_task_work_add_fallback(struct io_kiocb *req,
2207 void (*cb)(struct callback_head *))
2208{
2209 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2210
2211 init_task_work(&req->task_work, cb);
2212 task_work_add(tsk, &req->task_work, TWA_NONE);
2213 wake_up_process(tsk);
2214}
2215
Jens Axboec40f6372020-06-25 15:39:59 -06002216static void __io_req_task_cancel(struct io_kiocb *req, int error)
2217{
2218 struct io_ring_ctx *ctx = req->ctx;
2219
2220 spin_lock_irq(&ctx->completion_lock);
2221 io_cqring_fill_event(req, error);
2222 io_commit_cqring(ctx);
2223 spin_unlock_irq(&ctx->completion_lock);
2224
2225 io_cqring_ev_posted(ctx);
2226 req_set_fail_links(req);
2227 io_double_put_req(req);
2228}
2229
2230static void io_req_task_cancel(struct callback_head *cb)
2231{
2232 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002233 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002234
2235 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002236 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002237}
2238
2239static void __io_req_task_submit(struct io_kiocb *req)
2240{
2241 struct io_ring_ctx *ctx = req->ctx;
2242
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002243 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002244 if (!ctx->sqo_dead &&
2245 !__io_sq_thread_acquire_mm(ctx) &&
2246 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002247 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002248 else
Jens Axboec40f6372020-06-25 15:39:59 -06002249 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002250 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002251}
2252
Jens Axboec40f6372020-06-25 15:39:59 -06002253static void io_req_task_submit(struct callback_head *cb)
2254{
2255 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002256 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002257
2258 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002259 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002260}
2261
2262static void io_req_task_queue(struct io_kiocb *req)
2263{
Jens Axboec40f6372020-06-25 15:39:59 -06002264 int ret;
2265
2266 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002267 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002268
Jens Axboe355fb9e2020-10-22 20:19:35 -06002269 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002270 if (unlikely(ret))
2271 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002272}
2273
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002274static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002275{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002276 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002277
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002278 if (nxt)
2279 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002280}
2281
Jens Axboe9e645e112019-05-10 16:07:28 -06002282static void io_free_req(struct io_kiocb *req)
2283{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002284 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002285 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002286}
2287
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002288struct req_batch {
2289 void *reqs[IO_IOPOLL_BATCH];
2290 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002291
2292 struct task_struct *task;
2293 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002294};
2295
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002296static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002297{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002298 rb->to_free = 0;
2299 rb->task_refs = 0;
2300 rb->task = NULL;
2301}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002302
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002303static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2304 struct req_batch *rb)
2305{
2306 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2307 percpu_ref_put_many(&ctx->refs, rb->to_free);
2308 rb->to_free = 0;
2309}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002310
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002311static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2312 struct req_batch *rb)
2313{
2314 if (rb->to_free)
2315 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002316 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002317 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002318 rb->task = NULL;
2319 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002320}
2321
2322static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2323{
2324 if (unlikely(io_is_fallback_req(req))) {
2325 io_free_req(req);
2326 return;
2327 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002328 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002329
Jens Axboee3bc8e92020-09-24 08:45:57 -06002330 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002331 if (rb->task)
2332 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002333 rb->task = req->task;
2334 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002335 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002336 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002337
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002338 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002339 rb->reqs[rb->to_free++] = req;
2340 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2341 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002342}
2343
Jens Axboeba816ad2019-09-28 11:36:45 -06002344/*
2345 * Drop reference to request, return next in chain (if there is one) if this
2346 * was the last reference to this request.
2347 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002348static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002349{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002350 struct io_kiocb *nxt = NULL;
2351
Jens Axboe2a44f462020-02-25 13:25:41 -07002352 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002353 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002354 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002355 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002356 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357}
2358
Jens Axboe2b188cc2019-01-07 10:46:33 -07002359static void io_put_req(struct io_kiocb *req)
2360{
Jens Axboedef596e2019-01-09 08:59:42 -07002361 if (refcount_dec_and_test(&req->refs))
2362 io_free_req(req);
2363}
2364
Pavel Begunkov216578e2020-10-13 09:44:00 +01002365static void io_put_req_deferred_cb(struct callback_head *cb)
2366{
2367 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2368
2369 io_free_req(req);
2370}
2371
2372static void io_free_req_deferred(struct io_kiocb *req)
2373{
2374 int ret;
2375
2376 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002377 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002378 if (unlikely(ret))
2379 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002380}
2381
2382static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2383{
2384 if (refcount_sub_and_test(refs, &req->refs))
2385 io_free_req_deferred(req);
2386}
2387
Jens Axboe978db572019-11-14 22:39:04 -07002388static void io_double_put_req(struct io_kiocb *req)
2389{
2390 /* drop both submit and complete references */
2391 if (refcount_sub_and_test(2, &req->refs))
2392 io_free_req(req);
2393}
2394
Pavel Begunkov6c503152021-01-04 20:36:36 +00002395static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002396{
2397 /* See comment at the top of this file */
2398 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002399 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002400}
2401
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002402static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2403{
2404 struct io_rings *rings = ctx->rings;
2405
2406 /* make sure SQ entry isn't read before tail */
2407 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2408}
2409
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002410static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002411{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002412 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002413
Jens Axboebcda7ba2020-02-23 16:42:51 -07002414 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2415 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002416 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002417 kfree(kbuf);
2418 return cflags;
2419}
2420
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002421static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2422{
2423 struct io_buffer *kbuf;
2424
2425 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2426 return io_put_kbuf(req, kbuf);
2427}
2428
Jens Axboe4c6e2772020-07-01 11:29:10 -06002429static inline bool io_run_task_work(void)
2430{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002431 /*
2432 * Not safe to run on exiting task, and the task_work handling will
2433 * not add work to such a task.
2434 */
2435 if (unlikely(current->flags & PF_EXITING))
2436 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002437 if (current->task_works) {
2438 __set_current_state(TASK_RUNNING);
2439 task_work_run();
2440 return true;
2441 }
2442
2443 return false;
2444}
2445
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002446static void io_iopoll_queue(struct list_head *again)
2447{
2448 struct io_kiocb *req;
2449
2450 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002451 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2452 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002453 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002454 } while (!list_empty(again));
2455}
2456
Jens Axboedef596e2019-01-09 08:59:42 -07002457/*
2458 * Find and free completed poll iocbs
2459 */
2460static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2461 struct list_head *done)
2462{
Jens Axboe8237e042019-12-28 10:48:22 -07002463 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002464 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002465 LIST_HEAD(again);
2466
2467 /* order with ->result store in io_complete_rw_iopoll() */
2468 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002469
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002470 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002471 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002472 int cflags = 0;
2473
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002474 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002475 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002476 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002477 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002478 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002479 continue;
2480 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002481 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002482
Jens Axboebcda7ba2020-02-23 16:42:51 -07002483 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002484 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002485
2486 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002487 (*nr_events)++;
2488
Pavel Begunkovc3524382020-06-28 12:52:32 +03002489 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002490 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002491 }
Jens Axboedef596e2019-01-09 08:59:42 -07002492
Jens Axboe09bb8392019-03-13 12:39:28 -06002493 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002494 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002495 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002496
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002497 if (!list_empty(&again))
2498 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002499}
2500
Jens Axboedef596e2019-01-09 08:59:42 -07002501static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2502 long min)
2503{
2504 struct io_kiocb *req, *tmp;
2505 LIST_HEAD(done);
2506 bool spin;
2507 int ret;
2508
2509 /*
2510 * Only spin for completions if we don't have multiple devices hanging
2511 * off our complete list, and we're under the requested amount.
2512 */
2513 spin = !ctx->poll_multi_file && *nr_events < min;
2514
2515 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002516 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002517 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002518
2519 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002520 * Move completed and retryable entries to our local lists.
2521 * If we find a request that requires polling, break out
2522 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002523 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002524 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002525 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002526 continue;
2527 }
2528 if (!list_empty(&done))
2529 break;
2530
2531 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2532 if (ret < 0)
2533 break;
2534
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002535 /* iopoll may have completed current req */
2536 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002537 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002538
Jens Axboedef596e2019-01-09 08:59:42 -07002539 if (ret && spin)
2540 spin = false;
2541 ret = 0;
2542 }
2543
2544 if (!list_empty(&done))
2545 io_iopoll_complete(ctx, nr_events, &done);
2546
2547 return ret;
2548}
2549
2550/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002551 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002552 * non-spinning poll check - we'll still enter the driver poll loop, but only
2553 * as a non-spinning completion check.
2554 */
2555static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2556 long min)
2557{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002558 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002559 int ret;
2560
2561 ret = io_do_iopoll(ctx, nr_events, min);
2562 if (ret < 0)
2563 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002564 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002565 return 0;
2566 }
2567
2568 return 1;
2569}
2570
2571/*
2572 * We can't just wait for polled events to come to us, we have to actively
2573 * find and complete them.
2574 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002575static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002576{
2577 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2578 return;
2579
2580 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002581 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002582 unsigned int nr_events = 0;
2583
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002584 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002585
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002586 /* let it sleep and repeat later if can't complete a request */
2587 if (nr_events == 0)
2588 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002589 /*
2590 * Ensure we allow local-to-the-cpu processing to take place,
2591 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002592 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002593 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002594 if (need_resched()) {
2595 mutex_unlock(&ctx->uring_lock);
2596 cond_resched();
2597 mutex_lock(&ctx->uring_lock);
2598 }
Jens Axboedef596e2019-01-09 08:59:42 -07002599 }
2600 mutex_unlock(&ctx->uring_lock);
2601}
2602
Pavel Begunkov7668b922020-07-07 16:36:21 +03002603static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002604{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002605 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002606 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002607
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002608 /*
2609 * We disallow the app entering submit/complete with polling, but we
2610 * still need to lock the ring to prevent racing with polled issue
2611 * that got punted to a workqueue.
2612 */
2613 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002614 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002615 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002616 * Don't enter poll loop if we already have events pending.
2617 * If we do, we can potentially be spinning for commands that
2618 * already triggered a CQE (eg in error).
2619 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002620 if (test_bit(0, &ctx->cq_check_overflow))
2621 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2622 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002623 break;
2624
2625 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002626 * If a submit got punted to a workqueue, we can have the
2627 * application entering polling for a command before it gets
2628 * issued. That app will hold the uring_lock for the duration
2629 * of the poll right here, so we need to take a breather every
2630 * now and then to ensure that the issue has a chance to add
2631 * the poll to the issued list. Otherwise we can spin here
2632 * forever, while the workqueue is stuck trying to acquire the
2633 * very same mutex.
2634 */
2635 if (!(++iters & 7)) {
2636 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002637 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002638 mutex_lock(&ctx->uring_lock);
2639 }
2640
Pavel Begunkov7668b922020-07-07 16:36:21 +03002641 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002642 if (ret <= 0)
2643 break;
2644 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002645 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002646
Jens Axboe500f9fb2019-08-19 12:15:59 -06002647 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002648 return ret;
2649}
2650
Jens Axboe491381ce2019-10-17 09:20:46 -06002651static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002652{
Jens Axboe491381ce2019-10-17 09:20:46 -06002653 /*
2654 * Tell lockdep we inherited freeze protection from submission
2655 * thread.
2656 */
2657 if (req->flags & REQ_F_ISREG) {
2658 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002659
Jens Axboe491381ce2019-10-17 09:20:46 -06002660 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002661 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002662 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663}
2664
Jens Axboea1d7c392020-06-22 11:09:46 -06002665static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002666 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002667{
Jens Axboe9adbd452019-12-20 08:45:55 -07002668 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002669 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670
Jens Axboe491381ce2019-10-17 09:20:46 -06002671 if (kiocb->ki_flags & IOCB_WRITE)
2672 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002674 if (res != req->result)
2675 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002676 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002677 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002678 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002679}
2680
Jens Axboeb63534c2020-06-04 11:28:00 -06002681#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002682static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002683{
2684 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002685 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002686 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002687
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002688 /* already prepared */
2689 if (req->async_data)
2690 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002691
2692 switch (req->opcode) {
2693 case IORING_OP_READV:
2694 case IORING_OP_READ_FIXED:
2695 case IORING_OP_READ:
2696 rw = READ;
2697 break;
2698 case IORING_OP_WRITEV:
2699 case IORING_OP_WRITE_FIXED:
2700 case IORING_OP_WRITE:
2701 rw = WRITE;
2702 break;
2703 default:
2704 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2705 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002706 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002707 }
2708
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002709 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2710 if (ret < 0)
2711 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002712 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002713}
Jens Axboeb63534c2020-06-04 11:28:00 -06002714#endif
2715
2716static bool io_rw_reissue(struct io_kiocb *req, long res)
2717{
2718#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002719 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002720 int ret;
2721
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002722 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002723 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002724 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002725 if (!S_ISBLK(mode) && !S_ISREG(mode))
2726 return false;
2727 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002728 return false;
2729
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002730 lockdep_assert_held(&req->ctx->uring_lock);
2731
Jens Axboe28cea78a2020-09-14 10:51:17 -06002732 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002733
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002734 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002735 refcount_inc(&req->refs);
2736 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002737 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002738 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002739 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002740#endif
2741 return false;
2742}
2743
Jens Axboea1d7c392020-06-22 11:09:46 -06002744static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002745 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002746{
2747 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002748 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002749}
2750
2751static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2752{
Jens Axboe9adbd452019-12-20 08:45:55 -07002753 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002754
Pavel Begunkov889fca72021-02-10 00:03:09 +00002755 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002756}
2757
Jens Axboedef596e2019-01-09 08:59:42 -07002758static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2759{
Jens Axboe9adbd452019-12-20 08:45:55 -07002760 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002761
Jens Axboe491381ce2019-10-17 09:20:46 -06002762 if (kiocb->ki_flags & IOCB_WRITE)
2763 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002764
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002765 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002766 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002767
2768 WRITE_ONCE(req->result, res);
2769 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002770 smp_wmb();
2771 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002772}
2773
2774/*
2775 * After the iocb has been issued, it's safe to be found on the poll list.
2776 * Adding the kiocb to the list AFTER submission ensures that we don't
2777 * find it from a io_iopoll_getevents() thread before the issuer is done
2778 * accessing the kiocb cookie.
2779 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002780static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002781{
2782 struct io_ring_ctx *ctx = req->ctx;
2783
2784 /*
2785 * Track whether we have multiple files in our lists. This will impact
2786 * how we do polling eventually, not spinning if we're on potentially
2787 * different devices.
2788 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002789 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002790 ctx->poll_multi_file = false;
2791 } else if (!ctx->poll_multi_file) {
2792 struct io_kiocb *list_req;
2793
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002794 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002795 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002796 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002797 ctx->poll_multi_file = true;
2798 }
2799
2800 /*
2801 * For fast devices, IO may have already completed. If it has, add
2802 * it to the front so we find it first.
2803 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002804 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002805 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002806 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002807 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002808
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002809 /*
2810 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2811 * task context or in io worker task context. If current task context is
2812 * sq thread, we don't need to check whether should wake up sq thread.
2813 */
2814 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002815 wq_has_sleeper(&ctx->sq_data->wait))
2816 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002817}
2818
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002819static inline void io_state_file_put(struct io_submit_state *state)
2820{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002821 if (state->file_refs) {
2822 fput_many(state->file, state->file_refs);
2823 state->file_refs = 0;
2824 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002825}
2826
2827/*
2828 * Get as many references to a file as we have IOs left in this submission,
2829 * assuming most submissions are for one file, or at least that each file
2830 * has more than one submission.
2831 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002832static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002833{
2834 if (!state)
2835 return fget(fd);
2836
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002837 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002838 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002839 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002840 return state->file;
2841 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002842 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002843 }
2844 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002845 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002846 return NULL;
2847
2848 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002849 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002850 return state->file;
2851}
2852
Jens Axboe4503b762020-06-01 10:00:27 -06002853static bool io_bdev_nowait(struct block_device *bdev)
2854{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002855 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002856}
2857
Jens Axboe2b188cc2019-01-07 10:46:33 -07002858/*
2859 * If we tracked the file through the SCM inflight mechanism, we could support
2860 * any file. For now, just ensure that anything potentially problematic is done
2861 * inline.
2862 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002863static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002864{
2865 umode_t mode = file_inode(file)->i_mode;
2866
Jens Axboe4503b762020-06-01 10:00:27 -06002867 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002868 if (IS_ENABLED(CONFIG_BLOCK) &&
2869 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002870 return true;
2871 return false;
2872 }
2873 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002874 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002875 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002876 if (IS_ENABLED(CONFIG_BLOCK) &&
2877 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002878 file->f_op != &io_uring_fops)
2879 return true;
2880 return false;
2881 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882
Jens Axboec5b85622020-06-09 19:23:05 -06002883 /* any ->read/write should understand O_NONBLOCK */
2884 if (file->f_flags & O_NONBLOCK)
2885 return true;
2886
Jens Axboeaf197f52020-04-28 13:15:06 -06002887 if (!(file->f_mode & FMODE_NOWAIT))
2888 return false;
2889
2890 if (rw == READ)
2891 return file->f_op->read_iter != NULL;
2892
2893 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002894}
2895
Pavel Begunkova88fc402020-09-30 22:57:53 +03002896static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002897{
Jens Axboedef596e2019-01-09 08:59:42 -07002898 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002899 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002900 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002901 unsigned ioprio;
2902 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002903
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002904 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002905 req->flags |= REQ_F_ISREG;
2906
Jens Axboe2b188cc2019-01-07 10:46:33 -07002907 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002908 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002909 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002910 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002911 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002912 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002913 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2914 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2915 if (unlikely(ret))
2916 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002917
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002918 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2919 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2920 req->flags |= REQ_F_NOWAIT;
2921
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922 ioprio = READ_ONCE(sqe->ioprio);
2923 if (ioprio) {
2924 ret = ioprio_check_cap(ioprio);
2925 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002926 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002927
2928 kiocb->ki_ioprio = ioprio;
2929 } else
2930 kiocb->ki_ioprio = get_current_ioprio();
2931
Jens Axboedef596e2019-01-09 08:59:42 -07002932 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002933 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2934 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002935 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936
Jens Axboedef596e2019-01-09 08:59:42 -07002937 kiocb->ki_flags |= IOCB_HIPRI;
2938 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002939 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002940 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002941 if (kiocb->ki_flags & IOCB_HIPRI)
2942 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002943 kiocb->ki_complete = io_complete_rw;
2944 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002945
Jens Axboe3529d8c2019-12-19 18:24:38 -07002946 req->rw.addr = READ_ONCE(sqe->addr);
2947 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002948 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950}
2951
2952static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2953{
2954 switch (ret) {
2955 case -EIOCBQUEUED:
2956 break;
2957 case -ERESTARTSYS:
2958 case -ERESTARTNOINTR:
2959 case -ERESTARTNOHAND:
2960 case -ERESTART_RESTARTBLOCK:
2961 /*
2962 * We can't just restart the syscall, since previously
2963 * submitted sqes may already be in progress. Just fail this
2964 * IO with EINTR.
2965 */
2966 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002967 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968 default:
2969 kiocb->ki_complete(kiocb, ret, 0);
2970 }
2971}
2972
Jens Axboea1d7c392020-06-22 11:09:46 -06002973static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002974 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002975{
Jens Axboeba042912019-12-25 16:33:42 -07002976 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002977 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002978
Jens Axboe227c0c92020-08-13 11:51:40 -06002979 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002980 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002981 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002982 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002983 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002984 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002985 }
2986
Jens Axboeba042912019-12-25 16:33:42 -07002987 if (req->flags & REQ_F_CUR_POS)
2988 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002989 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002990 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002991 else
2992 io_rw_done(kiocb, ret);
2993}
2994
Pavel Begunkov847595d2021-02-04 13:52:06 +00002995static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002996{
Jens Axboe9adbd452019-12-20 08:45:55 -07002997 struct io_ring_ctx *ctx = req->ctx;
2998 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002999 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003000 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003001 size_t offset;
3002 u64 buf_addr;
3003
Jens Axboeedafcce2019-01-09 09:16:05 -07003004 if (unlikely(buf_index >= ctx->nr_user_bufs))
3005 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003006 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3007 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003008 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003009
3010 /* overflow */
3011 if (buf_addr + len < buf_addr)
3012 return -EFAULT;
3013 /* not inside the mapped region */
3014 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3015 return -EFAULT;
3016
3017 /*
3018 * May not be a start of buffer, set size appropriately
3019 * and advance us to the beginning.
3020 */
3021 offset = buf_addr - imu->ubuf;
3022 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003023
3024 if (offset) {
3025 /*
3026 * Don't use iov_iter_advance() here, as it's really slow for
3027 * using the latter parts of a big fixed buffer - it iterates
3028 * over each segment manually. We can cheat a bit here, because
3029 * we know that:
3030 *
3031 * 1) it's a BVEC iter, we set it up
3032 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3033 * first and last bvec
3034 *
3035 * So just find our index, and adjust the iterator afterwards.
3036 * If the offset is within the first bvec (or the whole first
3037 * bvec, just use iov_iter_advance(). This makes it easier
3038 * since we can just skip the first segment, which may not
3039 * be PAGE_SIZE aligned.
3040 */
3041 const struct bio_vec *bvec = imu->bvec;
3042
3043 if (offset <= bvec->bv_len) {
3044 iov_iter_advance(iter, offset);
3045 } else {
3046 unsigned long seg_skip;
3047
3048 /* skip first vec */
3049 offset -= bvec->bv_len;
3050 seg_skip = 1 + (offset >> PAGE_SHIFT);
3051
3052 iter->bvec = bvec + seg_skip;
3053 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003054 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003055 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003056 }
3057 }
3058
Pavel Begunkov847595d2021-02-04 13:52:06 +00003059 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003060}
3061
Jens Axboebcda7ba2020-02-23 16:42:51 -07003062static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3063{
3064 if (needs_lock)
3065 mutex_unlock(&ctx->uring_lock);
3066}
3067
3068static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3069{
3070 /*
3071 * "Normal" inline submissions always hold the uring_lock, since we
3072 * grab it from the system call. Same is true for the SQPOLL offload.
3073 * The only exception is when we've detached the request and issue it
3074 * from an async worker thread, grab the lock for that case.
3075 */
3076 if (needs_lock)
3077 mutex_lock(&ctx->uring_lock);
3078}
3079
3080static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3081 int bgid, struct io_buffer *kbuf,
3082 bool needs_lock)
3083{
3084 struct io_buffer *head;
3085
3086 if (req->flags & REQ_F_BUFFER_SELECTED)
3087 return kbuf;
3088
3089 io_ring_submit_lock(req->ctx, needs_lock);
3090
3091 lockdep_assert_held(&req->ctx->uring_lock);
3092
3093 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3094 if (head) {
3095 if (!list_empty(&head->list)) {
3096 kbuf = list_last_entry(&head->list, struct io_buffer,
3097 list);
3098 list_del(&kbuf->list);
3099 } else {
3100 kbuf = head;
3101 idr_remove(&req->ctx->io_buffer_idr, bgid);
3102 }
3103 if (*len > kbuf->len)
3104 *len = kbuf->len;
3105 } else {
3106 kbuf = ERR_PTR(-ENOBUFS);
3107 }
3108
3109 io_ring_submit_unlock(req->ctx, needs_lock);
3110
3111 return kbuf;
3112}
3113
Jens Axboe4d954c22020-02-27 07:31:19 -07003114static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3115 bool needs_lock)
3116{
3117 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003118 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003119
3120 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003121 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003122 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3123 if (IS_ERR(kbuf))
3124 return kbuf;
3125 req->rw.addr = (u64) (unsigned long) kbuf;
3126 req->flags |= REQ_F_BUFFER_SELECTED;
3127 return u64_to_user_ptr(kbuf->addr);
3128}
3129
3130#ifdef CONFIG_COMPAT
3131static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3132 bool needs_lock)
3133{
3134 struct compat_iovec __user *uiov;
3135 compat_ssize_t clen;
3136 void __user *buf;
3137 ssize_t len;
3138
3139 uiov = u64_to_user_ptr(req->rw.addr);
3140 if (!access_ok(uiov, sizeof(*uiov)))
3141 return -EFAULT;
3142 if (__get_user(clen, &uiov->iov_len))
3143 return -EFAULT;
3144 if (clen < 0)
3145 return -EINVAL;
3146
3147 len = clen;
3148 buf = io_rw_buffer_select(req, &len, needs_lock);
3149 if (IS_ERR(buf))
3150 return PTR_ERR(buf);
3151 iov[0].iov_base = buf;
3152 iov[0].iov_len = (compat_size_t) len;
3153 return 0;
3154}
3155#endif
3156
3157static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3158 bool needs_lock)
3159{
3160 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3161 void __user *buf;
3162 ssize_t len;
3163
3164 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3165 return -EFAULT;
3166
3167 len = iov[0].iov_len;
3168 if (len < 0)
3169 return -EINVAL;
3170 buf = io_rw_buffer_select(req, &len, needs_lock);
3171 if (IS_ERR(buf))
3172 return PTR_ERR(buf);
3173 iov[0].iov_base = buf;
3174 iov[0].iov_len = len;
3175 return 0;
3176}
3177
3178static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3179 bool needs_lock)
3180{
Jens Axboedddb3e22020-06-04 11:27:01 -06003181 if (req->flags & REQ_F_BUFFER_SELECTED) {
3182 struct io_buffer *kbuf;
3183
3184 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3185 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3186 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003187 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003188 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003189 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003190 return -EINVAL;
3191
3192#ifdef CONFIG_COMPAT
3193 if (req->ctx->compat)
3194 return io_compat_import(req, iov, needs_lock);
3195#endif
3196
3197 return __io_iov_buffer_select(req, iov, needs_lock);
3198}
3199
Pavel Begunkov847595d2021-02-04 13:52:06 +00003200static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3201 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003202{
Jens Axboe9adbd452019-12-20 08:45:55 -07003203 void __user *buf = u64_to_user_ptr(req->rw.addr);
3204 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003205 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003206 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003207
Pavel Begunkov7d009162019-11-25 23:14:40 +03003208 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003209 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003210 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003211 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212
Jens Axboebcda7ba2020-02-23 16:42:51 -07003213 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003214 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003215 return -EINVAL;
3216
Jens Axboe3a6820f2019-12-22 15:19:35 -07003217 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003218 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003219 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003220 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003221 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003222 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003223 }
3224
Jens Axboe3a6820f2019-12-22 15:19:35 -07003225 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3226 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003227 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003228 }
3229
Jens Axboe4d954c22020-02-27 07:31:19 -07003230 if (req->flags & REQ_F_BUFFER_SELECT) {
3231 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003232 if (!ret)
3233 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003234 *iovec = NULL;
3235 return ret;
3236 }
3237
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003238 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3239 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003240}
3241
Jens Axboe0fef9482020-08-26 10:36:20 -06003242static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3243{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003244 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003245}
3246
Jens Axboe32960612019-09-23 11:05:34 -06003247/*
3248 * For files that don't have ->read_iter() and ->write_iter(), handle them
3249 * by looping over ->read() or ->write() manually.
3250 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003251static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003252{
Jens Axboe4017eb92020-10-22 14:14:12 -06003253 struct kiocb *kiocb = &req->rw.kiocb;
3254 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003255 ssize_t ret = 0;
3256
3257 /*
3258 * Don't support polled IO through this interface, and we can't
3259 * support non-blocking either. For the latter, this just causes
3260 * the kiocb to be handled from an async context.
3261 */
3262 if (kiocb->ki_flags & IOCB_HIPRI)
3263 return -EOPNOTSUPP;
3264 if (kiocb->ki_flags & IOCB_NOWAIT)
3265 return -EAGAIN;
3266
3267 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003268 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003269 ssize_t nr;
3270
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003271 if (!iov_iter_is_bvec(iter)) {
3272 iovec = iov_iter_iovec(iter);
3273 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003274 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3275 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003276 }
3277
Jens Axboe32960612019-09-23 11:05:34 -06003278 if (rw == READ) {
3279 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003280 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003281 } else {
3282 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003283 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003284 }
3285
3286 if (nr < 0) {
3287 if (!ret)
3288 ret = nr;
3289 break;
3290 }
3291 ret += nr;
3292 if (nr != iovec.iov_len)
3293 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003294 req->rw.len -= nr;
3295 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003296 iov_iter_advance(iter, nr);
3297 }
3298
3299 return ret;
3300}
3301
Jens Axboeff6165b2020-08-13 09:47:43 -06003302static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3303 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003304{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003305 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003306
Jens Axboeff6165b2020-08-13 09:47:43 -06003307 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003308 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003309 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003310 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003311 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003312 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003313 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003314 unsigned iov_off = 0;
3315
3316 rw->iter.iov = rw->fast_iov;
3317 if (iter->iov != fast_iov) {
3318 iov_off = iter->iov - fast_iov;
3319 rw->iter.iov += iov_off;
3320 }
3321 if (rw->fast_iov != fast_iov)
3322 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003323 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003324 } else {
3325 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003326 }
3327}
3328
Jens Axboee8c2bc12020-08-15 18:44:09 -07003329static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003330{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003331 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3332 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3333 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003334}
3335
Jens Axboee8c2bc12020-08-15 18:44:09 -07003336static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003337{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003338 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003339 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003340
Jens Axboee8c2bc12020-08-15 18:44:09 -07003341 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003342}
3343
Jens Axboeff6165b2020-08-13 09:47:43 -06003344static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3345 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003346 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003347{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003348 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003349 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003350 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003351 if (__io_alloc_async_data(req)) {
3352 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003353 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003354 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003355
Jens Axboeff6165b2020-08-13 09:47:43 -06003356 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003357 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003358 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003359}
3360
Pavel Begunkov73debe62020-09-30 22:57:54 +03003361static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003362{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003363 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003364 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003365 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003366
Pavel Begunkov2846c482020-11-07 13:16:27 +00003367 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003368 if (unlikely(ret < 0))
3369 return ret;
3370
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003371 iorw->bytes_done = 0;
3372 iorw->free_iovec = iov;
3373 if (iov)
3374 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003375 return 0;
3376}
3377
Pavel Begunkov73debe62020-09-30 22:57:54 +03003378static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003379{
3380 ssize_t ret;
3381
Pavel Begunkova88fc402020-09-30 22:57:53 +03003382 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003383 if (ret)
3384 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003385
Jens Axboe3529d8c2019-12-19 18:24:38 -07003386 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3387 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003388
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003389 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003390 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003391 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003392 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003393}
3394
Jens Axboec1dd91d2020-08-03 16:43:59 -06003395/*
3396 * This is our waitqueue callback handler, registered through lock_page_async()
3397 * when we initially tried to do the IO with the iocb armed our waitqueue.
3398 * This gets called when the page is unlocked, and we generally expect that to
3399 * happen when the page IO is completed and the page is now uptodate. This will
3400 * queue a task_work based retry of the operation, attempting to copy the data
3401 * again. If the latter fails because the page was NOT uptodate, then we will
3402 * do a thread based blocking retry of the operation. That's the unexpected
3403 * slow path.
3404 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003405static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3406 int sync, void *arg)
3407{
3408 struct wait_page_queue *wpq;
3409 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003410 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003411 int ret;
3412
3413 wpq = container_of(wait, struct wait_page_queue, wait);
3414
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003415 if (!wake_page_match(wpq, key))
3416 return 0;
3417
Hao Xuc8d317a2020-09-29 20:00:45 +08003418 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003419 list_del_init(&wait->entry);
3420
Pavel Begunkove7375122020-07-12 20:42:04 +03003421 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003422 percpu_ref_get(&req->ctx->refs);
3423
Jens Axboebcf5a062020-05-22 09:24:42 -06003424 /* submit ref gets dropped, acquire a new one */
3425 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003426 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003427 if (unlikely(ret))
3428 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003429 return 1;
3430}
3431
Jens Axboec1dd91d2020-08-03 16:43:59 -06003432/*
3433 * This controls whether a given IO request should be armed for async page
3434 * based retry. If we return false here, the request is handed to the async
3435 * worker threads for retry. If we're doing buffered reads on a regular file,
3436 * we prepare a private wait_page_queue entry and retry the operation. This
3437 * will either succeed because the page is now uptodate and unlocked, or it
3438 * will register a callback when the page is unlocked at IO completion. Through
3439 * that callback, io_uring uses task_work to setup a retry of the operation.
3440 * That retry will attempt the buffered read again. The retry will generally
3441 * succeed, or in rare cases where it fails, we then fall back to using the
3442 * async worker threads for a blocking retry.
3443 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003444static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003445{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003446 struct io_async_rw *rw = req->async_data;
3447 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003448 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003449
3450 /* never retry for NOWAIT, we just complete with -EAGAIN */
3451 if (req->flags & REQ_F_NOWAIT)
3452 return false;
3453
Jens Axboe227c0c92020-08-13 11:51:40 -06003454 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003455 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003456 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003457
Jens Axboebcf5a062020-05-22 09:24:42 -06003458 /*
3459 * just use poll if we can, and don't attempt if the fs doesn't
3460 * support callback based unlocks
3461 */
3462 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3463 return false;
3464
Jens Axboe3b2a4432020-08-16 10:58:43 -07003465 wait->wait.func = io_async_buf_func;
3466 wait->wait.private = req;
3467 wait->wait.flags = 0;
3468 INIT_LIST_HEAD(&wait->wait.entry);
3469 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003470 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003471 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003472 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003473}
3474
3475static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3476{
3477 if (req->file->f_op->read_iter)
3478 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003479 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003480 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003481 else
3482 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003483}
3484
Pavel Begunkov889fca72021-02-10 00:03:09 +00003485static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486{
3487 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003488 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003489 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003490 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003491 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003492 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493
Pavel Begunkov2846c482020-11-07 13:16:27 +00003494 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003495 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003496 iovec = NULL;
3497 } else {
3498 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3499 if (ret < 0)
3500 return ret;
3501 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003502 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003503 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003504
Jens Axboefd6c2e42019-12-18 12:19:41 -07003505 /* Ensure we clear previously set non-block flag */
3506 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003507 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003508 else
3509 kiocb->ki_flags |= IOCB_NOWAIT;
3510
Pavel Begunkov24c74672020-06-21 13:09:51 +03003511 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003512 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3513 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003514 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003515 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003516
Pavel Begunkov632546c2020-11-07 13:16:26 +00003517 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003518 if (unlikely(ret)) {
3519 kfree(iovec);
3520 return ret;
3521 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522
Jens Axboe227c0c92020-08-13 11:51:40 -06003523 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003524
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003525 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003526 /* it's faster to check here then delegate to kfree */
3527 if (iovec)
3528 kfree(iovec);
3529 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003530 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003531 /* IOPOLL retry should happen for io-wq threads */
3532 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003533 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003534 /* no retry on NONBLOCK nor RWF_NOWAIT */
3535 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003536 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003537 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003538 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003539 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003540 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003541 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003542 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003543 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003544 }
3545
Jens Axboe227c0c92020-08-13 11:51:40 -06003546 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003547 if (ret2)
3548 return ret2;
3549
Jens Axboee8c2bc12020-08-15 18:44:09 -07003550 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003551 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003552 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003553
Pavel Begunkovb23df912021-02-04 13:52:04 +00003554 do {
3555 io_size -= ret;
3556 rw->bytes_done += ret;
3557 /* if we can retry, do so with the callbacks armed */
3558 if (!io_rw_should_retry(req)) {
3559 kiocb->ki_flags &= ~IOCB_WAITQ;
3560 return -EAGAIN;
3561 }
3562
3563 /*
3564 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3565 * we get -EIOCBQUEUED, then we'll get a notification when the
3566 * desired page gets unlocked. We can also get a partial read
3567 * here, and if we do, then just retry at the new offset.
3568 */
3569 ret = io_iter_do_read(req, iter);
3570 if (ret == -EIOCBQUEUED)
3571 return 0;
3572 /* we got some bytes, but not all. retry. */
3573 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003574done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003575 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003576 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003577}
3578
Pavel Begunkov73debe62020-09-30 22:57:54 +03003579static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003580{
3581 ssize_t ret;
3582
Pavel Begunkova88fc402020-09-30 22:57:53 +03003583 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003584 if (ret)
3585 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003586
Jens Axboe3529d8c2019-12-19 18:24:38 -07003587 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3588 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003589
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003590 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003591 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003592 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003593 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003594}
3595
Pavel Begunkov889fca72021-02-10 00:03:09 +00003596static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597{
3598 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003599 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003600 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003601 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003602 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003603 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003604
Pavel Begunkov2846c482020-11-07 13:16:27 +00003605 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003606 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003607 iovec = NULL;
3608 } else {
3609 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3610 if (ret < 0)
3611 return ret;
3612 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003613 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003614 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003615
Jens Axboefd6c2e42019-12-18 12:19:41 -07003616 /* Ensure we clear previously set non-block flag */
3617 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003618 kiocb->ki_flags &= ~IOCB_NOWAIT;
3619 else
3620 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003621
Pavel Begunkov24c74672020-06-21 13:09:51 +03003622 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003623 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003624 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003625
Jens Axboe10d59342019-12-09 20:16:22 -07003626 /* file path doesn't support NOWAIT for non-direct_IO */
3627 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3628 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003629 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003630
Pavel Begunkov632546c2020-11-07 13:16:26 +00003631 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003632 if (unlikely(ret))
3633 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003634
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003635 /*
3636 * Open-code file_start_write here to grab freeze protection,
3637 * which will be released by another thread in
3638 * io_complete_rw(). Fool lockdep by telling it the lock got
3639 * released so that it doesn't complain about the held lock when
3640 * we return to userspace.
3641 */
3642 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003643 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003644 __sb_writers_release(file_inode(req->file)->i_sb,
3645 SB_FREEZE_WRITE);
3646 }
3647 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003648
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003649 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003650 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003651 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003652 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003653 else
3654 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003655
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003656 /*
3657 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3658 * retry them without IOCB_NOWAIT.
3659 */
3660 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3661 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003662 /* no retry on NONBLOCK nor RWF_NOWAIT */
3663 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003664 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003665 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003666 /* IOPOLL retry should happen for io-wq threads */
3667 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3668 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003669done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003670 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003671 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003672copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003673 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003674 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003675 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003676 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003677 }
Jens Axboe31b51512019-01-18 22:56:34 -07003678out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003679 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003680 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003681 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003682 return ret;
3683}
3684
Jens Axboe80a261f2020-09-28 14:23:58 -06003685static int io_renameat_prep(struct io_kiocb *req,
3686 const struct io_uring_sqe *sqe)
3687{
3688 struct io_rename *ren = &req->rename;
3689 const char __user *oldf, *newf;
3690
3691 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3692 return -EBADF;
3693
3694 ren->old_dfd = READ_ONCE(sqe->fd);
3695 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3696 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3697 ren->new_dfd = READ_ONCE(sqe->len);
3698 ren->flags = READ_ONCE(sqe->rename_flags);
3699
3700 ren->oldpath = getname(oldf);
3701 if (IS_ERR(ren->oldpath))
3702 return PTR_ERR(ren->oldpath);
3703
3704 ren->newpath = getname(newf);
3705 if (IS_ERR(ren->newpath)) {
3706 putname(ren->oldpath);
3707 return PTR_ERR(ren->newpath);
3708 }
3709
3710 req->flags |= REQ_F_NEED_CLEANUP;
3711 return 0;
3712}
3713
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003714static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003715{
3716 struct io_rename *ren = &req->rename;
3717 int ret;
3718
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003719 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003720 return -EAGAIN;
3721
3722 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3723 ren->newpath, ren->flags);
3724
3725 req->flags &= ~REQ_F_NEED_CLEANUP;
3726 if (ret < 0)
3727 req_set_fail_links(req);
3728 io_req_complete(req, ret);
3729 return 0;
3730}
3731
Jens Axboe14a11432020-09-28 14:27:37 -06003732static int io_unlinkat_prep(struct io_kiocb *req,
3733 const struct io_uring_sqe *sqe)
3734{
3735 struct io_unlink *un = &req->unlink;
3736 const char __user *fname;
3737
3738 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3739 return -EBADF;
3740
3741 un->dfd = READ_ONCE(sqe->fd);
3742
3743 un->flags = READ_ONCE(sqe->unlink_flags);
3744 if (un->flags & ~AT_REMOVEDIR)
3745 return -EINVAL;
3746
3747 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3748 un->filename = getname(fname);
3749 if (IS_ERR(un->filename))
3750 return PTR_ERR(un->filename);
3751
3752 req->flags |= REQ_F_NEED_CLEANUP;
3753 return 0;
3754}
3755
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003756static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003757{
3758 struct io_unlink *un = &req->unlink;
3759 int ret;
3760
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003761 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003762 return -EAGAIN;
3763
3764 if (un->flags & AT_REMOVEDIR)
3765 ret = do_rmdir(un->dfd, un->filename);
3766 else
3767 ret = do_unlinkat(un->dfd, un->filename);
3768
3769 req->flags &= ~REQ_F_NEED_CLEANUP;
3770 if (ret < 0)
3771 req_set_fail_links(req);
3772 io_req_complete(req, ret);
3773 return 0;
3774}
3775
Jens Axboe36f4fa62020-09-05 11:14:22 -06003776static int io_shutdown_prep(struct io_kiocb *req,
3777 const struct io_uring_sqe *sqe)
3778{
3779#if defined(CONFIG_NET)
3780 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3781 return -EINVAL;
3782 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3783 sqe->buf_index)
3784 return -EINVAL;
3785
3786 req->shutdown.how = READ_ONCE(sqe->len);
3787 return 0;
3788#else
3789 return -EOPNOTSUPP;
3790#endif
3791}
3792
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003793static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003794{
3795#if defined(CONFIG_NET)
3796 struct socket *sock;
3797 int ret;
3798
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003799 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003800 return -EAGAIN;
3801
Linus Torvalds48aba792020-12-16 12:44:05 -08003802 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003803 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003804 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003805
3806 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003807 if (ret < 0)
3808 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003809 io_req_complete(req, ret);
3810 return 0;
3811#else
3812 return -EOPNOTSUPP;
3813#endif
3814}
3815
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003816static int __io_splice_prep(struct io_kiocb *req,
3817 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003818{
3819 struct io_splice* sp = &req->splice;
3820 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003821
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003822 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3823 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003824
3825 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003826 sp->len = READ_ONCE(sqe->len);
3827 sp->flags = READ_ONCE(sqe->splice_flags);
3828
3829 if (unlikely(sp->flags & ~valid_flags))
3830 return -EINVAL;
3831
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003832 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3833 (sp->flags & SPLICE_F_FD_IN_FIXED));
3834 if (!sp->file_in)
3835 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003836 req->flags |= REQ_F_NEED_CLEANUP;
3837
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003838 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3839 /*
3840 * Splice operation will be punted aync, and here need to
3841 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3842 */
3843 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003844 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003845 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003846
3847 return 0;
3848}
3849
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003850static int io_tee_prep(struct io_kiocb *req,
3851 const struct io_uring_sqe *sqe)
3852{
3853 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3854 return -EINVAL;
3855 return __io_splice_prep(req, sqe);
3856}
3857
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003858static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003859{
3860 struct io_splice *sp = &req->splice;
3861 struct file *in = sp->file_in;
3862 struct file *out = sp->file_out;
3863 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3864 long ret = 0;
3865
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003866 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003867 return -EAGAIN;
3868 if (sp->len)
3869 ret = do_tee(in, out, sp->len, flags);
3870
3871 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3872 req->flags &= ~REQ_F_NEED_CLEANUP;
3873
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003874 if (ret != sp->len)
3875 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003876 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003877 return 0;
3878}
3879
3880static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3881{
3882 struct io_splice* sp = &req->splice;
3883
3884 sp->off_in = READ_ONCE(sqe->splice_off_in);
3885 sp->off_out = READ_ONCE(sqe->off);
3886 return __io_splice_prep(req, sqe);
3887}
3888
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003889static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003890{
3891 struct io_splice *sp = &req->splice;
3892 struct file *in = sp->file_in;
3893 struct file *out = sp->file_out;
3894 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3895 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003896 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003897
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003898 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003899 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003900
3901 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3902 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003903
Jens Axboe948a7742020-05-17 14:21:38 -06003904 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003905 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003906
3907 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3908 req->flags &= ~REQ_F_NEED_CLEANUP;
3909
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003910 if (ret != sp->len)
3911 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003912 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003913 return 0;
3914}
3915
Jens Axboe2b188cc2019-01-07 10:46:33 -07003916/*
3917 * IORING_OP_NOP just posts a completion event, nothing else.
3918 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003919static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003920{
3921 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003922
Jens Axboedef596e2019-01-09 08:59:42 -07003923 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3924 return -EINVAL;
3925
Pavel Begunkov889fca72021-02-10 00:03:09 +00003926 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003927 return 0;
3928}
3929
Jens Axboe3529d8c2019-12-19 18:24:38 -07003930static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003931{
Jens Axboe6b063142019-01-10 22:13:58 -07003932 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003933
Jens Axboe09bb8392019-03-13 12:39:28 -06003934 if (!req->file)
3935 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003936
Jens Axboe6b063142019-01-10 22:13:58 -07003937 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003938 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003939 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003940 return -EINVAL;
3941
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003942 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3943 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3944 return -EINVAL;
3945
3946 req->sync.off = READ_ONCE(sqe->off);
3947 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003948 return 0;
3949}
3950
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003951static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003952{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003953 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003954 int ret;
3955
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003956 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003957 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003958 return -EAGAIN;
3959
Jens Axboe9adbd452019-12-20 08:45:55 -07003960 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003961 end > 0 ? end : LLONG_MAX,
3962 req->sync.flags & IORING_FSYNC_DATASYNC);
3963 if (ret < 0)
3964 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003965 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003966 return 0;
3967}
3968
Jens Axboed63d1b52019-12-10 10:38:56 -07003969static int io_fallocate_prep(struct io_kiocb *req,
3970 const struct io_uring_sqe *sqe)
3971{
3972 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3973 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003974 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3975 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003976
3977 req->sync.off = READ_ONCE(sqe->off);
3978 req->sync.len = READ_ONCE(sqe->addr);
3979 req->sync.mode = READ_ONCE(sqe->len);
3980 return 0;
3981}
3982
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003983static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003984{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003985 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003986
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003987 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003988 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003989 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003990 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3991 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003992 if (ret < 0)
3993 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003994 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003995 return 0;
3996}
3997
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003998static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003999{
Jens Axboef8748882020-01-08 17:47:02 -07004000 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004001 int ret;
4002
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004003 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004004 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004005 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004006 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004007
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004008 /* open.how should be already initialised */
4009 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004010 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004011
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004012 req->open.dfd = READ_ONCE(sqe->fd);
4013 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004014 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004015 if (IS_ERR(req->open.filename)) {
4016 ret = PTR_ERR(req->open.filename);
4017 req->open.filename = NULL;
4018 return ret;
4019 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004020 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004021 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004022 return 0;
4023}
4024
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004025static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4026{
4027 u64 flags, mode;
4028
Jens Axboe14587a462020-09-05 11:36:08 -06004029 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004030 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004031 mode = READ_ONCE(sqe->len);
4032 flags = READ_ONCE(sqe->open_flags);
4033 req->open.how = build_open_how(flags, mode);
4034 return __io_openat_prep(req, sqe);
4035}
4036
Jens Axboecebdb982020-01-08 17:59:24 -07004037static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4038{
4039 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004040 size_t len;
4041 int ret;
4042
Jens Axboe14587a462020-09-05 11:36:08 -06004043 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004044 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004045 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4046 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004047 if (len < OPEN_HOW_SIZE_VER0)
4048 return -EINVAL;
4049
4050 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4051 len);
4052 if (ret)
4053 return ret;
4054
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004055 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004056}
4057
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004058static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004059{
4060 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004061 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004062 bool nonblock_set;
4063 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004064 int ret;
4065
Jens Axboecebdb982020-01-08 17:59:24 -07004066 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004067 if (ret)
4068 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004069 nonblock_set = op.open_flag & O_NONBLOCK;
4070 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004071 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004072 /*
4073 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4074 * it'll always -EAGAIN
4075 */
4076 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4077 return -EAGAIN;
4078 op.lookup_flags |= LOOKUP_CACHED;
4079 op.open_flag |= O_NONBLOCK;
4080 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004081
Jens Axboe4022e7a2020-03-19 19:23:18 -06004082 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004083 if (ret < 0)
4084 goto err;
4085
4086 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004087 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004088 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4089 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004090 /*
4091 * We could hang on to this 'fd', but seems like marginal
4092 * gain for something that is now known to be a slower path.
4093 * So just put it, and we'll get a new one when we retry.
4094 */
4095 put_unused_fd(ret);
4096 return -EAGAIN;
4097 }
4098
Jens Axboe15b71ab2019-12-11 11:20:36 -07004099 if (IS_ERR(file)) {
4100 put_unused_fd(ret);
4101 ret = PTR_ERR(file);
4102 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004103 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004104 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004105 fsnotify_open(file);
4106 fd_install(ret, file);
4107 }
4108err:
4109 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004110 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004111 if (ret < 0)
4112 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004113 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004114 return 0;
4115}
4116
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004117static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004118{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004119 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004120}
4121
Jens Axboe067524e2020-03-02 16:32:28 -07004122static int io_remove_buffers_prep(struct io_kiocb *req,
4123 const struct io_uring_sqe *sqe)
4124{
4125 struct io_provide_buf *p = &req->pbuf;
4126 u64 tmp;
4127
4128 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4129 return -EINVAL;
4130
4131 tmp = READ_ONCE(sqe->fd);
4132 if (!tmp || tmp > USHRT_MAX)
4133 return -EINVAL;
4134
4135 memset(p, 0, sizeof(*p));
4136 p->nbufs = tmp;
4137 p->bgid = READ_ONCE(sqe->buf_group);
4138 return 0;
4139}
4140
4141static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4142 int bgid, unsigned nbufs)
4143{
4144 unsigned i = 0;
4145
4146 /* shouldn't happen */
4147 if (!nbufs)
4148 return 0;
4149
4150 /* the head kbuf is the list itself */
4151 while (!list_empty(&buf->list)) {
4152 struct io_buffer *nxt;
4153
4154 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4155 list_del(&nxt->list);
4156 kfree(nxt);
4157 if (++i == nbufs)
4158 return i;
4159 }
4160 i++;
4161 kfree(buf);
4162 idr_remove(&ctx->io_buffer_idr, bgid);
4163
4164 return i;
4165}
4166
Pavel Begunkov889fca72021-02-10 00:03:09 +00004167static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004168{
4169 struct io_provide_buf *p = &req->pbuf;
4170 struct io_ring_ctx *ctx = req->ctx;
4171 struct io_buffer *head;
4172 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004173 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004174
4175 io_ring_submit_lock(ctx, !force_nonblock);
4176
4177 lockdep_assert_held(&ctx->uring_lock);
4178
4179 ret = -ENOENT;
4180 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4181 if (head)
4182 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004183 if (ret < 0)
4184 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004185
4186 /* need to hold the lock to complete IOPOLL requests */
4187 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004188 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004189 io_ring_submit_unlock(ctx, !force_nonblock);
4190 } else {
4191 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004192 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004193 }
Jens Axboe067524e2020-03-02 16:32:28 -07004194 return 0;
4195}
4196
Jens Axboeddf0322d2020-02-23 16:41:33 -07004197static int io_provide_buffers_prep(struct io_kiocb *req,
4198 const struct io_uring_sqe *sqe)
4199{
4200 struct io_provide_buf *p = &req->pbuf;
4201 u64 tmp;
4202
4203 if (sqe->ioprio || sqe->rw_flags)
4204 return -EINVAL;
4205
4206 tmp = READ_ONCE(sqe->fd);
4207 if (!tmp || tmp > USHRT_MAX)
4208 return -E2BIG;
4209 p->nbufs = tmp;
4210 p->addr = READ_ONCE(sqe->addr);
4211 p->len = READ_ONCE(sqe->len);
4212
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004213 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004214 return -EFAULT;
4215
4216 p->bgid = READ_ONCE(sqe->buf_group);
4217 tmp = READ_ONCE(sqe->off);
4218 if (tmp > USHRT_MAX)
4219 return -E2BIG;
4220 p->bid = tmp;
4221 return 0;
4222}
4223
4224static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4225{
4226 struct io_buffer *buf;
4227 u64 addr = pbuf->addr;
4228 int i, bid = pbuf->bid;
4229
4230 for (i = 0; i < pbuf->nbufs; i++) {
4231 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4232 if (!buf)
4233 break;
4234
4235 buf->addr = addr;
4236 buf->len = pbuf->len;
4237 buf->bid = bid;
4238 addr += pbuf->len;
4239 bid++;
4240 if (!*head) {
4241 INIT_LIST_HEAD(&buf->list);
4242 *head = buf;
4243 } else {
4244 list_add_tail(&buf->list, &(*head)->list);
4245 }
4246 }
4247
4248 return i ? i : -ENOMEM;
4249}
4250
Pavel Begunkov889fca72021-02-10 00:03:09 +00004251static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004252{
4253 struct io_provide_buf *p = &req->pbuf;
4254 struct io_ring_ctx *ctx = req->ctx;
4255 struct io_buffer *head, *list;
4256 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004257 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004258
4259 io_ring_submit_lock(ctx, !force_nonblock);
4260
4261 lockdep_assert_held(&ctx->uring_lock);
4262
4263 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4264
4265 ret = io_add_buffers(p, &head);
4266 if (ret < 0)
4267 goto out;
4268
4269 if (!list) {
4270 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4271 GFP_KERNEL);
4272 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004273 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004274 goto out;
4275 }
4276 }
4277out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004278 if (ret < 0)
4279 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004280
4281 /* need to hold the lock to complete IOPOLL requests */
4282 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004283 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004284 io_ring_submit_unlock(ctx, !force_nonblock);
4285 } else {
4286 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004287 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004288 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004289 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290}
4291
Jens Axboe3e4827b2020-01-08 15:18:09 -07004292static int io_epoll_ctl_prep(struct io_kiocb *req,
4293 const struct io_uring_sqe *sqe)
4294{
4295#if defined(CONFIG_EPOLL)
4296 if (sqe->ioprio || sqe->buf_index)
4297 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004298 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004299 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004300
4301 req->epoll.epfd = READ_ONCE(sqe->fd);
4302 req->epoll.op = READ_ONCE(sqe->len);
4303 req->epoll.fd = READ_ONCE(sqe->off);
4304
4305 if (ep_op_has_event(req->epoll.op)) {
4306 struct epoll_event __user *ev;
4307
4308 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4309 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4310 return -EFAULT;
4311 }
4312
4313 return 0;
4314#else
4315 return -EOPNOTSUPP;
4316#endif
4317}
4318
Pavel Begunkov889fca72021-02-10 00:03:09 +00004319static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004320{
4321#if defined(CONFIG_EPOLL)
4322 struct io_epoll *ie = &req->epoll;
4323 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004324 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004325
4326 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4327 if (force_nonblock && ret == -EAGAIN)
4328 return -EAGAIN;
4329
4330 if (ret < 0)
4331 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004332 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004333 return 0;
4334#else
4335 return -EOPNOTSUPP;
4336#endif
4337}
4338
Jens Axboec1ca7572019-12-25 22:18:28 -07004339static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4340{
4341#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4342 if (sqe->ioprio || sqe->buf_index || sqe->off)
4343 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004344 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4345 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004346
4347 req->madvise.addr = READ_ONCE(sqe->addr);
4348 req->madvise.len = READ_ONCE(sqe->len);
4349 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4350 return 0;
4351#else
4352 return -EOPNOTSUPP;
4353#endif
4354}
4355
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004356static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004357{
4358#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4359 struct io_madvise *ma = &req->madvise;
4360 int ret;
4361
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004362 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004363 return -EAGAIN;
4364
Minchan Kim0726b012020-10-17 16:14:50 -07004365 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004366 if (ret < 0)
4367 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004368 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004369 return 0;
4370#else
4371 return -EOPNOTSUPP;
4372#endif
4373}
4374
Jens Axboe4840e412019-12-25 22:03:45 -07004375static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4376{
4377 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4378 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004379 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4380 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004381
4382 req->fadvise.offset = READ_ONCE(sqe->off);
4383 req->fadvise.len = READ_ONCE(sqe->len);
4384 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4385 return 0;
4386}
4387
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004388static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004389{
4390 struct io_fadvise *fa = &req->fadvise;
4391 int ret;
4392
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004393 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004394 switch (fa->advice) {
4395 case POSIX_FADV_NORMAL:
4396 case POSIX_FADV_RANDOM:
4397 case POSIX_FADV_SEQUENTIAL:
4398 break;
4399 default:
4400 return -EAGAIN;
4401 }
4402 }
Jens Axboe4840e412019-12-25 22:03:45 -07004403
4404 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4405 if (ret < 0)
4406 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004407 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004408 return 0;
4409}
4410
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004411static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4412{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004413 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004414 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004415 if (sqe->ioprio || sqe->buf_index)
4416 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004417 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004418 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004419
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004420 req->statx.dfd = READ_ONCE(sqe->fd);
4421 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004422 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004423 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4424 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004425
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004426 return 0;
4427}
4428
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004429static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004430{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004431 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004432 int ret;
4433
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004434 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004435 /* only need file table for an actual valid fd */
4436 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4437 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004438 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004439 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004440
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004441 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4442 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004443
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004444 if (ret < 0)
4445 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004446 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004447 return 0;
4448}
4449
Jens Axboeb5dba592019-12-11 14:02:38 -07004450static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4451{
Jens Axboe14587a462020-09-05 11:36:08 -06004452 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004453 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004454 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4455 sqe->rw_flags || sqe->buf_index)
4456 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004457 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004458 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004459
4460 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004461 return 0;
4462}
4463
Pavel Begunkov889fca72021-02-10 00:03:09 +00004464static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004465{
Jens Axboe9eac1902021-01-19 15:50:37 -07004466 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004467 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004468 struct fdtable *fdt;
4469 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004470 int ret;
4471
Jens Axboe9eac1902021-01-19 15:50:37 -07004472 file = NULL;
4473 ret = -EBADF;
4474 spin_lock(&files->file_lock);
4475 fdt = files_fdtable(files);
4476 if (close->fd >= fdt->max_fds) {
4477 spin_unlock(&files->file_lock);
4478 goto err;
4479 }
4480 file = fdt->fd[close->fd];
4481 if (!file) {
4482 spin_unlock(&files->file_lock);
4483 goto err;
4484 }
4485
4486 if (file->f_op == &io_uring_fops) {
4487 spin_unlock(&files->file_lock);
4488 file = NULL;
4489 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004490 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004491
4492 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004493 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004494 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004495 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004496 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004497
Jens Axboe9eac1902021-01-19 15:50:37 -07004498 ret = __close_fd_get_file(close->fd, &file);
4499 spin_unlock(&files->file_lock);
4500 if (ret < 0) {
4501 if (ret == -ENOENT)
4502 ret = -EBADF;
4503 goto err;
4504 }
4505
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004506 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004507 ret = filp_close(file, current->files);
4508err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004509 if (ret < 0)
4510 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004511 if (file)
4512 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004513 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004514 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004515}
4516
Jens Axboe3529d8c2019-12-19 18:24:38 -07004517static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004518{
4519 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004520
4521 if (!req->file)
4522 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004523
4524 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4525 return -EINVAL;
4526 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4527 return -EINVAL;
4528
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004529 req->sync.off = READ_ONCE(sqe->off);
4530 req->sync.len = READ_ONCE(sqe->len);
4531 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004532 return 0;
4533}
4534
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004535static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004536{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004537 int ret;
4538
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004539 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004540 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004541 return -EAGAIN;
4542
Jens Axboe9adbd452019-12-20 08:45:55 -07004543 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004544 req->sync.flags);
4545 if (ret < 0)
4546 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004547 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004548 return 0;
4549}
4550
YueHaibing469956e2020-03-04 15:53:52 +08004551#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004552static int io_setup_async_msg(struct io_kiocb *req,
4553 struct io_async_msghdr *kmsg)
4554{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004555 struct io_async_msghdr *async_msg = req->async_data;
4556
4557 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004558 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004559 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004560 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004561 return -ENOMEM;
4562 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004563 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004564 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004565 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004566 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004567 /* if were using fast_iov, set it to the new one */
4568 if (!async_msg->free_iov)
4569 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4570
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004571 return -EAGAIN;
4572}
4573
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004574static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4575 struct io_async_msghdr *iomsg)
4576{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004577 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004578 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004579 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004580 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004581}
4582
Jens Axboe3529d8c2019-12-19 18:24:38 -07004583static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004584{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004585 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004586 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004587 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004588
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004589 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4590 return -EINVAL;
4591
Jens Axboee47293f2019-12-20 08:58:21 -07004592 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004593 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004594 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004595
Jens Axboed8768362020-02-27 14:17:49 -07004596#ifdef CONFIG_COMPAT
4597 if (req->ctx->compat)
4598 sr->msg_flags |= MSG_CMSG_COMPAT;
4599#endif
4600
Jens Axboee8c2bc12020-08-15 18:44:09 -07004601 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004602 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004603 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004604 if (!ret)
4605 req->flags |= REQ_F_NEED_CLEANUP;
4606 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004607}
4608
Pavel Begunkov889fca72021-02-10 00:03:09 +00004609static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004610{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004611 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004612 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004613 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004614 int ret;
4615
Florent Revestdba4a922020-12-04 12:36:04 +01004616 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004617 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004618 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004619
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004620 kmsg = req->async_data;
4621 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004622 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004623 if (ret)
4624 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004625 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004626 }
4627
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 flags = req->sr_msg.msg_flags;
4629 if (flags & MSG_DONTWAIT)
4630 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004631 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004632 flags |= MSG_DONTWAIT;
4633
4634 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004635 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004636 return io_setup_async_msg(req, kmsg);
4637 if (ret == -ERESTARTSYS)
4638 ret = -EINTR;
4639
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004640 /* fast path, check for non-NULL to avoid function call */
4641 if (kmsg->free_iov)
4642 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004643 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004644 if (ret < 0)
4645 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004646 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004647 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004648}
4649
Pavel Begunkov889fca72021-02-10 00:03:09 +00004650static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004651{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004652 struct io_sr_msg *sr = &req->sr_msg;
4653 struct msghdr msg;
4654 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004655 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004656 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004657 int ret;
4658
Florent Revestdba4a922020-12-04 12:36:04 +01004659 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004660 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004661 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004662
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004663 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4664 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004665 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004666
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004667 msg.msg_name = NULL;
4668 msg.msg_control = NULL;
4669 msg.msg_controllen = 0;
4670 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004671
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004672 flags = req->sr_msg.msg_flags;
4673 if (flags & MSG_DONTWAIT)
4674 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004675 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004676 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004677
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004678 msg.msg_flags = flags;
4679 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004680 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004681 return -EAGAIN;
4682 if (ret == -ERESTARTSYS)
4683 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004684
Jens Axboe03b12302019-12-02 18:50:25 -07004685 if (ret < 0)
4686 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004687 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004688 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004689}
4690
Pavel Begunkov1400e692020-07-12 20:41:05 +03004691static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4692 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004693{
4694 struct io_sr_msg *sr = &req->sr_msg;
4695 struct iovec __user *uiov;
4696 size_t iov_len;
4697 int ret;
4698
Pavel Begunkov1400e692020-07-12 20:41:05 +03004699 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4700 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004701 if (ret)
4702 return ret;
4703
4704 if (req->flags & REQ_F_BUFFER_SELECT) {
4705 if (iov_len > 1)
4706 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004707 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004708 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004709 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004710 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004711 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004712 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004713 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004714 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004715 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004716 if (ret > 0)
4717 ret = 0;
4718 }
4719
4720 return ret;
4721}
4722
4723#ifdef CONFIG_COMPAT
4724static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004725 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004726{
4727 struct compat_msghdr __user *msg_compat;
4728 struct io_sr_msg *sr = &req->sr_msg;
4729 struct compat_iovec __user *uiov;
4730 compat_uptr_t ptr;
4731 compat_size_t len;
4732 int ret;
4733
Pavel Begunkov270a5942020-07-12 20:41:04 +03004734 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004735 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004736 &ptr, &len);
4737 if (ret)
4738 return ret;
4739
4740 uiov = compat_ptr(ptr);
4741 if (req->flags & REQ_F_BUFFER_SELECT) {
4742 compat_ssize_t clen;
4743
4744 if (len > 1)
4745 return -EINVAL;
4746 if (!access_ok(uiov, sizeof(*uiov)))
4747 return -EFAULT;
4748 if (__get_user(clen, &uiov->iov_len))
4749 return -EFAULT;
4750 if (clen < 0)
4751 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004752 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004753 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004754 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004755 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004756 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004757 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004758 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004759 if (ret < 0)
4760 return ret;
4761 }
4762
4763 return 0;
4764}
Jens Axboe03b12302019-12-02 18:50:25 -07004765#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004766
Pavel Begunkov1400e692020-07-12 20:41:05 +03004767static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4768 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004769{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004770 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004771
4772#ifdef CONFIG_COMPAT
4773 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004774 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004775#endif
4776
Pavel Begunkov1400e692020-07-12 20:41:05 +03004777 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004778}
4779
Jens Axboebcda7ba2020-02-23 16:42:51 -07004780static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004781 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004782{
4783 struct io_sr_msg *sr = &req->sr_msg;
4784 struct io_buffer *kbuf;
4785
Jens Axboebcda7ba2020-02-23 16:42:51 -07004786 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4787 if (IS_ERR(kbuf))
4788 return kbuf;
4789
4790 sr->kbuf = kbuf;
4791 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004792 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004793}
4794
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004795static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4796{
4797 return io_put_kbuf(req, req->sr_msg.kbuf);
4798}
4799
Jens Axboe3529d8c2019-12-19 18:24:38 -07004800static int io_recvmsg_prep(struct io_kiocb *req,
4801 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004802{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004803 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004804 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004805 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004806
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004807 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4808 return -EINVAL;
4809
Jens Axboe3529d8c2019-12-19 18:24:38 -07004810 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004811 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004812 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004813 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004814
Jens Axboed8768362020-02-27 14:17:49 -07004815#ifdef CONFIG_COMPAT
4816 if (req->ctx->compat)
4817 sr->msg_flags |= MSG_CMSG_COMPAT;
4818#endif
4819
Jens Axboee8c2bc12020-08-15 18:44:09 -07004820 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004821 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004822 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004823 if (!ret)
4824 req->flags |= REQ_F_NEED_CLEANUP;
4825 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004826}
4827
Pavel Begunkov889fca72021-02-10 00:03:09 +00004828static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004829{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004830 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004831 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004832 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004833 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004834 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004835 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004836
Florent Revestdba4a922020-12-04 12:36:04 +01004837 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004838 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004839 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004840
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004841 kmsg = req->async_data;
4842 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004843 ret = io_recvmsg_copy_hdr(req, &iomsg);
4844 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004845 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004846 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004847 }
4848
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004849 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004850 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004851 if (IS_ERR(kbuf))
4852 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004853 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004854 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4855 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004856 1, req->sr_msg.len);
4857 }
4858
4859 flags = req->sr_msg.msg_flags;
4860 if (flags & MSG_DONTWAIT)
4861 req->flags |= REQ_F_NOWAIT;
4862 else if (force_nonblock)
4863 flags |= MSG_DONTWAIT;
4864
4865 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4866 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004867 if (force_nonblock && ret == -EAGAIN)
4868 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004869 if (ret == -ERESTARTSYS)
4870 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004871
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004872 if (req->flags & REQ_F_BUFFER_SELECTED)
4873 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004874 /* fast path, check for non-NULL to avoid function call */
4875 if (kmsg->free_iov)
4876 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004877 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004878 if (ret < 0)
4879 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004880 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004881 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004882}
4883
Pavel Begunkov889fca72021-02-10 00:03:09 +00004884static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004885{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004886 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004887 struct io_sr_msg *sr = &req->sr_msg;
4888 struct msghdr msg;
4889 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004890 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004891 struct iovec iov;
4892 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004893 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004894 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004895
Florent Revestdba4a922020-12-04 12:36:04 +01004896 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004897 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004898 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004899
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004900 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004901 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004902 if (IS_ERR(kbuf))
4903 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004904 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004905 }
4906
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004907 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004908 if (unlikely(ret))
4909 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004910
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004911 msg.msg_name = NULL;
4912 msg.msg_control = NULL;
4913 msg.msg_controllen = 0;
4914 msg.msg_namelen = 0;
4915 msg.msg_iocb = NULL;
4916 msg.msg_flags = 0;
4917
4918 flags = req->sr_msg.msg_flags;
4919 if (flags & MSG_DONTWAIT)
4920 req->flags |= REQ_F_NOWAIT;
4921 else if (force_nonblock)
4922 flags |= MSG_DONTWAIT;
4923
4924 ret = sock_recvmsg(sock, &msg, flags);
4925 if (force_nonblock && ret == -EAGAIN)
4926 return -EAGAIN;
4927 if (ret == -ERESTARTSYS)
4928 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004929out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004930 if (req->flags & REQ_F_BUFFER_SELECTED)
4931 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004932 if (ret < 0)
4933 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004934 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004935 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004936}
4937
Jens Axboe3529d8c2019-12-19 18:24:38 -07004938static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004939{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004940 struct io_accept *accept = &req->accept;
4941
Jens Axboe14587a462020-09-05 11:36:08 -06004942 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004943 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004944 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004945 return -EINVAL;
4946
Jens Axboed55e5f52019-12-11 16:12:15 -07004947 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4948 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004949 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004950 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004951 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004952}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004953
Pavel Begunkov889fca72021-02-10 00:03:09 +00004954static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004955{
4956 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004957 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004958 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004959 int ret;
4960
Jiufei Xuee697dee2020-06-10 13:41:59 +08004961 if (req->file->f_flags & O_NONBLOCK)
4962 req->flags |= REQ_F_NOWAIT;
4963
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004964 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004965 accept->addr_len, accept->flags,
4966 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004967 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004968 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004969 if (ret < 0) {
4970 if (ret == -ERESTARTSYS)
4971 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004972 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004973 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004974 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004975 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004976}
4977
Jens Axboe3529d8c2019-12-19 18:24:38 -07004978static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004979{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004980 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004981 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004982
Jens Axboe14587a462020-09-05 11:36:08 -06004983 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004984 return -EINVAL;
4985 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4986 return -EINVAL;
4987
Jens Axboe3529d8c2019-12-19 18:24:38 -07004988 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4989 conn->addr_len = READ_ONCE(sqe->addr2);
4990
4991 if (!io)
4992 return 0;
4993
4994 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004995 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004996}
4997
Pavel Begunkov889fca72021-02-10 00:03:09 +00004998static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004999{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005000 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005001 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005002 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005003 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005004
Jens Axboee8c2bc12020-08-15 18:44:09 -07005005 if (req->async_data) {
5006 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005007 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005008 ret = move_addr_to_kernel(req->connect.addr,
5009 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005010 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005011 if (ret)
5012 goto out;
5013 io = &__io;
5014 }
5015
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005016 file_flags = force_nonblock ? O_NONBLOCK : 0;
5017
Jens Axboee8c2bc12020-08-15 18:44:09 -07005018 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005019 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005020 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005021 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005022 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005023 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005024 ret = -ENOMEM;
5025 goto out;
5026 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005027 io = req->async_data;
5028 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005029 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005030 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005031 if (ret == -ERESTARTSYS)
5032 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005033out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005034 if (ret < 0)
5035 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005036 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005037 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005038}
YueHaibing469956e2020-03-04 15:53:52 +08005039#else /* !CONFIG_NET */
5040static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5041{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005042 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005043}
5044
Pavel Begunkov889fca72021-02-10 00:03:09 +00005045static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005046{
YueHaibing469956e2020-03-04 15:53:52 +08005047 return -EOPNOTSUPP;
5048}
5049
Pavel Begunkov889fca72021-02-10 00:03:09 +00005050static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005051{
5052 return -EOPNOTSUPP;
5053}
5054
5055static int io_recvmsg_prep(struct io_kiocb *req,
5056 const struct io_uring_sqe *sqe)
5057{
5058 return -EOPNOTSUPP;
5059}
5060
Pavel Begunkov889fca72021-02-10 00:03:09 +00005061static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005062{
5063 return -EOPNOTSUPP;
5064}
5065
Pavel Begunkov889fca72021-02-10 00:03:09 +00005066static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005067{
5068 return -EOPNOTSUPP;
5069}
5070
5071static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5072{
5073 return -EOPNOTSUPP;
5074}
5075
Pavel Begunkov889fca72021-02-10 00:03:09 +00005076static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005077{
5078 return -EOPNOTSUPP;
5079}
5080
5081static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5082{
5083 return -EOPNOTSUPP;
5084}
5085
Pavel Begunkov889fca72021-02-10 00:03:09 +00005086static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005087{
5088 return -EOPNOTSUPP;
5089}
5090#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005091
Jens Axboed7718a92020-02-14 22:23:12 -07005092struct io_poll_table {
5093 struct poll_table_struct pt;
5094 struct io_kiocb *req;
5095 int error;
5096};
5097
Jens Axboed7718a92020-02-14 22:23:12 -07005098static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5099 __poll_t mask, task_work_func_t func)
5100{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005101 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005102
5103 /* for instances that support it check for an event match first: */
5104 if (mask && !(mask & poll->events))
5105 return 0;
5106
5107 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5108
5109 list_del_init(&poll->wait.entry);
5110
Jens Axboed7718a92020-02-14 22:23:12 -07005111 req->result = mask;
5112 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005113 percpu_ref_get(&req->ctx->refs);
5114
Jens Axboed7718a92020-02-14 22:23:12 -07005115 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005116 * If this fails, then the task is exiting. When a task exits, the
5117 * work gets canceled, so just cancel this request as well instead
5118 * of executing it. We can't safely execute it anyway, as we may not
5119 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005120 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005121 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005122 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005123 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005124 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005125 }
Jens Axboed7718a92020-02-14 22:23:12 -07005126 return 1;
5127}
5128
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005129static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5130 __acquires(&req->ctx->completion_lock)
5131{
5132 struct io_ring_ctx *ctx = req->ctx;
5133
5134 if (!req->result && !READ_ONCE(poll->canceled)) {
5135 struct poll_table_struct pt = { ._key = poll->events };
5136
5137 req->result = vfs_poll(req->file, &pt) & poll->events;
5138 }
5139
5140 spin_lock_irq(&ctx->completion_lock);
5141 if (!req->result && !READ_ONCE(poll->canceled)) {
5142 add_wait_queue(poll->head, &poll->wait);
5143 return true;
5144 }
5145
5146 return false;
5147}
5148
Jens Axboed4e7cd32020-08-15 11:44:50 -07005149static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005150{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005151 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005152 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005153 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005154 return req->apoll->double_poll;
5155}
5156
5157static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5158{
5159 if (req->opcode == IORING_OP_POLL_ADD)
5160 return &req->poll;
5161 return &req->apoll->poll;
5162}
5163
5164static void io_poll_remove_double(struct io_kiocb *req)
5165{
5166 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005167
5168 lockdep_assert_held(&req->ctx->completion_lock);
5169
5170 if (poll && poll->head) {
5171 struct wait_queue_head *head = poll->head;
5172
5173 spin_lock(&head->lock);
5174 list_del_init(&poll->wait.entry);
5175 if (poll->wait.private)
5176 refcount_dec(&req->refs);
5177 poll->head = NULL;
5178 spin_unlock(&head->lock);
5179 }
5180}
5181
5182static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5183{
5184 struct io_ring_ctx *ctx = req->ctx;
5185
Jens Axboed4e7cd32020-08-15 11:44:50 -07005186 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005187 req->poll.done = true;
5188 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5189 io_commit_cqring(ctx);
5190}
5191
Jens Axboe18bceab2020-05-15 11:56:54 -06005192static void io_poll_task_func(struct callback_head *cb)
5193{
5194 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005195 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005196 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005197
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005198 if (io_poll_rewait(req, &req->poll)) {
5199 spin_unlock_irq(&ctx->completion_lock);
5200 } else {
5201 hash_del(&req->hash_node);
5202 io_poll_complete(req, req->result, 0);
5203 spin_unlock_irq(&ctx->completion_lock);
5204
5205 nxt = io_put_req_find_next(req);
5206 io_cqring_ev_posted(ctx);
5207 if (nxt)
5208 __io_req_task_submit(nxt);
5209 }
5210
Jens Axboe6d816e02020-08-11 08:04:14 -06005211 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005212}
5213
5214static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5215 int sync, void *key)
5216{
5217 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005218 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005219 __poll_t mask = key_to_poll(key);
5220
5221 /* for instances that support it check for an event match first: */
5222 if (mask && !(mask & poll->events))
5223 return 0;
5224
Jens Axboe8706e042020-09-28 08:38:54 -06005225 list_del_init(&wait->entry);
5226
Jens Axboe807abcb2020-07-17 17:09:27 -06005227 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005228 bool done;
5229
Jens Axboe807abcb2020-07-17 17:09:27 -06005230 spin_lock(&poll->head->lock);
5231 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005232 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005233 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005234 /* make sure double remove sees this as being gone */
5235 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005236 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005237 if (!done) {
5238 /* use wait func handler, so it matches the rq type */
5239 poll->wait.func(&poll->wait, mode, sync, key);
5240 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005241 }
5242 refcount_dec(&req->refs);
5243 return 1;
5244}
5245
5246static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5247 wait_queue_func_t wake_func)
5248{
5249 poll->head = NULL;
5250 poll->done = false;
5251 poll->canceled = false;
5252 poll->events = events;
5253 INIT_LIST_HEAD(&poll->wait.entry);
5254 init_waitqueue_func_entry(&poll->wait, wake_func);
5255}
5256
5257static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005258 struct wait_queue_head *head,
5259 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005260{
5261 struct io_kiocb *req = pt->req;
5262
5263 /*
5264 * If poll->head is already set, it's because the file being polled
5265 * uses multiple waitqueues for poll handling (eg one for read, one
5266 * for write). Setup a separate io_poll_iocb if this happens.
5267 */
5268 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005269 struct io_poll_iocb *poll_one = poll;
5270
Jens Axboe18bceab2020-05-15 11:56:54 -06005271 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005272 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005273 pt->error = -EINVAL;
5274 return;
5275 }
5276 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5277 if (!poll) {
5278 pt->error = -ENOMEM;
5279 return;
5280 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005281 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005282 refcount_inc(&req->refs);
5283 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005284 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005285 }
5286
5287 pt->error = 0;
5288 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005289
5290 if (poll->events & EPOLLEXCLUSIVE)
5291 add_wait_queue_exclusive(head, &poll->wait);
5292 else
5293 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005294}
5295
5296static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5297 struct poll_table_struct *p)
5298{
5299 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005300 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005301
Jens Axboe807abcb2020-07-17 17:09:27 -06005302 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005303}
5304
Jens Axboed7718a92020-02-14 22:23:12 -07005305static void io_async_task_func(struct callback_head *cb)
5306{
5307 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5308 struct async_poll *apoll = req->apoll;
5309 struct io_ring_ctx *ctx = req->ctx;
5310
5311 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5312
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005313 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005314 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005315 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005316 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005317 }
5318
Jens Axboe31067252020-05-17 17:43:31 -06005319 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005320 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005321 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005322
Jens Axboed4e7cd32020-08-15 11:44:50 -07005323 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005324 spin_unlock_irq(&ctx->completion_lock);
5325
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005326 if (!READ_ONCE(apoll->poll.canceled))
5327 __io_req_task_submit(req);
5328 else
5329 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005330
Jens Axboe6d816e02020-08-11 08:04:14 -06005331 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005332 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005333 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005334}
5335
5336static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5337 void *key)
5338{
5339 struct io_kiocb *req = wait->private;
5340 struct io_poll_iocb *poll = &req->apoll->poll;
5341
5342 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5343 key_to_poll(key));
5344
5345 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5346}
5347
5348static void io_poll_req_insert(struct io_kiocb *req)
5349{
5350 struct io_ring_ctx *ctx = req->ctx;
5351 struct hlist_head *list;
5352
5353 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5354 hlist_add_head(&req->hash_node, list);
5355}
5356
5357static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5358 struct io_poll_iocb *poll,
5359 struct io_poll_table *ipt, __poll_t mask,
5360 wait_queue_func_t wake_func)
5361 __acquires(&ctx->completion_lock)
5362{
5363 struct io_ring_ctx *ctx = req->ctx;
5364 bool cancel = false;
5365
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005366 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005367 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005368 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005369 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005370
5371 ipt->pt._key = mask;
5372 ipt->req = req;
5373 ipt->error = -EINVAL;
5374
Jens Axboed7718a92020-02-14 22:23:12 -07005375 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5376
5377 spin_lock_irq(&ctx->completion_lock);
5378 if (likely(poll->head)) {
5379 spin_lock(&poll->head->lock);
5380 if (unlikely(list_empty(&poll->wait.entry))) {
5381 if (ipt->error)
5382 cancel = true;
5383 ipt->error = 0;
5384 mask = 0;
5385 }
5386 if (mask || ipt->error)
5387 list_del_init(&poll->wait.entry);
5388 else if (cancel)
5389 WRITE_ONCE(poll->canceled, true);
5390 else if (!poll->done) /* actually waiting for an event */
5391 io_poll_req_insert(req);
5392 spin_unlock(&poll->head->lock);
5393 }
5394
5395 return mask;
5396}
5397
5398static bool io_arm_poll_handler(struct io_kiocb *req)
5399{
5400 const struct io_op_def *def = &io_op_defs[req->opcode];
5401 struct io_ring_ctx *ctx = req->ctx;
5402 struct async_poll *apoll;
5403 struct io_poll_table ipt;
5404 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005405 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005406
5407 if (!req->file || !file_can_poll(req->file))
5408 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005409 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005410 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005411 if (def->pollin)
5412 rw = READ;
5413 else if (def->pollout)
5414 rw = WRITE;
5415 else
5416 return false;
5417 /* if we can't nonblock try, then no point in arming a poll handler */
5418 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005419 return false;
5420
5421 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5422 if (unlikely(!apoll))
5423 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005424 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005425
5426 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005427 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005428
Nathan Chancellor8755d972020-03-02 16:01:19 -07005429 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005430 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005431 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005432 if (def->pollout)
5433 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005434
5435 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5436 if ((req->opcode == IORING_OP_RECVMSG) &&
5437 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5438 mask &= ~POLLIN;
5439
Jens Axboed7718a92020-02-14 22:23:12 -07005440 mask |= POLLERR | POLLPRI;
5441
5442 ipt.pt._qproc = io_async_queue_proc;
5443
5444 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5445 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005446 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005447 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005448 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005449 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005450 kfree(apoll);
5451 return false;
5452 }
5453 spin_unlock_irq(&ctx->completion_lock);
5454 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5455 apoll->poll.events);
5456 return true;
5457}
5458
5459static bool __io_poll_remove_one(struct io_kiocb *req,
5460 struct io_poll_iocb *poll)
5461{
Jens Axboeb41e9852020-02-17 09:52:41 -07005462 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005463
5464 spin_lock(&poll->head->lock);
5465 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005466 if (!list_empty(&poll->wait.entry)) {
5467 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005468 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005469 }
5470 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005471 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005472 return do_complete;
5473}
5474
5475static bool io_poll_remove_one(struct io_kiocb *req)
5476{
5477 bool do_complete;
5478
Jens Axboed4e7cd32020-08-15 11:44:50 -07005479 io_poll_remove_double(req);
5480
Jens Axboed7718a92020-02-14 22:23:12 -07005481 if (req->opcode == IORING_OP_POLL_ADD) {
5482 do_complete = __io_poll_remove_one(req, &req->poll);
5483 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005484 struct async_poll *apoll = req->apoll;
5485
Jens Axboed7718a92020-02-14 22:23:12 -07005486 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005487 do_complete = __io_poll_remove_one(req, &apoll->poll);
5488 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005489 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005490 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005491 kfree(apoll);
5492 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005493 }
5494
Jens Axboeb41e9852020-02-17 09:52:41 -07005495 if (do_complete) {
5496 io_cqring_fill_event(req, -ECANCELED);
5497 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005498 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005499 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005500 }
5501
5502 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005503}
5504
Jens Axboe76e1b642020-09-26 15:05:03 -06005505/*
5506 * Returns true if we found and killed one or more poll requests
5507 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005508static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5509 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005510{
Jens Axboe78076bb2019-12-04 19:56:40 -07005511 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005512 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005513 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005514
5515 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005516 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5517 struct hlist_head *list;
5518
5519 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005520 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005521 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005522 posted += io_poll_remove_one(req);
5523 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005524 }
5525 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005526
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005527 if (posted)
5528 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005529
5530 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005531}
5532
Jens Axboe47f46762019-11-09 17:43:02 -07005533static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5534{
Jens Axboe78076bb2019-12-04 19:56:40 -07005535 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005536 struct io_kiocb *req;
5537
Jens Axboe78076bb2019-12-04 19:56:40 -07005538 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5539 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005540 if (sqe_addr != req->user_data)
5541 continue;
5542 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005543 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005544 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005545 }
5546
5547 return -ENOENT;
5548}
5549
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550static int io_poll_remove_prep(struct io_kiocb *req,
5551 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005552{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005553 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5554 return -EINVAL;
5555 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5556 sqe->poll_events)
5557 return -EINVAL;
5558
Pavel Begunkov018043b2020-10-27 23:17:18 +00005559 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005560 return 0;
5561}
5562
5563/*
5564 * Find a running poll command that matches one specified in sqe->addr,
5565 * and remove it if found.
5566 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005567static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005568{
5569 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005570 int ret;
5571
Jens Axboe221c5eb2019-01-17 09:41:58 -07005572 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005573 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005574 spin_unlock_irq(&ctx->completion_lock);
5575
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005576 if (ret < 0)
5577 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005578 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005579 return 0;
5580}
5581
Jens Axboe221c5eb2019-01-17 09:41:58 -07005582static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5583 void *key)
5584{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005585 struct io_kiocb *req = wait->private;
5586 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005587
Jens Axboed7718a92020-02-14 22:23:12 -07005588 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005589}
5590
Jens Axboe221c5eb2019-01-17 09:41:58 -07005591static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5592 struct poll_table_struct *p)
5593{
5594 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5595
Jens Axboee8c2bc12020-08-15 18:44:09 -07005596 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005597}
5598
Jens Axboe3529d8c2019-12-19 18:24:38 -07005599static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005600{
5601 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005602 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005603
5604 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5605 return -EINVAL;
5606 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5607 return -EINVAL;
5608
Jiufei Xue5769a352020-06-17 17:53:55 +08005609 events = READ_ONCE(sqe->poll32_events);
5610#ifdef __BIG_ENDIAN
5611 events = swahw32(events);
5612#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005613 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5614 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005615 return 0;
5616}
5617
Pavel Begunkov61e98202021-02-10 00:03:08 +00005618static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005619{
5620 struct io_poll_iocb *poll = &req->poll;
5621 struct io_ring_ctx *ctx = req->ctx;
5622 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005623 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005624
Jens Axboed7718a92020-02-14 22:23:12 -07005625 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005626
Jens Axboed7718a92020-02-14 22:23:12 -07005627 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5628 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005629
Jens Axboe8c838782019-03-12 15:48:16 -06005630 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005631 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005632 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005633 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005634 spin_unlock_irq(&ctx->completion_lock);
5635
Jens Axboe8c838782019-03-12 15:48:16 -06005636 if (mask) {
5637 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005638 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005639 }
Jens Axboe8c838782019-03-12 15:48:16 -06005640 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005641}
5642
Jens Axboe5262f562019-09-17 12:26:57 -06005643static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5644{
Jens Axboead8a48a2019-11-15 08:49:11 -07005645 struct io_timeout_data *data = container_of(timer,
5646 struct io_timeout_data, timer);
5647 struct io_kiocb *req = data->req;
5648 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005649 unsigned long flags;
5650
Jens Axboe5262f562019-09-17 12:26:57 -06005651 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005652 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005653 atomic_set(&req->ctx->cq_timeouts,
5654 atomic_read(&req->ctx->cq_timeouts) + 1);
5655
Jens Axboe78e19bb2019-11-06 15:21:34 -07005656 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005657 io_commit_cqring(ctx);
5658 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5659
5660 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005661 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005662 io_put_req(req);
5663 return HRTIMER_NORESTART;
5664}
5665
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005666static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5667 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005668{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005669 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005670 struct io_kiocb *req;
5671 int ret = -ENOENT;
5672
5673 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5674 if (user_data == req->user_data) {
5675 ret = 0;
5676 break;
5677 }
5678 }
5679
5680 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005681 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005682
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005683 io = req->async_data;
5684 ret = hrtimer_try_to_cancel(&io->timer);
5685 if (ret == -1)
5686 return ERR_PTR(-EALREADY);
5687 list_del_init(&req->timeout.list);
5688 return req;
5689}
5690
5691static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5692{
5693 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5694
5695 if (IS_ERR(req))
5696 return PTR_ERR(req);
5697
5698 req_set_fail_links(req);
5699 io_cqring_fill_event(req, -ECANCELED);
5700 io_put_req_deferred(req, 1);
5701 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005702}
5703
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005704static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5705 struct timespec64 *ts, enum hrtimer_mode mode)
5706{
5707 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5708 struct io_timeout_data *data;
5709
5710 if (IS_ERR(req))
5711 return PTR_ERR(req);
5712
5713 req->timeout.off = 0; /* noseq */
5714 data = req->async_data;
5715 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5716 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5717 data->timer.function = io_timeout_fn;
5718 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5719 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005720}
5721
Jens Axboe3529d8c2019-12-19 18:24:38 -07005722static int io_timeout_remove_prep(struct io_kiocb *req,
5723 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005724{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005725 struct io_timeout_rem *tr = &req->timeout_rem;
5726
Jens Axboeb29472e2019-12-17 18:50:29 -07005727 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5728 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005729 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5730 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005731 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005732 return -EINVAL;
5733
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005734 tr->addr = READ_ONCE(sqe->addr);
5735 tr->flags = READ_ONCE(sqe->timeout_flags);
5736 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5737 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5738 return -EINVAL;
5739 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5740 return -EFAULT;
5741 } else if (tr->flags) {
5742 /* timeout removal doesn't support flags */
5743 return -EINVAL;
5744 }
5745
Jens Axboeb29472e2019-12-17 18:50:29 -07005746 return 0;
5747}
5748
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005749static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5750{
5751 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5752 : HRTIMER_MODE_REL;
5753}
5754
Jens Axboe11365042019-10-16 09:08:32 -06005755/*
5756 * Remove or update an existing timeout command
5757 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005758static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005759{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005760 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005761 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005762 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005763
Jens Axboe11365042019-10-16 09:08:32 -06005764 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005765 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005766 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005767 else
5768 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5769 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005770
Jens Axboe47f46762019-11-09 17:43:02 -07005771 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005772 io_commit_cqring(ctx);
5773 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005774 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005775 if (ret < 0)
5776 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005777 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005778 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005779}
5780
Jens Axboe3529d8c2019-12-19 18:24:38 -07005781static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005782 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005783{
Jens Axboead8a48a2019-11-15 08:49:11 -07005784 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005785 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005786 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005787
Jens Axboead8a48a2019-11-15 08:49:11 -07005788 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005789 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005790 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005791 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005792 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005793 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005794 flags = READ_ONCE(sqe->timeout_flags);
5795 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005796 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005797
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005798 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005799
Jens Axboee8c2bc12020-08-15 18:44:09 -07005800 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005801 return -ENOMEM;
5802
Jens Axboee8c2bc12020-08-15 18:44:09 -07005803 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005804 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005805
5806 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005807 return -EFAULT;
5808
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005809 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005810 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5811 return 0;
5812}
5813
Pavel Begunkov61e98202021-02-10 00:03:08 +00005814static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005815{
Jens Axboead8a48a2019-11-15 08:49:11 -07005816 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005817 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005818 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005819 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005820
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005821 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005822
Jens Axboe5262f562019-09-17 12:26:57 -06005823 /*
5824 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005825 * timeout event to be satisfied. If it isn't set, then this is
5826 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005827 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005828 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005829 entry = ctx->timeout_list.prev;
5830 goto add;
5831 }
Jens Axboe5262f562019-09-17 12:26:57 -06005832
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005833 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5834 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005835
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005836 /* Update the last seq here in case io_flush_timeouts() hasn't.
5837 * This is safe because ->completion_lock is held, and submissions
5838 * and completions are never mixed in the same ->completion_lock section.
5839 */
5840 ctx->cq_last_tm_flush = tail;
5841
Jens Axboe5262f562019-09-17 12:26:57 -06005842 /*
5843 * Insertion sort, ensuring the first entry in the list is always
5844 * the one we need first.
5845 */
Jens Axboe5262f562019-09-17 12:26:57 -06005846 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005847 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5848 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005849
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005850 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005851 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005852 /* nxt.seq is behind @tail, otherwise would've been completed */
5853 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005854 break;
5855 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005856add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005857 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005858 data->timer.function = io_timeout_fn;
5859 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005860 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005861 return 0;
5862}
5863
Jens Axboe62755e32019-10-28 21:49:21 -06005864static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005865{
Jens Axboe62755e32019-10-28 21:49:21 -06005866 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005867
Jens Axboe62755e32019-10-28 21:49:21 -06005868 return req->user_data == (unsigned long) data;
5869}
5870
Jens Axboee977d6d2019-11-05 12:39:45 -07005871static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005872{
Jens Axboe62755e32019-10-28 21:49:21 -06005873 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005874 int ret = 0;
5875
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005876 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005877 switch (cancel_ret) {
5878 case IO_WQ_CANCEL_OK:
5879 ret = 0;
5880 break;
5881 case IO_WQ_CANCEL_RUNNING:
5882 ret = -EALREADY;
5883 break;
5884 case IO_WQ_CANCEL_NOTFOUND:
5885 ret = -ENOENT;
5886 break;
5887 }
5888
Jens Axboee977d6d2019-11-05 12:39:45 -07005889 return ret;
5890}
5891
Jens Axboe47f46762019-11-09 17:43:02 -07005892static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5893 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005894 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005895{
5896 unsigned long flags;
5897 int ret;
5898
5899 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5900 if (ret != -ENOENT) {
5901 spin_lock_irqsave(&ctx->completion_lock, flags);
5902 goto done;
5903 }
5904
5905 spin_lock_irqsave(&ctx->completion_lock, flags);
5906 ret = io_timeout_cancel(ctx, sqe_addr);
5907 if (ret != -ENOENT)
5908 goto done;
5909 ret = io_poll_cancel(ctx, sqe_addr);
5910done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005911 if (!ret)
5912 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005913 io_cqring_fill_event(req, ret);
5914 io_commit_cqring(ctx);
5915 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5916 io_cqring_ev_posted(ctx);
5917
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005918 if (ret < 0)
5919 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005920 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005921}
5922
Jens Axboe3529d8c2019-12-19 18:24:38 -07005923static int io_async_cancel_prep(struct io_kiocb *req,
5924 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005925{
Jens Axboefbf23842019-12-17 18:45:56 -07005926 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005927 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005928 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5929 return -EINVAL;
5930 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005931 return -EINVAL;
5932
Jens Axboefbf23842019-12-17 18:45:56 -07005933 req->cancel.addr = READ_ONCE(sqe->addr);
5934 return 0;
5935}
5936
Pavel Begunkov61e98202021-02-10 00:03:08 +00005937static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005938{
5939 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005940
Pavel Begunkov014db002020-03-03 21:33:12 +03005941 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005942 return 0;
5943}
5944
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005945static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005946 const struct io_uring_sqe *sqe)
5947{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005948 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5949 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005950 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5951 return -EINVAL;
5952 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005953 return -EINVAL;
5954
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005955 req->rsrc_update.offset = READ_ONCE(sqe->off);
5956 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5957 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005958 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005959 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005960 return 0;
5961}
5962
Pavel Begunkov889fca72021-02-10 00:03:09 +00005963static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005964{
5965 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005966 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005967 int ret;
5968
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005969 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005970 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005971
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005972 up.offset = req->rsrc_update.offset;
5973 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005974
5975 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005976 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005977 mutex_unlock(&ctx->uring_lock);
5978
5979 if (ret < 0)
5980 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005981 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005982 return 0;
5983}
5984
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005985static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005986{
Jens Axboed625c6e2019-12-17 19:53:05 -07005987 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005988 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005989 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005990 case IORING_OP_READV:
5991 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005992 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005993 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005994 case IORING_OP_WRITEV:
5995 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005996 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005997 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005998 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005999 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006000 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006001 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006002 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006003 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006004 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006005 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006006 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006007 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006008 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006009 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006010 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006011 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006012 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006013 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006014 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006015 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006016 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006017 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006018 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006019 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006020 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006021 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006022 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006023 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006024 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006025 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006026 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006027 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006028 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006029 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006030 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006031 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006032 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006033 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006034 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006035 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006036 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006037 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006038 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006039 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006040 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006041 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006042 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006043 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006044 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006045 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006046 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006047 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006048 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006049 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006050 case IORING_OP_SHUTDOWN:
6051 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006052 case IORING_OP_RENAMEAT:
6053 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006054 case IORING_OP_UNLINKAT:
6055 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006056 }
6057
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006058 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6059 req->opcode);
6060 return-EINVAL;
6061}
6062
Jens Axboedef596e2019-01-09 08:59:42 -07006063static int io_req_defer_prep(struct io_kiocb *req,
6064 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006065{
Jens Axboedef596e2019-01-09 08:59:42 -07006066 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006067 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006068 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006069 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006070 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006071}
6072
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006073static u32 io_get_sequence(struct io_kiocb *req)
6074{
6075 struct io_kiocb *pos;
6076 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006077 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006078
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006079 io_for_each_link(pos, req)
6080 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006081
6082 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6083 return total_submitted - nr_reqs;
6084}
6085
Jens Axboe3529d8c2019-12-19 18:24:38 -07006086static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087{
6088 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006089 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006090 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006091 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092
6093 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006094 if (likely(list_empty_careful(&ctx->defer_list) &&
6095 !(req->flags & REQ_F_IO_DRAIN)))
6096 return 0;
6097
6098 seq = io_get_sequence(req);
6099 /* Still a chance to pass the sequence check */
6100 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006101 return 0;
6102
Jens Axboee8c2bc12020-08-15 18:44:09 -07006103 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006104 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006105 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006106 return ret;
6107 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006108 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006109 de = kmalloc(sizeof(*de), GFP_KERNEL);
6110 if (!de)
6111 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006112
6113 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006114 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006115 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006116 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006117 io_queue_async_work(req);
6118 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006119 }
6120
6121 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006122 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006123 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006124 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006125 spin_unlock_irq(&ctx->completion_lock);
6126 return -EIOCBQUEUED;
6127}
Jens Axboeedafcce2019-01-09 09:16:05 -07006128
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006129static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006130{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006131 if (req->flags & REQ_F_BUFFER_SELECTED) {
6132 switch (req->opcode) {
6133 case IORING_OP_READV:
6134 case IORING_OP_READ_FIXED:
6135 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006136 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006137 break;
6138 case IORING_OP_RECVMSG:
6139 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006140 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006141 break;
6142 }
6143 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006144 }
6145
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006146 if (req->flags & REQ_F_NEED_CLEANUP) {
6147 switch (req->opcode) {
6148 case IORING_OP_READV:
6149 case IORING_OP_READ_FIXED:
6150 case IORING_OP_READ:
6151 case IORING_OP_WRITEV:
6152 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006153 case IORING_OP_WRITE: {
6154 struct io_async_rw *io = req->async_data;
6155 if (io->free_iovec)
6156 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006157 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006158 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006159 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006160 case IORING_OP_SENDMSG: {
6161 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006162
6163 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006164 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006165 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006166 case IORING_OP_SPLICE:
6167 case IORING_OP_TEE:
6168 io_put_file(req, req->splice.file_in,
6169 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6170 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006171 case IORING_OP_OPENAT:
6172 case IORING_OP_OPENAT2:
6173 if (req->open.filename)
6174 putname(req->open.filename);
6175 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006176 case IORING_OP_RENAMEAT:
6177 putname(req->rename.oldpath);
6178 putname(req->rename.newpath);
6179 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006180 case IORING_OP_UNLINKAT:
6181 putname(req->unlink.filename);
6182 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006183 }
6184 req->flags &= ~REQ_F_NEED_CLEANUP;
6185 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006186}
6187
Pavel Begunkov889fca72021-02-10 00:03:09 +00006188static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006189{
Jens Axboeedafcce2019-01-09 09:16:05 -07006190 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006191 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006192
Jens Axboed625c6e2019-12-17 19:53:05 -07006193 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006195 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006196 break;
6197 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006198 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006199 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006200 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201 break;
6202 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006204 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006205 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206 break;
6207 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006208 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 break;
6210 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006211 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212 break;
6213 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006214 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006215 break;
6216 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006217 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006218 break;
6219 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006220 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006221 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006222 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006223 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006224 break;
6225 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006226 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006227 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006228 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006229 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006230 break;
6231 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006232 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233 break;
6234 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006235 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006236 break;
6237 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006238 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006239 break;
6240 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006241 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006242 break;
6243 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006244 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006246 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006247 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006248 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006249 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006250 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006251 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006252 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006253 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006254 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006255 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006256 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006257 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006258 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006259 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006260 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006261 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006262 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006263 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006264 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006265 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006266 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006267 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006268 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006269 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006270 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006271 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006272 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006273 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006274 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006275 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006276 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006277 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006278 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006279 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006280 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006281 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006282 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006283 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006284 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006285 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006286 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006287 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006288 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006289 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006290 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006291 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006292 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006293 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294 default:
6295 ret = -EINVAL;
6296 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006297 }
6298
6299 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006300 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301
Jens Axboeb5325762020-05-19 21:20:27 -06006302 /* If the op doesn't have a file, we're not polling for it */
6303 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006304 const bool in_async = io_wq_current_is_worker();
6305
Jens Axboe11ba8202020-01-15 21:51:17 -07006306 /* workqueue context doesn't hold uring_lock, grab it now */
6307 if (in_async)
6308 mutex_lock(&ctx->uring_lock);
6309
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006310 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006311
6312 if (in_async)
6313 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 }
6315
6316 return 0;
6317}
6318
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006319static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006320{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006322 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006323 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006324
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006325 timeout = io_prep_linked_timeout(req);
6326 if (timeout)
6327 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006328
Jens Axboe4014d942021-01-19 15:53:54 -07006329 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006330 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006331
Jens Axboe561fb042019-10-24 07:25:42 -06006332 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006333 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006334 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006335 /*
6336 * We can get EAGAIN for polled IO even though we're
6337 * forcing a sync submission from here, since we can't
6338 * wait for request slots on the block side.
6339 */
6340 if (ret != -EAGAIN)
6341 break;
6342 cond_resched();
6343 } while (1);
6344 }
Jens Axboe31b51512019-01-18 22:56:34 -07006345
Jens Axboe561fb042019-10-24 07:25:42 -06006346 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006347 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006348
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006349 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6350 lock_ctx = req->ctx;
6351
6352 /*
6353 * io_iopoll_complete() does not hold completion_lock to
6354 * complete polled io, so here for polled io, we can not call
6355 * io_req_complete() directly, otherwise there maybe concurrent
6356 * access to cqring, defer_list, etc, which is not safe. Given
6357 * that io_iopoll_complete() is always called under uring_lock,
6358 * so here for polled io, we also get uring_lock to complete
6359 * it.
6360 */
6361 if (lock_ctx)
6362 mutex_lock(&lock_ctx->uring_lock);
6363
6364 req_set_fail_links(req);
6365 io_req_complete(req, ret);
6366
6367 if (lock_ctx)
6368 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006369 }
Jens Axboe31b51512019-01-18 22:56:34 -07006370}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371
Jens Axboe65e19f52019-10-26 07:20:21 -06006372static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6373 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006374{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006375 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006376
Jens Axboe05f3fb32019-12-09 11:22:50 -07006377 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006378 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006379}
6380
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006381static struct file *io_file_get(struct io_submit_state *state,
6382 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006383{
6384 struct io_ring_ctx *ctx = req->ctx;
6385 struct file *file;
6386
6387 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006388 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006389 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006390 fd = array_index_nospec(fd, ctx->nr_user_files);
6391 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006392 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006393 } else {
6394 trace_io_uring_file_get(ctx, fd);
6395 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006396 }
6397
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006398 if (file && unlikely(file->f_op == &io_uring_fops))
6399 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006400 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006401}
6402
Jens Axboe2665abf2019-11-05 12:40:47 -07006403static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6404{
Jens Axboead8a48a2019-11-15 08:49:11 -07006405 struct io_timeout_data *data = container_of(timer,
6406 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006407 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006408 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006409 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006410
6411 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006412 prev = req->timeout.head;
6413 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006414
6415 /*
6416 * We don't expect the list to be empty, that will only happen if we
6417 * race with the completion of the linked work.
6418 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006419 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006420 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006421 else
6422 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006423 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6424
6425 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006426 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006427 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006428 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006429 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006430 io_req_complete_post(req, -ETIME, 0);
6431 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006432 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006433 return HRTIMER_NORESTART;
6434}
6435
Jens Axboe7271ef32020-08-10 09:55:22 -06006436static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006437{
Jens Axboe76a46e02019-11-10 23:34:16 -07006438 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006439 * If the back reference is NULL, then our linked request finished
6440 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006441 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006442 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006443 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006444
Jens Axboead8a48a2019-11-15 08:49:11 -07006445 data->timer.function = io_link_timeout_fn;
6446 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6447 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006448 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006449}
6450
6451static void io_queue_linked_timeout(struct io_kiocb *req)
6452{
6453 struct io_ring_ctx *ctx = req->ctx;
6454
6455 spin_lock_irq(&ctx->completion_lock);
6456 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006457 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006458
Jens Axboe2665abf2019-11-05 12:40:47 -07006459 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006460 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006461}
6462
Jens Axboead8a48a2019-11-15 08:49:11 -07006463static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006464{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006465 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006466
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006467 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6468 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006469 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006470
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006471 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006472 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006473 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006474 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006475}
6476
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006477static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006478{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006479 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006480 const struct cred *old_creds = NULL;
Pavel Begunkov889fca72021-02-10 00:03:09 +00006481 int ret, issue_flags = IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006482
Pavel Begunkov889fca72021-02-10 00:03:09 +00006483 if (cs)
6484 issue_flags |= IO_URING_F_COMPLETE_DEFER;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006485again:
6486 linked_timeout = io_prep_linked_timeout(req);
6487
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006488 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6489 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006490 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006491 if (old_creds)
6492 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006493 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006494 old_creds = NULL; /* restored original creds */
6495 else
Jens Axboe98447d62020-10-14 10:48:51 -06006496 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006497 }
6498
Pavel Begunkov889fca72021-02-10 00:03:09 +00006499 ret = io_issue_sqe(req, issue_flags);
Jens Axboe491381ce2019-10-17 09:20:46 -06006500
6501 /*
6502 * We async punt it if the file wasn't marked NOWAIT, or if the file
6503 * doesn't support non-blocking read/write attempts
6504 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006505 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006506 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006507 /*
6508 * Queued up for async execution, worker will release
6509 * submit reference when the iocb is actually submitted.
6510 */
6511 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006512 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006513
Pavel Begunkovf063c542020-07-25 14:41:59 +03006514 if (linked_timeout)
6515 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006516 } else if (likely(!ret)) {
6517 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006518 if (req->flags & REQ_F_COMPLETE_INLINE) {
6519 list_add_tail(&req->compl.list, &cs->list);
6520 if (++cs->nr >= 32)
Pavel Begunkov9affd662021-01-19 13:32:46 +00006521 io_submit_flush_completions(cs);
6522 req = NULL;
6523 } else {
6524 req = io_put_req_find_next(req);
6525 }
6526
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006527 if (linked_timeout)
6528 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006529
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006530 if (req) {
6531 if (!(req->flags & REQ_F_FORCE_ASYNC))
6532 goto again;
6533 io_queue_async_work(req);
6534 }
6535 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006536 /* un-prep timeout, so it'll be killed as any other linked */
6537 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006538 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006539 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006540 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006541 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006542
Jens Axboe193155c2020-02-22 23:22:19 -07006543 if (old_creds)
6544 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006545}
6546
Jens Axboef13fad72020-06-22 09:34:30 -06006547static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6548 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006549{
6550 int ret;
6551
Jens Axboe3529d8c2019-12-19 18:24:38 -07006552 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006553 if (ret) {
6554 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006555fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006556 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006557 io_put_req(req);
6558 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006559 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006560 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006561 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006562 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006563 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006564 goto fail_req;
6565 }
Jens Axboece35a472019-12-17 08:04:44 -07006566 io_queue_async_work(req);
6567 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006568 if (sqe) {
6569 ret = io_req_prep(req, sqe);
6570 if (unlikely(ret))
6571 goto fail_req;
6572 }
6573 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006574 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006575}
6576
Jens Axboef13fad72020-06-22 09:34:30 -06006577static inline void io_queue_link_head(struct io_kiocb *req,
6578 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006579{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006580 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006581 io_put_req(req);
6582 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006583 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006584 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006585}
6586
Pavel Begunkov863e0562020-10-27 23:25:35 +00006587struct io_submit_link {
6588 struct io_kiocb *head;
6589 struct io_kiocb *last;
6590};
6591
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006592static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006593 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006594{
Jackie Liua197f662019-11-08 08:09:12 -07006595 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006596 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006597
Jens Axboe9e645e112019-05-10 16:07:28 -06006598 /*
6599 * If we already have a head request, queue this one for async
6600 * submittal once the head completes. If we don't have a head but
6601 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6602 * submitted sync once the chain is complete. If none of those
6603 * conditions are true (normal request), then just queue it.
6604 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006605 if (link->head) {
6606 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006607
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006608 /*
6609 * Taking sequential execution of a link, draining both sides
6610 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6611 * requests in the link. So, it drains the head and the
6612 * next after the link request. The last one is done via
6613 * drain_next flag to persist the effect across calls.
6614 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006615 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006616 head->flags |= REQ_F_IO_DRAIN;
6617 ctx->drain_next = 1;
6618 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006619 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006620 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006621 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006622 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006623 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006624 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006625 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006626 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006627 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006628
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006629 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006630 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006631 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006632 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006633 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006634 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006635 if (unlikely(ctx->drain_next)) {
6636 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006637 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006638 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006639 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006640 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006641 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006642 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006643 link->head = req;
6644 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006645 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006646 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006647 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006648 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006649
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006650 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006651}
6652
Jens Axboe9a56a232019-01-09 09:06:50 -07006653/*
6654 * Batched submission is done, ensure local IO is flushed out.
6655 */
6656static void io_submit_state_end(struct io_submit_state *state)
6657{
Jens Axboef13fad72020-06-22 09:34:30 -06006658 if (!list_empty(&state->comp.list))
6659 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006660 if (state->plug_started)
6661 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006662 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006663 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006664 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006665}
6666
6667/*
6668 * Start submission side cache.
6669 */
6670static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006671 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006672{
Jens Axboe27926b62020-10-28 09:33:23 -06006673 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006674 state->comp.nr = 0;
6675 INIT_LIST_HEAD(&state->comp.list);
6676 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006677 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006678 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006679 state->ios_left = max_ios;
6680}
6681
Jens Axboe2b188cc2019-01-07 10:46:33 -07006682static void io_commit_sqring(struct io_ring_ctx *ctx)
6683{
Hristo Venev75b28af2019-08-26 17:23:46 +00006684 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006685
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006686 /*
6687 * Ensure any loads from the SQEs are done at this point,
6688 * since once we write the new head, the application could
6689 * write new data to them.
6690 */
6691 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006692}
6693
6694/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006695 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006696 * that is mapped by userspace. This means that care needs to be taken to
6697 * ensure that reads are stable, as we cannot rely on userspace always
6698 * being a good citizen. If members of the sqe are validated and then later
6699 * used, it's important that those reads are done through READ_ONCE() to
6700 * prevent a re-load down the line.
6701 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006702static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006703{
Hristo Venev75b28af2019-08-26 17:23:46 +00006704 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006705 unsigned head;
6706
6707 /*
6708 * The cached sq head (or cq tail) serves two purposes:
6709 *
6710 * 1) allows us to batch the cost of updating the user visible
6711 * head updates.
6712 * 2) allows the kernel side to track the head on its own, even
6713 * though the application is the one updating it.
6714 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006715 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006716 if (likely(head < ctx->sq_entries))
6717 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006718
6719 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006720 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006721 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006722 return NULL;
6723}
6724
6725static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6726{
6727 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006728}
6729
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006730/*
6731 * Check SQE restrictions (opcode and flags).
6732 *
6733 * Returns 'true' if SQE is allowed, 'false' otherwise.
6734 */
6735static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6736 struct io_kiocb *req,
6737 unsigned int sqe_flags)
6738{
6739 if (!ctx->restricted)
6740 return true;
6741
6742 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6743 return false;
6744
6745 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6746 ctx->restrictions.sqe_flags_required)
6747 return false;
6748
6749 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6750 ctx->restrictions.sqe_flags_required))
6751 return false;
6752
6753 return true;
6754}
6755
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006756#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6757 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6758 IOSQE_BUFFER_SELECT)
6759
6760static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6761 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006762 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006763{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006764 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006765 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006766
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006767 req->opcode = READ_ONCE(sqe->opcode);
6768 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006769 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006770 req->file = NULL;
6771 req->ctx = ctx;
6772 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006773 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006774 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006775 /* one is dropped after submission, the other at completion */
6776 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006777 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006778 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006779
6780 if (unlikely(req->opcode >= IORING_OP_LAST))
6781 return -EINVAL;
6782
Jens Axboe28cea78a2020-09-14 10:51:17 -06006783 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006784 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006785
6786 sqe_flags = READ_ONCE(sqe->flags);
6787 /* enforce forwards compatibility on users */
6788 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6789 return -EINVAL;
6790
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006791 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6792 return -EACCES;
6793
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006794 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6795 !io_op_defs[req->opcode].buffer_select)
6796 return -EOPNOTSUPP;
6797
6798 id = READ_ONCE(sqe->personality);
6799 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006800 struct io_identity *iod;
6801
Jens Axboe1e6fa522020-10-15 08:46:24 -06006802 iod = idr_find(&ctx->personality_idr, id);
6803 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006804 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006805 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006806
6807 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006808 get_cred(iod->creds);
6809 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006810 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006811 }
6812
6813 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006814 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006815
Jens Axboe27926b62020-10-28 09:33:23 -06006816 /*
6817 * Plug now if we have more than 1 IO left after this, and the target
6818 * is potentially a read/write to block based storage.
6819 */
6820 if (!state->plug_started && state->ios_left > 1 &&
6821 io_op_defs[req->opcode].plug) {
6822 blk_start_plug(&state->plug);
6823 state->plug_started = true;
6824 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006825
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006826 ret = 0;
6827 if (io_op_defs[req->opcode].needs_file) {
6828 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006829
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006830 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006831 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006832 ret = -EBADF;
6833 }
6834
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006835 state->ios_left--;
6836 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006837}
6838
Jens Axboe0f212202020-09-13 13:09:39 -06006839static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006840{
Jens Axboeac8691c2020-06-01 08:30:41 -06006841 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006842 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006843 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006844
Jens Axboec4a2ed72019-11-21 21:01:26 -07006845 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006846 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006847 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006848 return -EBUSY;
6849 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006850
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006851 /* make sure SQ entry isn't read before tail */
6852 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006853
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006854 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6855 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006856
Jens Axboed8a6df12020-10-15 16:24:45 -06006857 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006858 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006859
Jens Axboe6c271ce2019-01-10 11:22:30 -07006860 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006861 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006862
Jens Axboe6c271ce2019-01-10 11:22:30 -07006863 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006864 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006865 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006866 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006867
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006868 sqe = io_get_sqe(ctx);
6869 if (unlikely(!sqe)) {
6870 io_consume_sqe(ctx);
6871 break;
6872 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006873 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006874 if (unlikely(!req)) {
6875 if (!submitted)
6876 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006877 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006878 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006879 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006880 /* will complete beyond this point, count as submitted */
6881 submitted++;
6882
Pavel Begunkov692d8362020-10-10 18:34:13 +01006883 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006884 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006885fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006886 io_put_req(req);
6887 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006888 break;
6889 }
6890
Jens Axboe354420f2020-01-08 18:55:15 -07006891 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006892 true, ctx->flags & IORING_SETUP_SQPOLL);
Jens Axboef13fad72020-06-22 09:34:30 -06006893 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006894 if (err)
6895 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006896 }
6897
Pavel Begunkov9466f432020-01-25 22:34:01 +03006898 if (unlikely(submitted != nr)) {
6899 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006900 struct io_uring_task *tctx = current->io_uring;
6901 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006902
Jens Axboed8a6df12020-10-15 16:24:45 -06006903 percpu_ref_put_many(&ctx->refs, unused);
6904 percpu_counter_sub(&tctx->inflight, unused);
6905 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006906 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006907 if (link.head)
6908 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006909 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006910
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006911 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6912 io_commit_sqring(ctx);
6913
Jens Axboe6c271ce2019-01-10 11:22:30 -07006914 return submitted;
6915}
6916
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006917static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6918{
6919 /* Tell userspace we may need a wakeup call */
6920 spin_lock_irq(&ctx->completion_lock);
6921 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6922 spin_unlock_irq(&ctx->completion_lock);
6923}
6924
6925static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6926{
6927 spin_lock_irq(&ctx->completion_lock);
6928 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6929 spin_unlock_irq(&ctx->completion_lock);
6930}
6931
Xiaoguang Wang08369242020-11-03 14:15:59 +08006932static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006933{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006934 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006935 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006936
Jens Axboec8d1ba52020-09-14 11:07:26 -06006937 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006938 /* if we're handling multiple rings, cap submit size for fairness */
6939 if (cap_entries && to_submit > 8)
6940 to_submit = 8;
6941
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006942 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6943 unsigned nr_events = 0;
6944
Xiaoguang Wang08369242020-11-03 14:15:59 +08006945 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006946 if (!list_empty(&ctx->iopoll_list))
6947 io_do_iopoll(ctx, &nr_events, 0);
6948
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006949 if (to_submit && !ctx->sqo_dead &&
6950 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006951 ret = io_submit_sqes(ctx, to_submit);
6952 mutex_unlock(&ctx->uring_lock);
6953 }
Jens Axboe90554202020-09-03 12:12:41 -06006954
6955 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6956 wake_up(&ctx->sqo_sq_wait);
6957
Xiaoguang Wang08369242020-11-03 14:15:59 +08006958 return ret;
6959}
6960
6961static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6962{
6963 struct io_ring_ctx *ctx;
6964 unsigned sq_thread_idle = 0;
6965
6966 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6967 if (sq_thread_idle < ctx->sq_thread_idle)
6968 sq_thread_idle = ctx->sq_thread_idle;
6969 }
6970
6971 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006972}
6973
Jens Axboe69fb2132020-09-14 11:16:23 -06006974static void io_sqd_init_new(struct io_sq_data *sqd)
6975{
6976 struct io_ring_ctx *ctx;
6977
6978 while (!list_empty(&sqd->ctx_new_list)) {
6979 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006980 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6981 complete(&ctx->sq_thread_comp);
6982 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006983
6984 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006985}
6986
Jens Axboe6c271ce2019-01-10 11:22:30 -07006987static int io_sq_thread(void *data)
6988{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006989 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006990 struct files_struct *old_files = current->files;
6991 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006992 const struct cred *old_cred = NULL;
6993 struct io_sq_data *sqd = data;
6994 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006995 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006996 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006997
Jens Axboe28cea78a2020-09-14 10:51:17 -06006998 task_lock(current);
6999 current->files = NULL;
7000 current->nsproxy = NULL;
7001 task_unlock(current);
7002
Jens Axboe69fb2132020-09-14 11:16:23 -06007003 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007004 int ret;
7005 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007006
7007 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007008 * Any changes to the sqd lists are synchronized through the
7009 * kthread parking. This synchronizes the thread vs users,
7010 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007011 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007012 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007013 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007014 /*
7015 * When sq thread is unparked, in case the previous park operation
7016 * comes from io_put_sq_data(), which means that sq thread is going
7017 * to be stopped, so here needs to have a check.
7018 */
7019 if (kthread_should_stop())
7020 break;
7021 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007022
Xiaoguang Wang08369242020-11-03 14:15:59 +08007023 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007024 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007025 timeout = jiffies + sqd->sq_thread_idle;
7026 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007027
Xiaoguang Wang08369242020-11-03 14:15:59 +08007028 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007029 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007030 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7031 if (current->cred != ctx->creds) {
7032 if (old_cred)
7033 revert_creds(old_cred);
7034 old_cred = override_creds(ctx->creds);
7035 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007036 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007037#ifdef CONFIG_AUDIT
7038 current->loginuid = ctx->loginuid;
7039 current->sessionid = ctx->sessionid;
7040#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007041
Xiaoguang Wang08369242020-11-03 14:15:59 +08007042 ret = __io_sq_thread(ctx, cap_entries);
7043 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7044 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007045
Jens Axboe28cea78a2020-09-14 10:51:17 -06007046 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007047 }
7048
Xiaoguang Wang08369242020-11-03 14:15:59 +08007049 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007050 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007051 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007052 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007053 if (sqt_spin)
7054 timeout = jiffies + sqd->sq_thread_idle;
7055 continue;
7056 }
7057
Xiaoguang Wang08369242020-11-03 14:15:59 +08007058 needs_sched = true;
7059 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7060 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7061 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7062 !list_empty_careful(&ctx->iopoll_list)) {
7063 needs_sched = false;
7064 break;
7065 }
7066 if (io_sqring_entries(ctx)) {
7067 needs_sched = false;
7068 break;
7069 }
7070 }
7071
Hao Xu8b28fdf2021-01-31 22:39:04 +08007072 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007073 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7074 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007075
Jens Axboe69fb2132020-09-14 11:16:23 -06007076 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007077 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7078 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007079 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007080
7081 finish_wait(&sqd->wait, &wait);
7082 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007083 }
7084
Jens Axboe4c6e2772020-07-01 11:29:10 -06007085 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007086 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007087
Dennis Zhou91d8f512020-09-16 13:41:05 -07007088 if (cur_css)
7089 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007090 if (old_cred)
7091 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007092
Jens Axboe28cea78a2020-09-14 10:51:17 -06007093 task_lock(current);
7094 current->files = old_files;
7095 current->nsproxy = old_nsproxy;
7096 task_unlock(current);
7097
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007098 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007099
Jens Axboe6c271ce2019-01-10 11:22:30 -07007100 return 0;
7101}
7102
Jens Axboebda52162019-09-24 13:47:15 -06007103struct io_wait_queue {
7104 struct wait_queue_entry wq;
7105 struct io_ring_ctx *ctx;
7106 unsigned to_wait;
7107 unsigned nr_timeouts;
7108};
7109
Pavel Begunkov6c503152021-01-04 20:36:36 +00007110static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007111{
7112 struct io_ring_ctx *ctx = iowq->ctx;
7113
7114 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007115 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007116 * started waiting. For timeouts, we always want to return to userspace,
7117 * regardless of event count.
7118 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007119 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007120 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7121}
7122
7123static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7124 int wake_flags, void *key)
7125{
7126 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7127 wq);
7128
Pavel Begunkov6c503152021-01-04 20:36:36 +00007129 /*
7130 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7131 * the task, and the next invocation will do it.
7132 */
7133 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7134 return autoremove_wake_function(curr, mode, wake_flags, key);
7135 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007136}
7137
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007138static int io_run_task_work_sig(void)
7139{
7140 if (io_run_task_work())
7141 return 1;
7142 if (!signal_pending(current))
7143 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007144 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7145 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007146 return -EINTR;
7147}
7148
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007149/* when returns >0, the caller should retry */
7150static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7151 struct io_wait_queue *iowq,
7152 signed long *timeout)
7153{
7154 int ret;
7155
7156 /* make sure we run task_work before checking for signals */
7157 ret = io_run_task_work_sig();
7158 if (ret || io_should_wake(iowq))
7159 return ret;
7160 /* let the caller flush overflows, retry */
7161 if (test_bit(0, &ctx->cq_check_overflow))
7162 return 1;
7163
7164 *timeout = schedule_timeout(*timeout);
7165 return !*timeout ? -ETIME : 1;
7166}
7167
Jens Axboe2b188cc2019-01-07 10:46:33 -07007168/*
7169 * Wait until events become available, if we don't already have some. The
7170 * application must reap them itself, as they reside on the shared cq ring.
7171 */
7172static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007173 const sigset_t __user *sig, size_t sigsz,
7174 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007175{
Jens Axboebda52162019-09-24 13:47:15 -06007176 struct io_wait_queue iowq = {
7177 .wq = {
7178 .private = current,
7179 .func = io_wake_function,
7180 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7181 },
7182 .ctx = ctx,
7183 .to_wait = min_events,
7184 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007185 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007186 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7187 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007188
Jens Axboeb41e9852020-02-17 09:52:41 -07007189 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007190 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7191 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007192 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007193 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007194 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007195 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007196
7197 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007198#ifdef CONFIG_COMPAT
7199 if (in_compat_syscall())
7200 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007201 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007202 else
7203#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007204 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007205
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206 if (ret)
7207 return ret;
7208 }
7209
Hao Xuc73ebb62020-11-03 10:54:37 +08007210 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007211 struct timespec64 ts;
7212
Hao Xuc73ebb62020-11-03 10:54:37 +08007213 if (get_timespec64(&ts, uts))
7214 return -EFAULT;
7215 timeout = timespec64_to_jiffies(&ts);
7216 }
7217
Jens Axboebda52162019-09-24 13:47:15 -06007218 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007219 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007220 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007221 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007222 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7223 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007224 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7225 finish_wait(&ctx->wait, &iowq.wq);
7226 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007227
Jens Axboeb7db41c2020-07-04 08:55:50 -06007228 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007229
Hristo Venev75b28af2019-08-26 17:23:46 +00007230 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007231}
7232
Jens Axboe6b063142019-01-10 22:13:58 -07007233static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7234{
7235#if defined(CONFIG_UNIX)
7236 if (ctx->ring_sock) {
7237 struct sock *sock = ctx->ring_sock->sk;
7238 struct sk_buff *skb;
7239
7240 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7241 kfree_skb(skb);
7242 }
7243#else
7244 int i;
7245
Jens Axboe65e19f52019-10-26 07:20:21 -06007246 for (i = 0; i < ctx->nr_user_files; i++) {
7247 struct file *file;
7248
7249 file = io_file_from_index(ctx, i);
7250 if (file)
7251 fput(file);
7252 }
Jens Axboe6b063142019-01-10 22:13:58 -07007253#endif
7254}
7255
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007256static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007257{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007258 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007259
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007260 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007261 complete(&data->done);
7262}
7263
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007264static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7265{
7266 spin_lock_bh(&ctx->rsrc_ref_lock);
7267}
7268
7269static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7270{
7271 spin_unlock_bh(&ctx->rsrc_ref_lock);
7272}
7273
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007274static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7275 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007276 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007277{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007278 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007279 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007280 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007281 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007282 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007283}
7284
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007285static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7286 struct io_ring_ctx *ctx,
7287 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007288{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007289 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007290 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007291
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007292 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007293 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007294 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007295 if (ref_node)
7296 percpu_ref_kill(&ref_node->refs);
7297
7298 percpu_ref_kill(&data->refs);
7299
7300 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007301 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007302 do {
7303 ret = wait_for_completion_interruptible(&data->done);
7304 if (!ret)
7305 break;
7306 ret = io_run_task_work_sig();
7307 if (ret < 0) {
7308 percpu_ref_resurrect(&data->refs);
7309 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007310 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007311 return ret;
7312 }
7313 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007314
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007315 destroy_fixed_rsrc_ref_node(backup_node);
7316 return 0;
7317}
7318
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007319static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7320{
7321 struct fixed_rsrc_data *data;
7322
7323 data = kzalloc(sizeof(*data), GFP_KERNEL);
7324 if (!data)
7325 return NULL;
7326
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007327 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007328 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7329 kfree(data);
7330 return NULL;
7331 }
7332 data->ctx = ctx;
7333 init_completion(&data->done);
7334 return data;
7335}
7336
7337static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7338{
7339 percpu_ref_exit(&data->refs);
7340 kfree(data->table);
7341 kfree(data);
7342}
7343
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007344static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7345{
7346 struct fixed_rsrc_data *data = ctx->file_data;
7347 struct fixed_rsrc_ref_node *backup_node;
7348 unsigned nr_tables, i;
7349 int ret;
7350
7351 if (!data)
7352 return -ENXIO;
7353 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7354 if (!backup_node)
7355 return -ENOMEM;
7356 init_fixed_file_ref_node(ctx, backup_node);
7357
7358 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7359 if (ret)
7360 return ret;
7361
Jens Axboe6b063142019-01-10 22:13:58 -07007362 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007363 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7364 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007365 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007366 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007367 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007368 ctx->nr_user_files = 0;
7369 return 0;
7370}
7371
Jens Axboe534ca6d2020-09-02 13:52:19 -06007372static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007373{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007374 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007375 /*
7376 * The park is a bit of a work-around, without it we get
7377 * warning spews on shutdown with SQPOLL set and affinity
7378 * set to a single CPU.
7379 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007380 if (sqd->thread) {
7381 kthread_park(sqd->thread);
7382 kthread_stop(sqd->thread);
7383 }
7384
7385 kfree(sqd);
7386 }
7387}
7388
Jens Axboeaa061652020-09-02 14:50:27 -06007389static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7390{
7391 struct io_ring_ctx *ctx_attach;
7392 struct io_sq_data *sqd;
7393 struct fd f;
7394
7395 f = fdget(p->wq_fd);
7396 if (!f.file)
7397 return ERR_PTR(-ENXIO);
7398 if (f.file->f_op != &io_uring_fops) {
7399 fdput(f);
7400 return ERR_PTR(-EINVAL);
7401 }
7402
7403 ctx_attach = f.file->private_data;
7404 sqd = ctx_attach->sq_data;
7405 if (!sqd) {
7406 fdput(f);
7407 return ERR_PTR(-EINVAL);
7408 }
7409
7410 refcount_inc(&sqd->refs);
7411 fdput(f);
7412 return sqd;
7413}
7414
Jens Axboe534ca6d2020-09-02 13:52:19 -06007415static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7416{
7417 struct io_sq_data *sqd;
7418
Jens Axboeaa061652020-09-02 14:50:27 -06007419 if (p->flags & IORING_SETUP_ATTACH_WQ)
7420 return io_attach_sq_data(p);
7421
Jens Axboe534ca6d2020-09-02 13:52:19 -06007422 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7423 if (!sqd)
7424 return ERR_PTR(-ENOMEM);
7425
7426 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007427 INIT_LIST_HEAD(&sqd->ctx_list);
7428 INIT_LIST_HEAD(&sqd->ctx_new_list);
7429 mutex_init(&sqd->ctx_lock);
7430 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007431 init_waitqueue_head(&sqd->wait);
7432 return sqd;
7433}
7434
Jens Axboe69fb2132020-09-14 11:16:23 -06007435static void io_sq_thread_unpark(struct io_sq_data *sqd)
7436 __releases(&sqd->lock)
7437{
7438 if (!sqd->thread)
7439 return;
7440 kthread_unpark(sqd->thread);
7441 mutex_unlock(&sqd->lock);
7442}
7443
7444static void io_sq_thread_park(struct io_sq_data *sqd)
7445 __acquires(&sqd->lock)
7446{
7447 if (!sqd->thread)
7448 return;
7449 mutex_lock(&sqd->lock);
7450 kthread_park(sqd->thread);
7451}
7452
Jens Axboe534ca6d2020-09-02 13:52:19 -06007453static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7454{
7455 struct io_sq_data *sqd = ctx->sq_data;
7456
7457 if (sqd) {
7458 if (sqd->thread) {
7459 /*
7460 * We may arrive here from the error branch in
7461 * io_sq_offload_create() where the kthread is created
7462 * without being waked up, thus wake it up now to make
7463 * sure the wait will complete.
7464 */
7465 wake_up_process(sqd->thread);
7466 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007467
7468 io_sq_thread_park(sqd);
7469 }
7470
7471 mutex_lock(&sqd->ctx_lock);
7472 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007473 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007474 mutex_unlock(&sqd->ctx_lock);
7475
Xiaoguang Wang08369242020-11-03 14:15:59 +08007476 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007477 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007478
7479 io_put_sq_data(sqd);
7480 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007481 }
7482}
7483
Jens Axboe6b063142019-01-10 22:13:58 -07007484static void io_finish_async(struct io_ring_ctx *ctx)
7485{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007486 io_sq_thread_stop(ctx);
7487
Jens Axboe561fb042019-10-24 07:25:42 -06007488 if (ctx->io_wq) {
7489 io_wq_destroy(ctx->io_wq);
7490 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007491 }
7492}
7493
7494#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007495/*
7496 * Ensure the UNIX gc is aware of our file set, so we are certain that
7497 * the io_uring can be safely unregistered on process exit, even if we have
7498 * loops in the file referencing.
7499 */
7500static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7501{
7502 struct sock *sk = ctx->ring_sock->sk;
7503 struct scm_fp_list *fpl;
7504 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007505 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007506
Jens Axboe6b063142019-01-10 22:13:58 -07007507 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7508 if (!fpl)
7509 return -ENOMEM;
7510
7511 skb = alloc_skb(0, GFP_KERNEL);
7512 if (!skb) {
7513 kfree(fpl);
7514 return -ENOMEM;
7515 }
7516
7517 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007518
Jens Axboe08a45172019-10-03 08:11:03 -06007519 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007520 fpl->user = get_uid(ctx->user);
7521 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007522 struct file *file = io_file_from_index(ctx, i + offset);
7523
7524 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007525 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007526 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007527 unix_inflight(fpl->user, fpl->fp[nr_files]);
7528 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007529 }
7530
Jens Axboe08a45172019-10-03 08:11:03 -06007531 if (nr_files) {
7532 fpl->max = SCM_MAX_FD;
7533 fpl->count = nr_files;
7534 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007535 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007536 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7537 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007538
Jens Axboe08a45172019-10-03 08:11:03 -06007539 for (i = 0; i < nr_files; i++)
7540 fput(fpl->fp[i]);
7541 } else {
7542 kfree_skb(skb);
7543 kfree(fpl);
7544 }
Jens Axboe6b063142019-01-10 22:13:58 -07007545
7546 return 0;
7547}
7548
7549/*
7550 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7551 * causes regular reference counting to break down. We rely on the UNIX
7552 * garbage collection to take care of this problem for us.
7553 */
7554static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7555{
7556 unsigned left, total;
7557 int ret = 0;
7558
7559 total = 0;
7560 left = ctx->nr_user_files;
7561 while (left) {
7562 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007563
7564 ret = __io_sqe_files_scm(ctx, this_files, total);
7565 if (ret)
7566 break;
7567 left -= this_files;
7568 total += this_files;
7569 }
7570
7571 if (!ret)
7572 return 0;
7573
7574 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007575 struct file *file = io_file_from_index(ctx, total);
7576
7577 if (file)
7578 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007579 total++;
7580 }
7581
7582 return ret;
7583}
7584#else
7585static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7586{
7587 return 0;
7588}
7589#endif
7590
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007591static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007592 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007593{
7594 int i;
7595
7596 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007597 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007598 unsigned this_files;
7599
7600 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7601 table->files = kcalloc(this_files, sizeof(struct file *),
7602 GFP_KERNEL);
7603 if (!table->files)
7604 break;
7605 nr_files -= this_files;
7606 }
7607
7608 if (i == nr_tables)
7609 return 0;
7610
7611 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007612 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007613 kfree(table->files);
7614 }
7615 return 1;
7616}
7617
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007618static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007619{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007620 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007621#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007622 struct sock *sock = ctx->ring_sock->sk;
7623 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7624 struct sk_buff *skb;
7625 int i;
7626
7627 __skb_queue_head_init(&list);
7628
7629 /*
7630 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7631 * remove this entry and rearrange the file array.
7632 */
7633 skb = skb_dequeue(head);
7634 while (skb) {
7635 struct scm_fp_list *fp;
7636
7637 fp = UNIXCB(skb).fp;
7638 for (i = 0; i < fp->count; i++) {
7639 int left;
7640
7641 if (fp->fp[i] != file)
7642 continue;
7643
7644 unix_notinflight(fp->user, fp->fp[i]);
7645 left = fp->count - 1 - i;
7646 if (left) {
7647 memmove(&fp->fp[i], &fp->fp[i + 1],
7648 left * sizeof(struct file *));
7649 }
7650 fp->count--;
7651 if (!fp->count) {
7652 kfree_skb(skb);
7653 skb = NULL;
7654 } else {
7655 __skb_queue_tail(&list, skb);
7656 }
7657 fput(file);
7658 file = NULL;
7659 break;
7660 }
7661
7662 if (!file)
7663 break;
7664
7665 __skb_queue_tail(&list, skb);
7666
7667 skb = skb_dequeue(head);
7668 }
7669
7670 if (skb_peek(&list)) {
7671 spin_lock_irq(&head->lock);
7672 while ((skb = __skb_dequeue(&list)) != NULL)
7673 __skb_queue_tail(head, skb);
7674 spin_unlock_irq(&head->lock);
7675 }
7676#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007677 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007678#endif
7679}
7680
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007681static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007682{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007683 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7684 struct io_ring_ctx *ctx = rsrc_data->ctx;
7685 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007686
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007687 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7688 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007689 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007690 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007691 }
7692
Xiaoguang Wang05589552020-03-31 14:05:18 +08007693 percpu_ref_exit(&ref_node->refs);
7694 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007695 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007696}
7697
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007698static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007699{
7700 struct io_ring_ctx *ctx;
7701 struct llist_node *node;
7702
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007703 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7704 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007705
7706 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007707 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007708 struct llist_node *next = node->next;
7709
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007710 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7711 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007712 node = next;
7713 }
7714}
7715
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007716static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7717 unsigned i)
7718{
7719 struct fixed_rsrc_table *table;
7720
7721 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7722 return &table->files[i & IORING_FILE_TABLE_MASK];
7723}
7724
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007725static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007726{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007727 struct fixed_rsrc_ref_node *ref_node;
7728 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007729 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007730 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007731 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007732
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007733 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7734 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007735 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007736
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007737 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007738 ref_node->done = true;
7739
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007740 while (!list_empty(&ctx->rsrc_ref_list)) {
7741 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007742 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007743 /* recycle ref nodes in order */
7744 if (!ref_node->done)
7745 break;
7746 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007747 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007748 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007749 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007750
7751 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007752 delay = 0;
7753
Jens Axboe4a38aed22020-05-14 17:21:15 -06007754 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007755 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007756 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007757 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007758}
7759
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007760static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007761 struct io_ring_ctx *ctx)
7762{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007763 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007764
7765 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7766 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007767 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007768
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007769 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007770 0, GFP_KERNEL)) {
7771 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007772 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007773 }
7774 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007775 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007776 ref_node->done = false;
7777 return ref_node;
7778}
7779
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007780static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7781 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007782{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007783 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007784 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007785}
7786
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007787static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007788{
7789 percpu_ref_exit(&ref_node->refs);
7790 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007791}
7792
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007793
Jens Axboe05f3fb32019-12-09 11:22:50 -07007794static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7795 unsigned nr_args)
7796{
7797 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007798 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007799 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007800 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007801 struct fixed_rsrc_ref_node *ref_node;
7802 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007803
7804 if (ctx->file_data)
7805 return -EBUSY;
7806 if (!nr_args)
7807 return -EINVAL;
7808 if (nr_args > IORING_MAX_FIXED_FILES)
7809 return -EMFILE;
7810
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007811 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007812 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007813 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007814 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007815
7816 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007817 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007818 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007819 if (!file_data->table)
7820 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007822 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007823 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007824
7825 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007826 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7827 ret = -EFAULT;
7828 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007829 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007830 /* allow sparse sets */
7831 if (fd == -1)
7832 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007833
Jens Axboe05f3fb32019-12-09 11:22:50 -07007834 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007835 ret = -EBADF;
7836 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007837 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007838
7839 /*
7840 * Don't allow io_uring instances to be registered. If UNIX
7841 * isn't enabled, then this causes a reference cycle and this
7842 * instance can never get freed. If UNIX is enabled we'll
7843 * handle it just fine, but there's still no point in allowing
7844 * a ring fd as it doesn't support regular read/write anyway.
7845 */
7846 if (file->f_op == &io_uring_fops) {
7847 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007848 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007849 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007850 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007851 }
7852
Jens Axboe05f3fb32019-12-09 11:22:50 -07007853 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007854 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007855 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007856 return ret;
7857 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007858
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007859 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007860 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007861 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007862 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007863 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007864 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007865
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007866 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007867 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007868out_fput:
7869 for (i = 0; i < ctx->nr_user_files; i++) {
7870 file = io_file_from_index(ctx, i);
7871 if (file)
7872 fput(file);
7873 }
7874 for (i = 0; i < nr_tables; i++)
7875 kfree(file_data->table[i].files);
7876 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007877out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007878 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007879 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007880 return ret;
7881}
7882
Jens Axboec3a31e62019-10-03 13:59:56 -06007883static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7884 int index)
7885{
7886#if defined(CONFIG_UNIX)
7887 struct sock *sock = ctx->ring_sock->sk;
7888 struct sk_buff_head *head = &sock->sk_receive_queue;
7889 struct sk_buff *skb;
7890
7891 /*
7892 * See if we can merge this file into an existing skb SCM_RIGHTS
7893 * file set. If there's no room, fall back to allocating a new skb
7894 * and filling it in.
7895 */
7896 spin_lock_irq(&head->lock);
7897 skb = skb_peek(head);
7898 if (skb) {
7899 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7900
7901 if (fpl->count < SCM_MAX_FD) {
7902 __skb_unlink(skb, head);
7903 spin_unlock_irq(&head->lock);
7904 fpl->fp[fpl->count] = get_file(file);
7905 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7906 fpl->count++;
7907 spin_lock_irq(&head->lock);
7908 __skb_queue_head(head, skb);
7909 } else {
7910 skb = NULL;
7911 }
7912 }
7913 spin_unlock_irq(&head->lock);
7914
7915 if (skb) {
7916 fput(file);
7917 return 0;
7918 }
7919
7920 return __io_sqe_files_scm(ctx, 1, index);
7921#else
7922 return 0;
7923#endif
7924}
7925
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007926static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007927{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007928 struct io_rsrc_put *prsrc;
7929 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007930
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007931 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7932 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007933 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007934
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007935 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007936 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007937
Hillf Dantona5318d32020-03-23 17:47:15 +08007938 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007939}
7940
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007941static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7942 struct file *file)
7943{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007944 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007945}
7946
Jens Axboe05f3fb32019-12-09 11:22:50 -07007947static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007948 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007949 unsigned nr_args)
7950{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007951 struct fixed_rsrc_data *data = ctx->file_data;
7952 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007953 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007954 __s32 __user *fds;
7955 int fd, i, err;
7956 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007957 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007958
Jens Axboe05f3fb32019-12-09 11:22:50 -07007959 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007960 return -EOVERFLOW;
7961 if (done > ctx->nr_user_files)
7962 return -EINVAL;
7963
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007964 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007965 if (!ref_node)
7966 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007967 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007968
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007969 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007970 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007971 err = 0;
7972 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7973 err = -EFAULT;
7974 break;
7975 }
noah4e0377a2021-01-26 15:23:28 -05007976 if (fd == IORING_REGISTER_FILES_SKIP)
7977 continue;
7978
Pavel Begunkov67973b92021-01-26 13:51:09 +00007979 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007980 file_slot = io_fixed_file_slot(ctx->file_data, i);
7981
7982 if (*file_slot) {
7983 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007984 if (err)
7985 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007986 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007987 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007988 }
7989 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007990 file = fget(fd);
7991 if (!file) {
7992 err = -EBADF;
7993 break;
7994 }
7995 /*
7996 * Don't allow io_uring instances to be registered. If
7997 * UNIX isn't enabled, then this causes a reference
7998 * cycle and this instance can never get freed. If UNIX
7999 * is enabled we'll handle it just fine, but there's
8000 * still no point in allowing a ring fd as it doesn't
8001 * support regular read/write anyway.
8002 */
8003 if (file->f_op == &io_uring_fops) {
8004 fput(file);
8005 err = -EBADF;
8006 break;
8007 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008008 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008009 if (err) {
8010 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008011 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008012 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008013 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008014 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008015 }
8016
Xiaoguang Wang05589552020-03-31 14:05:18 +08008017 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008018 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008019 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008020 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008021 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008022
8023 return done ? done : err;
8024}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008025
Jens Axboe05f3fb32019-12-09 11:22:50 -07008026static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8027 unsigned nr_args)
8028{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008029 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008030
8031 if (!ctx->file_data)
8032 return -ENXIO;
8033 if (!nr_args)
8034 return -EINVAL;
8035 if (copy_from_user(&up, arg, sizeof(up)))
8036 return -EFAULT;
8037 if (up.resv)
8038 return -EINVAL;
8039
8040 return __io_sqe_files_update(ctx, &up, nr_args);
8041}
Jens Axboec3a31e62019-10-03 13:59:56 -06008042
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008043static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008044{
8045 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8046
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008047 req = io_put_req_find_next(req);
8048 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008049}
8050
Pavel Begunkov24369c22020-01-28 03:15:48 +03008051static int io_init_wq_offload(struct io_ring_ctx *ctx,
8052 struct io_uring_params *p)
8053{
8054 struct io_wq_data data;
8055 struct fd f;
8056 struct io_ring_ctx *ctx_attach;
8057 unsigned int concurrency;
8058 int ret = 0;
8059
8060 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008061 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008062 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008063
8064 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8065 /* Do QD, or 4 * CPUS, whatever is smallest */
8066 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8067
8068 ctx->io_wq = io_wq_create(concurrency, &data);
8069 if (IS_ERR(ctx->io_wq)) {
8070 ret = PTR_ERR(ctx->io_wq);
8071 ctx->io_wq = NULL;
8072 }
8073 return ret;
8074 }
8075
8076 f = fdget(p->wq_fd);
8077 if (!f.file)
8078 return -EBADF;
8079
8080 if (f.file->f_op != &io_uring_fops) {
8081 ret = -EINVAL;
8082 goto out_fput;
8083 }
8084
8085 ctx_attach = f.file->private_data;
8086 /* @io_wq is protected by holding the fd */
8087 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8088 ret = -EINVAL;
8089 goto out_fput;
8090 }
8091
8092 ctx->io_wq = ctx_attach->io_wq;
8093out_fput:
8094 fdput(f);
8095 return ret;
8096}
8097
Jens Axboe0f212202020-09-13 13:09:39 -06008098static int io_uring_alloc_task_context(struct task_struct *task)
8099{
8100 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008101 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008102
8103 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8104 if (unlikely(!tctx))
8105 return -ENOMEM;
8106
Jens Axboed8a6df12020-10-15 16:24:45 -06008107 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8108 if (unlikely(ret)) {
8109 kfree(tctx);
8110 return ret;
8111 }
8112
Jens Axboe0f212202020-09-13 13:09:39 -06008113 xa_init(&tctx->xa);
8114 init_waitqueue_head(&tctx->wait);
8115 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008116 atomic_set(&tctx->in_idle, 0);
8117 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008118 io_init_identity(&tctx->__identity);
8119 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008120 task->io_uring = tctx;
8121 return 0;
8122}
8123
8124void __io_uring_free(struct task_struct *tsk)
8125{
8126 struct io_uring_task *tctx = tsk->io_uring;
8127
8128 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008129 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8130 if (tctx->identity != &tctx->__identity)
8131 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008132 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008133 kfree(tctx);
8134 tsk->io_uring = NULL;
8135}
8136
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008137static int io_sq_offload_create(struct io_ring_ctx *ctx,
8138 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008139{
8140 int ret;
8141
Jens Axboe6c271ce2019-01-10 11:22:30 -07008142 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008143 struct io_sq_data *sqd;
8144
Jens Axboe3ec482d2019-04-08 10:51:01 -06008145 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008146 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008147 goto err;
8148
Jens Axboe534ca6d2020-09-02 13:52:19 -06008149 sqd = io_get_sq_data(p);
8150 if (IS_ERR(sqd)) {
8151 ret = PTR_ERR(sqd);
8152 goto err;
8153 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008154
Jens Axboe534ca6d2020-09-02 13:52:19 -06008155 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008156 io_sq_thread_park(sqd);
8157 mutex_lock(&sqd->ctx_lock);
8158 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8159 mutex_unlock(&sqd->ctx_lock);
8160 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008161
Jens Axboe917257d2019-04-13 09:28:55 -06008162 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8163 if (!ctx->sq_thread_idle)
8164 ctx->sq_thread_idle = HZ;
8165
Jens Axboeaa061652020-09-02 14:50:27 -06008166 if (sqd->thread)
8167 goto done;
8168
Jens Axboe6c271ce2019-01-10 11:22:30 -07008169 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008170 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008171
Jens Axboe917257d2019-04-13 09:28:55 -06008172 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008173 if (cpu >= nr_cpu_ids)
8174 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008175 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008176 goto err;
8177
Jens Axboe69fb2132020-09-14 11:16:23 -06008178 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008179 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008180 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008181 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008182 "io_uring-sq");
8183 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008184 if (IS_ERR(sqd->thread)) {
8185 ret = PTR_ERR(sqd->thread);
8186 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008187 goto err;
8188 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008189 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008190 if (ret)
8191 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008192 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8193 /* Can't have SQ_AFF without SQPOLL */
8194 ret = -EINVAL;
8195 goto err;
8196 }
8197
Jens Axboeaa061652020-09-02 14:50:27 -06008198done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008199 ret = io_init_wq_offload(ctx, p);
8200 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008201 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008202
8203 return 0;
8204err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008205 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008206 return ret;
8207}
8208
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008209static void io_sq_offload_start(struct io_ring_ctx *ctx)
8210{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008211 struct io_sq_data *sqd = ctx->sq_data;
8212
8213 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8214 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008215}
8216
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008217static inline void __io_unaccount_mem(struct user_struct *user,
8218 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219{
8220 atomic_long_sub(nr_pages, &user->locked_vm);
8221}
8222
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008223static inline int __io_account_mem(struct user_struct *user,
8224 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008225{
8226 unsigned long page_limit, cur_pages, new_pages;
8227
8228 /* Don't allow more pages than we can safely lock */
8229 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8230
8231 do {
8232 cur_pages = atomic_long_read(&user->locked_vm);
8233 new_pages = cur_pages + nr_pages;
8234 if (new_pages > page_limit)
8235 return -ENOMEM;
8236 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8237 new_pages) != cur_pages);
8238
8239 return 0;
8240}
8241
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008242static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8243 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008244{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008245 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008246 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008247
Jens Axboe2aede0e2020-09-14 10:45:53 -06008248 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008249 if (acct == ACCT_LOCKED) {
8250 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008251 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008252 mmap_write_unlock(ctx->mm_account);
8253 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008254 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008255 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008256 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008257}
8258
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008259static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8260 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008261{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008262 int ret;
8263
8264 if (ctx->limit_mem) {
8265 ret = __io_account_mem(ctx->user, nr_pages);
8266 if (ret)
8267 return ret;
8268 }
8269
Jens Axboe2aede0e2020-09-14 10:45:53 -06008270 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008271 if (acct == ACCT_LOCKED) {
8272 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008273 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008274 mmap_write_unlock(ctx->mm_account);
8275 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008276 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008277 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008278 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008279
8280 return 0;
8281}
8282
Jens Axboe2b188cc2019-01-07 10:46:33 -07008283static void io_mem_free(void *ptr)
8284{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008285 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008286
Mark Rutland52e04ef2019-04-30 17:30:21 +01008287 if (!ptr)
8288 return;
8289
8290 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008291 if (put_page_testzero(page))
8292 free_compound_page(page);
8293}
8294
8295static void *io_mem_alloc(size_t size)
8296{
8297 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8298 __GFP_NORETRY;
8299
8300 return (void *) __get_free_pages(gfp_flags, get_order(size));
8301}
8302
Hristo Venev75b28af2019-08-26 17:23:46 +00008303static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8304 size_t *sq_offset)
8305{
8306 struct io_rings *rings;
8307 size_t off, sq_array_size;
8308
8309 off = struct_size(rings, cqes, cq_entries);
8310 if (off == SIZE_MAX)
8311 return SIZE_MAX;
8312
8313#ifdef CONFIG_SMP
8314 off = ALIGN(off, SMP_CACHE_BYTES);
8315 if (off == 0)
8316 return SIZE_MAX;
8317#endif
8318
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008319 if (sq_offset)
8320 *sq_offset = off;
8321
Hristo Venev75b28af2019-08-26 17:23:46 +00008322 sq_array_size = array_size(sizeof(u32), sq_entries);
8323 if (sq_array_size == SIZE_MAX)
8324 return SIZE_MAX;
8325
8326 if (check_add_overflow(off, sq_array_size, &off))
8327 return SIZE_MAX;
8328
Hristo Venev75b28af2019-08-26 17:23:46 +00008329 return off;
8330}
8331
Jens Axboe2b188cc2019-01-07 10:46:33 -07008332static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8333{
Hristo Venev75b28af2019-08-26 17:23:46 +00008334 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008335
Hristo Venev75b28af2019-08-26 17:23:46 +00008336 pages = (size_t)1 << get_order(
8337 rings_size(sq_entries, cq_entries, NULL));
8338 pages += (size_t)1 << get_order(
8339 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008340
Hristo Venev75b28af2019-08-26 17:23:46 +00008341 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008342}
8343
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008344static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008345{
8346 int i, j;
8347
8348 if (!ctx->user_bufs)
8349 return -ENXIO;
8350
8351 for (i = 0; i < ctx->nr_user_bufs; i++) {
8352 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8353
8354 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008355 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008356
Jens Axboede293932020-09-17 16:19:16 -06008357 if (imu->acct_pages)
8358 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008359 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008360 imu->nr_bvecs = 0;
8361 }
8362
8363 kfree(ctx->user_bufs);
8364 ctx->user_bufs = NULL;
8365 ctx->nr_user_bufs = 0;
8366 return 0;
8367}
8368
8369static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8370 void __user *arg, unsigned index)
8371{
8372 struct iovec __user *src;
8373
8374#ifdef CONFIG_COMPAT
8375 if (ctx->compat) {
8376 struct compat_iovec __user *ciovs;
8377 struct compat_iovec ciov;
8378
8379 ciovs = (struct compat_iovec __user *) arg;
8380 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8381 return -EFAULT;
8382
Jens Axboed55e5f52019-12-11 16:12:15 -07008383 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008384 dst->iov_len = ciov.iov_len;
8385 return 0;
8386 }
8387#endif
8388 src = (struct iovec __user *) arg;
8389 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8390 return -EFAULT;
8391 return 0;
8392}
8393
Jens Axboede293932020-09-17 16:19:16 -06008394/*
8395 * Not super efficient, but this is just a registration time. And we do cache
8396 * the last compound head, so generally we'll only do a full search if we don't
8397 * match that one.
8398 *
8399 * We check if the given compound head page has already been accounted, to
8400 * avoid double accounting it. This allows us to account the full size of the
8401 * page, not just the constituent pages of a huge page.
8402 */
8403static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8404 int nr_pages, struct page *hpage)
8405{
8406 int i, j;
8407
8408 /* check current page array */
8409 for (i = 0; i < nr_pages; i++) {
8410 if (!PageCompound(pages[i]))
8411 continue;
8412 if (compound_head(pages[i]) == hpage)
8413 return true;
8414 }
8415
8416 /* check previously registered pages */
8417 for (i = 0; i < ctx->nr_user_bufs; i++) {
8418 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8419
8420 for (j = 0; j < imu->nr_bvecs; j++) {
8421 if (!PageCompound(imu->bvec[j].bv_page))
8422 continue;
8423 if (compound_head(imu->bvec[j].bv_page) == hpage)
8424 return true;
8425 }
8426 }
8427
8428 return false;
8429}
8430
8431static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8432 int nr_pages, struct io_mapped_ubuf *imu,
8433 struct page **last_hpage)
8434{
8435 int i, ret;
8436
8437 for (i = 0; i < nr_pages; i++) {
8438 if (!PageCompound(pages[i])) {
8439 imu->acct_pages++;
8440 } else {
8441 struct page *hpage;
8442
8443 hpage = compound_head(pages[i]);
8444 if (hpage == *last_hpage)
8445 continue;
8446 *last_hpage = hpage;
8447 if (headpage_already_acct(ctx, pages, i, hpage))
8448 continue;
8449 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8450 }
8451 }
8452
8453 if (!imu->acct_pages)
8454 return 0;
8455
8456 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8457 if (ret)
8458 imu->acct_pages = 0;
8459 return ret;
8460}
8461
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008462static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8463 struct io_mapped_ubuf *imu,
8464 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008465{
8466 struct vm_area_struct **vmas = NULL;
8467 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008468 unsigned long off, start, end, ubuf;
8469 size_t size;
8470 int ret, pret, nr_pages, i;
8471
8472 ubuf = (unsigned long) iov->iov_base;
8473 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8474 start = ubuf >> PAGE_SHIFT;
8475 nr_pages = end - start;
8476
8477 ret = -ENOMEM;
8478
8479 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8480 if (!pages)
8481 goto done;
8482
8483 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8484 GFP_KERNEL);
8485 if (!vmas)
8486 goto done;
8487
8488 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8489 GFP_KERNEL);
8490 if (!imu->bvec)
8491 goto done;
8492
8493 ret = 0;
8494 mmap_read_lock(current->mm);
8495 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8496 pages, vmas);
8497 if (pret == nr_pages) {
8498 /* don't support file backed memory */
8499 for (i = 0; i < nr_pages; i++) {
8500 struct vm_area_struct *vma = vmas[i];
8501
8502 if (vma->vm_file &&
8503 !is_file_hugepages(vma->vm_file)) {
8504 ret = -EOPNOTSUPP;
8505 break;
8506 }
8507 }
8508 } else {
8509 ret = pret < 0 ? pret : -EFAULT;
8510 }
8511 mmap_read_unlock(current->mm);
8512 if (ret) {
8513 /*
8514 * if we did partial map, or found file backed vmas,
8515 * release any pages we did get
8516 */
8517 if (pret > 0)
8518 unpin_user_pages(pages, pret);
8519 kvfree(imu->bvec);
8520 goto done;
8521 }
8522
8523 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8524 if (ret) {
8525 unpin_user_pages(pages, pret);
8526 kvfree(imu->bvec);
8527 goto done;
8528 }
8529
8530 off = ubuf & ~PAGE_MASK;
8531 size = iov->iov_len;
8532 for (i = 0; i < nr_pages; i++) {
8533 size_t vec_len;
8534
8535 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8536 imu->bvec[i].bv_page = pages[i];
8537 imu->bvec[i].bv_len = vec_len;
8538 imu->bvec[i].bv_offset = off;
8539 off = 0;
8540 size -= vec_len;
8541 }
8542 /* store original address for later verification */
8543 imu->ubuf = ubuf;
8544 imu->len = iov->iov_len;
8545 imu->nr_bvecs = nr_pages;
8546 ret = 0;
8547done:
8548 kvfree(pages);
8549 kvfree(vmas);
8550 return ret;
8551}
8552
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008553static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008554{
Jens Axboeedafcce2019-01-09 09:16:05 -07008555 if (ctx->user_bufs)
8556 return -EBUSY;
8557 if (!nr_args || nr_args > UIO_MAXIOV)
8558 return -EINVAL;
8559
8560 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8561 GFP_KERNEL);
8562 if (!ctx->user_bufs)
8563 return -ENOMEM;
8564
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008565 return 0;
8566}
8567
8568static int io_buffer_validate(struct iovec *iov)
8569{
8570 /*
8571 * Don't impose further limits on the size and buffer
8572 * constraints here, we'll -EINVAL later when IO is
8573 * submitted if they are wrong.
8574 */
8575 if (!iov->iov_base || !iov->iov_len)
8576 return -EFAULT;
8577
8578 /* arbitrary limit, but we need something */
8579 if (iov->iov_len > SZ_1G)
8580 return -EFAULT;
8581
8582 return 0;
8583}
8584
8585static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8586 unsigned int nr_args)
8587{
8588 int i, ret;
8589 struct iovec iov;
8590 struct page *last_hpage = NULL;
8591
8592 ret = io_buffers_map_alloc(ctx, nr_args);
8593 if (ret)
8594 return ret;
8595
Jens Axboeedafcce2019-01-09 09:16:05 -07008596 for (i = 0; i < nr_args; i++) {
8597 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008598
8599 ret = io_copy_iov(ctx, &iov, arg, i);
8600 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008601 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008602
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008603 ret = io_buffer_validate(&iov);
8604 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008605 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008606
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008607 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8608 if (ret)
8609 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008610
8611 ctx->nr_user_bufs++;
8612 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008613
8614 if (ret)
8615 io_sqe_buffers_unregister(ctx);
8616
Jens Axboeedafcce2019-01-09 09:16:05 -07008617 return ret;
8618}
8619
Jens Axboe9b402842019-04-11 11:45:41 -06008620static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8621{
8622 __s32 __user *fds = arg;
8623 int fd;
8624
8625 if (ctx->cq_ev_fd)
8626 return -EBUSY;
8627
8628 if (copy_from_user(&fd, fds, sizeof(*fds)))
8629 return -EFAULT;
8630
8631 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8632 if (IS_ERR(ctx->cq_ev_fd)) {
8633 int ret = PTR_ERR(ctx->cq_ev_fd);
8634 ctx->cq_ev_fd = NULL;
8635 return ret;
8636 }
8637
8638 return 0;
8639}
8640
8641static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8642{
8643 if (ctx->cq_ev_fd) {
8644 eventfd_ctx_put(ctx->cq_ev_fd);
8645 ctx->cq_ev_fd = NULL;
8646 return 0;
8647 }
8648
8649 return -ENXIO;
8650}
8651
Jens Axboe5a2e7452020-02-23 16:23:11 -07008652static int __io_destroy_buffers(int id, void *p, void *data)
8653{
8654 struct io_ring_ctx *ctx = data;
8655 struct io_buffer *buf = p;
8656
Jens Axboe067524e2020-03-02 16:32:28 -07008657 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008658 return 0;
8659}
8660
8661static void io_destroy_buffers(struct io_ring_ctx *ctx)
8662{
8663 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8664 idr_destroy(&ctx->io_buffer_idr);
8665}
8666
Jens Axboe2b188cc2019-01-07 10:46:33 -07008667static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8668{
Jens Axboe6b063142019-01-10 22:13:58 -07008669 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008670 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008671
8672 if (ctx->sqo_task) {
8673 put_task_struct(ctx->sqo_task);
8674 ctx->sqo_task = NULL;
8675 mmdrop(ctx->mm_account);
8676 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008677 }
Jens Axboedef596e2019-01-09 08:59:42 -07008678
Dennis Zhou91d8f512020-09-16 13:41:05 -07008679#ifdef CONFIG_BLK_CGROUP
8680 if (ctx->sqo_blkcg_css)
8681 css_put(ctx->sqo_blkcg_css);
8682#endif
8683
Jens Axboe6b063142019-01-10 22:13:58 -07008684 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008685 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008686 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008687 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008688
Jens Axboe2b188cc2019-01-07 10:46:33 -07008689#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008690 if (ctx->ring_sock) {
8691 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008692 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008693 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008694#endif
8695
Hristo Venev75b28af2019-08-26 17:23:46 +00008696 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008697 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008698
8699 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008700 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008701 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008702 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008703 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008704 kfree(ctx);
8705}
8706
8707static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8708{
8709 struct io_ring_ctx *ctx = file->private_data;
8710 __poll_t mask = 0;
8711
8712 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008713 /*
8714 * synchronizes with barrier from wq_has_sleeper call in
8715 * io_commit_cqring
8716 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008717 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008718 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008719 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008720 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8721 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008722 mask |= EPOLLIN | EPOLLRDNORM;
8723
8724 return mask;
8725}
8726
8727static int io_uring_fasync(int fd, struct file *file, int on)
8728{
8729 struct io_ring_ctx *ctx = file->private_data;
8730
8731 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8732}
8733
Yejune Deng0bead8c2020-12-24 11:02:20 +08008734static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008735{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008736 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008737
Jens Axboe1e6fa522020-10-15 08:46:24 -06008738 iod = idr_remove(&ctx->personality_idr, id);
8739 if (iod) {
8740 put_cred(iod->creds);
8741 if (refcount_dec_and_test(&iod->count))
8742 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008743 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008744 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008745
8746 return -EINVAL;
8747}
8748
8749static int io_remove_personalities(int id, void *p, void *data)
8750{
8751 struct io_ring_ctx *ctx = data;
8752
8753 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008754 return 0;
8755}
8756
Jens Axboe85faa7b2020-04-09 18:14:00 -06008757static void io_ring_exit_work(struct work_struct *work)
8758{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008759 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8760 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008761
Jens Axboe56952e92020-06-17 15:00:04 -06008762 /*
8763 * If we're doing polled IO and end up having requests being
8764 * submitted async (out-of-line), then completions can come in while
8765 * we're waiting for refs to drop. We need to reap these manually,
8766 * as nobody else will be looking for them.
8767 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008768 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008769 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008770 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008771 io_ring_ctx_free(ctx);
8772}
8773
Jens Axboe00c18642020-12-20 10:45:02 -07008774static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8775{
8776 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8777
8778 return req->ctx == data;
8779}
8780
Jens Axboe2b188cc2019-01-07 10:46:33 -07008781static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8782{
8783 mutex_lock(&ctx->uring_lock);
8784 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008785
8786 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8787 ctx->sqo_dead = 1;
8788
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008789 /* if force is set, the ring is going away. always drop after that */
8790 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008791 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008792 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008793 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008794 mutex_unlock(&ctx->uring_lock);
8795
Pavel Begunkov6b819282020-11-06 13:00:25 +00008796 io_kill_timeouts(ctx, NULL, NULL);
8797 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008798
8799 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008800 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008801
Jens Axboe15dff282019-11-13 09:09:23 -07008802 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008803 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008804
8805 /*
8806 * Do this upfront, so we won't have a grace period where the ring
8807 * is closed but resources aren't reaped yet. This can cause
8808 * spurious failure in setting up a new ring.
8809 */
Jens Axboe760618f2020-07-24 12:53:31 -06008810 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8811 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008812
Jens Axboe85faa7b2020-04-09 18:14:00 -06008813 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008814 /*
8815 * Use system_unbound_wq to avoid spawning tons of event kworkers
8816 * if we're exiting a ton of rings at the same time. It just adds
8817 * noise and overhead, there's no discernable change in runtime
8818 * over using system_wq.
8819 */
8820 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008821}
8822
8823static int io_uring_release(struct inode *inode, struct file *file)
8824{
8825 struct io_ring_ctx *ctx = file->private_data;
8826
8827 file->private_data = NULL;
8828 io_ring_ctx_wait_and_kill(ctx);
8829 return 0;
8830}
8831
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008832struct io_task_cancel {
8833 struct task_struct *task;
8834 struct files_struct *files;
8835};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008836
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008837static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008838{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008839 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008840 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008841 bool ret;
8842
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008843 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008844 unsigned long flags;
8845 struct io_ring_ctx *ctx = req->ctx;
8846
8847 /* protect against races with linked timeouts */
8848 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008849 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008850 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8851 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008852 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008853 }
8854 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008855}
8856
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008857static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008858 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008859 struct files_struct *files)
8860{
8861 struct io_defer_entry *de = NULL;
8862 LIST_HEAD(list);
8863
8864 spin_lock_irq(&ctx->completion_lock);
8865 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008866 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008867 list_cut_position(&list, &ctx->defer_list, &de->list);
8868 break;
8869 }
8870 }
8871 spin_unlock_irq(&ctx->completion_lock);
8872
8873 while (!list_empty(&list)) {
8874 de = list_first_entry(&list, struct io_defer_entry, list);
8875 list_del_init(&de->list);
8876 req_set_fail_links(de->req);
8877 io_put_req(de->req);
8878 io_req_complete(de->req, -ECANCELED);
8879 kfree(de);
8880 }
8881}
8882
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008883static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8884 struct task_struct *task,
8885 struct files_struct *files)
8886{
8887 struct io_task_cancel cancel = { .task = task, .files = files, };
8888
8889 while (1) {
8890 enum io_wq_cancel cret;
8891 bool ret = false;
8892
8893 if (ctx->io_wq) {
8894 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8895 &cancel, true);
8896 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8897 }
8898
8899 /* SQPOLL thread does its own polling */
8900 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8901 while (!list_empty_careful(&ctx->iopoll_list)) {
8902 io_iopoll_try_reap_events(ctx);
8903 ret = true;
8904 }
8905 }
8906
8907 ret |= io_poll_remove_all(ctx, task, files);
8908 ret |= io_kill_timeouts(ctx, task, files);
8909 ret |= io_run_task_work();
8910 io_cqring_overflow_flush(ctx, true, task, files);
8911 if (!ret)
8912 break;
8913 cond_resched();
8914 }
8915}
8916
Pavel Begunkovca70f002021-01-26 15:28:27 +00008917static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8918 struct task_struct *task,
8919 struct files_struct *files)
8920{
8921 struct io_kiocb *req;
8922 int cnt = 0;
8923
8924 spin_lock_irq(&ctx->inflight_lock);
8925 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8926 cnt += io_match_task(req, task, files);
8927 spin_unlock_irq(&ctx->inflight_lock);
8928 return cnt;
8929}
8930
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008931static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008932 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008933 struct files_struct *files)
8934{
Jens Axboefcb323c2019-10-24 12:39:47 -06008935 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008936 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008937 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008938
Pavel Begunkovca70f002021-01-26 15:28:27 +00008939 inflight = io_uring_count_inflight(ctx, task, files);
8940 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008941 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008942
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008943 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008944 prepare_to_wait(&task->io_uring->wait, &wait,
8945 TASK_UNINTERRUPTIBLE);
8946 if (inflight == io_uring_count_inflight(ctx, task, files))
8947 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008948 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008949 }
8950}
8951
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008952static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8953{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008954 mutex_lock(&ctx->uring_lock);
8955 ctx->sqo_dead = 1;
8956 mutex_unlock(&ctx->uring_lock);
8957
8958 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008959 if (ctx->rings)
8960 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008961}
8962
Jens Axboe0f212202020-09-13 13:09:39 -06008963/*
8964 * We need to iteratively cancel requests, in case a request has dependent
8965 * hard links. These persist even for failure of cancelations, hence keep
8966 * looping until none are found.
8967 */
8968static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8969 struct files_struct *files)
8970{
8971 struct task_struct *task = current;
8972
Jens Axboefdaf0832020-10-30 09:37:30 -06008973 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008974 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008975 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008976 atomic_inc(&task->io_uring->in_idle);
8977 io_sq_thread_park(ctx->sq_data);
8978 }
Jens Axboe0f212202020-09-13 13:09:39 -06008979
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008980 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008981
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008982 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008983 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008984 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008985
8986 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8987 atomic_dec(&task->io_uring->in_idle);
8988 /*
8989 * If the files that are going away are the ones in the thread
8990 * identity, clear them out.
8991 */
8992 if (task->io_uring->identity->files == files)
8993 task->io_uring->identity->files = NULL;
8994 io_sq_thread_unpark(ctx->sq_data);
8995 }
Jens Axboe0f212202020-09-13 13:09:39 -06008996}
8997
8998/*
8999 * Note that this task has used io_uring. We use it for cancelation purposes.
9000 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009001static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009002{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009003 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009004 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009005
9006 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009007 ret = io_uring_alloc_task_context(current);
9008 if (unlikely(ret))
9009 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009010 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009011 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009012 if (tctx->last != file) {
9013 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009014
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009015 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009016 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009017 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9018 file, GFP_KERNEL));
9019 if (ret) {
9020 fput(file);
9021 return ret;
9022 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009023
9024 /* one and only SQPOLL file note, held by sqo_task */
9025 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9026 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009027 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009028 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009029 }
9030
Jens Axboefdaf0832020-10-30 09:37:30 -06009031 /*
9032 * This is race safe in that the task itself is doing this, hence it
9033 * cannot be going through the exit/cancel paths at the same time.
9034 * This cannot be modified while exit/cancel is running.
9035 */
9036 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9037 tctx->sqpoll = true;
9038
Jens Axboe0f212202020-09-13 13:09:39 -06009039 return 0;
9040}
9041
9042/*
9043 * Remove this io_uring_file -> task mapping.
9044 */
9045static void io_uring_del_task_file(struct file *file)
9046{
9047 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009048
9049 if (tctx->last == file)
9050 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009051 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009052 if (file)
9053 fput(file);
9054}
9055
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009056static void io_uring_remove_task_files(struct io_uring_task *tctx)
9057{
9058 struct file *file;
9059 unsigned long index;
9060
9061 xa_for_each(&tctx->xa, index, file)
9062 io_uring_del_task_file(file);
9063}
9064
Jens Axboe0f212202020-09-13 13:09:39 -06009065void __io_uring_files_cancel(struct files_struct *files)
9066{
9067 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009068 struct file *file;
9069 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009070
9071 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009072 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009073 xa_for_each(&tctx->xa, index, file)
9074 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009075 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009076
9077 if (files)
9078 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009079}
9080
9081static s64 tctx_inflight(struct io_uring_task *tctx)
9082{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009083 return percpu_counter_sum(&tctx->inflight);
9084}
9085
9086static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9087{
9088 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009089 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009090 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009091
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009092 if (!ctx->sq_data)
9093 return;
9094 tctx = ctx->sq_data->thread->io_uring;
9095 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009096
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009097 atomic_inc(&tctx->in_idle);
9098 do {
9099 /* read completions before cancelations */
9100 inflight = tctx_inflight(tctx);
9101 if (!inflight)
9102 break;
9103 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009104
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009105 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9106 /*
9107 * If we've seen completions, retry without waiting. This
9108 * avoids a race where a completion comes in before we did
9109 * prepare_to_wait().
9110 */
9111 if (inflight == tctx_inflight(tctx))
9112 schedule();
9113 finish_wait(&tctx->wait, &wait);
9114 } while (1);
9115 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009116}
9117
Jens Axboe0f212202020-09-13 13:09:39 -06009118/*
9119 * Find any io_uring fd that this task has registered or done IO on, and cancel
9120 * requests.
9121 */
9122void __io_uring_task_cancel(void)
9123{
9124 struct io_uring_task *tctx = current->io_uring;
9125 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009126 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009127
9128 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009129 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009130
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009131 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009132 if (tctx->sqpoll) {
9133 struct file *file;
9134 unsigned long index;
9135
9136 xa_for_each(&tctx->xa, index, file)
9137 io_uring_cancel_sqpoll(file->private_data);
9138 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009139
Jens Axboed8a6df12020-10-15 16:24:45 -06009140 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009141 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009142 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009143 if (!inflight)
9144 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009145 __io_uring_files_cancel(NULL);
9146
9147 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9148
9149 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009150 * If we've seen completions, retry without waiting. This
9151 * avoids a race where a completion comes in before we did
9152 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009153 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009154 if (inflight == tctx_inflight(tctx))
9155 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009156 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009157 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009158
Jens Axboefdaf0832020-10-30 09:37:30 -06009159 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009160
9161 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009162}
9163
Jens Axboefcb323c2019-10-24 12:39:47 -06009164static int io_uring_flush(struct file *file, void *data)
9165{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009166 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009167 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009168
Jens Axboe84965ff2021-01-23 15:51:11 -07009169 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9170 io_uring_cancel_task_requests(ctx, NULL);
9171
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009172 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009173 return 0;
9174
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009175 /* we should have cancelled and erased it before PF_EXITING */
9176 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9177 xa_load(&tctx->xa, (unsigned long)file));
9178
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009179 /*
9180 * fput() is pending, will be 2 if the only other ref is our potential
9181 * task file note. If the task is exiting, drop regardless of count.
9182 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009183 if (atomic_long_read(&file->f_count) != 2)
9184 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009185
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009186 if (ctx->flags & IORING_SETUP_SQPOLL) {
9187 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009188 WARN_ON_ONCE(ctx->sqo_task != current &&
9189 xa_load(&tctx->xa, (unsigned long)file));
9190 /* sqo_dead check is for when this happens after cancellation */
9191 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009192 !xa_load(&tctx->xa, (unsigned long)file));
9193
9194 io_disable_sqo_submit(ctx);
9195 }
9196
9197 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9198 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009199 return 0;
9200}
9201
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009202static void *io_uring_validate_mmap_request(struct file *file,
9203 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009204{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009205 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009206 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009207 struct page *page;
9208 void *ptr;
9209
9210 switch (offset) {
9211 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009212 case IORING_OFF_CQ_RING:
9213 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009214 break;
9215 case IORING_OFF_SQES:
9216 ptr = ctx->sq_sqes;
9217 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009218 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009219 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009220 }
9221
9222 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009223 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009224 return ERR_PTR(-EINVAL);
9225
9226 return ptr;
9227}
9228
9229#ifdef CONFIG_MMU
9230
9231static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9232{
9233 size_t sz = vma->vm_end - vma->vm_start;
9234 unsigned long pfn;
9235 void *ptr;
9236
9237 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9238 if (IS_ERR(ptr))
9239 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009240
9241 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9242 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9243}
9244
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009245#else /* !CONFIG_MMU */
9246
9247static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9248{
9249 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9250}
9251
9252static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9253{
9254 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9255}
9256
9257static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9258 unsigned long addr, unsigned long len,
9259 unsigned long pgoff, unsigned long flags)
9260{
9261 void *ptr;
9262
9263 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9264 if (IS_ERR(ptr))
9265 return PTR_ERR(ptr);
9266
9267 return (unsigned long) ptr;
9268}
9269
9270#endif /* !CONFIG_MMU */
9271
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009272static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009273{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009274 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009275 DEFINE_WAIT(wait);
9276
9277 do {
9278 if (!io_sqring_full(ctx))
9279 break;
9280
9281 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9282
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009283 if (unlikely(ctx->sqo_dead)) {
9284 ret = -EOWNERDEAD;
9285 goto out;
9286 }
9287
Jens Axboe90554202020-09-03 12:12:41 -06009288 if (!io_sqring_full(ctx))
9289 break;
9290
9291 schedule();
9292 } while (!signal_pending(current));
9293
9294 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009295out:
9296 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009297}
9298
Hao Xuc73ebb62020-11-03 10:54:37 +08009299static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9300 struct __kernel_timespec __user **ts,
9301 const sigset_t __user **sig)
9302{
9303 struct io_uring_getevents_arg arg;
9304
9305 /*
9306 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9307 * is just a pointer to the sigset_t.
9308 */
9309 if (!(flags & IORING_ENTER_EXT_ARG)) {
9310 *sig = (const sigset_t __user *) argp;
9311 *ts = NULL;
9312 return 0;
9313 }
9314
9315 /*
9316 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9317 * timespec and sigset_t pointers if good.
9318 */
9319 if (*argsz != sizeof(arg))
9320 return -EINVAL;
9321 if (copy_from_user(&arg, argp, sizeof(arg)))
9322 return -EFAULT;
9323 *sig = u64_to_user_ptr(arg.sigmask);
9324 *argsz = arg.sigmask_sz;
9325 *ts = u64_to_user_ptr(arg.ts);
9326 return 0;
9327}
9328
Jens Axboe2b188cc2019-01-07 10:46:33 -07009329SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009330 u32, min_complete, u32, flags, const void __user *, argp,
9331 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009332{
9333 struct io_ring_ctx *ctx;
9334 long ret = -EBADF;
9335 int submitted = 0;
9336 struct fd f;
9337
Jens Axboe4c6e2772020-07-01 11:29:10 -06009338 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009339
Jens Axboe90554202020-09-03 12:12:41 -06009340 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009341 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342 return -EINVAL;
9343
9344 f = fdget(fd);
9345 if (!f.file)
9346 return -EBADF;
9347
9348 ret = -EOPNOTSUPP;
9349 if (f.file->f_op != &io_uring_fops)
9350 goto out_fput;
9351
9352 ret = -ENXIO;
9353 ctx = f.file->private_data;
9354 if (!percpu_ref_tryget(&ctx->refs))
9355 goto out_fput;
9356
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009357 ret = -EBADFD;
9358 if (ctx->flags & IORING_SETUP_R_DISABLED)
9359 goto out;
9360
Jens Axboe6c271ce2019-01-10 11:22:30 -07009361 /*
9362 * For SQ polling, the thread will do all submissions and completions.
9363 * Just return the requested submit count, and wake the thread if
9364 * we were asked to.
9365 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009366 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009367 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009368 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009369
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009370 ret = -EOWNERDEAD;
9371 if (unlikely(ctx->sqo_dead))
9372 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009373 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009374 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009375 if (flags & IORING_ENTER_SQ_WAIT) {
9376 ret = io_sqpoll_wait_sq(ctx);
9377 if (ret)
9378 goto out;
9379 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009380 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009381 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009382 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009383 if (unlikely(ret))
9384 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009385 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009386 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009387 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009388
9389 if (submitted != to_submit)
9390 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009391 }
9392 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009393 const sigset_t __user *sig;
9394 struct __kernel_timespec __user *ts;
9395
9396 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9397 if (unlikely(ret))
9398 goto out;
9399
Jens Axboe2b188cc2019-01-07 10:46:33 -07009400 min_complete = min(min_complete, ctx->cq_entries);
9401
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009402 /*
9403 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9404 * space applications don't need to do io completion events
9405 * polling again, they can rely on io_sq_thread to do polling
9406 * work, which can reduce cpu usage and uring_lock contention.
9407 */
9408 if (ctx->flags & IORING_SETUP_IOPOLL &&
9409 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009410 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009411 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009412 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009413 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414 }
9415
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009416out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009417 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009418out_fput:
9419 fdput(f);
9420 return submitted ? submitted : ret;
9421}
9422
Tobias Klauserbebdb652020-02-26 18:38:32 +01009423#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009424static int io_uring_show_cred(int id, void *p, void *data)
9425{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009426 struct io_identity *iod = p;
9427 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009428 struct seq_file *m = data;
9429 struct user_namespace *uns = seq_user_ns(m);
9430 struct group_info *gi;
9431 kernel_cap_t cap;
9432 unsigned __capi;
9433 int g;
9434
9435 seq_printf(m, "%5d\n", id);
9436 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9437 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9438 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9439 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9440 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9441 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9442 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9443 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9444 seq_puts(m, "\n\tGroups:\t");
9445 gi = cred->group_info;
9446 for (g = 0; g < gi->ngroups; g++) {
9447 seq_put_decimal_ull(m, g ? " " : "",
9448 from_kgid_munged(uns, gi->gid[g]));
9449 }
9450 seq_puts(m, "\n\tCapEff:\t");
9451 cap = cred->cap_effective;
9452 CAP_FOR_EACH_U32(__capi)
9453 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9454 seq_putc(m, '\n');
9455 return 0;
9456}
9457
9458static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9459{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009460 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009461 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009462 int i;
9463
Jens Axboefad8e0d2020-09-28 08:57:48 -06009464 /*
9465 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9466 * since fdinfo case grabs it in the opposite direction of normal use
9467 * cases. If we fail to get the lock, we just don't iterate any
9468 * structures that could be going away outside the io_uring mutex.
9469 */
9470 has_lock = mutex_trylock(&ctx->uring_lock);
9471
Joseph Qidbbe9c62020-09-29 09:01:22 -06009472 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9473 sq = ctx->sq_data;
9474
9475 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9476 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009477 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009478 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009479 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009480
Jens Axboe87ce9552020-01-30 08:25:34 -07009481 if (f)
9482 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9483 else
9484 seq_printf(m, "%5u: <none>\n", i);
9485 }
9486 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009487 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009488 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9489
9490 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9491 (unsigned int) buf->len);
9492 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009493 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009494 seq_printf(m, "Personalities:\n");
9495 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9496 }
Jens Axboed7718a92020-02-14 22:23:12 -07009497 seq_printf(m, "PollList:\n");
9498 spin_lock_irq(&ctx->completion_lock);
9499 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9500 struct hlist_head *list = &ctx->cancel_hash[i];
9501 struct io_kiocb *req;
9502
9503 hlist_for_each_entry(req, list, hash_node)
9504 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9505 req->task->task_works != NULL);
9506 }
9507 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009508 if (has_lock)
9509 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009510}
9511
9512static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9513{
9514 struct io_ring_ctx *ctx = f->private_data;
9515
9516 if (percpu_ref_tryget(&ctx->refs)) {
9517 __io_uring_show_fdinfo(ctx, m);
9518 percpu_ref_put(&ctx->refs);
9519 }
9520}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009521#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009522
Jens Axboe2b188cc2019-01-07 10:46:33 -07009523static const struct file_operations io_uring_fops = {
9524 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009525 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009526 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009527#ifndef CONFIG_MMU
9528 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9529 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9530#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009531 .poll = io_uring_poll,
9532 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009533#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009534 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009535#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009536};
9537
9538static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9539 struct io_uring_params *p)
9540{
Hristo Venev75b28af2019-08-26 17:23:46 +00009541 struct io_rings *rings;
9542 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009543
Jens Axboebd740482020-08-05 12:58:23 -06009544 /* make sure these are sane, as we already accounted them */
9545 ctx->sq_entries = p->sq_entries;
9546 ctx->cq_entries = p->cq_entries;
9547
Hristo Venev75b28af2019-08-26 17:23:46 +00009548 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9549 if (size == SIZE_MAX)
9550 return -EOVERFLOW;
9551
9552 rings = io_mem_alloc(size);
9553 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009554 return -ENOMEM;
9555
Hristo Venev75b28af2019-08-26 17:23:46 +00009556 ctx->rings = rings;
9557 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9558 rings->sq_ring_mask = p->sq_entries - 1;
9559 rings->cq_ring_mask = p->cq_entries - 1;
9560 rings->sq_ring_entries = p->sq_entries;
9561 rings->cq_ring_entries = p->cq_entries;
9562 ctx->sq_mask = rings->sq_ring_mask;
9563 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009564
9565 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009566 if (size == SIZE_MAX) {
9567 io_mem_free(ctx->rings);
9568 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009569 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009570 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009571
9572 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009573 if (!ctx->sq_sqes) {
9574 io_mem_free(ctx->rings);
9575 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009576 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009577 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009578
Jens Axboe2b188cc2019-01-07 10:46:33 -07009579 return 0;
9580}
9581
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009582static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9583{
9584 int ret, fd;
9585
9586 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9587 if (fd < 0)
9588 return fd;
9589
9590 ret = io_uring_add_task_file(ctx, file);
9591 if (ret) {
9592 put_unused_fd(fd);
9593 return ret;
9594 }
9595 fd_install(fd, file);
9596 return fd;
9597}
9598
Jens Axboe2b188cc2019-01-07 10:46:33 -07009599/*
9600 * Allocate an anonymous fd, this is what constitutes the application
9601 * visible backing of an io_uring instance. The application mmaps this
9602 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9603 * we have to tie this fd to a socket for file garbage collection purposes.
9604 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009605static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009606{
9607 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009608#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009609 int ret;
9610
Jens Axboe2b188cc2019-01-07 10:46:33 -07009611 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9612 &ctx->ring_sock);
9613 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009614 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009615#endif
9616
Jens Axboe2b188cc2019-01-07 10:46:33 -07009617 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9618 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009619#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009620 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009621 sock_release(ctx->ring_sock);
9622 ctx->ring_sock = NULL;
9623 } else {
9624 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009625 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009626#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009627 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009628}
9629
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009630static int io_uring_create(unsigned entries, struct io_uring_params *p,
9631 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009632{
9633 struct user_struct *user = NULL;
9634 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009635 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009636 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009637 int ret;
9638
Jens Axboe8110c1a2019-12-28 15:39:54 -07009639 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009640 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009641 if (entries > IORING_MAX_ENTRIES) {
9642 if (!(p->flags & IORING_SETUP_CLAMP))
9643 return -EINVAL;
9644 entries = IORING_MAX_ENTRIES;
9645 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009646
9647 /*
9648 * Use twice as many entries for the CQ ring. It's possible for the
9649 * application to drive a higher depth than the size of the SQ ring,
9650 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009651 * some flexibility in overcommitting a bit. If the application has
9652 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9653 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009654 */
9655 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009656 if (p->flags & IORING_SETUP_CQSIZE) {
9657 /*
9658 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9659 * to a power-of-two, if it isn't already. We do NOT impose
9660 * any cq vs sq ring sizing.
9661 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009662 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009663 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009664 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9665 if (!(p->flags & IORING_SETUP_CLAMP))
9666 return -EINVAL;
9667 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9668 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009669 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9670 if (p->cq_entries < p->sq_entries)
9671 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009672 } else {
9673 p->cq_entries = 2 * p->sq_entries;
9674 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009675
9676 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009677 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009678
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009679 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009680 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009681 ring_pages(p->sq_entries, p->cq_entries));
9682 if (ret) {
9683 free_uid(user);
9684 return ret;
9685 }
9686 }
9687
9688 ctx = io_ring_ctx_alloc(p);
9689 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009690 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009691 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009692 p->cq_entries));
9693 free_uid(user);
9694 return -ENOMEM;
9695 }
9696 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009697 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009698 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009699#ifdef CONFIG_AUDIT
9700 ctx->loginuid = current->loginuid;
9701 ctx->sessionid = current->sessionid;
9702#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009703 ctx->sqo_task = get_task_struct(current);
9704
9705 /*
9706 * This is just grabbed for accounting purposes. When a process exits,
9707 * the mm is exited and dropped before the files, hence we need to hang
9708 * on to this mm purely for the purposes of being able to unaccount
9709 * memory (locked/pinned vm). It's not used for anything else.
9710 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009711 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009712 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009713
Dennis Zhou91d8f512020-09-16 13:41:05 -07009714#ifdef CONFIG_BLK_CGROUP
9715 /*
9716 * The sq thread will belong to the original cgroup it was inited in.
9717 * If the cgroup goes offline (e.g. disabling the io controller), then
9718 * issued bios will be associated with the closest cgroup later in the
9719 * block layer.
9720 */
9721 rcu_read_lock();
9722 ctx->sqo_blkcg_css = blkcg_css();
9723 ret = css_tryget_online(ctx->sqo_blkcg_css);
9724 rcu_read_unlock();
9725 if (!ret) {
9726 /* don't init against a dying cgroup, have the user try again */
9727 ctx->sqo_blkcg_css = NULL;
9728 ret = -ENODEV;
9729 goto err;
9730 }
9731#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009732
Jens Axboe2b188cc2019-01-07 10:46:33 -07009733 /*
9734 * Account memory _before_ installing the file descriptor. Once
9735 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009736 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009737 * will un-account as well.
9738 */
9739 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9740 ACCT_LOCKED);
9741 ctx->limit_mem = limit_mem;
9742
9743 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009744 if (ret)
9745 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009746
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009747 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009748 if (ret)
9749 goto err;
9750
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009751 if (!(p->flags & IORING_SETUP_R_DISABLED))
9752 io_sq_offload_start(ctx);
9753
Jens Axboe2b188cc2019-01-07 10:46:33 -07009754 memset(&p->sq_off, 0, sizeof(p->sq_off));
9755 p->sq_off.head = offsetof(struct io_rings, sq.head);
9756 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9757 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9758 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9759 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9760 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9761 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9762
9763 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009764 p->cq_off.head = offsetof(struct io_rings, cq.head);
9765 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9766 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9767 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9768 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9769 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009770 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009771
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009772 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9773 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009774 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009775 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9776 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009777
9778 if (copy_to_user(params, p, sizeof(*p))) {
9779 ret = -EFAULT;
9780 goto err;
9781 }
Jens Axboed1719f72020-07-30 13:43:53 -06009782
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009783 file = io_uring_get_file(ctx);
9784 if (IS_ERR(file)) {
9785 ret = PTR_ERR(file);
9786 goto err;
9787 }
9788
Jens Axboed1719f72020-07-30 13:43:53 -06009789 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009790 * Install ring fd as the very last thing, so we don't risk someone
9791 * having closed it before we finish setup
9792 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009793 ret = io_uring_install_fd(ctx, file);
9794 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009795 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009796 /* fput will clean it up */
9797 fput(file);
9798 return ret;
9799 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009800
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009801 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009802 return ret;
9803err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009804 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009805 io_ring_ctx_wait_and_kill(ctx);
9806 return ret;
9807}
9808
9809/*
9810 * Sets up an aio uring context, and returns the fd. Applications asks for a
9811 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9812 * params structure passed in.
9813 */
9814static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9815{
9816 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009817 int i;
9818
9819 if (copy_from_user(&p, params, sizeof(p)))
9820 return -EFAULT;
9821 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9822 if (p.resv[i])
9823 return -EINVAL;
9824 }
9825
Jens Axboe6c271ce2019-01-10 11:22:30 -07009826 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009827 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009828 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9829 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009830 return -EINVAL;
9831
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009832 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009833}
9834
9835SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9836 struct io_uring_params __user *, params)
9837{
9838 return io_uring_setup(entries, params);
9839}
9840
Jens Axboe66f4af92020-01-16 15:36:52 -07009841static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9842{
9843 struct io_uring_probe *p;
9844 size_t size;
9845 int i, ret;
9846
9847 size = struct_size(p, ops, nr_args);
9848 if (size == SIZE_MAX)
9849 return -EOVERFLOW;
9850 p = kzalloc(size, GFP_KERNEL);
9851 if (!p)
9852 return -ENOMEM;
9853
9854 ret = -EFAULT;
9855 if (copy_from_user(p, arg, size))
9856 goto out;
9857 ret = -EINVAL;
9858 if (memchr_inv(p, 0, size))
9859 goto out;
9860
9861 p->last_op = IORING_OP_LAST - 1;
9862 if (nr_args > IORING_OP_LAST)
9863 nr_args = IORING_OP_LAST;
9864
9865 for (i = 0; i < nr_args; i++) {
9866 p->ops[i].op = i;
9867 if (!io_op_defs[i].not_supported)
9868 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9869 }
9870 p->ops_len = i;
9871
9872 ret = 0;
9873 if (copy_to_user(arg, p, size))
9874 ret = -EFAULT;
9875out:
9876 kfree(p);
9877 return ret;
9878}
9879
Jens Axboe071698e2020-01-28 10:04:42 -07009880static int io_register_personality(struct io_ring_ctx *ctx)
9881{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009882 struct io_identity *id;
9883 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009884
Jens Axboe1e6fa522020-10-15 08:46:24 -06009885 id = kmalloc(sizeof(*id), GFP_KERNEL);
9886 if (unlikely(!id))
9887 return -ENOMEM;
9888
9889 io_init_identity(id);
9890 id->creds = get_current_cred();
9891
9892 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9893 if (ret < 0) {
9894 put_cred(id->creds);
9895 kfree(id);
9896 }
9897 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009898}
9899
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009900static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9901 unsigned int nr_args)
9902{
9903 struct io_uring_restriction *res;
9904 size_t size;
9905 int i, ret;
9906
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009907 /* Restrictions allowed only if rings started disabled */
9908 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9909 return -EBADFD;
9910
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009911 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009912 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009913 return -EBUSY;
9914
9915 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9916 return -EINVAL;
9917
9918 size = array_size(nr_args, sizeof(*res));
9919 if (size == SIZE_MAX)
9920 return -EOVERFLOW;
9921
9922 res = memdup_user(arg, size);
9923 if (IS_ERR(res))
9924 return PTR_ERR(res);
9925
9926 ret = 0;
9927
9928 for (i = 0; i < nr_args; i++) {
9929 switch (res[i].opcode) {
9930 case IORING_RESTRICTION_REGISTER_OP:
9931 if (res[i].register_op >= IORING_REGISTER_LAST) {
9932 ret = -EINVAL;
9933 goto out;
9934 }
9935
9936 __set_bit(res[i].register_op,
9937 ctx->restrictions.register_op);
9938 break;
9939 case IORING_RESTRICTION_SQE_OP:
9940 if (res[i].sqe_op >= IORING_OP_LAST) {
9941 ret = -EINVAL;
9942 goto out;
9943 }
9944
9945 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9946 break;
9947 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9948 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9949 break;
9950 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9951 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9952 break;
9953 default:
9954 ret = -EINVAL;
9955 goto out;
9956 }
9957 }
9958
9959out:
9960 /* Reset all restrictions if an error happened */
9961 if (ret != 0)
9962 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9963 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009964 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009965
9966 kfree(res);
9967 return ret;
9968}
9969
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009970static int io_register_enable_rings(struct io_ring_ctx *ctx)
9971{
9972 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9973 return -EBADFD;
9974
9975 if (ctx->restrictions.registered)
9976 ctx->restricted = 1;
9977
9978 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9979
9980 io_sq_offload_start(ctx);
9981
9982 return 0;
9983}
9984
Jens Axboe071698e2020-01-28 10:04:42 -07009985static bool io_register_op_must_quiesce(int op)
9986{
9987 switch (op) {
9988 case IORING_UNREGISTER_FILES:
9989 case IORING_REGISTER_FILES_UPDATE:
9990 case IORING_REGISTER_PROBE:
9991 case IORING_REGISTER_PERSONALITY:
9992 case IORING_UNREGISTER_PERSONALITY:
9993 return false;
9994 default:
9995 return true;
9996 }
9997}
9998
Jens Axboeedafcce2019-01-09 09:16:05 -07009999static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10000 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010001 __releases(ctx->uring_lock)
10002 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010003{
10004 int ret;
10005
Jens Axboe35fa71a2019-04-22 10:23:23 -060010006 /*
10007 * We're inside the ring mutex, if the ref is already dying, then
10008 * someone else killed the ctx or is already going through
10009 * io_uring_register().
10010 */
10011 if (percpu_ref_is_dying(&ctx->refs))
10012 return -ENXIO;
10013
Jens Axboe071698e2020-01-28 10:04:42 -070010014 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010015 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010016
Jens Axboe05f3fb32019-12-09 11:22:50 -070010017 /*
10018 * Drop uring mutex before waiting for references to exit. If
10019 * another thread is currently inside io_uring_enter() it might
10020 * need to grab the uring_lock to make progress. If we hold it
10021 * here across the drain wait, then we can deadlock. It's safe
10022 * to drop the mutex here, since no new references will come in
10023 * after we've killed the percpu ref.
10024 */
10025 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010026 do {
10027 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10028 if (!ret)
10029 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010030 ret = io_run_task_work_sig();
10031 if (ret < 0)
10032 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010033 } while (1);
10034
Jens Axboe05f3fb32019-12-09 11:22:50 -070010035 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010036
Jens Axboec1503682020-01-08 08:26:07 -070010037 if (ret) {
10038 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010039 goto out_quiesce;
10040 }
10041 }
10042
10043 if (ctx->restricted) {
10044 if (opcode >= IORING_REGISTER_LAST) {
10045 ret = -EINVAL;
10046 goto out;
10047 }
10048
10049 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10050 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010051 goto out;
10052 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010053 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010054
10055 switch (opcode) {
10056 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010057 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010058 break;
10059 case IORING_UNREGISTER_BUFFERS:
10060 ret = -EINVAL;
10061 if (arg || nr_args)
10062 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010063 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010064 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010065 case IORING_REGISTER_FILES:
10066 ret = io_sqe_files_register(ctx, arg, nr_args);
10067 break;
10068 case IORING_UNREGISTER_FILES:
10069 ret = -EINVAL;
10070 if (arg || nr_args)
10071 break;
10072 ret = io_sqe_files_unregister(ctx);
10073 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010074 case IORING_REGISTER_FILES_UPDATE:
10075 ret = io_sqe_files_update(ctx, arg, nr_args);
10076 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010077 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010078 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010079 ret = -EINVAL;
10080 if (nr_args != 1)
10081 break;
10082 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010083 if (ret)
10084 break;
10085 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10086 ctx->eventfd_async = 1;
10087 else
10088 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010089 break;
10090 case IORING_UNREGISTER_EVENTFD:
10091 ret = -EINVAL;
10092 if (arg || nr_args)
10093 break;
10094 ret = io_eventfd_unregister(ctx);
10095 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010096 case IORING_REGISTER_PROBE:
10097 ret = -EINVAL;
10098 if (!arg || nr_args > 256)
10099 break;
10100 ret = io_probe(ctx, arg, nr_args);
10101 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010102 case IORING_REGISTER_PERSONALITY:
10103 ret = -EINVAL;
10104 if (arg || nr_args)
10105 break;
10106 ret = io_register_personality(ctx);
10107 break;
10108 case IORING_UNREGISTER_PERSONALITY:
10109 ret = -EINVAL;
10110 if (arg)
10111 break;
10112 ret = io_unregister_personality(ctx, nr_args);
10113 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010114 case IORING_REGISTER_ENABLE_RINGS:
10115 ret = -EINVAL;
10116 if (arg || nr_args)
10117 break;
10118 ret = io_register_enable_rings(ctx);
10119 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010120 case IORING_REGISTER_RESTRICTIONS:
10121 ret = io_register_restrictions(ctx, arg, nr_args);
10122 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010123 default:
10124 ret = -EINVAL;
10125 break;
10126 }
10127
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010128out:
Jens Axboe071698e2020-01-28 10:04:42 -070010129 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010130 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010131 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010132out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010133 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010134 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010135 return ret;
10136}
10137
10138SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10139 void __user *, arg, unsigned int, nr_args)
10140{
10141 struct io_ring_ctx *ctx;
10142 long ret = -EBADF;
10143 struct fd f;
10144
10145 f = fdget(fd);
10146 if (!f.file)
10147 return -EBADF;
10148
10149 ret = -EOPNOTSUPP;
10150 if (f.file->f_op != &io_uring_fops)
10151 goto out_fput;
10152
10153 ctx = f.file->private_data;
10154
10155 mutex_lock(&ctx->uring_lock);
10156 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10157 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010158 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10159 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010160out_fput:
10161 fdput(f);
10162 return ret;
10163}
10164
Jens Axboe2b188cc2019-01-07 10:46:33 -070010165static int __init io_uring_init(void)
10166{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010167#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10168 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10169 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10170} while (0)
10171
10172#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10173 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10174 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10175 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10176 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10177 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10178 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10179 BUILD_BUG_SQE_ELEM(8, __u64, off);
10180 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10181 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010182 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010183 BUILD_BUG_SQE_ELEM(24, __u32, len);
10184 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10185 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10186 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10187 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010188 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10189 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010190 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10191 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10192 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10193 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10194 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10195 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10196 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10197 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010198 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010199 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10200 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10201 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010202 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010203
Jens Axboed3656342019-12-18 09:50:26 -070010204 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010205 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010206 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10207 return 0;
10208};
10209__initcall(io_uring_init);