blob: 9b84d6314c118b21d3dfef264bc5c65999fab506 [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
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000267#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000268#define IO_COMPL_BATCH 32
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000269
270struct io_comp_state {
271 unsigned int nr;
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000272 struct io_kiocb *reqs[IO_COMPL_BATCH];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000273};
274
275struct io_submit_state {
276 struct blk_plug plug;
277
278 /*
279 * io_kiocb alloc cache
280 */
281 void *reqs[IO_IOPOLL_BATCH];
282 unsigned int free_reqs;
283
284 bool plug_started;
285
286 /*
287 * Batch completion logic
288 */
289 struct io_comp_state comp;
290
291 /*
292 * File reference cache
293 */
294 struct file *file;
295 unsigned int fd;
296 unsigned int file_refs;
297 unsigned int ios_left;
298};
299
Jens Axboe2b188cc2019-01-07 10:46:33 -0700300struct io_ring_ctx {
301 struct {
302 struct percpu_ref refs;
303 } ____cacheline_aligned_in_smp;
304
305 struct {
306 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800307 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700308 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800309 unsigned int cq_overflow_flushed: 1;
310 unsigned int drain_next: 1;
311 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200312 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000313 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700314
Hristo Venev75b28af2019-08-26 17:23:46 +0000315 /*
316 * Ring buffer of indices into array of io_uring_sqe, which is
317 * mmapped by the application using the IORING_OFF_SQES offset.
318 *
319 * This indirection could e.g. be used to assign fixed
320 * io_uring_sqe entries to operations and only submit them to
321 * the queue when needed.
322 *
323 * The kernel modifies neither the indices array nor the entries
324 * array.
325 */
326 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700327 unsigned cached_sq_head;
328 unsigned sq_entries;
329 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700330 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600331 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100332 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700333 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600334
335 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600336 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700337 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338
Jens Axboead3eb2c2019-12-18 17:12:20 -0700339 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700340 } ____cacheline_aligned_in_smp;
341
Hristo Venev75b28af2019-08-26 17:23:46 +0000342 struct io_rings *rings;
343
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600345 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600346
347 /*
348 * For SQPOLL usage - we hold a reference to the parent task, so we
349 * have access to the ->files
350 */
351 struct task_struct *sqo_task;
352
353 /* Only used for accounting purposes */
354 struct mm_struct *mm_account;
355
Dennis Zhou91d8f512020-09-16 13:41:05 -0700356#ifdef CONFIG_BLK_CGROUP
357 struct cgroup_subsys_state *sqo_blkcg_css;
358#endif
359
Jens Axboe534ca6d2020-09-02 13:52:19 -0600360 struct io_sq_data *sq_data; /* if using sq thread polling */
361
Jens Axboe90554202020-09-03 12:12:41 -0600362 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600363 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364
Jens Axboe6b063142019-01-10 22:13:58 -0700365 /*
366 * If used, fixed file set. Writers must ensure that ->refs is dead,
367 * readers must ensure that ->refs is alive as long as the file* is
368 * used. Only updated through io_uring_register(2).
369 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000370 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700371 unsigned nr_user_files;
372
Jens Axboeedafcce2019-01-09 09:16:05 -0700373 /* if used, fixed mapped user buffers */
374 unsigned nr_user_bufs;
375 struct io_mapped_ubuf *user_bufs;
376
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377 struct user_struct *user;
378
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700379 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700380
Jens Axboe4ea33a92020-10-15 13:46:44 -0600381#ifdef CONFIG_AUDIT
382 kuid_t loginuid;
383 unsigned int sessionid;
384#endif
385
Jens Axboe0f158b42020-05-14 17:18:39 -0600386 struct completion ref_comp;
387 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700388
389#if defined(CONFIG_UNIX)
390 struct socket *ring_sock;
391#endif
392
Jens Axboe5a2e7452020-02-23 16:23:11 -0700393 struct idr io_buffer_idr;
394
Jens Axboe071698e2020-01-28 10:04:42 -0700395 struct idr personality_idr;
396
Jens Axboe206aefd2019-11-07 18:27:42 -0700397 struct {
398 unsigned cached_cq_tail;
399 unsigned cq_entries;
400 unsigned cq_mask;
401 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500402 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700403 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700404 struct wait_queue_head cq_wait;
405 struct fasync_struct *cq_fasync;
406 struct eventfd_ctx *cq_ev_fd;
407 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408
409 struct {
410 struct mutex uring_lock;
411 wait_queue_head_t wait;
412 } ____cacheline_aligned_in_smp;
413
414 struct {
415 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700416
Jens Axboedef596e2019-01-09 08:59:42 -0700417 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300418 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700419 * io_uring instances that don't use IORING_SETUP_SQPOLL.
420 * For SQPOLL, only the single threaded io_sq_thread() will
421 * manipulate the list, hence no extra locking is needed there.
422 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300423 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700424 struct hlist_head *cancel_hash;
425 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700426 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600427
428 spinlock_t inflight_lock;
429 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600431
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000432 struct delayed_work rsrc_put_work;
433 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000434 struct list_head rsrc_ref_list;
435 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600436
Jens Axboe85faa7b2020-04-09 18:14:00 -0600437 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200438 struct io_restriction restrictions;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000439 struct io_submit_state submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700440};
441
Jens Axboe09bb8392019-03-13 12:39:28 -0600442/*
443 * First field must be the file pointer in all the
444 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
445 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700446struct io_poll_iocb {
447 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000448 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700449 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600450 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700451 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700452 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700453};
454
Pavel Begunkov018043b2020-10-27 23:17:18 +0000455struct io_poll_remove {
456 struct file *file;
457 u64 addr;
458};
459
Jens Axboeb5dba592019-12-11 14:02:38 -0700460struct io_close {
461 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700462 int fd;
463};
464
Jens Axboead8a48a2019-11-15 08:49:11 -0700465struct io_timeout_data {
466 struct io_kiocb *req;
467 struct hrtimer timer;
468 struct timespec64 ts;
469 enum hrtimer_mode mode;
470};
471
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700472struct io_accept {
473 struct file *file;
474 struct sockaddr __user *addr;
475 int __user *addr_len;
476 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600477 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700478};
479
480struct io_sync {
481 struct file *file;
482 loff_t len;
483 loff_t off;
484 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700485 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700486};
487
Jens Axboefbf23842019-12-17 18:45:56 -0700488struct io_cancel {
489 struct file *file;
490 u64 addr;
491};
492
Jens Axboeb29472e2019-12-17 18:50:29 -0700493struct io_timeout {
494 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300495 u32 off;
496 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300497 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000498 /* head of the link, used by linked timeouts only */
499 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700500};
501
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100502struct io_timeout_rem {
503 struct file *file;
504 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000505
506 /* timeout update */
507 struct timespec64 ts;
508 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100509};
510
Jens Axboe9adbd452019-12-20 08:45:55 -0700511struct io_rw {
512 /* NOTE: kiocb has the file as the first member, so don't do it here */
513 struct kiocb kiocb;
514 u64 addr;
515 u64 len;
516};
517
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700518struct io_connect {
519 struct file *file;
520 struct sockaddr __user *addr;
521 int addr_len;
522};
523
Jens Axboee47293f2019-12-20 08:58:21 -0700524struct io_sr_msg {
525 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700526 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300527 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700528 void __user *buf;
529 };
Jens Axboee47293f2019-12-20 08:58:21 -0700530 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700531 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700532 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700533 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700534};
535
Jens Axboe15b71ab2019-12-11 11:20:36 -0700536struct io_open {
537 struct file *file;
538 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700539 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700540 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600541 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700542};
543
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000544struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700545 struct file *file;
546 u64 arg;
547 u32 nr_args;
548 u32 offset;
549};
550
Jens Axboe4840e412019-12-25 22:03:45 -0700551struct io_fadvise {
552 struct file *file;
553 u64 offset;
554 u32 len;
555 u32 advice;
556};
557
Jens Axboec1ca7572019-12-25 22:18:28 -0700558struct io_madvise {
559 struct file *file;
560 u64 addr;
561 u32 len;
562 u32 advice;
563};
564
Jens Axboe3e4827b2020-01-08 15:18:09 -0700565struct io_epoll {
566 struct file *file;
567 int epfd;
568 int op;
569 int fd;
570 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571};
572
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300573struct io_splice {
574 struct file *file_out;
575 struct file *file_in;
576 loff_t off_out;
577 loff_t off_in;
578 u64 len;
579 unsigned int flags;
580};
581
Jens Axboeddf0322d2020-02-23 16:41:33 -0700582struct io_provide_buf {
583 struct file *file;
584 __u64 addr;
585 __s32 len;
586 __u32 bgid;
587 __u16 nbufs;
588 __u16 bid;
589};
590
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700591struct io_statx {
592 struct file *file;
593 int dfd;
594 unsigned int mask;
595 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700596 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700597 struct statx __user *buffer;
598};
599
Jens Axboe36f4fa62020-09-05 11:14:22 -0600600struct io_shutdown {
601 struct file *file;
602 int how;
603};
604
Jens Axboe80a261f2020-09-28 14:23:58 -0600605struct io_rename {
606 struct file *file;
607 int old_dfd;
608 int new_dfd;
609 struct filename *oldpath;
610 struct filename *newpath;
611 int flags;
612};
613
Jens Axboe14a11432020-09-28 14:27:37 -0600614struct io_unlink {
615 struct file *file;
616 int dfd;
617 int flags;
618 struct filename *filename;
619};
620
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300621struct io_completion {
622 struct file *file;
623 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300624 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300625};
626
Jens Axboef499a022019-12-02 16:28:46 -0700627struct io_async_connect {
628 struct sockaddr_storage address;
629};
630
Jens Axboe03b12302019-12-02 18:50:25 -0700631struct io_async_msghdr {
632 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000633 /* points to an allocated iov, if NULL we use fast_iov instead */
634 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700635 struct sockaddr __user *uaddr;
636 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700637 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700638};
639
Jens Axboef67676d2019-12-02 11:03:47 -0700640struct io_async_rw {
641 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600642 const struct iovec *free_iovec;
643 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600644 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600645 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700646};
647
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300648enum {
649 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
650 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
651 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
652 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
653 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700654 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300655
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300656 REQ_F_FAIL_LINK_BIT,
657 REQ_F_INFLIGHT_BIT,
658 REQ_F_CUR_POS_BIT,
659 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300660 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300661 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300662 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700663 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700664 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600665 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800666 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100667 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000668 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700669
670 /* not a real bit, just to check we're not overflowing the space */
671 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300672};
673
674enum {
675 /* ctx owns file */
676 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
677 /* drain existing IO first */
678 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
679 /* linked sqes */
680 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
681 /* doesn't sever on completion < 0 */
682 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
683 /* IOSQE_ASYNC */
684 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700685 /* IOSQE_BUFFER_SELECT */
686 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300687
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300688 /* fail rest of links */
689 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
690 /* on inflight list */
691 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
692 /* read/write uses file position */
693 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
694 /* must not punt to workers */
695 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100696 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300697 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300698 /* regular file */
699 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300700 /* needs cleanup */
701 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700702 /* already went through poll handler */
703 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700704 /* buffer already selected */
705 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600706 /* doesn't need file table for this request */
707 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800708 /* io_wq_work is initialized */
709 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100710 /* linked timeout is active, i.e. prepared by link's head */
711 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000712 /* completion is deferred through io_comp_state */
713 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700714};
715
716struct async_poll {
717 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600718 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300719};
720
Jens Axboe09bb8392019-03-13 12:39:28 -0600721/*
722 * NOTE! Each of the iocb union members has the file pointer
723 * as the first entry in their struct definition. So you can
724 * access the file pointer through any of the sub-structs,
725 * or directly as just 'ki_filp' in this struct.
726 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700728 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600729 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700730 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700731 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000732 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700733 struct io_accept accept;
734 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700735 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700736 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100737 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700738 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700739 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700740 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700741 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000742 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700743 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700744 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700745 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300746 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700747 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700748 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600749 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600750 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600751 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300752 /* use only after cleaning per-op data, see io_clean_op() */
753 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700754 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700755
Jens Axboee8c2bc12020-08-15 18:44:09 -0700756 /* opcode allocated if it needs to store data for async defer */
757 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700758 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800759 /* polled IO has completed */
760 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700761
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700762 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300763 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700764
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300765 struct io_ring_ctx *ctx;
766 unsigned int flags;
767 refcount_t refs;
768 struct task_struct *task;
769 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700770
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000771 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000772 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700773
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300774 /*
775 * 1. used with ctx->iopoll_list with reads/writes
776 * 2. to track reqs with ->files (see io_op_def::file_table)
777 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300778 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300779 struct callback_head task_work;
780 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
781 struct hlist_node hash_node;
782 struct async_poll *apoll;
783 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784};
785
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300786struct io_defer_entry {
787 struct list_head list;
788 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300789 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300790};
791
Jens Axboed3656342019-12-18 09:50:26 -0700792struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700793 /* needs req->file assigned */
794 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700795 /* hash wq insertion if file is a regular file */
796 unsigned hash_reg_file : 1;
797 /* unbound wq insertion if file is a non-regular file */
798 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700799 /* opcode is not supported by this kernel */
800 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700801 /* set if opcode supports polled "wait" */
802 unsigned pollin : 1;
803 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700804 /* op supports buffer selection */
805 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700806 /* must always have async data allocated */
807 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600808 /* should block plug */
809 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700810 /* size of async data needed, if any */
811 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600812 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700813};
814
Jens Axboe09186822020-10-13 15:01:40 -0600815static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_NOP] = {},
817 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700818 .needs_file = 1,
819 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700820 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700821 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700822 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600823 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600825 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700826 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300827 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700828 .needs_file = 1,
829 .hash_reg_file = 1,
830 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700831 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700832 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600833 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700834 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600835 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
836 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700837 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300838 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700839 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600840 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_file = 1,
844 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700845 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600846 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700847 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600848 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700851 .needs_file = 1,
852 .hash_reg_file = 1,
853 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700854 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600855 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700856 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600857 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
858 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700861 .needs_file = 1,
862 .unbound_nonreg_file = 1,
863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_POLL_REMOVE] = {},
865 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700866 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600867 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700868 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300869 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700870 .needs_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700872 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700873 .needs_async_data = 1,
874 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000875 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700876 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300877 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700878 .needs_file = 1,
879 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700880 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700881 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700882 .needs_async_data = 1,
883 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000884 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700885 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300886 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700887 .needs_async_data = 1,
888 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600889 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700890 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000891 [IORING_OP_TIMEOUT_REMOVE] = {
892 /* used by timeout updates' prep() */
893 .work_flags = IO_WQ_WORK_MM,
894 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300895 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700896 .needs_file = 1,
897 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700898 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600899 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_ASYNC_CANCEL] = {},
902 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700903 .needs_async_data = 1,
904 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600905 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700906 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300907 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700908 .needs_file = 1,
909 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700910 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700911 .needs_async_data = 1,
912 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600913 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700916 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600917 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700918 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300919 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600920 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600921 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700922 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300923 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600924 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700925 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300926 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600927 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600930 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
931 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700932 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300933 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700934 .needs_file = 1,
935 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700936 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700937 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600938 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700939 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600940 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700941 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300942 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700943 .needs_file = 1,
944 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700945 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600946 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700947 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600948 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
949 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700950 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300951 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700952 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600953 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700954 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300955 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600956 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700957 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300958 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700959 .needs_file = 1,
960 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700961 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600962 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700963 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300964 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700965 .needs_file = 1,
966 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700967 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700968 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600969 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700970 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300971 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600972 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600973 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700974 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700975 [IORING_OP_EPOLL_CTL] = {
976 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600977 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700978 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300979 [IORING_OP_SPLICE] = {
980 .needs_file = 1,
981 .hash_reg_file = 1,
982 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600983 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700984 },
985 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700986 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300987 [IORING_OP_TEE] = {
988 .needs_file = 1,
989 .hash_reg_file = 1,
990 .unbound_nonreg_file = 1,
991 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600992 [IORING_OP_SHUTDOWN] = {
993 .needs_file = 1,
994 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600995 [IORING_OP_RENAMEAT] = {
996 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
997 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
998 },
Jens Axboe14a11432020-09-28 14:27:37 -0600999 [IORING_OP_UNLINKAT] = {
1000 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1001 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1002 },
Jens Axboed3656342019-12-18 09:50:26 -07001003};
1004
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001005enum io_mem_account {
1006 ACCT_LOCKED,
1007 ACCT_PINNED,
1008};
1009
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001010static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1011 struct task_struct *task,
1012 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001013static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001014static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001015 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001016static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1017 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001018
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001019static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001020 unsigned int issue_flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001021static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001022static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001023static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001024static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001025static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001026static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001027static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001028static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001029 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001030 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001031static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001032static struct file *io_file_get(struct io_submit_state *state,
1033 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001034static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001035static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001036
Pavel Begunkov847595d2021-02-04 13:52:06 +00001037static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1038 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001039static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1040 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001041 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001042static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001043
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044static struct kmem_cache *req_cachep;
1045
Jens Axboe09186822020-10-13 15:01:40 -06001046static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048struct sock *io_uring_get_socket(struct file *file)
1049{
1050#if defined(CONFIG_UNIX)
1051 if (file->f_op == &io_uring_fops) {
1052 struct io_ring_ctx *ctx = file->private_data;
1053
1054 return ctx->ring_sock->sk;
1055 }
1056#endif
1057 return NULL;
1058}
1059EXPORT_SYMBOL(io_uring_get_socket);
1060
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001061#define io_for_each_link(pos, head) \
1062 for (pos = (head); pos; pos = pos->link)
1063
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001064static inline void io_clean_op(struct io_kiocb *req)
1065{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001066 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001067 __io_clean_op(req);
1068}
1069
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001070static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001071{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001072 struct io_ring_ctx *ctx = req->ctx;
1073
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001074 if (!req->fixed_rsrc_refs) {
1075 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1076 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001077 }
1078}
1079
Pavel Begunkov08d23632020-11-06 13:00:22 +00001080static bool io_match_task(struct io_kiocb *head,
1081 struct task_struct *task,
1082 struct files_struct *files)
1083{
1084 struct io_kiocb *req;
1085
Jens Axboe84965ff2021-01-23 15:51:11 -07001086 if (task && head->task != task) {
1087 /* in terms of cancelation, always match if req task is dead */
1088 if (head->task->flags & PF_EXITING)
1089 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001090 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001091 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001092 if (!files)
1093 return true;
1094
1095 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001096 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1097 continue;
1098 if (req->file && req->file->f_op == &io_uring_fops)
1099 return true;
1100 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001101 req->work.identity->files == files)
1102 return true;
1103 }
1104 return false;
1105}
1106
Jens Axboe28cea78a2020-09-14 10:51:17 -06001107static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001108{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001109 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001110 struct mm_struct *mm = current->mm;
1111
1112 if (mm) {
1113 kthread_unuse_mm(mm);
1114 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001115 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001116 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001117 if (files) {
1118 struct nsproxy *nsproxy = current->nsproxy;
1119
1120 task_lock(current);
1121 current->files = NULL;
1122 current->nsproxy = NULL;
1123 task_unlock(current);
1124 put_files_struct(files);
1125 put_nsproxy(nsproxy);
1126 }
1127}
1128
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001129static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001130{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001131 if (current->flags & PF_EXITING)
1132 return -EFAULT;
1133
Jens Axboe28cea78a2020-09-14 10:51:17 -06001134 if (!current->files) {
1135 struct files_struct *files;
1136 struct nsproxy *nsproxy;
1137
1138 task_lock(ctx->sqo_task);
1139 files = ctx->sqo_task->files;
1140 if (!files) {
1141 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001142 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001143 }
1144 atomic_inc(&files->count);
1145 get_nsproxy(ctx->sqo_task->nsproxy);
1146 nsproxy = ctx->sqo_task->nsproxy;
1147 task_unlock(ctx->sqo_task);
1148
1149 task_lock(current);
1150 current->files = files;
1151 current->nsproxy = nsproxy;
1152 task_unlock(current);
1153 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001154 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001155}
1156
1157static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1158{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001159 struct mm_struct *mm;
1160
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001161 if (current->flags & PF_EXITING)
1162 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001163 if (current->mm)
1164 return 0;
1165
1166 /* Should never happen */
1167 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1168 return -EFAULT;
1169
1170 task_lock(ctx->sqo_task);
1171 mm = ctx->sqo_task->mm;
1172 if (unlikely(!mm || !mmget_not_zero(mm)))
1173 mm = NULL;
1174 task_unlock(ctx->sqo_task);
1175
1176 if (mm) {
1177 kthread_use_mm(mm);
1178 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001179 }
1180
Jens Axboe4b70cf92020-11-02 10:39:05 -07001181 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001182}
1183
Jens Axboe28cea78a2020-09-14 10:51:17 -06001184static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1185 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001186{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001187 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001188 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001189
1190 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001191 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001192 if (unlikely(ret))
1193 return ret;
1194 }
1195
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001196 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1197 ret = __io_sq_thread_acquire_files(ctx);
1198 if (unlikely(ret))
1199 return ret;
1200 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001201
1202 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001203}
1204
Dennis Zhou91d8f512020-09-16 13:41:05 -07001205static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1206 struct cgroup_subsys_state **cur_css)
1207
1208{
1209#ifdef CONFIG_BLK_CGROUP
1210 /* puts the old one when swapping */
1211 if (*cur_css != ctx->sqo_blkcg_css) {
1212 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1213 *cur_css = ctx->sqo_blkcg_css;
1214 }
1215#endif
1216}
1217
1218static void io_sq_thread_unassociate_blkcg(void)
1219{
1220#ifdef CONFIG_BLK_CGROUP
1221 kthread_associate_blkcg(NULL);
1222#endif
1223}
1224
Jens Axboec40f6372020-06-25 15:39:59 -06001225static inline void req_set_fail_links(struct io_kiocb *req)
1226{
1227 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1228 req->flags |= REQ_F_FAIL_LINK;
1229}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001230
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001231/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001232 * None of these are dereferenced, they are simply used to check if any of
1233 * them have changed. If we're under current and check they are still the
1234 * same, we're fine to grab references to them for actual out-of-line use.
1235 */
1236static void io_init_identity(struct io_identity *id)
1237{
1238 id->files = current->files;
1239 id->mm = current->mm;
1240#ifdef CONFIG_BLK_CGROUP
1241 rcu_read_lock();
1242 id->blkcg_css = blkcg_css();
1243 rcu_read_unlock();
1244#endif
1245 id->creds = current_cred();
1246 id->nsproxy = current->nsproxy;
1247 id->fs = current->fs;
1248 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001249#ifdef CONFIG_AUDIT
1250 id->loginuid = current->loginuid;
1251 id->sessionid = current->sessionid;
1252#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001253 refcount_set(&id->count, 1);
1254}
1255
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001256static inline void __io_req_init_async(struct io_kiocb *req)
1257{
1258 memset(&req->work, 0, sizeof(req->work));
1259 req->flags |= REQ_F_WORK_INITIALIZED;
1260}
1261
Jens Axboe1e6fa522020-10-15 08:46:24 -06001262/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001263 * Note: must call io_req_init_async() for the first time you
1264 * touch any members of io_wq_work.
1265 */
1266static inline void io_req_init_async(struct io_kiocb *req)
1267{
Jens Axboe500a3732020-10-15 17:38:03 -06001268 struct io_uring_task *tctx = current->io_uring;
1269
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001270 if (req->flags & REQ_F_WORK_INITIALIZED)
1271 return;
1272
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001273 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001274
1275 /* Grab a ref if this isn't our static identity */
1276 req->work.identity = tctx->identity;
1277 if (tctx->identity != &tctx->__identity)
1278 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001279}
1280
Jens Axboe2b188cc2019-01-07 10:46:33 -07001281static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1282{
1283 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1284
Jens Axboe0f158b42020-05-14 17:18:39 -06001285 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286}
1287
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001288static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1289{
1290 return !req->timeout.off;
1291}
1292
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1294{
1295 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001296 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297
1298 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1299 if (!ctx)
1300 return NULL;
1301
Jens Axboe78076bb2019-12-04 19:56:40 -07001302 /*
1303 * Use 5 bits less than the max cq entries, that should give us around
1304 * 32 entries per hash list if totally full and uniformly spread.
1305 */
1306 hash_bits = ilog2(p->cq_entries);
1307 hash_bits -= 5;
1308 if (hash_bits <= 0)
1309 hash_bits = 1;
1310 ctx->cancel_hash_bits = hash_bits;
1311 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1312 GFP_KERNEL);
1313 if (!ctx->cancel_hash)
1314 goto err;
1315 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1316
Roman Gushchin21482892019-05-07 10:01:48 -07001317 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001318 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1319 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001320
1321 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001322 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001323 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001324 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001325 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001326 init_completion(&ctx->ref_comp);
1327 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001328 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001329 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 mutex_init(&ctx->uring_lock);
1331 init_waitqueue_head(&ctx->wait);
1332 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001333 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001334 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001335 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001336 spin_lock_init(&ctx->inflight_lock);
1337 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001338 spin_lock_init(&ctx->rsrc_ref_lock);
1339 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001340 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1341 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001343err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001344 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001345 kfree(ctx);
1346 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347}
1348
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001349static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001350{
Jens Axboe2bc99302020-07-09 09:43:27 -06001351 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1352 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001353
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001354 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001355 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001356 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001357
Bob Liu9d858b22019-11-13 18:06:25 +08001358 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001359}
1360
Jens Axboe5c3462c2020-10-15 09:02:33 -06001361static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001362{
Jens Axboe500a3732020-10-15 17:38:03 -06001363 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001364 return;
1365 if (refcount_dec_and_test(&req->work.identity->count))
1366 kfree(req->work.identity);
1367}
1368
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001369static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001370{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001371 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001372 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001373
Pavel Begunkove86d0042021-02-01 18:59:54 +00001374 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001375 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001376#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001377 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001378 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001379#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001380 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001381 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001382 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001383 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001384
Jens Axboe98447d62020-10-14 10:48:51 -06001385 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001386 if (--fs->users)
1387 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001388 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001389 if (fs)
1390 free_fs_struct(fs);
1391 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001392 if (req->work.flags & IO_WQ_WORK_FILES) {
1393 put_files_struct(req->work.identity->files);
1394 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001395 }
1396 if (req->flags & REQ_F_INFLIGHT) {
1397 struct io_ring_ctx *ctx = req->ctx;
1398 struct io_uring_task *tctx = req->task->io_uring;
1399 unsigned long flags;
1400
1401 spin_lock_irqsave(&ctx->inflight_lock, flags);
1402 list_del(&req->inflight_entry);
1403 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1404 req->flags &= ~REQ_F_INFLIGHT;
1405 if (atomic_read(&tctx->in_idle))
1406 wake_up(&tctx->wait);
1407 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001408
Pavel Begunkove86d0042021-02-01 18:59:54 +00001409 req->flags &= ~REQ_F_WORK_INITIALIZED;
1410 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1411 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001412 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001413}
1414
1415/*
1416 * Create a private copy of io_identity, since some fields don't match
1417 * the current context.
1418 */
1419static bool io_identity_cow(struct io_kiocb *req)
1420{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001421 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001422 const struct cred *creds = NULL;
1423 struct io_identity *id;
1424
1425 if (req->work.flags & IO_WQ_WORK_CREDS)
1426 creds = req->work.identity->creds;
1427
1428 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1429 if (unlikely(!id)) {
1430 req->work.flags |= IO_WQ_WORK_CANCEL;
1431 return false;
1432 }
1433
1434 /*
1435 * We can safely just re-init the creds we copied Either the field
1436 * matches the current one, or we haven't grabbed it yet. The only
1437 * exception is ->creds, through registered personalities, so handle
1438 * that one separately.
1439 */
1440 io_init_identity(id);
1441 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001442 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001443
1444 /* add one for this request */
1445 refcount_inc(&id->count);
1446
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001447 /* drop tctx and req identity references, if needed */
1448 if (tctx->identity != &tctx->__identity &&
1449 refcount_dec_and_test(&tctx->identity->count))
1450 kfree(tctx->identity);
1451 if (req->work.identity != &tctx->__identity &&
1452 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001453 kfree(req->work.identity);
1454
1455 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001456 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001457 return true;
1458}
1459
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001460static void io_req_track_inflight(struct io_kiocb *req)
1461{
1462 struct io_ring_ctx *ctx = req->ctx;
1463
1464 if (!(req->flags & REQ_F_INFLIGHT)) {
1465 io_req_init_async(req);
1466 req->flags |= REQ_F_INFLIGHT;
1467
1468 spin_lock_irq(&ctx->inflight_lock);
1469 list_add(&req->inflight_entry, &ctx->inflight_list);
1470 spin_unlock_irq(&ctx->inflight_lock);
1471 }
1472}
1473
Jens Axboe1e6fa522020-10-15 08:46:24 -06001474static bool io_grab_identity(struct io_kiocb *req)
1475{
1476 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001477 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001478
Jens Axboe69228332020-10-20 14:28:41 -06001479 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1480 if (id->fsize != rlimit(RLIMIT_FSIZE))
1481 return false;
1482 req->work.flags |= IO_WQ_WORK_FSIZE;
1483 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001484#ifdef CONFIG_BLK_CGROUP
1485 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1486 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1487 rcu_read_lock();
1488 if (id->blkcg_css != blkcg_css()) {
1489 rcu_read_unlock();
1490 return false;
1491 }
1492 /*
1493 * This should be rare, either the cgroup is dying or the task
1494 * is moving cgroups. Just punt to root for the handful of ios.
1495 */
1496 if (css_tryget_online(id->blkcg_css))
1497 req->work.flags |= IO_WQ_WORK_BLKCG;
1498 rcu_read_unlock();
1499 }
1500#endif
1501 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1502 if (id->creds != current_cred())
1503 return false;
1504 get_cred(id->creds);
1505 req->work.flags |= IO_WQ_WORK_CREDS;
1506 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001507#ifdef CONFIG_AUDIT
1508 if (!uid_eq(current->loginuid, id->loginuid) ||
1509 current->sessionid != id->sessionid)
1510 return false;
1511#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001512 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1513 (def->work_flags & IO_WQ_WORK_FS)) {
1514 if (current->fs != id->fs)
1515 return false;
1516 spin_lock(&id->fs->lock);
1517 if (!id->fs->in_exec) {
1518 id->fs->users++;
1519 req->work.flags |= IO_WQ_WORK_FS;
1520 } else {
1521 req->work.flags |= IO_WQ_WORK_CANCEL;
1522 }
1523 spin_unlock(&current->fs->lock);
1524 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001525 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1526 (def->work_flags & IO_WQ_WORK_FILES) &&
1527 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1528 if (id->files != current->files ||
1529 id->nsproxy != current->nsproxy)
1530 return false;
1531 atomic_inc(&id->files->count);
1532 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001533 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001534 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001535 }
Jens Axboe77788772020-12-29 10:50:46 -07001536 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1537 (def->work_flags & IO_WQ_WORK_MM)) {
1538 if (id->mm != current->mm)
1539 return false;
1540 mmgrab(id->mm);
1541 req->work.flags |= IO_WQ_WORK_MM;
1542 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001543
1544 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001545}
1546
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001547static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001548{
Jens Axboed3656342019-12-18 09:50:26 -07001549 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001550 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001551
Pavel Begunkov16d59802020-07-12 16:16:47 +03001552 io_req_init_async(req);
1553
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001554 if (req->flags & REQ_F_FORCE_ASYNC)
1555 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1556
Jens Axboed3656342019-12-18 09:50:26 -07001557 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001558 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001559 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001560 } else {
1561 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001562 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001563 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001564
Jens Axboe1e6fa522020-10-15 08:46:24 -06001565 /* if we fail grabbing identity, we must COW, regrab, and retry */
1566 if (io_grab_identity(req))
1567 return;
1568
1569 if (!io_identity_cow(req))
1570 return;
1571
1572 /* can't fail at this point */
1573 if (!io_grab_identity(req))
1574 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001575}
1576
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001577static void io_prep_async_link(struct io_kiocb *req)
1578{
1579 struct io_kiocb *cur;
1580
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001581 io_for_each_link(cur, req)
1582 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001583}
1584
Jens Axboe7271ef32020-08-10 09:55:22 -06001585static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001586{
Jackie Liua197f662019-11-08 08:09:12 -07001587 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001588 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001589
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001590 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1591 &req->work, req->flags);
1592 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001593 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001594}
1595
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001596static void io_queue_async_work(struct io_kiocb *req)
1597{
Jens Axboe7271ef32020-08-10 09:55:22 -06001598 struct io_kiocb *link;
1599
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001600 /* init ->work of the whole link before punting */
1601 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001602 link = __io_queue_async_work(req);
1603
1604 if (link)
1605 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001606}
1607
Jens Axboe5262f562019-09-17 12:26:57 -06001608static void io_kill_timeout(struct io_kiocb *req)
1609{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001610 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001611 int ret;
1612
Jens Axboee8c2bc12020-08-15 18:44:09 -07001613 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001614 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001615 atomic_set(&req->ctx->cq_timeouts,
1616 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001617 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001618 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001619 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001620 }
1621}
1622
Jens Axboe76e1b642020-09-26 15:05:03 -06001623/*
1624 * Returns true if we found and killed one or more timeouts
1625 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001626static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1627 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001628{
1629 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001630 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001631
1632 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001633 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001634 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001635 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001636 canceled++;
1637 }
Jens Axboef3606e32020-09-22 08:18:24 -06001638 }
Jens Axboe5262f562019-09-17 12:26:57 -06001639 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001640 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001641}
1642
Pavel Begunkov04518942020-05-26 20:34:05 +03001643static void __io_queue_deferred(struct io_ring_ctx *ctx)
1644{
1645 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001646 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1647 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001648
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001649 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001650 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001651 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001652 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001653 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001654 } while (!list_empty(&ctx->defer_list));
1655}
1656
Pavel Begunkov360428f2020-05-30 14:54:17 +03001657static void io_flush_timeouts(struct io_ring_ctx *ctx)
1658{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001659 u32 seq;
1660
1661 if (list_empty(&ctx->timeout_list))
1662 return;
1663
1664 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1665
1666 do {
1667 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001668 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001669 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001670
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001671 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001672 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001673
1674 /*
1675 * Since seq can easily wrap around over time, subtract
1676 * the last seq at which timeouts were flushed before comparing.
1677 * Assuming not more than 2^31-1 events have happened since,
1678 * these subtractions won't have wrapped, so we can check if
1679 * target is in [last_seq, current_seq] by comparing the two.
1680 */
1681 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1682 events_got = seq - ctx->cq_last_tm_flush;
1683 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001684 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001685
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001686 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001687 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001688 } while (!list_empty(&ctx->timeout_list));
1689
1690 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001691}
1692
Jens Axboede0617e2019-04-06 21:51:27 -06001693static void io_commit_cqring(struct io_ring_ctx *ctx)
1694{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001695 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001696
1697 /* order cqe stores with ring update */
1698 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001699
Pavel Begunkov04518942020-05-26 20:34:05 +03001700 if (unlikely(!list_empty(&ctx->defer_list)))
1701 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001702}
1703
Jens Axboe90554202020-09-03 12:12:41 -06001704static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1705{
1706 struct io_rings *r = ctx->rings;
1707
1708 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1709}
1710
Pavel Begunkov888aae22021-01-19 13:32:39 +00001711static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1712{
1713 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1714}
1715
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1717{
Hristo Venev75b28af2019-08-26 17:23:46 +00001718 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719 unsigned tail;
1720
Stefan Bühler115e12e2019-04-24 23:54:18 +02001721 /*
1722 * writes to the cq entry need to come after reading head; the
1723 * control dependency is enough as we're using WRITE_ONCE to
1724 * fill the cq entry
1725 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001726 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727 return NULL;
1728
Pavel Begunkov888aae22021-01-19 13:32:39 +00001729 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001730 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731}
1732
Jens Axboef2842ab2020-01-08 11:04:00 -07001733static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1734{
Jens Axboef0b493e2020-02-01 21:30:11 -07001735 if (!ctx->cq_ev_fd)
1736 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001737 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1738 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001739 if (!ctx->eventfd_async)
1740 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001741 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001742}
1743
Jens Axboeb41e9852020-02-17 09:52:41 -07001744static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001745{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001746 /* see waitqueue_active() comment */
1747 smp_mb();
1748
Jens Axboe8c838782019-03-12 15:48:16 -06001749 if (waitqueue_active(&ctx->wait))
1750 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001751 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1752 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001753 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001754 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001755 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001756 wake_up_interruptible(&ctx->cq_wait);
1757 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1758 }
Jens Axboe8c838782019-03-12 15:48:16 -06001759}
1760
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001761static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1762{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001763 /* see waitqueue_active() comment */
1764 smp_mb();
1765
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001766 if (ctx->flags & IORING_SETUP_SQPOLL) {
1767 if (waitqueue_active(&ctx->wait))
1768 wake_up(&ctx->wait);
1769 }
1770 if (io_should_trigger_evfd(ctx))
1771 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001772 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001773 wake_up_interruptible(&ctx->cq_wait);
1774 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1775 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001776}
1777
Jens Axboec4a2ed72019-11-21 21:01:26 -07001778/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001779static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1780 struct task_struct *tsk,
1781 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001783 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001784 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001785 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001786 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001787 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001788 LIST_HEAD(list);
1789
Pavel Begunkove23de152020-12-17 00:24:37 +00001790 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1791 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001792
Jens Axboeb18032b2021-01-24 16:58:56 -07001793 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001794 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001795 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001796 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001797 continue;
1798
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001799 cqe = io_get_cqring(ctx);
1800 if (!cqe && !force)
1801 break;
1802
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001803 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001804 if (cqe) {
1805 WRITE_ONCE(cqe->user_data, req->user_data);
1806 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001807 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001808 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001809 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001810 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001811 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001812 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001813 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001814 }
1815
Pavel Begunkov09e88402020-12-17 00:24:38 +00001816 all_flushed = list_empty(&ctx->cq_overflow_list);
1817 if (all_flushed) {
1818 clear_bit(0, &ctx->sq_check_overflow);
1819 clear_bit(0, &ctx->cq_check_overflow);
1820 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1821 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001822
Jens Axboeb18032b2021-01-24 16:58:56 -07001823 if (posted)
1824 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001825 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001826 if (posted)
1827 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001828
1829 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001830 req = list_first_entry(&list, struct io_kiocb, compl.list);
1831 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001832 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001833 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001834
Pavel Begunkov09e88402020-12-17 00:24:38 +00001835 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001836}
1837
Pavel Begunkov6c503152021-01-04 20:36:36 +00001838static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1839 struct task_struct *tsk,
1840 struct files_struct *files)
1841{
1842 if (test_bit(0, &ctx->cq_check_overflow)) {
1843 /* iopoll syncs against uring_lock, not completion_lock */
1844 if (ctx->flags & IORING_SETUP_IOPOLL)
1845 mutex_lock(&ctx->uring_lock);
1846 __io_cqring_overflow_flush(ctx, force, tsk, files);
1847 if (ctx->flags & IORING_SETUP_IOPOLL)
1848 mutex_unlock(&ctx->uring_lock);
1849 }
1850}
1851
Jens Axboebcda7ba2020-02-23 16:42:51 -07001852static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001854 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855 struct io_uring_cqe *cqe;
1856
Jens Axboe78e19bb2019-11-06 15:21:34 -07001857 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001858
Jens Axboe2b188cc2019-01-07 10:46:33 -07001859 /*
1860 * If we can't get a cq entry, userspace overflowed the
1861 * submission (by quite a lot). Increment the overflow count in
1862 * the ring.
1863 */
1864 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001865 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001866 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001868 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001869 } else if (ctx->cq_overflow_flushed ||
1870 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001871 /*
1872 * If we're in ring overflow flush mode, or in task cancel mode,
1873 * then we cannot store the request for later flushing, we need
1874 * to drop it on the floor.
1875 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001876 ctx->cached_cq_overflow++;
1877 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001878 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001879 if (list_empty(&ctx->cq_overflow_list)) {
1880 set_bit(0, &ctx->sq_check_overflow);
1881 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001882 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001883 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001884 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001885 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001886 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001887 refcount_inc(&req->refs);
1888 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889 }
1890}
1891
Jens Axboebcda7ba2020-02-23 16:42:51 -07001892static void io_cqring_fill_event(struct io_kiocb *req, long res)
1893{
1894 __io_cqring_fill_event(req, res, 0);
1895}
1896
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001897static void io_req_complete_post(struct io_kiocb *req, long res,
1898 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901 unsigned long flags;
1902
1903 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001904 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905 io_commit_cqring(ctx);
1906 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1907
Jens Axboe8c838782019-03-12 15:48:16 -06001908 io_cqring_ev_posted(ctx);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001909}
1910
1911static inline void io_req_complete_nostate(struct io_kiocb *req, long res,
1912 unsigned int cflags)
1913{
1914 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001915 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916}
1917
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001918static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001919 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001920{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001921 io_clean_op(req);
1922 req->result = res;
1923 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001924 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001925}
1926
Pavel Begunkov889fca72021-02-10 00:03:09 +00001927static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1928 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001929{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001930 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1931 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001932 else
Pavel Begunkov889fca72021-02-10 00:03:09 +00001933 io_req_complete_nostate(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001934}
1935
1936static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001937{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001938 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001939}
1940
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001941static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001943 struct io_submit_state *state = &ctx->submit_state;
1944
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001945 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001946 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001947 size_t sz;
1948 int ret;
1949
1950 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001951 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1952
1953 /*
1954 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1955 * retry single alloc to be on the safe side.
1956 */
1957 if (unlikely(ret <= 0)) {
1958 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1959 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001960 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001961 ret = 1;
1962 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001963 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001964 }
1965
Pavel Begunkov291b2822020-09-30 22:57:01 +03001966 state->free_reqs--;
1967 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001968}
1969
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001970static inline void io_put_file(struct io_kiocb *req, struct file *file,
1971 bool fixed)
1972{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001973 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001974 fput(file);
1975}
1976
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001977static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001978{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001979 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001980
Jens Axboee8c2bc12020-08-15 18:44:09 -07001981 if (req->async_data)
1982 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001983 if (req->file)
1984 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001985 if (req->fixed_rsrc_refs)
1986 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001987 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001988}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001989
Pavel Begunkov7c660732021-01-25 11:42:21 +00001990static inline void io_put_task(struct task_struct *task, int nr)
1991{
1992 struct io_uring_task *tctx = task->io_uring;
1993
1994 percpu_counter_sub(&tctx->inflight, nr);
1995 if (unlikely(atomic_read(&tctx->in_idle)))
1996 wake_up(&tctx->wait);
1997 put_task_struct_many(task, nr);
1998}
1999
Pavel Begunkov216578e2020-10-13 09:44:00 +01002000static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002001{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002002 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002003
Pavel Begunkov216578e2020-10-13 09:44:00 +01002004 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002005 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002006
Pavel Begunkov3893f392021-02-10 00:03:15 +00002007 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002008 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002009}
2010
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002011static inline void io_remove_next_linked(struct io_kiocb *req)
2012{
2013 struct io_kiocb *nxt = req->link;
2014
2015 req->link = nxt->link;
2016 nxt->link = NULL;
2017}
2018
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002019static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002020{
Jackie Liua197f662019-11-08 08:09:12 -07002021 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002022 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002023 bool cancelled = false;
2024 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002025
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002026 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002027 link = req->link;
2028
Pavel Begunkov900fad42020-10-19 16:39:16 +01002029 /*
2030 * Can happen if a linked timeout fired and link had been like
2031 * req -> link t-out -> link t-out [-> ...]
2032 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002033 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2034 struct io_timeout_data *io = link->async_data;
2035 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002036
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002037 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002038 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002039 ret = hrtimer_try_to_cancel(&io->timer);
2040 if (ret != -1) {
2041 io_cqring_fill_event(link, -ECANCELED);
2042 io_commit_cqring(ctx);
2043 cancelled = true;
2044 }
2045 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002046 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002047 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002048
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002049 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002050 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002051 io_put_req(link);
2052 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002053}
2054
Jens Axboe4d7dd462019-11-20 13:03:52 -07002055
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002056static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002057{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002058 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002059 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002060 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002061
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002062 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002063 link = req->link;
2064 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002065
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002066 while (link) {
2067 nxt = link->link;
2068 link->link = NULL;
2069
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002070 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002071 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002072
2073 /*
2074 * It's ok to free under spinlock as they're not linked anymore,
2075 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2076 * work.fs->lock.
2077 */
2078 if (link->flags & REQ_F_WORK_INITIALIZED)
2079 io_put_req_deferred(link, 2);
2080 else
2081 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002082 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002083 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002084 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002085 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002086
Jens Axboe2665abf2019-11-05 12:40:47 -07002087 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002088}
2089
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002090static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002091{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002092 if (req->flags & REQ_F_LINK_TIMEOUT)
2093 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002094
Jens Axboe9e645e112019-05-10 16:07:28 -06002095 /*
2096 * If LINK is set, we have dependent requests in this chain. If we
2097 * didn't fail this request, queue the first one up, moving any other
2098 * dependencies to the next request. In case of failure, fail the rest
2099 * of the chain.
2100 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002101 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2102 struct io_kiocb *nxt = req->link;
2103
2104 req->link = NULL;
2105 return nxt;
2106 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002107 io_fail_links(req);
2108 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002109}
Jens Axboe2665abf2019-11-05 12:40:47 -07002110
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002111static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002112{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002113 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002114 return NULL;
2115 return __io_req_find_next(req);
2116}
2117
Jens Axboe355fb9e2020-10-22 20:19:35 -06002118static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002119{
2120 struct task_struct *tsk = req->task;
2121 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002122 enum task_work_notify_mode notify;
2123 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002124
Jens Axboe6200b0a2020-09-13 14:38:30 -06002125 if (tsk->flags & PF_EXITING)
2126 return -ESRCH;
2127
Jens Axboec2c4c832020-07-01 15:37:11 -06002128 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002129 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2130 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2131 * processing task_work. There's no reliable way to tell if TWA_RESUME
2132 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002133 */
Jens Axboe91989c72020-10-16 09:02:26 -06002134 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002135 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002136 notify = TWA_SIGNAL;
2137
Jens Axboe87c43112020-09-30 21:00:14 -06002138 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002139 if (!ret)
2140 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002141
Jens Axboec2c4c832020-07-01 15:37:11 -06002142 return ret;
2143}
2144
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002145static void io_req_task_work_add_fallback(struct io_kiocb *req,
2146 void (*cb)(struct callback_head *))
2147{
2148 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2149
2150 init_task_work(&req->task_work, cb);
2151 task_work_add(tsk, &req->task_work, TWA_NONE);
2152 wake_up_process(tsk);
2153}
2154
Jens Axboec40f6372020-06-25 15:39:59 -06002155static void __io_req_task_cancel(struct io_kiocb *req, int error)
2156{
2157 struct io_ring_ctx *ctx = req->ctx;
2158
2159 spin_lock_irq(&ctx->completion_lock);
2160 io_cqring_fill_event(req, error);
2161 io_commit_cqring(ctx);
2162 spin_unlock_irq(&ctx->completion_lock);
2163
2164 io_cqring_ev_posted(ctx);
2165 req_set_fail_links(req);
2166 io_double_put_req(req);
2167}
2168
2169static void io_req_task_cancel(struct callback_head *cb)
2170{
2171 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002172 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002173
2174 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002175 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002176}
2177
2178static void __io_req_task_submit(struct io_kiocb *req)
2179{
2180 struct io_ring_ctx *ctx = req->ctx;
2181
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002182 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002183 if (!ctx->sqo_dead &&
2184 !__io_sq_thread_acquire_mm(ctx) &&
2185 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002186 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002187 else
Jens Axboec40f6372020-06-25 15:39:59 -06002188 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002189 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002190}
2191
Jens Axboec40f6372020-06-25 15:39:59 -06002192static void io_req_task_submit(struct callback_head *cb)
2193{
2194 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002195 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002196
2197 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002198 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002199}
2200
2201static void io_req_task_queue(struct io_kiocb *req)
2202{
Jens Axboec40f6372020-06-25 15:39:59 -06002203 int ret;
2204
2205 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002206 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002207
Jens Axboe355fb9e2020-10-22 20:19:35 -06002208 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002209 if (unlikely(ret))
2210 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002211}
2212
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002213static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002214{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002215 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002216
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002217 if (nxt)
2218 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002219}
2220
Jens Axboe9e645e112019-05-10 16:07:28 -06002221static void io_free_req(struct io_kiocb *req)
2222{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002223 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002224 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002225}
2226
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002227struct req_batch {
2228 void *reqs[IO_IOPOLL_BATCH];
2229 int to_free;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002230 int ctx_refs;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002231
2232 struct task_struct *task;
2233 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002234};
2235
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002236static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002237{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002238 rb->to_free = 0;
2239 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002240 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002241 rb->task = NULL;
2242}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002243
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002244static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2245 struct req_batch *rb)
2246{
2247 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002248 rb->to_free = 0;
2249}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002250
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002251static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2252 struct req_batch *rb)
2253{
2254 if (rb->to_free)
2255 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002256 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002257 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002258 rb->task = NULL;
2259 }
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002260 if (rb->ctx_refs)
2261 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002262}
2263
2264static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2265{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002266 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002267
Jens Axboee3bc8e92020-09-24 08:45:57 -06002268 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002269 if (rb->task)
2270 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002271 rb->task = req->task;
2272 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002273 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002274 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002275 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002276
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002277 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002278 rb->reqs[rb->to_free++] = req;
2279 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2280 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002281}
2282
Pavel Begunkov905c1722021-02-10 00:03:14 +00002283static void io_submit_flush_completions(struct io_comp_state *cs,
2284 struct io_ring_ctx *ctx)
2285{
2286 int i, nr = cs->nr;
2287 struct io_kiocb *req;
2288 struct req_batch rb;
2289
2290 io_init_req_batch(&rb);
2291 spin_lock_irq(&ctx->completion_lock);
2292 for (i = 0; i < nr; i++) {
2293 req = cs->reqs[i];
2294 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2295 }
2296 io_commit_cqring(ctx);
2297 spin_unlock_irq(&ctx->completion_lock);
2298
2299 io_cqring_ev_posted(ctx);
2300 for (i = 0; i < nr; i++) {
2301 req = cs->reqs[i];
2302
2303 /* submission and completion refs */
2304 if (refcount_sub_and_test(2, &req->refs))
2305 io_req_free_batch(&rb, req);
2306 }
2307
2308 io_req_free_batch_finish(ctx, &rb);
2309 cs->nr = 0;
2310}
2311
Jens Axboeba816ad2019-09-28 11:36:45 -06002312/*
2313 * Drop reference to request, return next in chain (if there is one) if this
2314 * was the last reference to this request.
2315 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002316static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002317{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002318 struct io_kiocb *nxt = NULL;
2319
Jens Axboe2a44f462020-02-25 13:25:41 -07002320 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002321 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002322 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002323 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002324 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325}
2326
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327static void io_put_req(struct io_kiocb *req)
2328{
Jens Axboedef596e2019-01-09 08:59:42 -07002329 if (refcount_dec_and_test(&req->refs))
2330 io_free_req(req);
2331}
2332
Pavel Begunkov216578e2020-10-13 09:44:00 +01002333static void io_put_req_deferred_cb(struct callback_head *cb)
2334{
2335 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2336
2337 io_free_req(req);
2338}
2339
2340static void io_free_req_deferred(struct io_kiocb *req)
2341{
2342 int ret;
2343
2344 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002345 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002346 if (unlikely(ret))
2347 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002348}
2349
2350static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2351{
2352 if (refcount_sub_and_test(refs, &req->refs))
2353 io_free_req_deferred(req);
2354}
2355
Jens Axboe978db572019-11-14 22:39:04 -07002356static void io_double_put_req(struct io_kiocb *req)
2357{
2358 /* drop both submit and complete references */
2359 if (refcount_sub_and_test(2, &req->refs))
2360 io_free_req(req);
2361}
2362
Pavel Begunkov6c503152021-01-04 20:36:36 +00002363static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002364{
2365 /* See comment at the top of this file */
2366 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002367 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002368}
2369
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002370static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2371{
2372 struct io_rings *rings = ctx->rings;
2373
2374 /* make sure SQ entry isn't read before tail */
2375 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2376}
2377
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002378static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002379{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002380 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002381
Jens Axboebcda7ba2020-02-23 16:42:51 -07002382 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2383 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002384 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002385 kfree(kbuf);
2386 return cflags;
2387}
2388
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002389static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2390{
2391 struct io_buffer *kbuf;
2392
2393 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2394 return io_put_kbuf(req, kbuf);
2395}
2396
Jens Axboe4c6e2772020-07-01 11:29:10 -06002397static inline bool io_run_task_work(void)
2398{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002399 /*
2400 * Not safe to run on exiting task, and the task_work handling will
2401 * not add work to such a task.
2402 */
2403 if (unlikely(current->flags & PF_EXITING))
2404 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002405 if (current->task_works) {
2406 __set_current_state(TASK_RUNNING);
2407 task_work_run();
2408 return true;
2409 }
2410
2411 return false;
2412}
2413
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002414static void io_iopoll_queue(struct list_head *again)
2415{
2416 struct io_kiocb *req;
2417
2418 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002419 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2420 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002421 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002422 } while (!list_empty(again));
2423}
2424
Jens Axboedef596e2019-01-09 08:59:42 -07002425/*
2426 * Find and free completed poll iocbs
2427 */
2428static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2429 struct list_head *done)
2430{
Jens Axboe8237e042019-12-28 10:48:22 -07002431 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002432 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002433 LIST_HEAD(again);
2434
2435 /* order with ->result store in io_complete_rw_iopoll() */
2436 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002437
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002438 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002439 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002440 int cflags = 0;
2441
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002442 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002443 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002444 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002445 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002446 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002447 continue;
2448 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002449 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002450
Jens Axboebcda7ba2020-02-23 16:42:51 -07002451 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002452 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002453
2454 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002455 (*nr_events)++;
2456
Pavel Begunkovc3524382020-06-28 12:52:32 +03002457 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002458 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002459 }
Jens Axboedef596e2019-01-09 08:59:42 -07002460
Jens Axboe09bb8392019-03-13 12:39:28 -06002461 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002462 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002463 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002464
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002465 if (!list_empty(&again))
2466 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002467}
2468
Jens Axboedef596e2019-01-09 08:59:42 -07002469static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2470 long min)
2471{
2472 struct io_kiocb *req, *tmp;
2473 LIST_HEAD(done);
2474 bool spin;
2475 int ret;
2476
2477 /*
2478 * Only spin for completions if we don't have multiple devices hanging
2479 * off our complete list, and we're under the requested amount.
2480 */
2481 spin = !ctx->poll_multi_file && *nr_events < min;
2482
2483 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002484 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002485 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002486
2487 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002488 * Move completed and retryable entries to our local lists.
2489 * If we find a request that requires polling, break out
2490 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002491 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002492 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002493 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002494 continue;
2495 }
2496 if (!list_empty(&done))
2497 break;
2498
2499 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2500 if (ret < 0)
2501 break;
2502
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002503 /* iopoll may have completed current req */
2504 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002505 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002506
Jens Axboedef596e2019-01-09 08:59:42 -07002507 if (ret && spin)
2508 spin = false;
2509 ret = 0;
2510 }
2511
2512 if (!list_empty(&done))
2513 io_iopoll_complete(ctx, nr_events, &done);
2514
2515 return ret;
2516}
2517
2518/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002519 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002520 * non-spinning poll check - we'll still enter the driver poll loop, but only
2521 * as a non-spinning completion check.
2522 */
2523static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2524 long min)
2525{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002526 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002527 int ret;
2528
2529 ret = io_do_iopoll(ctx, nr_events, min);
2530 if (ret < 0)
2531 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002532 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002533 return 0;
2534 }
2535
2536 return 1;
2537}
2538
2539/*
2540 * We can't just wait for polled events to come to us, we have to actively
2541 * find and complete them.
2542 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002543static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002544{
2545 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2546 return;
2547
2548 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002549 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002550 unsigned int nr_events = 0;
2551
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002552 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002553
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002554 /* let it sleep and repeat later if can't complete a request */
2555 if (nr_events == 0)
2556 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002557 /*
2558 * Ensure we allow local-to-the-cpu processing to take place,
2559 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002560 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002561 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002562 if (need_resched()) {
2563 mutex_unlock(&ctx->uring_lock);
2564 cond_resched();
2565 mutex_lock(&ctx->uring_lock);
2566 }
Jens Axboedef596e2019-01-09 08:59:42 -07002567 }
2568 mutex_unlock(&ctx->uring_lock);
2569}
2570
Pavel Begunkov7668b922020-07-07 16:36:21 +03002571static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002572{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002573 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002574 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002575
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002576 /*
2577 * We disallow the app entering submit/complete with polling, but we
2578 * still need to lock the ring to prevent racing with polled issue
2579 * that got punted to a workqueue.
2580 */
2581 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002582 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002583 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002584 * Don't enter poll loop if we already have events pending.
2585 * If we do, we can potentially be spinning for commands that
2586 * already triggered a CQE (eg in error).
2587 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002588 if (test_bit(0, &ctx->cq_check_overflow))
2589 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2590 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002591 break;
2592
2593 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002594 * If a submit got punted to a workqueue, we can have the
2595 * application entering polling for a command before it gets
2596 * issued. That app will hold the uring_lock for the duration
2597 * of the poll right here, so we need to take a breather every
2598 * now and then to ensure that the issue has a chance to add
2599 * the poll to the issued list. Otherwise we can spin here
2600 * forever, while the workqueue is stuck trying to acquire the
2601 * very same mutex.
2602 */
2603 if (!(++iters & 7)) {
2604 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002605 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002606 mutex_lock(&ctx->uring_lock);
2607 }
2608
Pavel Begunkov7668b922020-07-07 16:36:21 +03002609 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002610 if (ret <= 0)
2611 break;
2612 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002613 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002614
Jens Axboe500f9fb2019-08-19 12:15:59 -06002615 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002616 return ret;
2617}
2618
Jens Axboe491381ce2019-10-17 09:20:46 -06002619static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620{
Jens Axboe491381ce2019-10-17 09:20:46 -06002621 /*
2622 * Tell lockdep we inherited freeze protection from submission
2623 * thread.
2624 */
2625 if (req->flags & REQ_F_ISREG) {
2626 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627
Jens Axboe491381ce2019-10-17 09:20:46 -06002628 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002630 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631}
2632
Jens Axboea1d7c392020-06-22 11:09:46 -06002633static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002634 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635{
Jens Axboe9adbd452019-12-20 08:45:55 -07002636 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002637 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638
Jens Axboe491381ce2019-10-17 09:20:46 -06002639 if (kiocb->ki_flags & IOCB_WRITE)
2640 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002642 if (res != req->result)
2643 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002644 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002645 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002646 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002647}
2648
Jens Axboeb63534c2020-06-04 11:28:00 -06002649#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002650static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002651{
2652 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002653 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002654 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002655
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002656 /* already prepared */
2657 if (req->async_data)
2658 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002659
2660 switch (req->opcode) {
2661 case IORING_OP_READV:
2662 case IORING_OP_READ_FIXED:
2663 case IORING_OP_READ:
2664 rw = READ;
2665 break;
2666 case IORING_OP_WRITEV:
2667 case IORING_OP_WRITE_FIXED:
2668 case IORING_OP_WRITE:
2669 rw = WRITE;
2670 break;
2671 default:
2672 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2673 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002674 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002675 }
2676
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002677 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2678 if (ret < 0)
2679 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002680 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002681}
Jens Axboeb63534c2020-06-04 11:28:00 -06002682#endif
2683
2684static bool io_rw_reissue(struct io_kiocb *req, long res)
2685{
2686#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002687 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002688 int ret;
2689
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002690 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002691 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002692 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002693 if (!S_ISBLK(mode) && !S_ISREG(mode))
2694 return false;
2695 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002696 return false;
2697
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002698 lockdep_assert_held(&req->ctx->uring_lock);
2699
Jens Axboe28cea78a2020-09-14 10:51:17 -06002700 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002701
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002702 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002703 refcount_inc(&req->refs);
2704 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002705 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002706 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002707 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002708#endif
2709 return false;
2710}
2711
Jens Axboea1d7c392020-06-22 11:09:46 -06002712static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002713 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002714{
2715 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002716 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002717}
2718
2719static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2720{
Jens Axboe9adbd452019-12-20 08:45:55 -07002721 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002722
Pavel Begunkov889fca72021-02-10 00:03:09 +00002723 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002724}
2725
Jens Axboedef596e2019-01-09 08:59:42 -07002726static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2727{
Jens Axboe9adbd452019-12-20 08:45:55 -07002728 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002729
Jens Axboe491381ce2019-10-17 09:20:46 -06002730 if (kiocb->ki_flags & IOCB_WRITE)
2731 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002732
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002733 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002734 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002735
2736 WRITE_ONCE(req->result, res);
2737 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002738 smp_wmb();
2739 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002740}
2741
2742/*
2743 * After the iocb has been issued, it's safe to be found on the poll list.
2744 * Adding the kiocb to the list AFTER submission ensures that we don't
2745 * find it from a io_iopoll_getevents() thread before the issuer is done
2746 * accessing the kiocb cookie.
2747 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002748static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002749{
2750 struct io_ring_ctx *ctx = req->ctx;
2751
2752 /*
2753 * Track whether we have multiple files in our lists. This will impact
2754 * how we do polling eventually, not spinning if we're on potentially
2755 * different devices.
2756 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002757 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002758 ctx->poll_multi_file = false;
2759 } else if (!ctx->poll_multi_file) {
2760 struct io_kiocb *list_req;
2761
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002762 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002763 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002764 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002765 ctx->poll_multi_file = true;
2766 }
2767
2768 /*
2769 * For fast devices, IO may have already completed. If it has, add
2770 * it to the front so we find it first.
2771 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002772 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002773 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002774 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002775 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002776
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002777 /*
2778 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2779 * task context or in io worker task context. If current task context is
2780 * sq thread, we don't need to check whether should wake up sq thread.
2781 */
2782 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002783 wq_has_sleeper(&ctx->sq_data->wait))
2784 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002785}
2786
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002787static inline void io_state_file_put(struct io_submit_state *state)
2788{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002789 if (state->file_refs) {
2790 fput_many(state->file, state->file_refs);
2791 state->file_refs = 0;
2792 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002793}
2794
2795/*
2796 * Get as many references to a file as we have IOs left in this submission,
2797 * assuming most submissions are for one file, or at least that each file
2798 * has more than one submission.
2799 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002800static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002801{
2802 if (!state)
2803 return fget(fd);
2804
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002805 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002806 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002807 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002808 return state->file;
2809 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002810 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002811 }
2812 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002813 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002814 return NULL;
2815
2816 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002817 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002818 return state->file;
2819}
2820
Jens Axboe4503b762020-06-01 10:00:27 -06002821static bool io_bdev_nowait(struct block_device *bdev)
2822{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002823 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002824}
2825
Jens Axboe2b188cc2019-01-07 10:46:33 -07002826/*
2827 * If we tracked the file through the SCM inflight mechanism, we could support
2828 * any file. For now, just ensure that anything potentially problematic is done
2829 * inline.
2830 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002831static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832{
2833 umode_t mode = file_inode(file)->i_mode;
2834
Jens Axboe4503b762020-06-01 10:00:27 -06002835 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002836 if (IS_ENABLED(CONFIG_BLOCK) &&
2837 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002838 return true;
2839 return false;
2840 }
2841 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002842 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002843 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002844 if (IS_ENABLED(CONFIG_BLOCK) &&
2845 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002846 file->f_op != &io_uring_fops)
2847 return true;
2848 return false;
2849 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850
Jens Axboec5b85622020-06-09 19:23:05 -06002851 /* any ->read/write should understand O_NONBLOCK */
2852 if (file->f_flags & O_NONBLOCK)
2853 return true;
2854
Jens Axboeaf197f52020-04-28 13:15:06 -06002855 if (!(file->f_mode & FMODE_NOWAIT))
2856 return false;
2857
2858 if (rw == READ)
2859 return file->f_op->read_iter != NULL;
2860
2861 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862}
2863
Pavel Begunkova88fc402020-09-30 22:57:53 +03002864static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002865{
Jens Axboedef596e2019-01-09 08:59:42 -07002866 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002867 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002868 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002869 unsigned ioprio;
2870 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002872 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002873 req->flags |= REQ_F_ISREG;
2874
Jens Axboe2b188cc2019-01-07 10:46:33 -07002875 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002876 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002877 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002878 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002879 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002880 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002881 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2882 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2883 if (unlikely(ret))
2884 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002885
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002886 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2887 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2888 req->flags |= REQ_F_NOWAIT;
2889
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890 ioprio = READ_ONCE(sqe->ioprio);
2891 if (ioprio) {
2892 ret = ioprio_check_cap(ioprio);
2893 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002894 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895
2896 kiocb->ki_ioprio = ioprio;
2897 } else
2898 kiocb->ki_ioprio = get_current_ioprio();
2899
Jens Axboedef596e2019-01-09 08:59:42 -07002900 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002901 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2902 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002903 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002904
Jens Axboedef596e2019-01-09 08:59:42 -07002905 kiocb->ki_flags |= IOCB_HIPRI;
2906 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002907 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002908 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002909 if (kiocb->ki_flags & IOCB_HIPRI)
2910 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002911 kiocb->ki_complete = io_complete_rw;
2912 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002913
Jens Axboe3529d8c2019-12-19 18:24:38 -07002914 req->rw.addr = READ_ONCE(sqe->addr);
2915 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002916 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002917 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918}
2919
2920static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2921{
2922 switch (ret) {
2923 case -EIOCBQUEUED:
2924 break;
2925 case -ERESTARTSYS:
2926 case -ERESTARTNOINTR:
2927 case -ERESTARTNOHAND:
2928 case -ERESTART_RESTARTBLOCK:
2929 /*
2930 * We can't just restart the syscall, since previously
2931 * submitted sqes may already be in progress. Just fail this
2932 * IO with EINTR.
2933 */
2934 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002935 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936 default:
2937 kiocb->ki_complete(kiocb, ret, 0);
2938 }
2939}
2940
Jens Axboea1d7c392020-06-22 11:09:46 -06002941static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002942 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002943{
Jens Axboeba042912019-12-25 16:33:42 -07002944 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002945 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002946
Jens Axboe227c0c92020-08-13 11:51:40 -06002947 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002948 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002949 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002950 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002951 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002952 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002953 }
2954
Jens Axboeba042912019-12-25 16:33:42 -07002955 if (req->flags & REQ_F_CUR_POS)
2956 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002957 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002958 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002959 else
2960 io_rw_done(kiocb, ret);
2961}
2962
Pavel Begunkov847595d2021-02-04 13:52:06 +00002963static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002964{
Jens Axboe9adbd452019-12-20 08:45:55 -07002965 struct io_ring_ctx *ctx = req->ctx;
2966 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002967 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002968 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002969 size_t offset;
2970 u64 buf_addr;
2971
Jens Axboeedafcce2019-01-09 09:16:05 -07002972 if (unlikely(buf_index >= ctx->nr_user_bufs))
2973 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002974 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2975 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002976 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002977
2978 /* overflow */
2979 if (buf_addr + len < buf_addr)
2980 return -EFAULT;
2981 /* not inside the mapped region */
2982 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2983 return -EFAULT;
2984
2985 /*
2986 * May not be a start of buffer, set size appropriately
2987 * and advance us to the beginning.
2988 */
2989 offset = buf_addr - imu->ubuf;
2990 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002991
2992 if (offset) {
2993 /*
2994 * Don't use iov_iter_advance() here, as it's really slow for
2995 * using the latter parts of a big fixed buffer - it iterates
2996 * over each segment manually. We can cheat a bit here, because
2997 * we know that:
2998 *
2999 * 1) it's a BVEC iter, we set it up
3000 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3001 * first and last bvec
3002 *
3003 * So just find our index, and adjust the iterator afterwards.
3004 * If the offset is within the first bvec (or the whole first
3005 * bvec, just use iov_iter_advance(). This makes it easier
3006 * since we can just skip the first segment, which may not
3007 * be PAGE_SIZE aligned.
3008 */
3009 const struct bio_vec *bvec = imu->bvec;
3010
3011 if (offset <= bvec->bv_len) {
3012 iov_iter_advance(iter, offset);
3013 } else {
3014 unsigned long seg_skip;
3015
3016 /* skip first vec */
3017 offset -= bvec->bv_len;
3018 seg_skip = 1 + (offset >> PAGE_SHIFT);
3019
3020 iter->bvec = bvec + seg_skip;
3021 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003022 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003023 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003024 }
3025 }
3026
Pavel Begunkov847595d2021-02-04 13:52:06 +00003027 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003028}
3029
Jens Axboebcda7ba2020-02-23 16:42:51 -07003030static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3031{
3032 if (needs_lock)
3033 mutex_unlock(&ctx->uring_lock);
3034}
3035
3036static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3037{
3038 /*
3039 * "Normal" inline submissions always hold the uring_lock, since we
3040 * grab it from the system call. Same is true for the SQPOLL offload.
3041 * The only exception is when we've detached the request and issue it
3042 * from an async worker thread, grab the lock for that case.
3043 */
3044 if (needs_lock)
3045 mutex_lock(&ctx->uring_lock);
3046}
3047
3048static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3049 int bgid, struct io_buffer *kbuf,
3050 bool needs_lock)
3051{
3052 struct io_buffer *head;
3053
3054 if (req->flags & REQ_F_BUFFER_SELECTED)
3055 return kbuf;
3056
3057 io_ring_submit_lock(req->ctx, needs_lock);
3058
3059 lockdep_assert_held(&req->ctx->uring_lock);
3060
3061 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3062 if (head) {
3063 if (!list_empty(&head->list)) {
3064 kbuf = list_last_entry(&head->list, struct io_buffer,
3065 list);
3066 list_del(&kbuf->list);
3067 } else {
3068 kbuf = head;
3069 idr_remove(&req->ctx->io_buffer_idr, bgid);
3070 }
3071 if (*len > kbuf->len)
3072 *len = kbuf->len;
3073 } else {
3074 kbuf = ERR_PTR(-ENOBUFS);
3075 }
3076
3077 io_ring_submit_unlock(req->ctx, needs_lock);
3078
3079 return kbuf;
3080}
3081
Jens Axboe4d954c22020-02-27 07:31:19 -07003082static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3083 bool needs_lock)
3084{
3085 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003086 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003087
3088 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003089 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003090 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3091 if (IS_ERR(kbuf))
3092 return kbuf;
3093 req->rw.addr = (u64) (unsigned long) kbuf;
3094 req->flags |= REQ_F_BUFFER_SELECTED;
3095 return u64_to_user_ptr(kbuf->addr);
3096}
3097
3098#ifdef CONFIG_COMPAT
3099static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3100 bool needs_lock)
3101{
3102 struct compat_iovec __user *uiov;
3103 compat_ssize_t clen;
3104 void __user *buf;
3105 ssize_t len;
3106
3107 uiov = u64_to_user_ptr(req->rw.addr);
3108 if (!access_ok(uiov, sizeof(*uiov)))
3109 return -EFAULT;
3110 if (__get_user(clen, &uiov->iov_len))
3111 return -EFAULT;
3112 if (clen < 0)
3113 return -EINVAL;
3114
3115 len = clen;
3116 buf = io_rw_buffer_select(req, &len, needs_lock);
3117 if (IS_ERR(buf))
3118 return PTR_ERR(buf);
3119 iov[0].iov_base = buf;
3120 iov[0].iov_len = (compat_size_t) len;
3121 return 0;
3122}
3123#endif
3124
3125static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3126 bool needs_lock)
3127{
3128 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3129 void __user *buf;
3130 ssize_t len;
3131
3132 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3133 return -EFAULT;
3134
3135 len = iov[0].iov_len;
3136 if (len < 0)
3137 return -EINVAL;
3138 buf = io_rw_buffer_select(req, &len, needs_lock);
3139 if (IS_ERR(buf))
3140 return PTR_ERR(buf);
3141 iov[0].iov_base = buf;
3142 iov[0].iov_len = len;
3143 return 0;
3144}
3145
3146static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3147 bool needs_lock)
3148{
Jens Axboedddb3e22020-06-04 11:27:01 -06003149 if (req->flags & REQ_F_BUFFER_SELECTED) {
3150 struct io_buffer *kbuf;
3151
3152 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3153 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3154 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003155 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003156 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003157 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003158 return -EINVAL;
3159
3160#ifdef CONFIG_COMPAT
3161 if (req->ctx->compat)
3162 return io_compat_import(req, iov, needs_lock);
3163#endif
3164
3165 return __io_iov_buffer_select(req, iov, needs_lock);
3166}
3167
Pavel Begunkov847595d2021-02-04 13:52:06 +00003168static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3169 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003170{
Jens Axboe9adbd452019-12-20 08:45:55 -07003171 void __user *buf = u64_to_user_ptr(req->rw.addr);
3172 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003173 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003174 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003175
Pavel Begunkov7d009162019-11-25 23:14:40 +03003176 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003177 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003178 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003179 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003180
Jens Axboebcda7ba2020-02-23 16:42:51 -07003181 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003182 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003183 return -EINVAL;
3184
Jens Axboe3a6820f2019-12-22 15:19:35 -07003185 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003186 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003187 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003188 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003189 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003190 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003191 }
3192
Jens Axboe3a6820f2019-12-22 15:19:35 -07003193 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3194 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003195 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003196 }
3197
Jens Axboe4d954c22020-02-27 07:31:19 -07003198 if (req->flags & REQ_F_BUFFER_SELECT) {
3199 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003200 if (!ret)
3201 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003202 *iovec = NULL;
3203 return ret;
3204 }
3205
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003206 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3207 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003208}
3209
Jens Axboe0fef9482020-08-26 10:36:20 -06003210static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3211{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003212 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003213}
3214
Jens Axboe32960612019-09-23 11:05:34 -06003215/*
3216 * For files that don't have ->read_iter() and ->write_iter(), handle them
3217 * by looping over ->read() or ->write() manually.
3218 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003219static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003220{
Jens Axboe4017eb92020-10-22 14:14:12 -06003221 struct kiocb *kiocb = &req->rw.kiocb;
3222 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003223 ssize_t ret = 0;
3224
3225 /*
3226 * Don't support polled IO through this interface, and we can't
3227 * support non-blocking either. For the latter, this just causes
3228 * the kiocb to be handled from an async context.
3229 */
3230 if (kiocb->ki_flags & IOCB_HIPRI)
3231 return -EOPNOTSUPP;
3232 if (kiocb->ki_flags & IOCB_NOWAIT)
3233 return -EAGAIN;
3234
3235 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003236 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003237 ssize_t nr;
3238
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003239 if (!iov_iter_is_bvec(iter)) {
3240 iovec = iov_iter_iovec(iter);
3241 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003242 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3243 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003244 }
3245
Jens Axboe32960612019-09-23 11:05:34 -06003246 if (rw == READ) {
3247 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003248 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003249 } else {
3250 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003251 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003252 }
3253
3254 if (nr < 0) {
3255 if (!ret)
3256 ret = nr;
3257 break;
3258 }
3259 ret += nr;
3260 if (nr != iovec.iov_len)
3261 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003262 req->rw.len -= nr;
3263 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003264 iov_iter_advance(iter, nr);
3265 }
3266
3267 return ret;
3268}
3269
Jens Axboeff6165b2020-08-13 09:47:43 -06003270static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3271 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003272{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003273 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003274
Jens Axboeff6165b2020-08-13 09:47:43 -06003275 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003276 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003277 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003278 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003279 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003280 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003281 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003282 unsigned iov_off = 0;
3283
3284 rw->iter.iov = rw->fast_iov;
3285 if (iter->iov != fast_iov) {
3286 iov_off = iter->iov - fast_iov;
3287 rw->iter.iov += iov_off;
3288 }
3289 if (rw->fast_iov != fast_iov)
3290 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003291 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003292 } else {
3293 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003294 }
3295}
3296
Jens Axboee8c2bc12020-08-15 18:44:09 -07003297static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003298{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003299 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3300 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3301 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003302}
3303
Jens Axboee8c2bc12020-08-15 18:44:09 -07003304static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003305{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003306 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003307 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003308
Jens Axboee8c2bc12020-08-15 18:44:09 -07003309 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003310}
3311
Jens Axboeff6165b2020-08-13 09:47:43 -06003312static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3313 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003314 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003315{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003316 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003317 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003318 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003319 if (__io_alloc_async_data(req)) {
3320 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003321 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003322 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003323
Jens Axboeff6165b2020-08-13 09:47:43 -06003324 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003325 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003326 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003327}
3328
Pavel Begunkov73debe62020-09-30 22:57:54 +03003329static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003330{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003331 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003332 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003333 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003334
Pavel Begunkov2846c482020-11-07 13:16:27 +00003335 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003336 if (unlikely(ret < 0))
3337 return ret;
3338
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003339 iorw->bytes_done = 0;
3340 iorw->free_iovec = iov;
3341 if (iov)
3342 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003343 return 0;
3344}
3345
Pavel Begunkov73debe62020-09-30 22:57:54 +03003346static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003347{
3348 ssize_t ret;
3349
Pavel Begunkova88fc402020-09-30 22:57:53 +03003350 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003351 if (ret)
3352 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003353
Jens Axboe3529d8c2019-12-19 18:24:38 -07003354 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3355 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003356
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003357 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003358 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003359 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003360 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003361}
3362
Jens Axboec1dd91d2020-08-03 16:43:59 -06003363/*
3364 * This is our waitqueue callback handler, registered through lock_page_async()
3365 * when we initially tried to do the IO with the iocb armed our waitqueue.
3366 * This gets called when the page is unlocked, and we generally expect that to
3367 * happen when the page IO is completed and the page is now uptodate. This will
3368 * queue a task_work based retry of the operation, attempting to copy the data
3369 * again. If the latter fails because the page was NOT uptodate, then we will
3370 * do a thread based blocking retry of the operation. That's the unexpected
3371 * slow path.
3372 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003373static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3374 int sync, void *arg)
3375{
3376 struct wait_page_queue *wpq;
3377 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003378 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003379 int ret;
3380
3381 wpq = container_of(wait, struct wait_page_queue, wait);
3382
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003383 if (!wake_page_match(wpq, key))
3384 return 0;
3385
Hao Xuc8d317a2020-09-29 20:00:45 +08003386 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003387 list_del_init(&wait->entry);
3388
Pavel Begunkove7375122020-07-12 20:42:04 +03003389 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003390 percpu_ref_get(&req->ctx->refs);
3391
Jens Axboebcf5a062020-05-22 09:24:42 -06003392 /* submit ref gets dropped, acquire a new one */
3393 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003394 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003395 if (unlikely(ret))
3396 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003397 return 1;
3398}
3399
Jens Axboec1dd91d2020-08-03 16:43:59 -06003400/*
3401 * This controls whether a given IO request should be armed for async page
3402 * based retry. If we return false here, the request is handed to the async
3403 * worker threads for retry. If we're doing buffered reads on a regular file,
3404 * we prepare a private wait_page_queue entry and retry the operation. This
3405 * will either succeed because the page is now uptodate and unlocked, or it
3406 * will register a callback when the page is unlocked at IO completion. Through
3407 * that callback, io_uring uses task_work to setup a retry of the operation.
3408 * That retry will attempt the buffered read again. The retry will generally
3409 * succeed, or in rare cases where it fails, we then fall back to using the
3410 * async worker threads for a blocking retry.
3411 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003412static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003413{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003414 struct io_async_rw *rw = req->async_data;
3415 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003416 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003417
3418 /* never retry for NOWAIT, we just complete with -EAGAIN */
3419 if (req->flags & REQ_F_NOWAIT)
3420 return false;
3421
Jens Axboe227c0c92020-08-13 11:51:40 -06003422 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003423 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003424 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003425
Jens Axboebcf5a062020-05-22 09:24:42 -06003426 /*
3427 * just use poll if we can, and don't attempt if the fs doesn't
3428 * support callback based unlocks
3429 */
3430 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3431 return false;
3432
Jens Axboe3b2a4432020-08-16 10:58:43 -07003433 wait->wait.func = io_async_buf_func;
3434 wait->wait.private = req;
3435 wait->wait.flags = 0;
3436 INIT_LIST_HEAD(&wait->wait.entry);
3437 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003438 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003439 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003440 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003441}
3442
3443static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3444{
3445 if (req->file->f_op->read_iter)
3446 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003447 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003448 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003449 else
3450 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003451}
3452
Pavel Begunkov889fca72021-02-10 00:03:09 +00003453static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003454{
3455 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003456 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003457 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003458 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003459 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003460 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003461
Pavel Begunkov2846c482020-11-07 13:16:27 +00003462 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003463 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003464 iovec = NULL;
3465 } else {
3466 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3467 if (ret < 0)
3468 return ret;
3469 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003470 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003471 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003472
Jens Axboefd6c2e42019-12-18 12:19:41 -07003473 /* Ensure we clear previously set non-block flag */
3474 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003475 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003476 else
3477 kiocb->ki_flags |= IOCB_NOWAIT;
3478
Pavel Begunkov24c74672020-06-21 13:09:51 +03003479 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003480 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3481 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003482 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003483 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003484
Pavel Begunkov632546c2020-11-07 13:16:26 +00003485 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003486 if (unlikely(ret)) {
3487 kfree(iovec);
3488 return ret;
3489 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003490
Jens Axboe227c0c92020-08-13 11:51:40 -06003491 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003492
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003493 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003494 /* it's faster to check here then delegate to kfree */
3495 if (iovec)
3496 kfree(iovec);
3497 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003498 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003499 /* IOPOLL retry should happen for io-wq threads */
3500 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003501 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003502 /* no retry on NONBLOCK nor RWF_NOWAIT */
3503 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003504 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003505 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003506 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003507 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003508 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003509 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003510 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003511 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003512 }
3513
Jens Axboe227c0c92020-08-13 11:51:40 -06003514 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003515 if (ret2)
3516 return ret2;
3517
Jens Axboee8c2bc12020-08-15 18:44:09 -07003518 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003519 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003520 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003521
Pavel Begunkovb23df912021-02-04 13:52:04 +00003522 do {
3523 io_size -= ret;
3524 rw->bytes_done += ret;
3525 /* if we can retry, do so with the callbacks armed */
3526 if (!io_rw_should_retry(req)) {
3527 kiocb->ki_flags &= ~IOCB_WAITQ;
3528 return -EAGAIN;
3529 }
3530
3531 /*
3532 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3533 * we get -EIOCBQUEUED, then we'll get a notification when the
3534 * desired page gets unlocked. We can also get a partial read
3535 * here, and if we do, then just retry at the new offset.
3536 */
3537 ret = io_iter_do_read(req, iter);
3538 if (ret == -EIOCBQUEUED)
3539 return 0;
3540 /* we got some bytes, but not all. retry. */
3541 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003542done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003543 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003544 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545}
3546
Pavel Begunkov73debe62020-09-30 22:57:54 +03003547static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003548{
3549 ssize_t ret;
3550
Pavel Begunkova88fc402020-09-30 22:57:53 +03003551 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003552 if (ret)
3553 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003554
Jens Axboe3529d8c2019-12-19 18:24:38 -07003555 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3556 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003557
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003558 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003559 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003560 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003561 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003562}
3563
Pavel Begunkov889fca72021-02-10 00:03:09 +00003564static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003565{
3566 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003567 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003568 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003569 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003570 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003571 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003572
Pavel Begunkov2846c482020-11-07 13:16:27 +00003573 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003574 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003575 iovec = NULL;
3576 } else {
3577 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3578 if (ret < 0)
3579 return ret;
3580 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003581 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003582 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003583
Jens Axboefd6c2e42019-12-18 12:19:41 -07003584 /* Ensure we clear previously set non-block flag */
3585 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003586 kiocb->ki_flags &= ~IOCB_NOWAIT;
3587 else
3588 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003589
Pavel Begunkov24c74672020-06-21 13:09:51 +03003590 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003591 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003592 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003593
Jens Axboe10d59342019-12-09 20:16:22 -07003594 /* file path doesn't support NOWAIT for non-direct_IO */
3595 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3596 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003597 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003598
Pavel Begunkov632546c2020-11-07 13:16:26 +00003599 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003600 if (unlikely(ret))
3601 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003602
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003603 /*
3604 * Open-code file_start_write here to grab freeze protection,
3605 * which will be released by another thread in
3606 * io_complete_rw(). Fool lockdep by telling it the lock got
3607 * released so that it doesn't complain about the held lock when
3608 * we return to userspace.
3609 */
3610 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003611 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003612 __sb_writers_release(file_inode(req->file)->i_sb,
3613 SB_FREEZE_WRITE);
3614 }
3615 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003616
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003617 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003618 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003619 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003620 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003621 else
3622 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003623
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003624 /*
3625 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3626 * retry them without IOCB_NOWAIT.
3627 */
3628 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3629 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003630 /* no retry on NONBLOCK nor RWF_NOWAIT */
3631 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003632 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003633 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003634 /* IOPOLL retry should happen for io-wq threads */
3635 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3636 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003637done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003638 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003639 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003640copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003641 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003642 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003643 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003644 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003645 }
Jens Axboe31b51512019-01-18 22:56:34 -07003646out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003647 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003648 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003649 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003650 return ret;
3651}
3652
Jens Axboe80a261f2020-09-28 14:23:58 -06003653static int io_renameat_prep(struct io_kiocb *req,
3654 const struct io_uring_sqe *sqe)
3655{
3656 struct io_rename *ren = &req->rename;
3657 const char __user *oldf, *newf;
3658
3659 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3660 return -EBADF;
3661
3662 ren->old_dfd = READ_ONCE(sqe->fd);
3663 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3664 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3665 ren->new_dfd = READ_ONCE(sqe->len);
3666 ren->flags = READ_ONCE(sqe->rename_flags);
3667
3668 ren->oldpath = getname(oldf);
3669 if (IS_ERR(ren->oldpath))
3670 return PTR_ERR(ren->oldpath);
3671
3672 ren->newpath = getname(newf);
3673 if (IS_ERR(ren->newpath)) {
3674 putname(ren->oldpath);
3675 return PTR_ERR(ren->newpath);
3676 }
3677
3678 req->flags |= REQ_F_NEED_CLEANUP;
3679 return 0;
3680}
3681
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003682static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003683{
3684 struct io_rename *ren = &req->rename;
3685 int ret;
3686
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003687 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003688 return -EAGAIN;
3689
3690 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3691 ren->newpath, ren->flags);
3692
3693 req->flags &= ~REQ_F_NEED_CLEANUP;
3694 if (ret < 0)
3695 req_set_fail_links(req);
3696 io_req_complete(req, ret);
3697 return 0;
3698}
3699
Jens Axboe14a11432020-09-28 14:27:37 -06003700static int io_unlinkat_prep(struct io_kiocb *req,
3701 const struct io_uring_sqe *sqe)
3702{
3703 struct io_unlink *un = &req->unlink;
3704 const char __user *fname;
3705
3706 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3707 return -EBADF;
3708
3709 un->dfd = READ_ONCE(sqe->fd);
3710
3711 un->flags = READ_ONCE(sqe->unlink_flags);
3712 if (un->flags & ~AT_REMOVEDIR)
3713 return -EINVAL;
3714
3715 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3716 un->filename = getname(fname);
3717 if (IS_ERR(un->filename))
3718 return PTR_ERR(un->filename);
3719
3720 req->flags |= REQ_F_NEED_CLEANUP;
3721 return 0;
3722}
3723
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003724static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003725{
3726 struct io_unlink *un = &req->unlink;
3727 int ret;
3728
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003729 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003730 return -EAGAIN;
3731
3732 if (un->flags & AT_REMOVEDIR)
3733 ret = do_rmdir(un->dfd, un->filename);
3734 else
3735 ret = do_unlinkat(un->dfd, un->filename);
3736
3737 req->flags &= ~REQ_F_NEED_CLEANUP;
3738 if (ret < 0)
3739 req_set_fail_links(req);
3740 io_req_complete(req, ret);
3741 return 0;
3742}
3743
Jens Axboe36f4fa62020-09-05 11:14:22 -06003744static int io_shutdown_prep(struct io_kiocb *req,
3745 const struct io_uring_sqe *sqe)
3746{
3747#if defined(CONFIG_NET)
3748 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3749 return -EINVAL;
3750 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3751 sqe->buf_index)
3752 return -EINVAL;
3753
3754 req->shutdown.how = READ_ONCE(sqe->len);
3755 return 0;
3756#else
3757 return -EOPNOTSUPP;
3758#endif
3759}
3760
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003761static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003762{
3763#if defined(CONFIG_NET)
3764 struct socket *sock;
3765 int ret;
3766
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003767 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003768 return -EAGAIN;
3769
Linus Torvalds48aba792020-12-16 12:44:05 -08003770 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003771 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003772 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003773
3774 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003775 if (ret < 0)
3776 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003777 io_req_complete(req, ret);
3778 return 0;
3779#else
3780 return -EOPNOTSUPP;
3781#endif
3782}
3783
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003784static int __io_splice_prep(struct io_kiocb *req,
3785 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003786{
3787 struct io_splice* sp = &req->splice;
3788 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003789
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003790 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3791 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003792
3793 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003794 sp->len = READ_ONCE(sqe->len);
3795 sp->flags = READ_ONCE(sqe->splice_flags);
3796
3797 if (unlikely(sp->flags & ~valid_flags))
3798 return -EINVAL;
3799
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003800 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3801 (sp->flags & SPLICE_F_FD_IN_FIXED));
3802 if (!sp->file_in)
3803 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003804 req->flags |= REQ_F_NEED_CLEANUP;
3805
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003806 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3807 /*
3808 * Splice operation will be punted aync, and here need to
3809 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3810 */
3811 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003812 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003813 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003814
3815 return 0;
3816}
3817
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003818static int io_tee_prep(struct io_kiocb *req,
3819 const struct io_uring_sqe *sqe)
3820{
3821 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3822 return -EINVAL;
3823 return __io_splice_prep(req, sqe);
3824}
3825
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003826static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003827{
3828 struct io_splice *sp = &req->splice;
3829 struct file *in = sp->file_in;
3830 struct file *out = sp->file_out;
3831 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3832 long ret = 0;
3833
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003834 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003835 return -EAGAIN;
3836 if (sp->len)
3837 ret = do_tee(in, out, sp->len, flags);
3838
3839 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3840 req->flags &= ~REQ_F_NEED_CLEANUP;
3841
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003842 if (ret != sp->len)
3843 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003844 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003845 return 0;
3846}
3847
3848static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3849{
3850 struct io_splice* sp = &req->splice;
3851
3852 sp->off_in = READ_ONCE(sqe->splice_off_in);
3853 sp->off_out = READ_ONCE(sqe->off);
3854 return __io_splice_prep(req, sqe);
3855}
3856
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003857static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003858{
3859 struct io_splice *sp = &req->splice;
3860 struct file *in = sp->file_in;
3861 struct file *out = sp->file_out;
3862 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3863 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003864 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003865
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003866 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003867 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003868
3869 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3870 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003871
Jens Axboe948a7742020-05-17 14:21:38 -06003872 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003873 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003874
3875 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3876 req->flags &= ~REQ_F_NEED_CLEANUP;
3877
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003878 if (ret != sp->len)
3879 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003880 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003881 return 0;
3882}
3883
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884/*
3885 * IORING_OP_NOP just posts a completion event, nothing else.
3886 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003887static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003888{
3889 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003890
Jens Axboedef596e2019-01-09 08:59:42 -07003891 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3892 return -EINVAL;
3893
Pavel Begunkov889fca72021-02-10 00:03:09 +00003894 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003895 return 0;
3896}
3897
Jens Axboe3529d8c2019-12-19 18:24:38 -07003898static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003899{
Jens Axboe6b063142019-01-10 22:13:58 -07003900 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003901
Jens Axboe09bb8392019-03-13 12:39:28 -06003902 if (!req->file)
3903 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003904
Jens Axboe6b063142019-01-10 22:13:58 -07003905 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003906 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003907 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003908 return -EINVAL;
3909
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003910 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3911 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3912 return -EINVAL;
3913
3914 req->sync.off = READ_ONCE(sqe->off);
3915 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003916 return 0;
3917}
3918
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003919static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003920{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003921 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003922 int ret;
3923
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003924 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003925 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003926 return -EAGAIN;
3927
Jens Axboe9adbd452019-12-20 08:45:55 -07003928 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003929 end > 0 ? end : LLONG_MAX,
3930 req->sync.flags & IORING_FSYNC_DATASYNC);
3931 if (ret < 0)
3932 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003933 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003934 return 0;
3935}
3936
Jens Axboed63d1b52019-12-10 10:38:56 -07003937static int io_fallocate_prep(struct io_kiocb *req,
3938 const struct io_uring_sqe *sqe)
3939{
3940 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3941 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003942 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3943 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003944
3945 req->sync.off = READ_ONCE(sqe->off);
3946 req->sync.len = READ_ONCE(sqe->addr);
3947 req->sync.mode = READ_ONCE(sqe->len);
3948 return 0;
3949}
3950
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003951static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003952{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003953 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003954
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003955 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003956 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003957 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003958 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3959 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003960 if (ret < 0)
3961 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003962 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003963 return 0;
3964}
3965
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003966static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003967{
Jens Axboef8748882020-01-08 17:47:02 -07003968 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003969 int ret;
3970
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003971 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003972 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003973 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003974 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003975
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003976 /* open.how should be already initialised */
3977 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003978 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003979
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003980 req->open.dfd = READ_ONCE(sqe->fd);
3981 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003982 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003983 if (IS_ERR(req->open.filename)) {
3984 ret = PTR_ERR(req->open.filename);
3985 req->open.filename = NULL;
3986 return ret;
3987 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003988 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003989 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003990 return 0;
3991}
3992
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003993static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3994{
3995 u64 flags, mode;
3996
Jens Axboe14587a462020-09-05 11:36:08 -06003997 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003998 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003999 mode = READ_ONCE(sqe->len);
4000 flags = READ_ONCE(sqe->open_flags);
4001 req->open.how = build_open_how(flags, mode);
4002 return __io_openat_prep(req, sqe);
4003}
4004
Jens Axboecebdb982020-01-08 17:59:24 -07004005static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4006{
4007 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004008 size_t len;
4009 int ret;
4010
Jens Axboe14587a462020-09-05 11:36:08 -06004011 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004012 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004013 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4014 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004015 if (len < OPEN_HOW_SIZE_VER0)
4016 return -EINVAL;
4017
4018 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4019 len);
4020 if (ret)
4021 return ret;
4022
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004023 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004024}
4025
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004026static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004027{
4028 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004029 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004030 bool nonblock_set;
4031 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004032 int ret;
4033
Jens Axboecebdb982020-01-08 17:59:24 -07004034 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004035 if (ret)
4036 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004037 nonblock_set = op.open_flag & O_NONBLOCK;
4038 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004039 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004040 /*
4041 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4042 * it'll always -EAGAIN
4043 */
4044 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4045 return -EAGAIN;
4046 op.lookup_flags |= LOOKUP_CACHED;
4047 op.open_flag |= O_NONBLOCK;
4048 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004049
Jens Axboe4022e7a2020-03-19 19:23:18 -06004050 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004051 if (ret < 0)
4052 goto err;
4053
4054 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004055 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004056 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4057 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004058 /*
4059 * We could hang on to this 'fd', but seems like marginal
4060 * gain for something that is now known to be a slower path.
4061 * So just put it, and we'll get a new one when we retry.
4062 */
4063 put_unused_fd(ret);
4064 return -EAGAIN;
4065 }
4066
Jens Axboe15b71ab2019-12-11 11:20:36 -07004067 if (IS_ERR(file)) {
4068 put_unused_fd(ret);
4069 ret = PTR_ERR(file);
4070 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004071 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004072 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004073 fsnotify_open(file);
4074 fd_install(ret, file);
4075 }
4076err:
4077 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004078 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004079 if (ret < 0)
4080 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004081 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004082 return 0;
4083}
4084
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004085static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004086{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004087 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004088}
4089
Jens Axboe067524e2020-03-02 16:32:28 -07004090static int io_remove_buffers_prep(struct io_kiocb *req,
4091 const struct io_uring_sqe *sqe)
4092{
4093 struct io_provide_buf *p = &req->pbuf;
4094 u64 tmp;
4095
4096 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4097 return -EINVAL;
4098
4099 tmp = READ_ONCE(sqe->fd);
4100 if (!tmp || tmp > USHRT_MAX)
4101 return -EINVAL;
4102
4103 memset(p, 0, sizeof(*p));
4104 p->nbufs = tmp;
4105 p->bgid = READ_ONCE(sqe->buf_group);
4106 return 0;
4107}
4108
4109static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4110 int bgid, unsigned nbufs)
4111{
4112 unsigned i = 0;
4113
4114 /* shouldn't happen */
4115 if (!nbufs)
4116 return 0;
4117
4118 /* the head kbuf is the list itself */
4119 while (!list_empty(&buf->list)) {
4120 struct io_buffer *nxt;
4121
4122 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4123 list_del(&nxt->list);
4124 kfree(nxt);
4125 if (++i == nbufs)
4126 return i;
4127 }
4128 i++;
4129 kfree(buf);
4130 idr_remove(&ctx->io_buffer_idr, bgid);
4131
4132 return i;
4133}
4134
Pavel Begunkov889fca72021-02-10 00:03:09 +00004135static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004136{
4137 struct io_provide_buf *p = &req->pbuf;
4138 struct io_ring_ctx *ctx = req->ctx;
4139 struct io_buffer *head;
4140 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004141 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004142
4143 io_ring_submit_lock(ctx, !force_nonblock);
4144
4145 lockdep_assert_held(&ctx->uring_lock);
4146
4147 ret = -ENOENT;
4148 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4149 if (head)
4150 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004151 if (ret < 0)
4152 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004153
4154 /* need to hold the lock to complete IOPOLL requests */
4155 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004156 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004157 io_ring_submit_unlock(ctx, !force_nonblock);
4158 } else {
4159 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004160 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004161 }
Jens Axboe067524e2020-03-02 16:32:28 -07004162 return 0;
4163}
4164
Jens Axboeddf0322d2020-02-23 16:41:33 -07004165static int io_provide_buffers_prep(struct io_kiocb *req,
4166 const struct io_uring_sqe *sqe)
4167{
4168 struct io_provide_buf *p = &req->pbuf;
4169 u64 tmp;
4170
4171 if (sqe->ioprio || sqe->rw_flags)
4172 return -EINVAL;
4173
4174 tmp = READ_ONCE(sqe->fd);
4175 if (!tmp || tmp > USHRT_MAX)
4176 return -E2BIG;
4177 p->nbufs = tmp;
4178 p->addr = READ_ONCE(sqe->addr);
4179 p->len = READ_ONCE(sqe->len);
4180
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004181 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004182 return -EFAULT;
4183
4184 p->bgid = READ_ONCE(sqe->buf_group);
4185 tmp = READ_ONCE(sqe->off);
4186 if (tmp > USHRT_MAX)
4187 return -E2BIG;
4188 p->bid = tmp;
4189 return 0;
4190}
4191
4192static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4193{
4194 struct io_buffer *buf;
4195 u64 addr = pbuf->addr;
4196 int i, bid = pbuf->bid;
4197
4198 for (i = 0; i < pbuf->nbufs; i++) {
4199 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4200 if (!buf)
4201 break;
4202
4203 buf->addr = addr;
4204 buf->len = pbuf->len;
4205 buf->bid = bid;
4206 addr += pbuf->len;
4207 bid++;
4208 if (!*head) {
4209 INIT_LIST_HEAD(&buf->list);
4210 *head = buf;
4211 } else {
4212 list_add_tail(&buf->list, &(*head)->list);
4213 }
4214 }
4215
4216 return i ? i : -ENOMEM;
4217}
4218
Pavel Begunkov889fca72021-02-10 00:03:09 +00004219static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004220{
4221 struct io_provide_buf *p = &req->pbuf;
4222 struct io_ring_ctx *ctx = req->ctx;
4223 struct io_buffer *head, *list;
4224 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004225 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004226
4227 io_ring_submit_lock(ctx, !force_nonblock);
4228
4229 lockdep_assert_held(&ctx->uring_lock);
4230
4231 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4232
4233 ret = io_add_buffers(p, &head);
4234 if (ret < 0)
4235 goto out;
4236
4237 if (!list) {
4238 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4239 GFP_KERNEL);
4240 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004241 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004242 goto out;
4243 }
4244 }
4245out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004246 if (ret < 0)
4247 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004248
4249 /* need to hold the lock to complete IOPOLL requests */
4250 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004251 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004252 io_ring_submit_unlock(ctx, !force_nonblock);
4253 } else {
4254 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004255 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004256 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004257 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004258}
4259
Jens Axboe3e4827b2020-01-08 15:18:09 -07004260static int io_epoll_ctl_prep(struct io_kiocb *req,
4261 const struct io_uring_sqe *sqe)
4262{
4263#if defined(CONFIG_EPOLL)
4264 if (sqe->ioprio || sqe->buf_index)
4265 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004266 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004267 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004268
4269 req->epoll.epfd = READ_ONCE(sqe->fd);
4270 req->epoll.op = READ_ONCE(sqe->len);
4271 req->epoll.fd = READ_ONCE(sqe->off);
4272
4273 if (ep_op_has_event(req->epoll.op)) {
4274 struct epoll_event __user *ev;
4275
4276 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4277 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4278 return -EFAULT;
4279 }
4280
4281 return 0;
4282#else
4283 return -EOPNOTSUPP;
4284#endif
4285}
4286
Pavel Begunkov889fca72021-02-10 00:03:09 +00004287static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004288{
4289#if defined(CONFIG_EPOLL)
4290 struct io_epoll *ie = &req->epoll;
4291 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004292 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004293
4294 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4295 if (force_nonblock && ret == -EAGAIN)
4296 return -EAGAIN;
4297
4298 if (ret < 0)
4299 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004300 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004301 return 0;
4302#else
4303 return -EOPNOTSUPP;
4304#endif
4305}
4306
Jens Axboec1ca7572019-12-25 22:18:28 -07004307static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4308{
4309#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4310 if (sqe->ioprio || sqe->buf_index || sqe->off)
4311 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004312 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4313 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004314
4315 req->madvise.addr = READ_ONCE(sqe->addr);
4316 req->madvise.len = READ_ONCE(sqe->len);
4317 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4318 return 0;
4319#else
4320 return -EOPNOTSUPP;
4321#endif
4322}
4323
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004324static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004325{
4326#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4327 struct io_madvise *ma = &req->madvise;
4328 int ret;
4329
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004330 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004331 return -EAGAIN;
4332
Minchan Kim0726b012020-10-17 16:14:50 -07004333 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004334 if (ret < 0)
4335 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004336 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004337 return 0;
4338#else
4339 return -EOPNOTSUPP;
4340#endif
4341}
4342
Jens Axboe4840e412019-12-25 22:03:45 -07004343static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4344{
4345 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4346 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004347 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4348 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004349
4350 req->fadvise.offset = READ_ONCE(sqe->off);
4351 req->fadvise.len = READ_ONCE(sqe->len);
4352 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4353 return 0;
4354}
4355
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004356static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004357{
4358 struct io_fadvise *fa = &req->fadvise;
4359 int ret;
4360
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004361 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004362 switch (fa->advice) {
4363 case POSIX_FADV_NORMAL:
4364 case POSIX_FADV_RANDOM:
4365 case POSIX_FADV_SEQUENTIAL:
4366 break;
4367 default:
4368 return -EAGAIN;
4369 }
4370 }
Jens Axboe4840e412019-12-25 22:03:45 -07004371
4372 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4373 if (ret < 0)
4374 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004375 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004376 return 0;
4377}
4378
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004379static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4380{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004381 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004382 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004383 if (sqe->ioprio || sqe->buf_index)
4384 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004385 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004386 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004387
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004388 req->statx.dfd = READ_ONCE(sqe->fd);
4389 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004390 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004391 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4392 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004393
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004394 return 0;
4395}
4396
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004397static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004398{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004399 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004400 int ret;
4401
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004402 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004403 /* only need file table for an actual valid fd */
4404 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4405 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004406 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004407 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004408
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004409 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4410 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004411
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004412 if (ret < 0)
4413 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004414 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004415 return 0;
4416}
4417
Jens Axboeb5dba592019-12-11 14:02:38 -07004418static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4419{
Jens Axboe14587a462020-09-05 11:36:08 -06004420 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004421 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004422 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4423 sqe->rw_flags || sqe->buf_index)
4424 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004425 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004426 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004427
4428 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004429 return 0;
4430}
4431
Pavel Begunkov889fca72021-02-10 00:03:09 +00004432static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004433{
Jens Axboe9eac1902021-01-19 15:50:37 -07004434 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004435 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004436 struct fdtable *fdt;
4437 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004438 int ret;
4439
Jens Axboe9eac1902021-01-19 15:50:37 -07004440 file = NULL;
4441 ret = -EBADF;
4442 spin_lock(&files->file_lock);
4443 fdt = files_fdtable(files);
4444 if (close->fd >= fdt->max_fds) {
4445 spin_unlock(&files->file_lock);
4446 goto err;
4447 }
4448 file = fdt->fd[close->fd];
4449 if (!file) {
4450 spin_unlock(&files->file_lock);
4451 goto err;
4452 }
4453
4454 if (file->f_op == &io_uring_fops) {
4455 spin_unlock(&files->file_lock);
4456 file = NULL;
4457 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004458 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004459
4460 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004461 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004462 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004463 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004464 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004465
Jens Axboe9eac1902021-01-19 15:50:37 -07004466 ret = __close_fd_get_file(close->fd, &file);
4467 spin_unlock(&files->file_lock);
4468 if (ret < 0) {
4469 if (ret == -ENOENT)
4470 ret = -EBADF;
4471 goto err;
4472 }
4473
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004474 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004475 ret = filp_close(file, current->files);
4476err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004477 if (ret < 0)
4478 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004479 if (file)
4480 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004481 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004482 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004483}
4484
Jens Axboe3529d8c2019-12-19 18:24:38 -07004485static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004486{
4487 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004488
4489 if (!req->file)
4490 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004491
4492 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4493 return -EINVAL;
4494 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4495 return -EINVAL;
4496
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004497 req->sync.off = READ_ONCE(sqe->off);
4498 req->sync.len = READ_ONCE(sqe->len);
4499 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004500 return 0;
4501}
4502
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004503static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004504{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004505 int ret;
4506
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004507 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004508 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004509 return -EAGAIN;
4510
Jens Axboe9adbd452019-12-20 08:45:55 -07004511 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004512 req->sync.flags);
4513 if (ret < 0)
4514 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004515 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004516 return 0;
4517}
4518
YueHaibing469956e2020-03-04 15:53:52 +08004519#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004520static int io_setup_async_msg(struct io_kiocb *req,
4521 struct io_async_msghdr *kmsg)
4522{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004523 struct io_async_msghdr *async_msg = req->async_data;
4524
4525 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004526 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004527 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004528 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004529 return -ENOMEM;
4530 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004531 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004532 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004533 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004534 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004535 /* if were using fast_iov, set it to the new one */
4536 if (!async_msg->free_iov)
4537 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4538
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004539 return -EAGAIN;
4540}
4541
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004542static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4543 struct io_async_msghdr *iomsg)
4544{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004545 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004546 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004547 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004548 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004549}
4550
Jens Axboe3529d8c2019-12-19 18:24:38 -07004551static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004552{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004553 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004554 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004555 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004556
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004557 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4558 return -EINVAL;
4559
Jens Axboee47293f2019-12-20 08:58:21 -07004560 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004561 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004562 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004563
Jens Axboed8768362020-02-27 14:17:49 -07004564#ifdef CONFIG_COMPAT
4565 if (req->ctx->compat)
4566 sr->msg_flags |= MSG_CMSG_COMPAT;
4567#endif
4568
Jens Axboee8c2bc12020-08-15 18:44:09 -07004569 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004570 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004571 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004572 if (!ret)
4573 req->flags |= REQ_F_NEED_CLEANUP;
4574 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004575}
4576
Pavel Begunkov889fca72021-02-10 00:03:09 +00004577static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004578{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004579 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004580 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004581 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004582 int ret;
4583
Florent Revestdba4a922020-12-04 12:36:04 +01004584 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004585 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004586 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004587
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004588 kmsg = req->async_data;
4589 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004590 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004591 if (ret)
4592 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004593 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004594 }
4595
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004596 flags = req->sr_msg.msg_flags;
4597 if (flags & MSG_DONTWAIT)
4598 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004599 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004600 flags |= MSG_DONTWAIT;
4601
4602 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004603 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004604 return io_setup_async_msg(req, kmsg);
4605 if (ret == -ERESTARTSYS)
4606 ret = -EINTR;
4607
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004608 /* fast path, check for non-NULL to avoid function call */
4609 if (kmsg->free_iov)
4610 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004611 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004612 if (ret < 0)
4613 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004614 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004615 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004616}
4617
Pavel Begunkov889fca72021-02-10 00:03:09 +00004618static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004619{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004620 struct io_sr_msg *sr = &req->sr_msg;
4621 struct msghdr msg;
4622 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004623 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004624 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004625 int ret;
4626
Florent Revestdba4a922020-12-04 12:36:04 +01004627 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004629 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004630
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4632 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004633 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004634
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004635 msg.msg_name = NULL;
4636 msg.msg_control = NULL;
4637 msg.msg_controllen = 0;
4638 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004639
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004640 flags = req->sr_msg.msg_flags;
4641 if (flags & MSG_DONTWAIT)
4642 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004643 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004644 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004645
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004646 msg.msg_flags = flags;
4647 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004648 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004649 return -EAGAIN;
4650 if (ret == -ERESTARTSYS)
4651 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004652
Jens Axboe03b12302019-12-02 18:50:25 -07004653 if (ret < 0)
4654 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004655 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004656 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004657}
4658
Pavel Begunkov1400e692020-07-12 20:41:05 +03004659static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4660 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004661{
4662 struct io_sr_msg *sr = &req->sr_msg;
4663 struct iovec __user *uiov;
4664 size_t iov_len;
4665 int ret;
4666
Pavel Begunkov1400e692020-07-12 20:41:05 +03004667 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4668 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004669 if (ret)
4670 return ret;
4671
4672 if (req->flags & REQ_F_BUFFER_SELECT) {
4673 if (iov_len > 1)
4674 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004675 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004676 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004677 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004678 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004679 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004680 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004681 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004682 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004683 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004684 if (ret > 0)
4685 ret = 0;
4686 }
4687
4688 return ret;
4689}
4690
4691#ifdef CONFIG_COMPAT
4692static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004693 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004694{
4695 struct compat_msghdr __user *msg_compat;
4696 struct io_sr_msg *sr = &req->sr_msg;
4697 struct compat_iovec __user *uiov;
4698 compat_uptr_t ptr;
4699 compat_size_t len;
4700 int ret;
4701
Pavel Begunkov270a5942020-07-12 20:41:04 +03004702 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004703 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004704 &ptr, &len);
4705 if (ret)
4706 return ret;
4707
4708 uiov = compat_ptr(ptr);
4709 if (req->flags & REQ_F_BUFFER_SELECT) {
4710 compat_ssize_t clen;
4711
4712 if (len > 1)
4713 return -EINVAL;
4714 if (!access_ok(uiov, sizeof(*uiov)))
4715 return -EFAULT;
4716 if (__get_user(clen, &uiov->iov_len))
4717 return -EFAULT;
4718 if (clen < 0)
4719 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004720 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004721 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004722 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004723 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004724 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004725 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004726 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004727 if (ret < 0)
4728 return ret;
4729 }
4730
4731 return 0;
4732}
Jens Axboe03b12302019-12-02 18:50:25 -07004733#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004734
Pavel Begunkov1400e692020-07-12 20:41:05 +03004735static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4736 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004737{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004738 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004739
4740#ifdef CONFIG_COMPAT
4741 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004742 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004743#endif
4744
Pavel Begunkov1400e692020-07-12 20:41:05 +03004745 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004746}
4747
Jens Axboebcda7ba2020-02-23 16:42:51 -07004748static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004749 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004750{
4751 struct io_sr_msg *sr = &req->sr_msg;
4752 struct io_buffer *kbuf;
4753
Jens Axboebcda7ba2020-02-23 16:42:51 -07004754 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4755 if (IS_ERR(kbuf))
4756 return kbuf;
4757
4758 sr->kbuf = kbuf;
4759 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004760 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004761}
4762
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004763static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4764{
4765 return io_put_kbuf(req, req->sr_msg.kbuf);
4766}
4767
Jens Axboe3529d8c2019-12-19 18:24:38 -07004768static int io_recvmsg_prep(struct io_kiocb *req,
4769 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004770{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004771 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004772 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004773 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004774
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004775 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4776 return -EINVAL;
4777
Jens Axboe3529d8c2019-12-19 18:24:38 -07004778 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004779 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004780 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004781 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004782
Jens Axboed8768362020-02-27 14:17:49 -07004783#ifdef CONFIG_COMPAT
4784 if (req->ctx->compat)
4785 sr->msg_flags |= MSG_CMSG_COMPAT;
4786#endif
4787
Jens Axboee8c2bc12020-08-15 18:44:09 -07004788 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004789 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004790 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004791 if (!ret)
4792 req->flags |= REQ_F_NEED_CLEANUP;
4793 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004794}
4795
Pavel Begunkov889fca72021-02-10 00:03:09 +00004796static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004797{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004798 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004799 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004800 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004801 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004802 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004803 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004804
Florent Revestdba4a922020-12-04 12:36:04 +01004805 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004806 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004807 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004808
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004809 kmsg = req->async_data;
4810 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004811 ret = io_recvmsg_copy_hdr(req, &iomsg);
4812 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004813 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004814 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004815 }
4816
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004817 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004818 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004819 if (IS_ERR(kbuf))
4820 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004821 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004822 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4823 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004824 1, req->sr_msg.len);
4825 }
4826
4827 flags = req->sr_msg.msg_flags;
4828 if (flags & MSG_DONTWAIT)
4829 req->flags |= REQ_F_NOWAIT;
4830 else if (force_nonblock)
4831 flags |= MSG_DONTWAIT;
4832
4833 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4834 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004835 if (force_nonblock && ret == -EAGAIN)
4836 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004837 if (ret == -ERESTARTSYS)
4838 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004839
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004840 if (req->flags & REQ_F_BUFFER_SELECTED)
4841 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004842 /* fast path, check for non-NULL to avoid function call */
4843 if (kmsg->free_iov)
4844 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004845 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004846 if (ret < 0)
4847 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004848 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004849 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004850}
4851
Pavel Begunkov889fca72021-02-10 00:03:09 +00004852static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004853{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004854 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004855 struct io_sr_msg *sr = &req->sr_msg;
4856 struct msghdr msg;
4857 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004858 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004859 struct iovec iov;
4860 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004861 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004862 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004863
Florent Revestdba4a922020-12-04 12:36:04 +01004864 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004865 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004866 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004867
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004868 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004869 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004870 if (IS_ERR(kbuf))
4871 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004872 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004873 }
4874
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004875 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004876 if (unlikely(ret))
4877 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004878
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004879 msg.msg_name = NULL;
4880 msg.msg_control = NULL;
4881 msg.msg_controllen = 0;
4882 msg.msg_namelen = 0;
4883 msg.msg_iocb = NULL;
4884 msg.msg_flags = 0;
4885
4886 flags = req->sr_msg.msg_flags;
4887 if (flags & MSG_DONTWAIT)
4888 req->flags |= REQ_F_NOWAIT;
4889 else if (force_nonblock)
4890 flags |= MSG_DONTWAIT;
4891
4892 ret = sock_recvmsg(sock, &msg, flags);
4893 if (force_nonblock && ret == -EAGAIN)
4894 return -EAGAIN;
4895 if (ret == -ERESTARTSYS)
4896 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004897out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004898 if (req->flags & REQ_F_BUFFER_SELECTED)
4899 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004900 if (ret < 0)
4901 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004902 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004903 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004904}
4905
Jens Axboe3529d8c2019-12-19 18:24:38 -07004906static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004907{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004908 struct io_accept *accept = &req->accept;
4909
Jens Axboe14587a462020-09-05 11:36:08 -06004910 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004911 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004912 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004913 return -EINVAL;
4914
Jens Axboed55e5f52019-12-11 16:12:15 -07004915 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4916 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004917 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004918 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004919 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004920}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004921
Pavel Begunkov889fca72021-02-10 00:03:09 +00004922static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004923{
4924 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004925 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004926 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004927 int ret;
4928
Jiufei Xuee697dee2020-06-10 13:41:59 +08004929 if (req->file->f_flags & O_NONBLOCK)
4930 req->flags |= REQ_F_NOWAIT;
4931
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004932 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004933 accept->addr_len, accept->flags,
4934 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004935 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004936 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004937 if (ret < 0) {
4938 if (ret == -ERESTARTSYS)
4939 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004940 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004941 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004942 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004943 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004944}
4945
Jens Axboe3529d8c2019-12-19 18:24:38 -07004946static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004947{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004949 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004950
Jens Axboe14587a462020-09-05 11:36:08 -06004951 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004952 return -EINVAL;
4953 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4954 return -EINVAL;
4955
Jens Axboe3529d8c2019-12-19 18:24:38 -07004956 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4957 conn->addr_len = READ_ONCE(sqe->addr2);
4958
4959 if (!io)
4960 return 0;
4961
4962 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004963 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004964}
4965
Pavel Begunkov889fca72021-02-10 00:03:09 +00004966static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004967{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004968 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004969 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004970 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004971 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004972
Jens Axboee8c2bc12020-08-15 18:44:09 -07004973 if (req->async_data) {
4974 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004975 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004976 ret = move_addr_to_kernel(req->connect.addr,
4977 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004978 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004979 if (ret)
4980 goto out;
4981 io = &__io;
4982 }
4983
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004984 file_flags = force_nonblock ? O_NONBLOCK : 0;
4985
Jens Axboee8c2bc12020-08-15 18:44:09 -07004986 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004987 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004988 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004989 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004990 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004991 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004992 ret = -ENOMEM;
4993 goto out;
4994 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004995 io = req->async_data;
4996 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004997 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004998 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004999 if (ret == -ERESTARTSYS)
5000 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005001out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005002 if (ret < 0)
5003 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005004 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005005 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005006}
YueHaibing469956e2020-03-04 15:53:52 +08005007#else /* !CONFIG_NET */
5008static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5009{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005010 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005011}
5012
Pavel Begunkov889fca72021-02-10 00:03:09 +00005013static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005014{
YueHaibing469956e2020-03-04 15:53:52 +08005015 return -EOPNOTSUPP;
5016}
5017
Pavel Begunkov889fca72021-02-10 00:03:09 +00005018static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005019{
5020 return -EOPNOTSUPP;
5021}
5022
5023static int io_recvmsg_prep(struct io_kiocb *req,
5024 const struct io_uring_sqe *sqe)
5025{
5026 return -EOPNOTSUPP;
5027}
5028
Pavel Begunkov889fca72021-02-10 00:03:09 +00005029static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005030{
5031 return -EOPNOTSUPP;
5032}
5033
Pavel Begunkov889fca72021-02-10 00:03:09 +00005034static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005035{
5036 return -EOPNOTSUPP;
5037}
5038
5039static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5040{
5041 return -EOPNOTSUPP;
5042}
5043
Pavel Begunkov889fca72021-02-10 00:03:09 +00005044static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005045{
5046 return -EOPNOTSUPP;
5047}
5048
5049static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5050{
5051 return -EOPNOTSUPP;
5052}
5053
Pavel Begunkov889fca72021-02-10 00:03:09 +00005054static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005055{
5056 return -EOPNOTSUPP;
5057}
5058#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005059
Jens Axboed7718a92020-02-14 22:23:12 -07005060struct io_poll_table {
5061 struct poll_table_struct pt;
5062 struct io_kiocb *req;
5063 int error;
5064};
5065
Jens Axboed7718a92020-02-14 22:23:12 -07005066static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5067 __poll_t mask, task_work_func_t func)
5068{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005069 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005070
5071 /* for instances that support it check for an event match first: */
5072 if (mask && !(mask & poll->events))
5073 return 0;
5074
5075 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5076
5077 list_del_init(&poll->wait.entry);
5078
Jens Axboed7718a92020-02-14 22:23:12 -07005079 req->result = mask;
5080 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005081 percpu_ref_get(&req->ctx->refs);
5082
Jens Axboed7718a92020-02-14 22:23:12 -07005083 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005084 * If this fails, then the task is exiting. When a task exits, the
5085 * work gets canceled, so just cancel this request as well instead
5086 * of executing it. We can't safely execute it anyway, as we may not
5087 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005088 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005089 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005090 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005091 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005092 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005093 }
Jens Axboed7718a92020-02-14 22:23:12 -07005094 return 1;
5095}
5096
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005097static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5098 __acquires(&req->ctx->completion_lock)
5099{
5100 struct io_ring_ctx *ctx = req->ctx;
5101
5102 if (!req->result && !READ_ONCE(poll->canceled)) {
5103 struct poll_table_struct pt = { ._key = poll->events };
5104
5105 req->result = vfs_poll(req->file, &pt) & poll->events;
5106 }
5107
5108 spin_lock_irq(&ctx->completion_lock);
5109 if (!req->result && !READ_ONCE(poll->canceled)) {
5110 add_wait_queue(poll->head, &poll->wait);
5111 return true;
5112 }
5113
5114 return false;
5115}
5116
Jens Axboed4e7cd32020-08-15 11:44:50 -07005117static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005118{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005119 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005120 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005121 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005122 return req->apoll->double_poll;
5123}
5124
5125static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5126{
5127 if (req->opcode == IORING_OP_POLL_ADD)
5128 return &req->poll;
5129 return &req->apoll->poll;
5130}
5131
5132static void io_poll_remove_double(struct io_kiocb *req)
5133{
5134 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005135
5136 lockdep_assert_held(&req->ctx->completion_lock);
5137
5138 if (poll && poll->head) {
5139 struct wait_queue_head *head = poll->head;
5140
5141 spin_lock(&head->lock);
5142 list_del_init(&poll->wait.entry);
5143 if (poll->wait.private)
5144 refcount_dec(&req->refs);
5145 poll->head = NULL;
5146 spin_unlock(&head->lock);
5147 }
5148}
5149
5150static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5151{
5152 struct io_ring_ctx *ctx = req->ctx;
5153
Jens Axboed4e7cd32020-08-15 11:44:50 -07005154 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005155 req->poll.done = true;
5156 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5157 io_commit_cqring(ctx);
5158}
5159
Jens Axboe18bceab2020-05-15 11:56:54 -06005160static void io_poll_task_func(struct callback_head *cb)
5161{
5162 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005163 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005164 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005165
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005166 if (io_poll_rewait(req, &req->poll)) {
5167 spin_unlock_irq(&ctx->completion_lock);
5168 } else {
5169 hash_del(&req->hash_node);
5170 io_poll_complete(req, req->result, 0);
5171 spin_unlock_irq(&ctx->completion_lock);
5172
5173 nxt = io_put_req_find_next(req);
5174 io_cqring_ev_posted(ctx);
5175 if (nxt)
5176 __io_req_task_submit(nxt);
5177 }
5178
Jens Axboe6d816e02020-08-11 08:04:14 -06005179 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005180}
5181
5182static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5183 int sync, void *key)
5184{
5185 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005186 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005187 __poll_t mask = key_to_poll(key);
5188
5189 /* for instances that support it check for an event match first: */
5190 if (mask && !(mask & poll->events))
5191 return 0;
5192
Jens Axboe8706e042020-09-28 08:38:54 -06005193 list_del_init(&wait->entry);
5194
Jens Axboe807abcb2020-07-17 17:09:27 -06005195 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005196 bool done;
5197
Jens Axboe807abcb2020-07-17 17:09:27 -06005198 spin_lock(&poll->head->lock);
5199 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005200 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005201 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005202 /* make sure double remove sees this as being gone */
5203 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005204 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005205 if (!done) {
5206 /* use wait func handler, so it matches the rq type */
5207 poll->wait.func(&poll->wait, mode, sync, key);
5208 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005209 }
5210 refcount_dec(&req->refs);
5211 return 1;
5212}
5213
5214static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5215 wait_queue_func_t wake_func)
5216{
5217 poll->head = NULL;
5218 poll->done = false;
5219 poll->canceled = false;
5220 poll->events = events;
5221 INIT_LIST_HEAD(&poll->wait.entry);
5222 init_waitqueue_func_entry(&poll->wait, wake_func);
5223}
5224
5225static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005226 struct wait_queue_head *head,
5227 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005228{
5229 struct io_kiocb *req = pt->req;
5230
5231 /*
5232 * If poll->head is already set, it's because the file being polled
5233 * uses multiple waitqueues for poll handling (eg one for read, one
5234 * for write). Setup a separate io_poll_iocb if this happens.
5235 */
5236 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005237 struct io_poll_iocb *poll_one = poll;
5238
Jens Axboe18bceab2020-05-15 11:56:54 -06005239 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005240 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005241 pt->error = -EINVAL;
5242 return;
5243 }
5244 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5245 if (!poll) {
5246 pt->error = -ENOMEM;
5247 return;
5248 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005249 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005250 refcount_inc(&req->refs);
5251 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005252 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005253 }
5254
5255 pt->error = 0;
5256 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005257
5258 if (poll->events & EPOLLEXCLUSIVE)
5259 add_wait_queue_exclusive(head, &poll->wait);
5260 else
5261 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005262}
5263
5264static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5265 struct poll_table_struct *p)
5266{
5267 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005268 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005269
Jens Axboe807abcb2020-07-17 17:09:27 -06005270 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005271}
5272
Jens Axboed7718a92020-02-14 22:23:12 -07005273static void io_async_task_func(struct callback_head *cb)
5274{
5275 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5276 struct async_poll *apoll = req->apoll;
5277 struct io_ring_ctx *ctx = req->ctx;
5278
5279 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5280
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005281 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005282 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005283 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005284 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005285 }
5286
Jens Axboe31067252020-05-17 17:43:31 -06005287 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005288 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005289 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005290
Jens Axboed4e7cd32020-08-15 11:44:50 -07005291 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005292 spin_unlock_irq(&ctx->completion_lock);
5293
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005294 if (!READ_ONCE(apoll->poll.canceled))
5295 __io_req_task_submit(req);
5296 else
5297 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005298
Jens Axboe6d816e02020-08-11 08:04:14 -06005299 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005300 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005301 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005302}
5303
5304static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5305 void *key)
5306{
5307 struct io_kiocb *req = wait->private;
5308 struct io_poll_iocb *poll = &req->apoll->poll;
5309
5310 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5311 key_to_poll(key));
5312
5313 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5314}
5315
5316static void io_poll_req_insert(struct io_kiocb *req)
5317{
5318 struct io_ring_ctx *ctx = req->ctx;
5319 struct hlist_head *list;
5320
5321 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5322 hlist_add_head(&req->hash_node, list);
5323}
5324
5325static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5326 struct io_poll_iocb *poll,
5327 struct io_poll_table *ipt, __poll_t mask,
5328 wait_queue_func_t wake_func)
5329 __acquires(&ctx->completion_lock)
5330{
5331 struct io_ring_ctx *ctx = req->ctx;
5332 bool cancel = false;
5333
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005334 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005335 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005336 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005337 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005338
5339 ipt->pt._key = mask;
5340 ipt->req = req;
5341 ipt->error = -EINVAL;
5342
Jens Axboed7718a92020-02-14 22:23:12 -07005343 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5344
5345 spin_lock_irq(&ctx->completion_lock);
5346 if (likely(poll->head)) {
5347 spin_lock(&poll->head->lock);
5348 if (unlikely(list_empty(&poll->wait.entry))) {
5349 if (ipt->error)
5350 cancel = true;
5351 ipt->error = 0;
5352 mask = 0;
5353 }
5354 if (mask || ipt->error)
5355 list_del_init(&poll->wait.entry);
5356 else if (cancel)
5357 WRITE_ONCE(poll->canceled, true);
5358 else if (!poll->done) /* actually waiting for an event */
5359 io_poll_req_insert(req);
5360 spin_unlock(&poll->head->lock);
5361 }
5362
5363 return mask;
5364}
5365
5366static bool io_arm_poll_handler(struct io_kiocb *req)
5367{
5368 const struct io_op_def *def = &io_op_defs[req->opcode];
5369 struct io_ring_ctx *ctx = req->ctx;
5370 struct async_poll *apoll;
5371 struct io_poll_table ipt;
5372 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005373 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005374
5375 if (!req->file || !file_can_poll(req->file))
5376 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005377 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005378 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005379 if (def->pollin)
5380 rw = READ;
5381 else if (def->pollout)
5382 rw = WRITE;
5383 else
5384 return false;
5385 /* if we can't nonblock try, then no point in arming a poll handler */
5386 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005387 return false;
5388
5389 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5390 if (unlikely(!apoll))
5391 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005392 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005393
5394 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005395 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005396
Nathan Chancellor8755d972020-03-02 16:01:19 -07005397 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005398 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005399 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005400 if (def->pollout)
5401 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005402
5403 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5404 if ((req->opcode == IORING_OP_RECVMSG) &&
5405 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5406 mask &= ~POLLIN;
5407
Jens Axboed7718a92020-02-14 22:23:12 -07005408 mask |= POLLERR | POLLPRI;
5409
5410 ipt.pt._qproc = io_async_queue_proc;
5411
5412 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5413 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005414 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005415 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005416 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005417 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005418 kfree(apoll);
5419 return false;
5420 }
5421 spin_unlock_irq(&ctx->completion_lock);
5422 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5423 apoll->poll.events);
5424 return true;
5425}
5426
5427static bool __io_poll_remove_one(struct io_kiocb *req,
5428 struct io_poll_iocb *poll)
5429{
Jens Axboeb41e9852020-02-17 09:52:41 -07005430 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005431
5432 spin_lock(&poll->head->lock);
5433 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005434 if (!list_empty(&poll->wait.entry)) {
5435 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005436 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005437 }
5438 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005439 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005440 return do_complete;
5441}
5442
5443static bool io_poll_remove_one(struct io_kiocb *req)
5444{
5445 bool do_complete;
5446
Jens Axboed4e7cd32020-08-15 11:44:50 -07005447 io_poll_remove_double(req);
5448
Jens Axboed7718a92020-02-14 22:23:12 -07005449 if (req->opcode == IORING_OP_POLL_ADD) {
5450 do_complete = __io_poll_remove_one(req, &req->poll);
5451 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005452 struct async_poll *apoll = req->apoll;
5453
Jens Axboed7718a92020-02-14 22:23:12 -07005454 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005455 do_complete = __io_poll_remove_one(req, &apoll->poll);
5456 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005457 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005458 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005459 kfree(apoll);
5460 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005461 }
5462
Jens Axboeb41e9852020-02-17 09:52:41 -07005463 if (do_complete) {
5464 io_cqring_fill_event(req, -ECANCELED);
5465 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005466 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005467 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005468 }
5469
5470 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005471}
5472
Jens Axboe76e1b642020-09-26 15:05:03 -06005473/*
5474 * Returns true if we found and killed one or more poll requests
5475 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005476static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5477 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005478{
Jens Axboe78076bb2019-12-04 19:56:40 -07005479 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005480 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005481 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005482
5483 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005484 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5485 struct hlist_head *list;
5486
5487 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005488 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005489 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005490 posted += io_poll_remove_one(req);
5491 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005492 }
5493 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005494
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005495 if (posted)
5496 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005497
5498 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005499}
5500
Jens Axboe47f46762019-11-09 17:43:02 -07005501static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5502{
Jens Axboe78076bb2019-12-04 19:56:40 -07005503 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005504 struct io_kiocb *req;
5505
Jens Axboe78076bb2019-12-04 19:56:40 -07005506 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5507 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005508 if (sqe_addr != req->user_data)
5509 continue;
5510 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005511 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005512 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005513 }
5514
5515 return -ENOENT;
5516}
5517
Jens Axboe3529d8c2019-12-19 18:24:38 -07005518static int io_poll_remove_prep(struct io_kiocb *req,
5519 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005520{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005521 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5522 return -EINVAL;
5523 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5524 sqe->poll_events)
5525 return -EINVAL;
5526
Pavel Begunkov018043b2020-10-27 23:17:18 +00005527 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005528 return 0;
5529}
5530
5531/*
5532 * Find a running poll command that matches one specified in sqe->addr,
5533 * and remove it if found.
5534 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005535static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005536{
5537 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005538 int ret;
5539
Jens Axboe221c5eb2019-01-17 09:41:58 -07005540 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005541 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005542 spin_unlock_irq(&ctx->completion_lock);
5543
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005544 if (ret < 0)
5545 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005546 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005547 return 0;
5548}
5549
Jens Axboe221c5eb2019-01-17 09:41:58 -07005550static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5551 void *key)
5552{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005553 struct io_kiocb *req = wait->private;
5554 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005555
Jens Axboed7718a92020-02-14 22:23:12 -07005556 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005557}
5558
Jens Axboe221c5eb2019-01-17 09:41:58 -07005559static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5560 struct poll_table_struct *p)
5561{
5562 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5563
Jens Axboee8c2bc12020-08-15 18:44:09 -07005564 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005565}
5566
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005568{
5569 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005570 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005571
5572 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5573 return -EINVAL;
5574 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5575 return -EINVAL;
5576
Jiufei Xue5769a352020-06-17 17:53:55 +08005577 events = READ_ONCE(sqe->poll32_events);
5578#ifdef __BIG_ENDIAN
5579 events = swahw32(events);
5580#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005581 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5582 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005583 return 0;
5584}
5585
Pavel Begunkov61e98202021-02-10 00:03:08 +00005586static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005587{
5588 struct io_poll_iocb *poll = &req->poll;
5589 struct io_ring_ctx *ctx = req->ctx;
5590 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005591 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005592
Jens Axboed7718a92020-02-14 22:23:12 -07005593 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005594
Jens Axboed7718a92020-02-14 22:23:12 -07005595 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5596 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005597
Jens Axboe8c838782019-03-12 15:48:16 -06005598 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005599 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005600 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005601 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005602 spin_unlock_irq(&ctx->completion_lock);
5603
Jens Axboe8c838782019-03-12 15:48:16 -06005604 if (mask) {
5605 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005606 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005607 }
Jens Axboe8c838782019-03-12 15:48:16 -06005608 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005609}
5610
Jens Axboe5262f562019-09-17 12:26:57 -06005611static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5612{
Jens Axboead8a48a2019-11-15 08:49:11 -07005613 struct io_timeout_data *data = container_of(timer,
5614 struct io_timeout_data, timer);
5615 struct io_kiocb *req = data->req;
5616 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005617 unsigned long flags;
5618
Jens Axboe5262f562019-09-17 12:26:57 -06005619 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005620 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005621 atomic_set(&req->ctx->cq_timeouts,
5622 atomic_read(&req->ctx->cq_timeouts) + 1);
5623
Jens Axboe78e19bb2019-11-06 15:21:34 -07005624 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005625 io_commit_cqring(ctx);
5626 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5627
5628 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005629 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005630 io_put_req(req);
5631 return HRTIMER_NORESTART;
5632}
5633
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005634static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5635 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005636{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005637 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005638 struct io_kiocb *req;
5639 int ret = -ENOENT;
5640
5641 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5642 if (user_data == req->user_data) {
5643 ret = 0;
5644 break;
5645 }
5646 }
5647
5648 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005649 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005650
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005651 io = req->async_data;
5652 ret = hrtimer_try_to_cancel(&io->timer);
5653 if (ret == -1)
5654 return ERR_PTR(-EALREADY);
5655 list_del_init(&req->timeout.list);
5656 return req;
5657}
5658
5659static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5660{
5661 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5662
5663 if (IS_ERR(req))
5664 return PTR_ERR(req);
5665
5666 req_set_fail_links(req);
5667 io_cqring_fill_event(req, -ECANCELED);
5668 io_put_req_deferred(req, 1);
5669 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005670}
5671
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005672static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5673 struct timespec64 *ts, enum hrtimer_mode mode)
5674{
5675 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5676 struct io_timeout_data *data;
5677
5678 if (IS_ERR(req))
5679 return PTR_ERR(req);
5680
5681 req->timeout.off = 0; /* noseq */
5682 data = req->async_data;
5683 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5684 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5685 data->timer.function = io_timeout_fn;
5686 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5687 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005688}
5689
Jens Axboe3529d8c2019-12-19 18:24:38 -07005690static int io_timeout_remove_prep(struct io_kiocb *req,
5691 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005692{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005693 struct io_timeout_rem *tr = &req->timeout_rem;
5694
Jens Axboeb29472e2019-12-17 18:50:29 -07005695 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5696 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005697 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5698 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005699 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005700 return -EINVAL;
5701
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005702 tr->addr = READ_ONCE(sqe->addr);
5703 tr->flags = READ_ONCE(sqe->timeout_flags);
5704 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5705 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5706 return -EINVAL;
5707 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5708 return -EFAULT;
5709 } else if (tr->flags) {
5710 /* timeout removal doesn't support flags */
5711 return -EINVAL;
5712 }
5713
Jens Axboeb29472e2019-12-17 18:50:29 -07005714 return 0;
5715}
5716
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005717static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5718{
5719 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5720 : HRTIMER_MODE_REL;
5721}
5722
Jens Axboe11365042019-10-16 09:08:32 -06005723/*
5724 * Remove or update an existing timeout command
5725 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005726static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005727{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005728 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005729 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005730 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005731
Jens Axboe11365042019-10-16 09:08:32 -06005732 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005733 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005734 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005735 else
5736 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5737 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005738
Jens Axboe47f46762019-11-09 17:43:02 -07005739 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005740 io_commit_cqring(ctx);
5741 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005742 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005743 if (ret < 0)
5744 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005745 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005746 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005747}
5748
Jens Axboe3529d8c2019-12-19 18:24:38 -07005749static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005750 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005751{
Jens Axboead8a48a2019-11-15 08:49:11 -07005752 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005753 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005754 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005755
Jens Axboead8a48a2019-11-15 08:49:11 -07005756 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005757 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005758 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005759 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005760 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005761 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005762 flags = READ_ONCE(sqe->timeout_flags);
5763 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005764 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005765
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005766 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005767
Jens Axboee8c2bc12020-08-15 18:44:09 -07005768 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005769 return -ENOMEM;
5770
Jens Axboee8c2bc12020-08-15 18:44:09 -07005771 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005772 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005773
5774 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005775 return -EFAULT;
5776
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005777 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005778 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5779 return 0;
5780}
5781
Pavel Begunkov61e98202021-02-10 00:03:08 +00005782static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005783{
Jens Axboead8a48a2019-11-15 08:49:11 -07005784 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005785 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005786 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005787 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005788
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005789 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005790
Jens Axboe5262f562019-09-17 12:26:57 -06005791 /*
5792 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005793 * timeout event to be satisfied. If it isn't set, then this is
5794 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005795 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005796 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005797 entry = ctx->timeout_list.prev;
5798 goto add;
5799 }
Jens Axboe5262f562019-09-17 12:26:57 -06005800
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005801 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5802 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005803
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005804 /* Update the last seq here in case io_flush_timeouts() hasn't.
5805 * This is safe because ->completion_lock is held, and submissions
5806 * and completions are never mixed in the same ->completion_lock section.
5807 */
5808 ctx->cq_last_tm_flush = tail;
5809
Jens Axboe5262f562019-09-17 12:26:57 -06005810 /*
5811 * Insertion sort, ensuring the first entry in the list is always
5812 * the one we need first.
5813 */
Jens Axboe5262f562019-09-17 12:26:57 -06005814 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005815 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5816 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005817
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005818 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005819 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005820 /* nxt.seq is behind @tail, otherwise would've been completed */
5821 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005822 break;
5823 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005824add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005825 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005826 data->timer.function = io_timeout_fn;
5827 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005828 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005829 return 0;
5830}
5831
Jens Axboe62755e32019-10-28 21:49:21 -06005832static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005833{
Jens Axboe62755e32019-10-28 21:49:21 -06005834 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005835
Jens Axboe62755e32019-10-28 21:49:21 -06005836 return req->user_data == (unsigned long) data;
5837}
5838
Jens Axboee977d6d2019-11-05 12:39:45 -07005839static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005840{
Jens Axboe62755e32019-10-28 21:49:21 -06005841 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005842 int ret = 0;
5843
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005844 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005845 switch (cancel_ret) {
5846 case IO_WQ_CANCEL_OK:
5847 ret = 0;
5848 break;
5849 case IO_WQ_CANCEL_RUNNING:
5850 ret = -EALREADY;
5851 break;
5852 case IO_WQ_CANCEL_NOTFOUND:
5853 ret = -ENOENT;
5854 break;
5855 }
5856
Jens Axboee977d6d2019-11-05 12:39:45 -07005857 return ret;
5858}
5859
Jens Axboe47f46762019-11-09 17:43:02 -07005860static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5861 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005862 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005863{
5864 unsigned long flags;
5865 int ret;
5866
5867 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5868 if (ret != -ENOENT) {
5869 spin_lock_irqsave(&ctx->completion_lock, flags);
5870 goto done;
5871 }
5872
5873 spin_lock_irqsave(&ctx->completion_lock, flags);
5874 ret = io_timeout_cancel(ctx, sqe_addr);
5875 if (ret != -ENOENT)
5876 goto done;
5877 ret = io_poll_cancel(ctx, sqe_addr);
5878done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005879 if (!ret)
5880 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005881 io_cqring_fill_event(req, ret);
5882 io_commit_cqring(ctx);
5883 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5884 io_cqring_ev_posted(ctx);
5885
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005886 if (ret < 0)
5887 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005888 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005889}
5890
Jens Axboe3529d8c2019-12-19 18:24:38 -07005891static int io_async_cancel_prep(struct io_kiocb *req,
5892 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005893{
Jens Axboefbf23842019-12-17 18:45:56 -07005894 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005895 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005896 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5897 return -EINVAL;
5898 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005899 return -EINVAL;
5900
Jens Axboefbf23842019-12-17 18:45:56 -07005901 req->cancel.addr = READ_ONCE(sqe->addr);
5902 return 0;
5903}
5904
Pavel Begunkov61e98202021-02-10 00:03:08 +00005905static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005906{
5907 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005908
Pavel Begunkov014db002020-03-03 21:33:12 +03005909 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005910 return 0;
5911}
5912
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005913static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005914 const struct io_uring_sqe *sqe)
5915{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005916 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5917 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005918 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5919 return -EINVAL;
5920 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005921 return -EINVAL;
5922
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005923 req->rsrc_update.offset = READ_ONCE(sqe->off);
5924 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5925 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005926 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005927 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005928 return 0;
5929}
5930
Pavel Begunkov889fca72021-02-10 00:03:09 +00005931static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005932{
5933 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005934 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005935 int ret;
5936
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005937 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005938 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005939
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005940 up.offset = req->rsrc_update.offset;
5941 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005942
5943 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005944 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005945 mutex_unlock(&ctx->uring_lock);
5946
5947 if (ret < 0)
5948 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005949 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005950 return 0;
5951}
5952
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005953static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005954{
Jens Axboed625c6e2019-12-17 19:53:05 -07005955 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005956 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005957 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005958 case IORING_OP_READV:
5959 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005960 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005961 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005962 case IORING_OP_WRITEV:
5963 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005964 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005965 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005966 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005967 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005968 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005969 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005970 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005971 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005972 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005973 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005974 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005975 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005976 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005977 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005978 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005979 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005980 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005981 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005982 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005983 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005984 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005985 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005986 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005987 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005988 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005989 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005990 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005991 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005992 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005993 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005994 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005995 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005996 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005997 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005998 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005999 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006000 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006001 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006002 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006003 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006004 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006005 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006006 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006007 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006008 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006009 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006010 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006011 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006012 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006013 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006014 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006015 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006016 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006017 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006018 case IORING_OP_SHUTDOWN:
6019 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006020 case IORING_OP_RENAMEAT:
6021 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006022 case IORING_OP_UNLINKAT:
6023 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006024 }
6025
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006026 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6027 req->opcode);
6028 return-EINVAL;
6029}
6030
Jens Axboedef596e2019-01-09 08:59:42 -07006031static int io_req_defer_prep(struct io_kiocb *req,
6032 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006033{
Jens Axboedef596e2019-01-09 08:59:42 -07006034 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006035 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006036 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006037 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006038 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006039}
6040
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006041static u32 io_get_sequence(struct io_kiocb *req)
6042{
6043 struct io_kiocb *pos;
6044 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006045 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006046
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006047 io_for_each_link(pos, req)
6048 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006049
6050 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6051 return total_submitted - nr_reqs;
6052}
6053
Jens Axboe3529d8c2019-12-19 18:24:38 -07006054static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006055{
6056 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006057 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006059 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060
6061 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006062 if (likely(list_empty_careful(&ctx->defer_list) &&
6063 !(req->flags & REQ_F_IO_DRAIN)))
6064 return 0;
6065
6066 seq = io_get_sequence(req);
6067 /* Still a chance to pass the sequence check */
6068 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006069 return 0;
6070
Jens Axboee8c2bc12020-08-15 18:44:09 -07006071 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006072 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006073 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006074 return ret;
6075 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006076 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006077 de = kmalloc(sizeof(*de), GFP_KERNEL);
6078 if (!de)
6079 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006080
6081 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006082 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006083 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006084 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006085 io_queue_async_work(req);
6086 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006087 }
6088
6089 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006090 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006091 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006092 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006093 spin_unlock_irq(&ctx->completion_lock);
6094 return -EIOCBQUEUED;
6095}
Jens Axboeedafcce2019-01-09 09:16:05 -07006096
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006097static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006098{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006099 if (req->flags & REQ_F_BUFFER_SELECTED) {
6100 switch (req->opcode) {
6101 case IORING_OP_READV:
6102 case IORING_OP_READ_FIXED:
6103 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006104 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006105 break;
6106 case IORING_OP_RECVMSG:
6107 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006108 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006109 break;
6110 }
6111 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006112 }
6113
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006114 if (req->flags & REQ_F_NEED_CLEANUP) {
6115 switch (req->opcode) {
6116 case IORING_OP_READV:
6117 case IORING_OP_READ_FIXED:
6118 case IORING_OP_READ:
6119 case IORING_OP_WRITEV:
6120 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006121 case IORING_OP_WRITE: {
6122 struct io_async_rw *io = req->async_data;
6123 if (io->free_iovec)
6124 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006125 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006126 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006127 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006128 case IORING_OP_SENDMSG: {
6129 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006130
6131 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006132 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006133 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006134 case IORING_OP_SPLICE:
6135 case IORING_OP_TEE:
6136 io_put_file(req, req->splice.file_in,
6137 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6138 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006139 case IORING_OP_OPENAT:
6140 case IORING_OP_OPENAT2:
6141 if (req->open.filename)
6142 putname(req->open.filename);
6143 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006144 case IORING_OP_RENAMEAT:
6145 putname(req->rename.oldpath);
6146 putname(req->rename.newpath);
6147 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006148 case IORING_OP_UNLINKAT:
6149 putname(req->unlink.filename);
6150 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006151 }
6152 req->flags &= ~REQ_F_NEED_CLEANUP;
6153 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006154}
6155
Pavel Begunkov889fca72021-02-10 00:03:09 +00006156static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006157{
Jens Axboeedafcce2019-01-09 09:16:05 -07006158 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006159 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006160
Jens Axboed625c6e2019-12-17 19:53:05 -07006161 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006162 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006163 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006164 break;
6165 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006166 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006167 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006168 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006169 break;
6170 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006171 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006172 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006173 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006174 break;
6175 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006176 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177 break;
6178 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006179 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180 break;
6181 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006182 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006183 break;
6184 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006185 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006186 break;
6187 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006188 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006189 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006190 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006191 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006192 break;
6193 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006194 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006195 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006196 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006197 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198 break;
6199 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006200 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201 break;
6202 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006203 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 break;
6205 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006206 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006207 break;
6208 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006209 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006210 break;
6211 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006212 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006213 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006214 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006215 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006216 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006217 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006218 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006219 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006220 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006221 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006222 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006223 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006224 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006225 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006226 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006227 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006228 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006229 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006230 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006231 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006232 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006233 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006234 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006235 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006236 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006237 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006238 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006239 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006240 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006241 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006242 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006243 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006244 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006245 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006246 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006247 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006248 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006250 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006251 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006252 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006253 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006254 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006255 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006256 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006257 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006258 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006259 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006260 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006261 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262 default:
6263 ret = -EINVAL;
6264 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006265 }
6266
6267 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006268 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006269
Jens Axboeb5325762020-05-19 21:20:27 -06006270 /* If the op doesn't have a file, we're not polling for it */
6271 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006272 const bool in_async = io_wq_current_is_worker();
6273
Jens Axboe11ba8202020-01-15 21:51:17 -07006274 /* workqueue context doesn't hold uring_lock, grab it now */
6275 if (in_async)
6276 mutex_lock(&ctx->uring_lock);
6277
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006278 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006279
6280 if (in_async)
6281 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006282 }
6283
6284 return 0;
6285}
6286
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006287static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006288{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006289 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006290 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006291 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006292
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006293 timeout = io_prep_linked_timeout(req);
6294 if (timeout)
6295 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006296
Jens Axboe4014d942021-01-19 15:53:54 -07006297 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006298 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006299
Jens Axboe561fb042019-10-24 07:25:42 -06006300 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006301 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006302 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006303 /*
6304 * We can get EAGAIN for polled IO even though we're
6305 * forcing a sync submission from here, since we can't
6306 * wait for request slots on the block side.
6307 */
6308 if (ret != -EAGAIN)
6309 break;
6310 cond_resched();
6311 } while (1);
6312 }
Jens Axboe31b51512019-01-18 22:56:34 -07006313
Jens Axboe561fb042019-10-24 07:25:42 -06006314 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006315 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006316
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006317 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6318 lock_ctx = req->ctx;
6319
6320 /*
6321 * io_iopoll_complete() does not hold completion_lock to
6322 * complete polled io, so here for polled io, we can not call
6323 * io_req_complete() directly, otherwise there maybe concurrent
6324 * access to cqring, defer_list, etc, which is not safe. Given
6325 * that io_iopoll_complete() is always called under uring_lock,
6326 * so here for polled io, we also get uring_lock to complete
6327 * it.
6328 */
6329 if (lock_ctx)
6330 mutex_lock(&lock_ctx->uring_lock);
6331
6332 req_set_fail_links(req);
6333 io_req_complete(req, ret);
6334
6335 if (lock_ctx)
6336 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006337 }
Jens Axboe31b51512019-01-18 22:56:34 -07006338}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339
Jens Axboe65e19f52019-10-26 07:20:21 -06006340static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6341 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006342{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006343 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006344
Jens Axboe05f3fb32019-12-09 11:22:50 -07006345 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006346 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006347}
6348
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006349static struct file *io_file_get(struct io_submit_state *state,
6350 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006351{
6352 struct io_ring_ctx *ctx = req->ctx;
6353 struct file *file;
6354
6355 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006356 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006357 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006358 fd = array_index_nospec(fd, ctx->nr_user_files);
6359 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006360 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006361 } else {
6362 trace_io_uring_file_get(ctx, fd);
6363 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006364 }
6365
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006366 if (file && unlikely(file->f_op == &io_uring_fops))
6367 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006368 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006369}
6370
Jens Axboe2665abf2019-11-05 12:40:47 -07006371static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6372{
Jens Axboead8a48a2019-11-15 08:49:11 -07006373 struct io_timeout_data *data = container_of(timer,
6374 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006375 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006376 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006377 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006378
6379 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006380 prev = req->timeout.head;
6381 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006382
6383 /*
6384 * We don't expect the list to be empty, that will only happen if we
6385 * race with the completion of the linked work.
6386 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006387 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006388 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006389 else
6390 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006391 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6392
6393 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006394 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006395 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006396 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006397 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006398 io_req_complete_post(req, -ETIME, 0);
6399 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006400 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006401 return HRTIMER_NORESTART;
6402}
6403
Jens Axboe7271ef32020-08-10 09:55:22 -06006404static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006405{
Jens Axboe76a46e02019-11-10 23:34:16 -07006406 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006407 * If the back reference is NULL, then our linked request finished
6408 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006409 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006410 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006411 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006412
Jens Axboead8a48a2019-11-15 08:49:11 -07006413 data->timer.function = io_link_timeout_fn;
6414 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6415 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006416 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006417}
6418
6419static void io_queue_linked_timeout(struct io_kiocb *req)
6420{
6421 struct io_ring_ctx *ctx = req->ctx;
6422
6423 spin_lock_irq(&ctx->completion_lock);
6424 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006425 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006426
Jens Axboe2665abf2019-11-05 12:40:47 -07006427 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006428 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006429}
6430
Jens Axboead8a48a2019-11-15 08:49:11 -07006431static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006432{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006433 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006435 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6436 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006437 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006438
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006439 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006440 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006441 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006442 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006443}
6444
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006445static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006446{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006447 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006448 const struct cred *old_creds = NULL;
Pavel Begunkov889fca72021-02-10 00:03:09 +00006449 int ret, issue_flags = IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450
Pavel Begunkov889fca72021-02-10 00:03:09 +00006451 if (cs)
6452 issue_flags |= IO_URING_F_COMPLETE_DEFER;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006453again:
6454 linked_timeout = io_prep_linked_timeout(req);
6455
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006456 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6457 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006458 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006459 if (old_creds)
6460 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006461 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006462 old_creds = NULL; /* restored original creds */
6463 else
Jens Axboe98447d62020-10-14 10:48:51 -06006464 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006465 }
6466
Pavel Begunkov889fca72021-02-10 00:03:09 +00006467 ret = io_issue_sqe(req, issue_flags);
Jens Axboe491381ce2019-10-17 09:20:46 -06006468
6469 /*
6470 * We async punt it if the file wasn't marked NOWAIT, or if the file
6471 * doesn't support non-blocking read/write attempts
6472 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006473 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006474 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006475 /*
6476 * Queued up for async execution, worker will release
6477 * submit reference when the iocb is actually submitted.
6478 */
6479 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006480 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006481
Pavel Begunkovf063c542020-07-25 14:41:59 +03006482 if (linked_timeout)
6483 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006484 } else if (likely(!ret)) {
6485 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006486 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006487 cs->reqs[cs->nr++] = req;
6488 if (cs->nr == IO_COMPL_BATCH)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006489 io_submit_flush_completions(cs, req->ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006490 req = NULL;
6491 } else {
6492 req = io_put_req_find_next(req);
6493 }
6494
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006495 if (linked_timeout)
6496 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006497
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006498 if (req) {
6499 if (!(req->flags & REQ_F_FORCE_ASYNC))
6500 goto again;
6501 io_queue_async_work(req);
6502 }
6503 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006504 /* un-prep timeout, so it'll be killed as any other linked */
6505 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006506 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006507 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006508 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006509 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006510
Jens Axboe193155c2020-02-22 23:22:19 -07006511 if (old_creds)
6512 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006513}
6514
Jens Axboef13fad72020-06-22 09:34:30 -06006515static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6516 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006517{
6518 int ret;
6519
Jens Axboe3529d8c2019-12-19 18:24:38 -07006520 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006521 if (ret) {
6522 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006523fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006524 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006525 io_put_req(req);
6526 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006527 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006528 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006529 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006530 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006531 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006532 goto fail_req;
6533 }
Jens Axboece35a472019-12-17 08:04:44 -07006534 io_queue_async_work(req);
6535 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006536 if (sqe) {
6537 ret = io_req_prep(req, sqe);
6538 if (unlikely(ret))
6539 goto fail_req;
6540 }
6541 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006542 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006543}
6544
Jens Axboef13fad72020-06-22 09:34:30 -06006545static inline void io_queue_link_head(struct io_kiocb *req,
6546 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006547{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006548 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006549 io_put_req(req);
6550 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006551 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006552 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006553}
6554
Pavel Begunkov863e0562020-10-27 23:25:35 +00006555struct io_submit_link {
6556 struct io_kiocb *head;
6557 struct io_kiocb *last;
6558};
6559
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006560static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006561 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006562{
Jackie Liua197f662019-11-08 08:09:12 -07006563 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006564 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006565
Jens Axboe9e645e112019-05-10 16:07:28 -06006566 /*
6567 * If we already have a head request, queue this one for async
6568 * submittal once the head completes. If we don't have a head but
6569 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6570 * submitted sync once the chain is complete. If none of those
6571 * conditions are true (normal request), then just queue it.
6572 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006573 if (link->head) {
6574 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006575
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006576 /*
6577 * Taking sequential execution of a link, draining both sides
6578 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6579 * requests in the link. So, it drains the head and the
6580 * next after the link request. The last one is done via
6581 * drain_next flag to persist the effect across calls.
6582 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006583 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006584 head->flags |= REQ_F_IO_DRAIN;
6585 ctx->drain_next = 1;
6586 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006587 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006588 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006589 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006590 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006591 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006592 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006593 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006594 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006595 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006596
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006597 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006598 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006599 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006600 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006601 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006602 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006603 if (unlikely(ctx->drain_next)) {
6604 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006605 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006606 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006607 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006608 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006609 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006610 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006611 link->head = req;
6612 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006613 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006614 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006615 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006616 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006617
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006618 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006619}
6620
Jens Axboe9a56a232019-01-09 09:06:50 -07006621/*
6622 * Batched submission is done, ensure local IO is flushed out.
6623 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006624static void io_submit_state_end(struct io_submit_state *state,
6625 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006626{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006627 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006628 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006629 if (state->plug_started)
6630 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006631 io_state_file_put(state);
Pavel Begunkov50872752021-02-10 00:03:12 +00006632 if (state->free_reqs) {
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006633 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Pavel Begunkov50872752021-02-10 00:03:12 +00006634 state->free_reqs = 0;
6635 }
Jens Axboe9a56a232019-01-09 09:06:50 -07006636}
6637
6638/*
6639 * Start submission side cache.
6640 */
6641static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006642 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006643{
Jens Axboe27926b62020-10-28 09:33:23 -06006644 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006645 state->ios_left = max_ios;
6646}
6647
Jens Axboe2b188cc2019-01-07 10:46:33 -07006648static void io_commit_sqring(struct io_ring_ctx *ctx)
6649{
Hristo Venev75b28af2019-08-26 17:23:46 +00006650 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006651
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006652 /*
6653 * Ensure any loads from the SQEs are done at this point,
6654 * since once we write the new head, the application could
6655 * write new data to them.
6656 */
6657 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006658}
6659
6660/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006661 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662 * that is mapped by userspace. This means that care needs to be taken to
6663 * ensure that reads are stable, as we cannot rely on userspace always
6664 * being a good citizen. If members of the sqe are validated and then later
6665 * used, it's important that those reads are done through READ_ONCE() to
6666 * prevent a re-load down the line.
6667 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006668static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006669{
Hristo Venev75b28af2019-08-26 17:23:46 +00006670 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006671 unsigned head;
6672
6673 /*
6674 * The cached sq head (or cq tail) serves two purposes:
6675 *
6676 * 1) allows us to batch the cost of updating the user visible
6677 * head updates.
6678 * 2) allows the kernel side to track the head on its own, even
6679 * though the application is the one updating it.
6680 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006681 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006682 if (likely(head < ctx->sq_entries))
6683 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006684
6685 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006686 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006687 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006688 return NULL;
6689}
6690
6691static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6692{
6693 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006694}
6695
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006696/*
6697 * Check SQE restrictions (opcode and flags).
6698 *
6699 * Returns 'true' if SQE is allowed, 'false' otherwise.
6700 */
6701static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6702 struct io_kiocb *req,
6703 unsigned int sqe_flags)
6704{
6705 if (!ctx->restricted)
6706 return true;
6707
6708 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6709 return false;
6710
6711 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6712 ctx->restrictions.sqe_flags_required)
6713 return false;
6714
6715 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6716 ctx->restrictions.sqe_flags_required))
6717 return false;
6718
6719 return true;
6720}
6721
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006722#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6723 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6724 IOSQE_BUFFER_SELECT)
6725
6726static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006727 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006728{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006729 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006730 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006731 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006732
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006733 req->opcode = READ_ONCE(sqe->opcode);
6734 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006735 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006736 req->file = NULL;
6737 req->ctx = ctx;
6738 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006739 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006740 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006741 /* one is dropped after submission, the other at completion */
6742 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006743 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006744 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006745
6746 if (unlikely(req->opcode >= IORING_OP_LAST))
6747 return -EINVAL;
6748
Jens Axboe28cea78a2020-09-14 10:51:17 -06006749 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006750 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006751
6752 sqe_flags = READ_ONCE(sqe->flags);
6753 /* enforce forwards compatibility on users */
6754 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6755 return -EINVAL;
6756
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006757 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6758 return -EACCES;
6759
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006760 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6761 !io_op_defs[req->opcode].buffer_select)
6762 return -EOPNOTSUPP;
6763
6764 id = READ_ONCE(sqe->personality);
6765 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006766 struct io_identity *iod;
6767
Jens Axboe1e6fa522020-10-15 08:46:24 -06006768 iod = idr_find(&ctx->personality_idr, id);
6769 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006770 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006771 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006772
6773 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006774 get_cred(iod->creds);
6775 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006776 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006777 }
6778
6779 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006780 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006781 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006782
Jens Axboe27926b62020-10-28 09:33:23 -06006783 /*
6784 * Plug now if we have more than 1 IO left after this, and the target
6785 * is potentially a read/write to block based storage.
6786 */
6787 if (!state->plug_started && state->ios_left > 1 &&
6788 io_op_defs[req->opcode].plug) {
6789 blk_start_plug(&state->plug);
6790 state->plug_started = true;
6791 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006792
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006793 ret = 0;
6794 if (io_op_defs[req->opcode].needs_file) {
6795 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006796
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006797 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006798 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006799 ret = -EBADF;
6800 }
6801
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006802 state->ios_left--;
6803 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006804}
6805
Jens Axboe0f212202020-09-13 13:09:39 -06006806static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006807{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006808 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006809 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006810
Jens Axboec4a2ed72019-11-21 21:01:26 -07006811 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006812 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006813 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006814 return -EBUSY;
6815 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006816
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006817 /* make sure SQ entry isn't read before tail */
6818 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006819
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006820 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6821 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006822
Jens Axboed8a6df12020-10-15 16:24:45 -06006823 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006824 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006825
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006826 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006827 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006828
Jens Axboe6c271ce2019-01-10 11:22:30 -07006829 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006830 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006831 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006832 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006833
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006834 sqe = io_get_sqe(ctx);
6835 if (unlikely(!sqe)) {
6836 io_consume_sqe(ctx);
6837 break;
6838 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006839 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006840 if (unlikely(!req)) {
6841 if (!submitted)
6842 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006843 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006844 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006845 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006846 /* will complete beyond this point, count as submitted */
6847 submitted++;
6848
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006849 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006850 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006851fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006852 io_put_req(req);
6853 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006854 break;
6855 }
6856
Jens Axboe354420f2020-01-08 18:55:15 -07006857 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006858 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006859 err = io_submit_sqe(req, sqe, &link, &ctx->submit_state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006860 if (err)
6861 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006862 }
6863
Pavel Begunkov9466f432020-01-25 22:34:01 +03006864 if (unlikely(submitted != nr)) {
6865 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006866 struct io_uring_task *tctx = current->io_uring;
6867 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006868
Jens Axboed8a6df12020-10-15 16:24:45 -06006869 percpu_ref_put_many(&ctx->refs, unused);
6870 percpu_counter_sub(&tctx->inflight, unused);
6871 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006872 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006873 if (link.head)
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006874 io_queue_link_head(link.head, &ctx->submit_state.comp);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006875 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006876
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006877 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6878 io_commit_sqring(ctx);
6879
Jens Axboe6c271ce2019-01-10 11:22:30 -07006880 return submitted;
6881}
6882
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006883static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6884{
6885 /* Tell userspace we may need a wakeup call */
6886 spin_lock_irq(&ctx->completion_lock);
6887 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6888 spin_unlock_irq(&ctx->completion_lock);
6889}
6890
6891static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6892{
6893 spin_lock_irq(&ctx->completion_lock);
6894 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6895 spin_unlock_irq(&ctx->completion_lock);
6896}
6897
Xiaoguang Wang08369242020-11-03 14:15:59 +08006898static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006899{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006900 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006901 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006902
Jens Axboec8d1ba52020-09-14 11:07:26 -06006903 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006904 /* if we're handling multiple rings, cap submit size for fairness */
6905 if (cap_entries && to_submit > 8)
6906 to_submit = 8;
6907
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006908 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6909 unsigned nr_events = 0;
6910
Xiaoguang Wang08369242020-11-03 14:15:59 +08006911 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006912 if (!list_empty(&ctx->iopoll_list))
6913 io_do_iopoll(ctx, &nr_events, 0);
6914
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006915 if (to_submit && !ctx->sqo_dead &&
6916 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006917 ret = io_submit_sqes(ctx, to_submit);
6918 mutex_unlock(&ctx->uring_lock);
6919 }
Jens Axboe90554202020-09-03 12:12:41 -06006920
6921 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6922 wake_up(&ctx->sqo_sq_wait);
6923
Xiaoguang Wang08369242020-11-03 14:15:59 +08006924 return ret;
6925}
6926
6927static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6928{
6929 struct io_ring_ctx *ctx;
6930 unsigned sq_thread_idle = 0;
6931
6932 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6933 if (sq_thread_idle < ctx->sq_thread_idle)
6934 sq_thread_idle = ctx->sq_thread_idle;
6935 }
6936
6937 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006938}
6939
Jens Axboe69fb2132020-09-14 11:16:23 -06006940static void io_sqd_init_new(struct io_sq_data *sqd)
6941{
6942 struct io_ring_ctx *ctx;
6943
6944 while (!list_empty(&sqd->ctx_new_list)) {
6945 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006946 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6947 complete(&ctx->sq_thread_comp);
6948 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006949
6950 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006951}
6952
Jens Axboe6c271ce2019-01-10 11:22:30 -07006953static int io_sq_thread(void *data)
6954{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006955 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006956 struct files_struct *old_files = current->files;
6957 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006958 const struct cred *old_cred = NULL;
6959 struct io_sq_data *sqd = data;
6960 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006961 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006962 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006963
Jens Axboe28cea78a2020-09-14 10:51:17 -06006964 task_lock(current);
6965 current->files = NULL;
6966 current->nsproxy = NULL;
6967 task_unlock(current);
6968
Jens Axboe69fb2132020-09-14 11:16:23 -06006969 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006970 int ret;
6971 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006972
6973 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006974 * Any changes to the sqd lists are synchronized through the
6975 * kthread parking. This synchronizes the thread vs users,
6976 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006977 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006978 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006979 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006980 /*
6981 * When sq thread is unparked, in case the previous park operation
6982 * comes from io_put_sq_data(), which means that sq thread is going
6983 * to be stopped, so here needs to have a check.
6984 */
6985 if (kthread_should_stop())
6986 break;
6987 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006988
Xiaoguang Wang08369242020-11-03 14:15:59 +08006989 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006990 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006991 timeout = jiffies + sqd->sq_thread_idle;
6992 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006993
Xiaoguang Wang08369242020-11-03 14:15:59 +08006994 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006995 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006996 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6997 if (current->cred != ctx->creds) {
6998 if (old_cred)
6999 revert_creds(old_cred);
7000 old_cred = override_creds(ctx->creds);
7001 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007002 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007003#ifdef CONFIG_AUDIT
7004 current->loginuid = ctx->loginuid;
7005 current->sessionid = ctx->sessionid;
7006#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007007
Xiaoguang Wang08369242020-11-03 14:15:59 +08007008 ret = __io_sq_thread(ctx, cap_entries);
7009 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7010 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007011
Jens Axboe28cea78a2020-09-14 10:51:17 -06007012 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007013 }
7014
Xiaoguang Wang08369242020-11-03 14:15:59 +08007015 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007016 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007017 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007018 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007019 if (sqt_spin)
7020 timeout = jiffies + sqd->sq_thread_idle;
7021 continue;
7022 }
7023
Xiaoguang Wang08369242020-11-03 14:15:59 +08007024 needs_sched = true;
7025 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7026 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7027 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7028 !list_empty_careful(&ctx->iopoll_list)) {
7029 needs_sched = false;
7030 break;
7031 }
7032 if (io_sqring_entries(ctx)) {
7033 needs_sched = false;
7034 break;
7035 }
7036 }
7037
Hao Xu8b28fdf2021-01-31 22:39:04 +08007038 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007039 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7040 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007041
Jens Axboe69fb2132020-09-14 11:16:23 -06007042 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007043 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7044 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007045 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007046
7047 finish_wait(&sqd->wait, &wait);
7048 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007049 }
7050
Jens Axboe4c6e2772020-07-01 11:29:10 -06007051 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007052 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007053
Dennis Zhou91d8f512020-09-16 13:41:05 -07007054 if (cur_css)
7055 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007056 if (old_cred)
7057 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007058
Jens Axboe28cea78a2020-09-14 10:51:17 -06007059 task_lock(current);
7060 current->files = old_files;
7061 current->nsproxy = old_nsproxy;
7062 task_unlock(current);
7063
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007064 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007065
Jens Axboe6c271ce2019-01-10 11:22:30 -07007066 return 0;
7067}
7068
Jens Axboebda52162019-09-24 13:47:15 -06007069struct io_wait_queue {
7070 struct wait_queue_entry wq;
7071 struct io_ring_ctx *ctx;
7072 unsigned to_wait;
7073 unsigned nr_timeouts;
7074};
7075
Pavel Begunkov6c503152021-01-04 20:36:36 +00007076static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007077{
7078 struct io_ring_ctx *ctx = iowq->ctx;
7079
7080 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007081 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007082 * started waiting. For timeouts, we always want to return to userspace,
7083 * regardless of event count.
7084 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007085 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007086 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7087}
7088
7089static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7090 int wake_flags, void *key)
7091{
7092 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7093 wq);
7094
Pavel Begunkov6c503152021-01-04 20:36:36 +00007095 /*
7096 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7097 * the task, and the next invocation will do it.
7098 */
7099 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7100 return autoremove_wake_function(curr, mode, wake_flags, key);
7101 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007102}
7103
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007104static int io_run_task_work_sig(void)
7105{
7106 if (io_run_task_work())
7107 return 1;
7108 if (!signal_pending(current))
7109 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007110 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7111 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007112 return -EINTR;
7113}
7114
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007115/* when returns >0, the caller should retry */
7116static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7117 struct io_wait_queue *iowq,
7118 signed long *timeout)
7119{
7120 int ret;
7121
7122 /* make sure we run task_work before checking for signals */
7123 ret = io_run_task_work_sig();
7124 if (ret || io_should_wake(iowq))
7125 return ret;
7126 /* let the caller flush overflows, retry */
7127 if (test_bit(0, &ctx->cq_check_overflow))
7128 return 1;
7129
7130 *timeout = schedule_timeout(*timeout);
7131 return !*timeout ? -ETIME : 1;
7132}
7133
Jens Axboe2b188cc2019-01-07 10:46:33 -07007134/*
7135 * Wait until events become available, if we don't already have some. The
7136 * application must reap them itself, as they reside on the shared cq ring.
7137 */
7138static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007139 const sigset_t __user *sig, size_t sigsz,
7140 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007141{
Jens Axboebda52162019-09-24 13:47:15 -06007142 struct io_wait_queue iowq = {
7143 .wq = {
7144 .private = current,
7145 .func = io_wake_function,
7146 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7147 },
7148 .ctx = ctx,
7149 .to_wait = min_events,
7150 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007151 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007152 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7153 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007154
Jens Axboeb41e9852020-02-17 09:52:41 -07007155 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007156 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7157 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007158 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007159 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007160 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007161 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007162
7163 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007164#ifdef CONFIG_COMPAT
7165 if (in_compat_syscall())
7166 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007167 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007168 else
7169#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007170 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007171
Jens Axboe2b188cc2019-01-07 10:46:33 -07007172 if (ret)
7173 return ret;
7174 }
7175
Hao Xuc73ebb62020-11-03 10:54:37 +08007176 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007177 struct timespec64 ts;
7178
Hao Xuc73ebb62020-11-03 10:54:37 +08007179 if (get_timespec64(&ts, uts))
7180 return -EFAULT;
7181 timeout = timespec64_to_jiffies(&ts);
7182 }
7183
Jens Axboebda52162019-09-24 13:47:15 -06007184 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007185 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007186 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007187 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007188 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7189 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007190 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7191 finish_wait(&ctx->wait, &iowq.wq);
7192 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007193
Jens Axboeb7db41c2020-07-04 08:55:50 -06007194 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007195
Hristo Venev75b28af2019-08-26 17:23:46 +00007196 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007197}
7198
Jens Axboe6b063142019-01-10 22:13:58 -07007199static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7200{
7201#if defined(CONFIG_UNIX)
7202 if (ctx->ring_sock) {
7203 struct sock *sock = ctx->ring_sock->sk;
7204 struct sk_buff *skb;
7205
7206 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7207 kfree_skb(skb);
7208 }
7209#else
7210 int i;
7211
Jens Axboe65e19f52019-10-26 07:20:21 -06007212 for (i = 0; i < ctx->nr_user_files; i++) {
7213 struct file *file;
7214
7215 file = io_file_from_index(ctx, i);
7216 if (file)
7217 fput(file);
7218 }
Jens Axboe6b063142019-01-10 22:13:58 -07007219#endif
7220}
7221
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007222static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007223{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007224 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007225
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007226 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007227 complete(&data->done);
7228}
7229
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007230static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7231{
7232 spin_lock_bh(&ctx->rsrc_ref_lock);
7233}
7234
7235static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7236{
7237 spin_unlock_bh(&ctx->rsrc_ref_lock);
7238}
7239
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007240static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7241 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007242 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007243{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007244 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007245 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007246 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007247 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007248 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007249}
7250
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007251static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7252 struct io_ring_ctx *ctx,
7253 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007254{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007255 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007256 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007257
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007258 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007259 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007260 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007261 if (ref_node)
7262 percpu_ref_kill(&ref_node->refs);
7263
7264 percpu_ref_kill(&data->refs);
7265
7266 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007267 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007268 do {
7269 ret = wait_for_completion_interruptible(&data->done);
7270 if (!ret)
7271 break;
7272 ret = io_run_task_work_sig();
7273 if (ret < 0) {
7274 percpu_ref_resurrect(&data->refs);
7275 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007276 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007277 return ret;
7278 }
7279 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007280
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007281 destroy_fixed_rsrc_ref_node(backup_node);
7282 return 0;
7283}
7284
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007285static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7286{
7287 struct fixed_rsrc_data *data;
7288
7289 data = kzalloc(sizeof(*data), GFP_KERNEL);
7290 if (!data)
7291 return NULL;
7292
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007293 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007294 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7295 kfree(data);
7296 return NULL;
7297 }
7298 data->ctx = ctx;
7299 init_completion(&data->done);
7300 return data;
7301}
7302
7303static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7304{
7305 percpu_ref_exit(&data->refs);
7306 kfree(data->table);
7307 kfree(data);
7308}
7309
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007310static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7311{
7312 struct fixed_rsrc_data *data = ctx->file_data;
7313 struct fixed_rsrc_ref_node *backup_node;
7314 unsigned nr_tables, i;
7315 int ret;
7316
7317 if (!data)
7318 return -ENXIO;
7319 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7320 if (!backup_node)
7321 return -ENOMEM;
7322 init_fixed_file_ref_node(ctx, backup_node);
7323
7324 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7325 if (ret)
7326 return ret;
7327
Jens Axboe6b063142019-01-10 22:13:58 -07007328 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007329 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7330 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007332 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007333 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007334 ctx->nr_user_files = 0;
7335 return 0;
7336}
7337
Jens Axboe534ca6d2020-09-02 13:52:19 -06007338static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007339{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007340 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007341 /*
7342 * The park is a bit of a work-around, without it we get
7343 * warning spews on shutdown with SQPOLL set and affinity
7344 * set to a single CPU.
7345 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007346 if (sqd->thread) {
7347 kthread_park(sqd->thread);
7348 kthread_stop(sqd->thread);
7349 }
7350
7351 kfree(sqd);
7352 }
7353}
7354
Jens Axboeaa061652020-09-02 14:50:27 -06007355static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7356{
7357 struct io_ring_ctx *ctx_attach;
7358 struct io_sq_data *sqd;
7359 struct fd f;
7360
7361 f = fdget(p->wq_fd);
7362 if (!f.file)
7363 return ERR_PTR(-ENXIO);
7364 if (f.file->f_op != &io_uring_fops) {
7365 fdput(f);
7366 return ERR_PTR(-EINVAL);
7367 }
7368
7369 ctx_attach = f.file->private_data;
7370 sqd = ctx_attach->sq_data;
7371 if (!sqd) {
7372 fdput(f);
7373 return ERR_PTR(-EINVAL);
7374 }
7375
7376 refcount_inc(&sqd->refs);
7377 fdput(f);
7378 return sqd;
7379}
7380
Jens Axboe534ca6d2020-09-02 13:52:19 -06007381static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7382{
7383 struct io_sq_data *sqd;
7384
Jens Axboeaa061652020-09-02 14:50:27 -06007385 if (p->flags & IORING_SETUP_ATTACH_WQ)
7386 return io_attach_sq_data(p);
7387
Jens Axboe534ca6d2020-09-02 13:52:19 -06007388 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7389 if (!sqd)
7390 return ERR_PTR(-ENOMEM);
7391
7392 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007393 INIT_LIST_HEAD(&sqd->ctx_list);
7394 INIT_LIST_HEAD(&sqd->ctx_new_list);
7395 mutex_init(&sqd->ctx_lock);
7396 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007397 init_waitqueue_head(&sqd->wait);
7398 return sqd;
7399}
7400
Jens Axboe69fb2132020-09-14 11:16:23 -06007401static void io_sq_thread_unpark(struct io_sq_data *sqd)
7402 __releases(&sqd->lock)
7403{
7404 if (!sqd->thread)
7405 return;
7406 kthread_unpark(sqd->thread);
7407 mutex_unlock(&sqd->lock);
7408}
7409
7410static void io_sq_thread_park(struct io_sq_data *sqd)
7411 __acquires(&sqd->lock)
7412{
7413 if (!sqd->thread)
7414 return;
7415 mutex_lock(&sqd->lock);
7416 kthread_park(sqd->thread);
7417}
7418
Jens Axboe534ca6d2020-09-02 13:52:19 -06007419static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7420{
7421 struct io_sq_data *sqd = ctx->sq_data;
7422
7423 if (sqd) {
7424 if (sqd->thread) {
7425 /*
7426 * We may arrive here from the error branch in
7427 * io_sq_offload_create() where the kthread is created
7428 * without being waked up, thus wake it up now to make
7429 * sure the wait will complete.
7430 */
7431 wake_up_process(sqd->thread);
7432 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007433
7434 io_sq_thread_park(sqd);
7435 }
7436
7437 mutex_lock(&sqd->ctx_lock);
7438 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007439 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007440 mutex_unlock(&sqd->ctx_lock);
7441
Xiaoguang Wang08369242020-11-03 14:15:59 +08007442 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007443 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007444
7445 io_put_sq_data(sqd);
7446 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007447 }
7448}
7449
Jens Axboe6b063142019-01-10 22:13:58 -07007450static void io_finish_async(struct io_ring_ctx *ctx)
7451{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007452 io_sq_thread_stop(ctx);
7453
Jens Axboe561fb042019-10-24 07:25:42 -06007454 if (ctx->io_wq) {
7455 io_wq_destroy(ctx->io_wq);
7456 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007457 }
7458}
7459
7460#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007461/*
7462 * Ensure the UNIX gc is aware of our file set, so we are certain that
7463 * the io_uring can be safely unregistered on process exit, even if we have
7464 * loops in the file referencing.
7465 */
7466static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7467{
7468 struct sock *sk = ctx->ring_sock->sk;
7469 struct scm_fp_list *fpl;
7470 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007471 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007472
Jens Axboe6b063142019-01-10 22:13:58 -07007473 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7474 if (!fpl)
7475 return -ENOMEM;
7476
7477 skb = alloc_skb(0, GFP_KERNEL);
7478 if (!skb) {
7479 kfree(fpl);
7480 return -ENOMEM;
7481 }
7482
7483 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007484
Jens Axboe08a45172019-10-03 08:11:03 -06007485 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007486 fpl->user = get_uid(ctx->user);
7487 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007488 struct file *file = io_file_from_index(ctx, i + offset);
7489
7490 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007491 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007492 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007493 unix_inflight(fpl->user, fpl->fp[nr_files]);
7494 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007495 }
7496
Jens Axboe08a45172019-10-03 08:11:03 -06007497 if (nr_files) {
7498 fpl->max = SCM_MAX_FD;
7499 fpl->count = nr_files;
7500 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007501 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007502 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7503 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007504
Jens Axboe08a45172019-10-03 08:11:03 -06007505 for (i = 0; i < nr_files; i++)
7506 fput(fpl->fp[i]);
7507 } else {
7508 kfree_skb(skb);
7509 kfree(fpl);
7510 }
Jens Axboe6b063142019-01-10 22:13:58 -07007511
7512 return 0;
7513}
7514
7515/*
7516 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7517 * causes regular reference counting to break down. We rely on the UNIX
7518 * garbage collection to take care of this problem for us.
7519 */
7520static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7521{
7522 unsigned left, total;
7523 int ret = 0;
7524
7525 total = 0;
7526 left = ctx->nr_user_files;
7527 while (left) {
7528 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007529
7530 ret = __io_sqe_files_scm(ctx, this_files, total);
7531 if (ret)
7532 break;
7533 left -= this_files;
7534 total += this_files;
7535 }
7536
7537 if (!ret)
7538 return 0;
7539
7540 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007541 struct file *file = io_file_from_index(ctx, total);
7542
7543 if (file)
7544 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007545 total++;
7546 }
7547
7548 return ret;
7549}
7550#else
7551static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7552{
7553 return 0;
7554}
7555#endif
7556
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007557static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007558 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007559{
7560 int i;
7561
7562 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007563 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007564 unsigned this_files;
7565
7566 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7567 table->files = kcalloc(this_files, sizeof(struct file *),
7568 GFP_KERNEL);
7569 if (!table->files)
7570 break;
7571 nr_files -= this_files;
7572 }
7573
7574 if (i == nr_tables)
7575 return 0;
7576
7577 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007578 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007579 kfree(table->files);
7580 }
7581 return 1;
7582}
7583
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007584static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007585{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007586 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007587#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007588 struct sock *sock = ctx->ring_sock->sk;
7589 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7590 struct sk_buff *skb;
7591 int i;
7592
7593 __skb_queue_head_init(&list);
7594
7595 /*
7596 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7597 * remove this entry and rearrange the file array.
7598 */
7599 skb = skb_dequeue(head);
7600 while (skb) {
7601 struct scm_fp_list *fp;
7602
7603 fp = UNIXCB(skb).fp;
7604 for (i = 0; i < fp->count; i++) {
7605 int left;
7606
7607 if (fp->fp[i] != file)
7608 continue;
7609
7610 unix_notinflight(fp->user, fp->fp[i]);
7611 left = fp->count - 1 - i;
7612 if (left) {
7613 memmove(&fp->fp[i], &fp->fp[i + 1],
7614 left * sizeof(struct file *));
7615 }
7616 fp->count--;
7617 if (!fp->count) {
7618 kfree_skb(skb);
7619 skb = NULL;
7620 } else {
7621 __skb_queue_tail(&list, skb);
7622 }
7623 fput(file);
7624 file = NULL;
7625 break;
7626 }
7627
7628 if (!file)
7629 break;
7630
7631 __skb_queue_tail(&list, skb);
7632
7633 skb = skb_dequeue(head);
7634 }
7635
7636 if (skb_peek(&list)) {
7637 spin_lock_irq(&head->lock);
7638 while ((skb = __skb_dequeue(&list)) != NULL)
7639 __skb_queue_tail(head, skb);
7640 spin_unlock_irq(&head->lock);
7641 }
7642#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007643 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007644#endif
7645}
7646
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007647static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007648{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007649 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7650 struct io_ring_ctx *ctx = rsrc_data->ctx;
7651 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007652
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007653 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7654 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007655 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007656 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007657 }
7658
Xiaoguang Wang05589552020-03-31 14:05:18 +08007659 percpu_ref_exit(&ref_node->refs);
7660 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007661 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007662}
7663
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007664static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007665{
7666 struct io_ring_ctx *ctx;
7667 struct llist_node *node;
7668
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007669 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7670 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007671
7672 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007673 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007674 struct llist_node *next = node->next;
7675
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007676 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7677 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007678 node = next;
7679 }
7680}
7681
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007682static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7683 unsigned i)
7684{
7685 struct fixed_rsrc_table *table;
7686
7687 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7688 return &table->files[i & IORING_FILE_TABLE_MASK];
7689}
7690
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007691static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007692{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007693 struct fixed_rsrc_ref_node *ref_node;
7694 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007695 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007696 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007697 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007698
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007699 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7700 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007701 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007702
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007703 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007704 ref_node->done = true;
7705
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007706 while (!list_empty(&ctx->rsrc_ref_list)) {
7707 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007708 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007709 /* recycle ref nodes in order */
7710 if (!ref_node->done)
7711 break;
7712 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007713 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007714 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007715 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007716
7717 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007718 delay = 0;
7719
Jens Axboe4a38aed22020-05-14 17:21:15 -06007720 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007721 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007722 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007723 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007724}
7725
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007726static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007727 struct io_ring_ctx *ctx)
7728{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007729 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007730
7731 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7732 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007733 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007734
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007735 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007736 0, GFP_KERNEL)) {
7737 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007738 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007739 }
7740 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007741 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007742 ref_node->done = false;
7743 return ref_node;
7744}
7745
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007746static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7747 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007748{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007749 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007750 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007751}
7752
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007753static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007754{
7755 percpu_ref_exit(&ref_node->refs);
7756 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007757}
7758
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007759
Jens Axboe05f3fb32019-12-09 11:22:50 -07007760static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7761 unsigned nr_args)
7762{
7763 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007764 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007765 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007766 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007767 struct fixed_rsrc_ref_node *ref_node;
7768 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007769
7770 if (ctx->file_data)
7771 return -EBUSY;
7772 if (!nr_args)
7773 return -EINVAL;
7774 if (nr_args > IORING_MAX_FIXED_FILES)
7775 return -EMFILE;
7776
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007777 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007778 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007779 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007780 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007781
7782 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007783 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007784 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007785 if (!file_data->table)
7786 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007787
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007788 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007789 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007790
7791 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007792 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7793 ret = -EFAULT;
7794 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007795 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007796 /* allow sparse sets */
7797 if (fd == -1)
7798 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007799
Jens Axboe05f3fb32019-12-09 11:22:50 -07007800 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007801 ret = -EBADF;
7802 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007803 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007804
7805 /*
7806 * Don't allow io_uring instances to be registered. If UNIX
7807 * isn't enabled, then this causes a reference cycle and this
7808 * instance can never get freed. If UNIX is enabled we'll
7809 * handle it just fine, but there's still no point in allowing
7810 * a ring fd as it doesn't support regular read/write anyway.
7811 */
7812 if (file->f_op == &io_uring_fops) {
7813 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007814 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007815 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007816 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007817 }
7818
Jens Axboe05f3fb32019-12-09 11:22:50 -07007819 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007820 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007822 return ret;
7823 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007824
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007825 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007826 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007827 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007828 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007829 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007830 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007831
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007832 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007833 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007834out_fput:
7835 for (i = 0; i < ctx->nr_user_files; i++) {
7836 file = io_file_from_index(ctx, i);
7837 if (file)
7838 fput(file);
7839 }
7840 for (i = 0; i < nr_tables; i++)
7841 kfree(file_data->table[i].files);
7842 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007843out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007844 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007845 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007846 return ret;
7847}
7848
Jens Axboec3a31e62019-10-03 13:59:56 -06007849static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7850 int index)
7851{
7852#if defined(CONFIG_UNIX)
7853 struct sock *sock = ctx->ring_sock->sk;
7854 struct sk_buff_head *head = &sock->sk_receive_queue;
7855 struct sk_buff *skb;
7856
7857 /*
7858 * See if we can merge this file into an existing skb SCM_RIGHTS
7859 * file set. If there's no room, fall back to allocating a new skb
7860 * and filling it in.
7861 */
7862 spin_lock_irq(&head->lock);
7863 skb = skb_peek(head);
7864 if (skb) {
7865 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7866
7867 if (fpl->count < SCM_MAX_FD) {
7868 __skb_unlink(skb, head);
7869 spin_unlock_irq(&head->lock);
7870 fpl->fp[fpl->count] = get_file(file);
7871 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7872 fpl->count++;
7873 spin_lock_irq(&head->lock);
7874 __skb_queue_head(head, skb);
7875 } else {
7876 skb = NULL;
7877 }
7878 }
7879 spin_unlock_irq(&head->lock);
7880
7881 if (skb) {
7882 fput(file);
7883 return 0;
7884 }
7885
7886 return __io_sqe_files_scm(ctx, 1, index);
7887#else
7888 return 0;
7889#endif
7890}
7891
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007892static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007893{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007894 struct io_rsrc_put *prsrc;
7895 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007896
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007897 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7898 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007899 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007901 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007902 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007903
Hillf Dantona5318d32020-03-23 17:47:15 +08007904 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007905}
7906
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007907static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7908 struct file *file)
7909{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007910 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007911}
7912
Jens Axboe05f3fb32019-12-09 11:22:50 -07007913static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007914 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007915 unsigned nr_args)
7916{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007917 struct fixed_rsrc_data *data = ctx->file_data;
7918 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007919 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007920 __s32 __user *fds;
7921 int fd, i, err;
7922 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007923 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007924
Jens Axboe05f3fb32019-12-09 11:22:50 -07007925 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007926 return -EOVERFLOW;
7927 if (done > ctx->nr_user_files)
7928 return -EINVAL;
7929
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007930 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007931 if (!ref_node)
7932 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007933 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007934
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007935 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007936 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007937 err = 0;
7938 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7939 err = -EFAULT;
7940 break;
7941 }
noah4e0377a2021-01-26 15:23:28 -05007942 if (fd == IORING_REGISTER_FILES_SKIP)
7943 continue;
7944
Pavel Begunkov67973b92021-01-26 13:51:09 +00007945 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007946 file_slot = io_fixed_file_slot(ctx->file_data, i);
7947
7948 if (*file_slot) {
7949 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007950 if (err)
7951 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007952 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007953 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007954 }
7955 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007956 file = fget(fd);
7957 if (!file) {
7958 err = -EBADF;
7959 break;
7960 }
7961 /*
7962 * Don't allow io_uring instances to be registered. If
7963 * UNIX isn't enabled, then this causes a reference
7964 * cycle and this instance can never get freed. If UNIX
7965 * is enabled we'll handle it just fine, but there's
7966 * still no point in allowing a ring fd as it doesn't
7967 * support regular read/write anyway.
7968 */
7969 if (file->f_op == &io_uring_fops) {
7970 fput(file);
7971 err = -EBADF;
7972 break;
7973 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007974 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007975 if (err) {
7976 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007977 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007978 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007979 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007980 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007981 }
7982
Xiaoguang Wang05589552020-03-31 14:05:18 +08007983 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007984 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007985 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007986 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007987 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007988
7989 return done ? done : err;
7990}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007991
Jens Axboe05f3fb32019-12-09 11:22:50 -07007992static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7993 unsigned nr_args)
7994{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007995 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007996
7997 if (!ctx->file_data)
7998 return -ENXIO;
7999 if (!nr_args)
8000 return -EINVAL;
8001 if (copy_from_user(&up, arg, sizeof(up)))
8002 return -EFAULT;
8003 if (up.resv)
8004 return -EINVAL;
8005
8006 return __io_sqe_files_update(ctx, &up, nr_args);
8007}
Jens Axboec3a31e62019-10-03 13:59:56 -06008008
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008009static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008010{
8011 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8012
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008013 req = io_put_req_find_next(req);
8014 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008015}
8016
Pavel Begunkov24369c22020-01-28 03:15:48 +03008017static int io_init_wq_offload(struct io_ring_ctx *ctx,
8018 struct io_uring_params *p)
8019{
8020 struct io_wq_data data;
8021 struct fd f;
8022 struct io_ring_ctx *ctx_attach;
8023 unsigned int concurrency;
8024 int ret = 0;
8025
8026 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008027 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008028 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008029
8030 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8031 /* Do QD, or 4 * CPUS, whatever is smallest */
8032 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8033
8034 ctx->io_wq = io_wq_create(concurrency, &data);
8035 if (IS_ERR(ctx->io_wq)) {
8036 ret = PTR_ERR(ctx->io_wq);
8037 ctx->io_wq = NULL;
8038 }
8039 return ret;
8040 }
8041
8042 f = fdget(p->wq_fd);
8043 if (!f.file)
8044 return -EBADF;
8045
8046 if (f.file->f_op != &io_uring_fops) {
8047 ret = -EINVAL;
8048 goto out_fput;
8049 }
8050
8051 ctx_attach = f.file->private_data;
8052 /* @io_wq is protected by holding the fd */
8053 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8054 ret = -EINVAL;
8055 goto out_fput;
8056 }
8057
8058 ctx->io_wq = ctx_attach->io_wq;
8059out_fput:
8060 fdput(f);
8061 return ret;
8062}
8063
Jens Axboe0f212202020-09-13 13:09:39 -06008064static int io_uring_alloc_task_context(struct task_struct *task)
8065{
8066 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008067 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008068
8069 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8070 if (unlikely(!tctx))
8071 return -ENOMEM;
8072
Jens Axboed8a6df12020-10-15 16:24:45 -06008073 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8074 if (unlikely(ret)) {
8075 kfree(tctx);
8076 return ret;
8077 }
8078
Jens Axboe0f212202020-09-13 13:09:39 -06008079 xa_init(&tctx->xa);
8080 init_waitqueue_head(&tctx->wait);
8081 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008082 atomic_set(&tctx->in_idle, 0);
8083 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008084 io_init_identity(&tctx->__identity);
8085 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008086 task->io_uring = tctx;
8087 return 0;
8088}
8089
8090void __io_uring_free(struct task_struct *tsk)
8091{
8092 struct io_uring_task *tctx = tsk->io_uring;
8093
8094 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008095 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8096 if (tctx->identity != &tctx->__identity)
8097 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008098 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008099 kfree(tctx);
8100 tsk->io_uring = NULL;
8101}
8102
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008103static int io_sq_offload_create(struct io_ring_ctx *ctx,
8104 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008105{
8106 int ret;
8107
Jens Axboe6c271ce2019-01-10 11:22:30 -07008108 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008109 struct io_sq_data *sqd;
8110
Jens Axboe3ec482d2019-04-08 10:51:01 -06008111 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008112 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008113 goto err;
8114
Jens Axboe534ca6d2020-09-02 13:52:19 -06008115 sqd = io_get_sq_data(p);
8116 if (IS_ERR(sqd)) {
8117 ret = PTR_ERR(sqd);
8118 goto err;
8119 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008120
Jens Axboe534ca6d2020-09-02 13:52:19 -06008121 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008122 io_sq_thread_park(sqd);
8123 mutex_lock(&sqd->ctx_lock);
8124 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8125 mutex_unlock(&sqd->ctx_lock);
8126 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008127
Jens Axboe917257d2019-04-13 09:28:55 -06008128 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8129 if (!ctx->sq_thread_idle)
8130 ctx->sq_thread_idle = HZ;
8131
Jens Axboeaa061652020-09-02 14:50:27 -06008132 if (sqd->thread)
8133 goto done;
8134
Jens Axboe6c271ce2019-01-10 11:22:30 -07008135 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008136 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008137
Jens Axboe917257d2019-04-13 09:28:55 -06008138 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008139 if (cpu >= nr_cpu_ids)
8140 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008141 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008142 goto err;
8143
Jens Axboe69fb2132020-09-14 11:16:23 -06008144 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008145 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008146 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008147 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008148 "io_uring-sq");
8149 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008150 if (IS_ERR(sqd->thread)) {
8151 ret = PTR_ERR(sqd->thread);
8152 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008153 goto err;
8154 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008155 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008156 if (ret)
8157 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008158 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8159 /* Can't have SQ_AFF without SQPOLL */
8160 ret = -EINVAL;
8161 goto err;
8162 }
8163
Jens Axboeaa061652020-09-02 14:50:27 -06008164done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008165 ret = io_init_wq_offload(ctx, p);
8166 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008167 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008168
8169 return 0;
8170err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008171 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008172 return ret;
8173}
8174
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008175static void io_sq_offload_start(struct io_ring_ctx *ctx)
8176{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008177 struct io_sq_data *sqd = ctx->sq_data;
8178
8179 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8180 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008181}
8182
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008183static inline void __io_unaccount_mem(struct user_struct *user,
8184 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008185{
8186 atomic_long_sub(nr_pages, &user->locked_vm);
8187}
8188
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008189static inline int __io_account_mem(struct user_struct *user,
8190 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008191{
8192 unsigned long page_limit, cur_pages, new_pages;
8193
8194 /* Don't allow more pages than we can safely lock */
8195 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8196
8197 do {
8198 cur_pages = atomic_long_read(&user->locked_vm);
8199 new_pages = cur_pages + nr_pages;
8200 if (new_pages > page_limit)
8201 return -ENOMEM;
8202 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8203 new_pages) != cur_pages);
8204
8205 return 0;
8206}
8207
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008208static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8209 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008210{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008211 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008212 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008213
Jens Axboe2aede0e2020-09-14 10:45:53 -06008214 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008215 if (acct == ACCT_LOCKED) {
8216 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008217 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008218 mmap_write_unlock(ctx->mm_account);
8219 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008220 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008221 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008222 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008223}
8224
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008225static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8226 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008227{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008228 int ret;
8229
8230 if (ctx->limit_mem) {
8231 ret = __io_account_mem(ctx->user, nr_pages);
8232 if (ret)
8233 return ret;
8234 }
8235
Jens Axboe2aede0e2020-09-14 10:45:53 -06008236 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008237 if (acct == ACCT_LOCKED) {
8238 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008239 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008240 mmap_write_unlock(ctx->mm_account);
8241 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008242 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008243 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008244 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008245
8246 return 0;
8247}
8248
Jens Axboe2b188cc2019-01-07 10:46:33 -07008249static void io_mem_free(void *ptr)
8250{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008251 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008252
Mark Rutland52e04ef2019-04-30 17:30:21 +01008253 if (!ptr)
8254 return;
8255
8256 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008257 if (put_page_testzero(page))
8258 free_compound_page(page);
8259}
8260
8261static void *io_mem_alloc(size_t size)
8262{
8263 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8264 __GFP_NORETRY;
8265
8266 return (void *) __get_free_pages(gfp_flags, get_order(size));
8267}
8268
Hristo Venev75b28af2019-08-26 17:23:46 +00008269static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8270 size_t *sq_offset)
8271{
8272 struct io_rings *rings;
8273 size_t off, sq_array_size;
8274
8275 off = struct_size(rings, cqes, cq_entries);
8276 if (off == SIZE_MAX)
8277 return SIZE_MAX;
8278
8279#ifdef CONFIG_SMP
8280 off = ALIGN(off, SMP_CACHE_BYTES);
8281 if (off == 0)
8282 return SIZE_MAX;
8283#endif
8284
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008285 if (sq_offset)
8286 *sq_offset = off;
8287
Hristo Venev75b28af2019-08-26 17:23:46 +00008288 sq_array_size = array_size(sizeof(u32), sq_entries);
8289 if (sq_array_size == SIZE_MAX)
8290 return SIZE_MAX;
8291
8292 if (check_add_overflow(off, sq_array_size, &off))
8293 return SIZE_MAX;
8294
Hristo Venev75b28af2019-08-26 17:23:46 +00008295 return off;
8296}
8297
Jens Axboe2b188cc2019-01-07 10:46:33 -07008298static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8299{
Hristo Venev75b28af2019-08-26 17:23:46 +00008300 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008301
Hristo Venev75b28af2019-08-26 17:23:46 +00008302 pages = (size_t)1 << get_order(
8303 rings_size(sq_entries, cq_entries, NULL));
8304 pages += (size_t)1 << get_order(
8305 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008306
Hristo Venev75b28af2019-08-26 17:23:46 +00008307 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008308}
8309
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008310static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008311{
8312 int i, j;
8313
8314 if (!ctx->user_bufs)
8315 return -ENXIO;
8316
8317 for (i = 0; i < ctx->nr_user_bufs; i++) {
8318 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8319
8320 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008321 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008322
Jens Axboede293932020-09-17 16:19:16 -06008323 if (imu->acct_pages)
8324 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008325 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008326 imu->nr_bvecs = 0;
8327 }
8328
8329 kfree(ctx->user_bufs);
8330 ctx->user_bufs = NULL;
8331 ctx->nr_user_bufs = 0;
8332 return 0;
8333}
8334
8335static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8336 void __user *arg, unsigned index)
8337{
8338 struct iovec __user *src;
8339
8340#ifdef CONFIG_COMPAT
8341 if (ctx->compat) {
8342 struct compat_iovec __user *ciovs;
8343 struct compat_iovec ciov;
8344
8345 ciovs = (struct compat_iovec __user *) arg;
8346 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8347 return -EFAULT;
8348
Jens Axboed55e5f52019-12-11 16:12:15 -07008349 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008350 dst->iov_len = ciov.iov_len;
8351 return 0;
8352 }
8353#endif
8354 src = (struct iovec __user *) arg;
8355 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8356 return -EFAULT;
8357 return 0;
8358}
8359
Jens Axboede293932020-09-17 16:19:16 -06008360/*
8361 * Not super efficient, but this is just a registration time. And we do cache
8362 * the last compound head, so generally we'll only do a full search if we don't
8363 * match that one.
8364 *
8365 * We check if the given compound head page has already been accounted, to
8366 * avoid double accounting it. This allows us to account the full size of the
8367 * page, not just the constituent pages of a huge page.
8368 */
8369static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8370 int nr_pages, struct page *hpage)
8371{
8372 int i, j;
8373
8374 /* check current page array */
8375 for (i = 0; i < nr_pages; i++) {
8376 if (!PageCompound(pages[i]))
8377 continue;
8378 if (compound_head(pages[i]) == hpage)
8379 return true;
8380 }
8381
8382 /* check previously registered pages */
8383 for (i = 0; i < ctx->nr_user_bufs; i++) {
8384 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8385
8386 for (j = 0; j < imu->nr_bvecs; j++) {
8387 if (!PageCompound(imu->bvec[j].bv_page))
8388 continue;
8389 if (compound_head(imu->bvec[j].bv_page) == hpage)
8390 return true;
8391 }
8392 }
8393
8394 return false;
8395}
8396
8397static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8398 int nr_pages, struct io_mapped_ubuf *imu,
8399 struct page **last_hpage)
8400{
8401 int i, ret;
8402
8403 for (i = 0; i < nr_pages; i++) {
8404 if (!PageCompound(pages[i])) {
8405 imu->acct_pages++;
8406 } else {
8407 struct page *hpage;
8408
8409 hpage = compound_head(pages[i]);
8410 if (hpage == *last_hpage)
8411 continue;
8412 *last_hpage = hpage;
8413 if (headpage_already_acct(ctx, pages, i, hpage))
8414 continue;
8415 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8416 }
8417 }
8418
8419 if (!imu->acct_pages)
8420 return 0;
8421
8422 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8423 if (ret)
8424 imu->acct_pages = 0;
8425 return ret;
8426}
8427
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008428static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8429 struct io_mapped_ubuf *imu,
8430 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008431{
8432 struct vm_area_struct **vmas = NULL;
8433 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008434 unsigned long off, start, end, ubuf;
8435 size_t size;
8436 int ret, pret, nr_pages, i;
8437
8438 ubuf = (unsigned long) iov->iov_base;
8439 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8440 start = ubuf >> PAGE_SHIFT;
8441 nr_pages = end - start;
8442
8443 ret = -ENOMEM;
8444
8445 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8446 if (!pages)
8447 goto done;
8448
8449 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8450 GFP_KERNEL);
8451 if (!vmas)
8452 goto done;
8453
8454 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8455 GFP_KERNEL);
8456 if (!imu->bvec)
8457 goto done;
8458
8459 ret = 0;
8460 mmap_read_lock(current->mm);
8461 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8462 pages, vmas);
8463 if (pret == nr_pages) {
8464 /* don't support file backed memory */
8465 for (i = 0; i < nr_pages; i++) {
8466 struct vm_area_struct *vma = vmas[i];
8467
8468 if (vma->vm_file &&
8469 !is_file_hugepages(vma->vm_file)) {
8470 ret = -EOPNOTSUPP;
8471 break;
8472 }
8473 }
8474 } else {
8475 ret = pret < 0 ? pret : -EFAULT;
8476 }
8477 mmap_read_unlock(current->mm);
8478 if (ret) {
8479 /*
8480 * if we did partial map, or found file backed vmas,
8481 * release any pages we did get
8482 */
8483 if (pret > 0)
8484 unpin_user_pages(pages, pret);
8485 kvfree(imu->bvec);
8486 goto done;
8487 }
8488
8489 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8490 if (ret) {
8491 unpin_user_pages(pages, pret);
8492 kvfree(imu->bvec);
8493 goto done;
8494 }
8495
8496 off = ubuf & ~PAGE_MASK;
8497 size = iov->iov_len;
8498 for (i = 0; i < nr_pages; i++) {
8499 size_t vec_len;
8500
8501 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8502 imu->bvec[i].bv_page = pages[i];
8503 imu->bvec[i].bv_len = vec_len;
8504 imu->bvec[i].bv_offset = off;
8505 off = 0;
8506 size -= vec_len;
8507 }
8508 /* store original address for later verification */
8509 imu->ubuf = ubuf;
8510 imu->len = iov->iov_len;
8511 imu->nr_bvecs = nr_pages;
8512 ret = 0;
8513done:
8514 kvfree(pages);
8515 kvfree(vmas);
8516 return ret;
8517}
8518
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008519static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008520{
Jens Axboeedafcce2019-01-09 09:16:05 -07008521 if (ctx->user_bufs)
8522 return -EBUSY;
8523 if (!nr_args || nr_args > UIO_MAXIOV)
8524 return -EINVAL;
8525
8526 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8527 GFP_KERNEL);
8528 if (!ctx->user_bufs)
8529 return -ENOMEM;
8530
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008531 return 0;
8532}
8533
8534static int io_buffer_validate(struct iovec *iov)
8535{
8536 /*
8537 * Don't impose further limits on the size and buffer
8538 * constraints here, we'll -EINVAL later when IO is
8539 * submitted if they are wrong.
8540 */
8541 if (!iov->iov_base || !iov->iov_len)
8542 return -EFAULT;
8543
8544 /* arbitrary limit, but we need something */
8545 if (iov->iov_len > SZ_1G)
8546 return -EFAULT;
8547
8548 return 0;
8549}
8550
8551static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8552 unsigned int nr_args)
8553{
8554 int i, ret;
8555 struct iovec iov;
8556 struct page *last_hpage = NULL;
8557
8558 ret = io_buffers_map_alloc(ctx, nr_args);
8559 if (ret)
8560 return ret;
8561
Jens Axboeedafcce2019-01-09 09:16:05 -07008562 for (i = 0; i < nr_args; i++) {
8563 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008564
8565 ret = io_copy_iov(ctx, &iov, arg, i);
8566 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008567 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008568
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008569 ret = io_buffer_validate(&iov);
8570 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008571 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008572
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008573 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8574 if (ret)
8575 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008576
8577 ctx->nr_user_bufs++;
8578 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008579
8580 if (ret)
8581 io_sqe_buffers_unregister(ctx);
8582
Jens Axboeedafcce2019-01-09 09:16:05 -07008583 return ret;
8584}
8585
Jens Axboe9b402842019-04-11 11:45:41 -06008586static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8587{
8588 __s32 __user *fds = arg;
8589 int fd;
8590
8591 if (ctx->cq_ev_fd)
8592 return -EBUSY;
8593
8594 if (copy_from_user(&fd, fds, sizeof(*fds)))
8595 return -EFAULT;
8596
8597 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8598 if (IS_ERR(ctx->cq_ev_fd)) {
8599 int ret = PTR_ERR(ctx->cq_ev_fd);
8600 ctx->cq_ev_fd = NULL;
8601 return ret;
8602 }
8603
8604 return 0;
8605}
8606
8607static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8608{
8609 if (ctx->cq_ev_fd) {
8610 eventfd_ctx_put(ctx->cq_ev_fd);
8611 ctx->cq_ev_fd = NULL;
8612 return 0;
8613 }
8614
8615 return -ENXIO;
8616}
8617
Jens Axboe5a2e7452020-02-23 16:23:11 -07008618static int __io_destroy_buffers(int id, void *p, void *data)
8619{
8620 struct io_ring_ctx *ctx = data;
8621 struct io_buffer *buf = p;
8622
Jens Axboe067524e2020-03-02 16:32:28 -07008623 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008624 return 0;
8625}
8626
8627static void io_destroy_buffers(struct io_ring_ctx *ctx)
8628{
8629 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8630 idr_destroy(&ctx->io_buffer_idr);
8631}
8632
Jens Axboe2b188cc2019-01-07 10:46:33 -07008633static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8634{
Jens Axboe6b063142019-01-10 22:13:58 -07008635 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008636 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008637
8638 if (ctx->sqo_task) {
8639 put_task_struct(ctx->sqo_task);
8640 ctx->sqo_task = NULL;
8641 mmdrop(ctx->mm_account);
8642 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008643 }
Jens Axboedef596e2019-01-09 08:59:42 -07008644
Dennis Zhou91d8f512020-09-16 13:41:05 -07008645#ifdef CONFIG_BLK_CGROUP
8646 if (ctx->sqo_blkcg_css)
8647 css_put(ctx->sqo_blkcg_css);
8648#endif
8649
Jens Axboe6b063142019-01-10 22:13:58 -07008650 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008651 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008652 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008653 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008654
Jens Axboe2b188cc2019-01-07 10:46:33 -07008655#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008656 if (ctx->ring_sock) {
8657 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008658 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008659 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008660#endif
8661
Hristo Venev75b28af2019-08-26 17:23:46 +00008662 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008663 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008664
8665 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008666 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008667 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008668 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008669 kfree(ctx);
8670}
8671
8672static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8673{
8674 struct io_ring_ctx *ctx = file->private_data;
8675 __poll_t mask = 0;
8676
8677 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008678 /*
8679 * synchronizes with barrier from wq_has_sleeper call in
8680 * io_commit_cqring
8681 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008682 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008683 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008684 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008685 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8686 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008687 mask |= EPOLLIN | EPOLLRDNORM;
8688
8689 return mask;
8690}
8691
8692static int io_uring_fasync(int fd, struct file *file, int on)
8693{
8694 struct io_ring_ctx *ctx = file->private_data;
8695
8696 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8697}
8698
Yejune Deng0bead8c2020-12-24 11:02:20 +08008699static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008700{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008701 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008702
Jens Axboe1e6fa522020-10-15 08:46:24 -06008703 iod = idr_remove(&ctx->personality_idr, id);
8704 if (iod) {
8705 put_cred(iod->creds);
8706 if (refcount_dec_and_test(&iod->count))
8707 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008708 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008709 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008710
8711 return -EINVAL;
8712}
8713
8714static int io_remove_personalities(int id, void *p, void *data)
8715{
8716 struct io_ring_ctx *ctx = data;
8717
8718 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008719 return 0;
8720}
8721
Jens Axboe85faa7b2020-04-09 18:14:00 -06008722static void io_ring_exit_work(struct work_struct *work)
8723{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008724 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8725 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008726
Jens Axboe56952e92020-06-17 15:00:04 -06008727 /*
8728 * If we're doing polled IO and end up having requests being
8729 * submitted async (out-of-line), then completions can come in while
8730 * we're waiting for refs to drop. We need to reap these manually,
8731 * as nobody else will be looking for them.
8732 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008733 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008734 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008735 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008736 io_ring_ctx_free(ctx);
8737}
8738
Jens Axboe00c18642020-12-20 10:45:02 -07008739static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8740{
8741 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8742
8743 return req->ctx == data;
8744}
8745
Jens Axboe2b188cc2019-01-07 10:46:33 -07008746static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8747{
8748 mutex_lock(&ctx->uring_lock);
8749 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008750
8751 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8752 ctx->sqo_dead = 1;
8753
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008754 /* if force is set, the ring is going away. always drop after that */
8755 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008756 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008757 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008758 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008759 mutex_unlock(&ctx->uring_lock);
8760
Pavel Begunkov6b819282020-11-06 13:00:25 +00008761 io_kill_timeouts(ctx, NULL, NULL);
8762 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008763
8764 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008765 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008766
Jens Axboe15dff282019-11-13 09:09:23 -07008767 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008768 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008769
8770 /*
8771 * Do this upfront, so we won't have a grace period where the ring
8772 * is closed but resources aren't reaped yet. This can cause
8773 * spurious failure in setting up a new ring.
8774 */
Jens Axboe760618f2020-07-24 12:53:31 -06008775 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8776 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008777
Jens Axboe85faa7b2020-04-09 18:14:00 -06008778 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008779 /*
8780 * Use system_unbound_wq to avoid spawning tons of event kworkers
8781 * if we're exiting a ton of rings at the same time. It just adds
8782 * noise and overhead, there's no discernable change in runtime
8783 * over using system_wq.
8784 */
8785 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008786}
8787
8788static int io_uring_release(struct inode *inode, struct file *file)
8789{
8790 struct io_ring_ctx *ctx = file->private_data;
8791
8792 file->private_data = NULL;
8793 io_ring_ctx_wait_and_kill(ctx);
8794 return 0;
8795}
8796
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008797struct io_task_cancel {
8798 struct task_struct *task;
8799 struct files_struct *files;
8800};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008801
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008802static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008803{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008804 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008805 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008806 bool ret;
8807
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008808 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008809 unsigned long flags;
8810 struct io_ring_ctx *ctx = req->ctx;
8811
8812 /* protect against races with linked timeouts */
8813 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008814 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008815 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8816 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008817 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008818 }
8819 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008820}
8821
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008822static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008823 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008824 struct files_struct *files)
8825{
8826 struct io_defer_entry *de = NULL;
8827 LIST_HEAD(list);
8828
8829 spin_lock_irq(&ctx->completion_lock);
8830 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008831 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008832 list_cut_position(&list, &ctx->defer_list, &de->list);
8833 break;
8834 }
8835 }
8836 spin_unlock_irq(&ctx->completion_lock);
8837
8838 while (!list_empty(&list)) {
8839 de = list_first_entry(&list, struct io_defer_entry, list);
8840 list_del_init(&de->list);
8841 req_set_fail_links(de->req);
8842 io_put_req(de->req);
8843 io_req_complete(de->req, -ECANCELED);
8844 kfree(de);
8845 }
8846}
8847
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008848static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8849 struct task_struct *task,
8850 struct files_struct *files)
8851{
8852 struct io_task_cancel cancel = { .task = task, .files = files, };
8853
8854 while (1) {
8855 enum io_wq_cancel cret;
8856 bool ret = false;
8857
8858 if (ctx->io_wq) {
8859 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8860 &cancel, true);
8861 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8862 }
8863
8864 /* SQPOLL thread does its own polling */
8865 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8866 while (!list_empty_careful(&ctx->iopoll_list)) {
8867 io_iopoll_try_reap_events(ctx);
8868 ret = true;
8869 }
8870 }
8871
8872 ret |= io_poll_remove_all(ctx, task, files);
8873 ret |= io_kill_timeouts(ctx, task, files);
8874 ret |= io_run_task_work();
8875 io_cqring_overflow_flush(ctx, true, task, files);
8876 if (!ret)
8877 break;
8878 cond_resched();
8879 }
8880}
8881
Pavel Begunkovca70f002021-01-26 15:28:27 +00008882static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8883 struct task_struct *task,
8884 struct files_struct *files)
8885{
8886 struct io_kiocb *req;
8887 int cnt = 0;
8888
8889 spin_lock_irq(&ctx->inflight_lock);
8890 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8891 cnt += io_match_task(req, task, files);
8892 spin_unlock_irq(&ctx->inflight_lock);
8893 return cnt;
8894}
8895
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008896static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008897 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008898 struct files_struct *files)
8899{
Jens Axboefcb323c2019-10-24 12:39:47 -06008900 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008901 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008902 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008903
Pavel Begunkovca70f002021-01-26 15:28:27 +00008904 inflight = io_uring_count_inflight(ctx, task, files);
8905 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008906 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008907
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008908 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008909 prepare_to_wait(&task->io_uring->wait, &wait,
8910 TASK_UNINTERRUPTIBLE);
8911 if (inflight == io_uring_count_inflight(ctx, task, files))
8912 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008913 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008914 }
8915}
8916
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008917static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8918{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008919 mutex_lock(&ctx->uring_lock);
8920 ctx->sqo_dead = 1;
8921 mutex_unlock(&ctx->uring_lock);
8922
8923 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008924 if (ctx->rings)
8925 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008926}
8927
Jens Axboe0f212202020-09-13 13:09:39 -06008928/*
8929 * We need to iteratively cancel requests, in case a request has dependent
8930 * hard links. These persist even for failure of cancelations, hence keep
8931 * looping until none are found.
8932 */
8933static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8934 struct files_struct *files)
8935{
8936 struct task_struct *task = current;
8937
Jens Axboefdaf0832020-10-30 09:37:30 -06008938 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008939 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008940 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008941 atomic_inc(&task->io_uring->in_idle);
8942 io_sq_thread_park(ctx->sq_data);
8943 }
Jens Axboe0f212202020-09-13 13:09:39 -06008944
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008945 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008946
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008947 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008948 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008949 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008950
8951 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8952 atomic_dec(&task->io_uring->in_idle);
8953 /*
8954 * If the files that are going away are the ones in the thread
8955 * identity, clear them out.
8956 */
8957 if (task->io_uring->identity->files == files)
8958 task->io_uring->identity->files = NULL;
8959 io_sq_thread_unpark(ctx->sq_data);
8960 }
Jens Axboe0f212202020-09-13 13:09:39 -06008961}
8962
8963/*
8964 * Note that this task has used io_uring. We use it for cancelation purposes.
8965 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008966static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008967{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008968 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008969 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008970
8971 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008972 ret = io_uring_alloc_task_context(current);
8973 if (unlikely(ret))
8974 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008975 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008976 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008977 if (tctx->last != file) {
8978 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008979
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008980 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008981 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008982 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8983 file, GFP_KERNEL));
8984 if (ret) {
8985 fput(file);
8986 return ret;
8987 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008988
8989 /* one and only SQPOLL file note, held by sqo_task */
8990 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8991 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008992 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008993 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008994 }
8995
Jens Axboefdaf0832020-10-30 09:37:30 -06008996 /*
8997 * This is race safe in that the task itself is doing this, hence it
8998 * cannot be going through the exit/cancel paths at the same time.
8999 * This cannot be modified while exit/cancel is running.
9000 */
9001 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9002 tctx->sqpoll = true;
9003
Jens Axboe0f212202020-09-13 13:09:39 -06009004 return 0;
9005}
9006
9007/*
9008 * Remove this io_uring_file -> task mapping.
9009 */
9010static void io_uring_del_task_file(struct file *file)
9011{
9012 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009013
9014 if (tctx->last == file)
9015 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009016 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009017 if (file)
9018 fput(file);
9019}
9020
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009021static void io_uring_remove_task_files(struct io_uring_task *tctx)
9022{
9023 struct file *file;
9024 unsigned long index;
9025
9026 xa_for_each(&tctx->xa, index, file)
9027 io_uring_del_task_file(file);
9028}
9029
Jens Axboe0f212202020-09-13 13:09:39 -06009030void __io_uring_files_cancel(struct files_struct *files)
9031{
9032 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009033 struct file *file;
9034 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009035
9036 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009037 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009038 xa_for_each(&tctx->xa, index, file)
9039 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009040 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009041
9042 if (files)
9043 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009044}
9045
9046static s64 tctx_inflight(struct io_uring_task *tctx)
9047{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009048 return percpu_counter_sum(&tctx->inflight);
9049}
9050
9051static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9052{
9053 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009054 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009055 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009056
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009057 if (!ctx->sq_data)
9058 return;
9059 tctx = ctx->sq_data->thread->io_uring;
9060 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009061
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009062 atomic_inc(&tctx->in_idle);
9063 do {
9064 /* read completions before cancelations */
9065 inflight = tctx_inflight(tctx);
9066 if (!inflight)
9067 break;
9068 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009069
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009070 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9071 /*
9072 * If we've seen completions, retry without waiting. This
9073 * avoids a race where a completion comes in before we did
9074 * prepare_to_wait().
9075 */
9076 if (inflight == tctx_inflight(tctx))
9077 schedule();
9078 finish_wait(&tctx->wait, &wait);
9079 } while (1);
9080 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009081}
9082
Jens Axboe0f212202020-09-13 13:09:39 -06009083/*
9084 * Find any io_uring fd that this task has registered or done IO on, and cancel
9085 * requests.
9086 */
9087void __io_uring_task_cancel(void)
9088{
9089 struct io_uring_task *tctx = current->io_uring;
9090 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009091 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009092
9093 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009094 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009095
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009096 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009097 if (tctx->sqpoll) {
9098 struct file *file;
9099 unsigned long index;
9100
9101 xa_for_each(&tctx->xa, index, file)
9102 io_uring_cancel_sqpoll(file->private_data);
9103 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009104
Jens Axboed8a6df12020-10-15 16:24:45 -06009105 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009106 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009107 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009108 if (!inflight)
9109 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009110 __io_uring_files_cancel(NULL);
9111
9112 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9113
9114 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009115 * If we've seen completions, retry without waiting. This
9116 * avoids a race where a completion comes in before we did
9117 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009118 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009119 if (inflight == tctx_inflight(tctx))
9120 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009121 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009122 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009123
Jens Axboefdaf0832020-10-30 09:37:30 -06009124 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009125
9126 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009127}
9128
Jens Axboefcb323c2019-10-24 12:39:47 -06009129static int io_uring_flush(struct file *file, void *data)
9130{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009131 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009132 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009133
Jens Axboe84965ff2021-01-23 15:51:11 -07009134 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9135 io_uring_cancel_task_requests(ctx, NULL);
9136
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009137 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009138 return 0;
9139
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009140 /* we should have cancelled and erased it before PF_EXITING */
9141 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9142 xa_load(&tctx->xa, (unsigned long)file));
9143
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009144 /*
9145 * fput() is pending, will be 2 if the only other ref is our potential
9146 * task file note. If the task is exiting, drop regardless of count.
9147 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009148 if (atomic_long_read(&file->f_count) != 2)
9149 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009150
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009151 if (ctx->flags & IORING_SETUP_SQPOLL) {
9152 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009153 WARN_ON_ONCE(ctx->sqo_task != current &&
9154 xa_load(&tctx->xa, (unsigned long)file));
9155 /* sqo_dead check is for when this happens after cancellation */
9156 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009157 !xa_load(&tctx->xa, (unsigned long)file));
9158
9159 io_disable_sqo_submit(ctx);
9160 }
9161
9162 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9163 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009164 return 0;
9165}
9166
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009167static void *io_uring_validate_mmap_request(struct file *file,
9168 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009169{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009170 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009171 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009172 struct page *page;
9173 void *ptr;
9174
9175 switch (offset) {
9176 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009177 case IORING_OFF_CQ_RING:
9178 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009179 break;
9180 case IORING_OFF_SQES:
9181 ptr = ctx->sq_sqes;
9182 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009184 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009185 }
9186
9187 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009188 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009189 return ERR_PTR(-EINVAL);
9190
9191 return ptr;
9192}
9193
9194#ifdef CONFIG_MMU
9195
9196static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9197{
9198 size_t sz = vma->vm_end - vma->vm_start;
9199 unsigned long pfn;
9200 void *ptr;
9201
9202 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9203 if (IS_ERR(ptr))
9204 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009205
9206 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9207 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9208}
9209
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009210#else /* !CONFIG_MMU */
9211
9212static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9213{
9214 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9215}
9216
9217static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9218{
9219 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9220}
9221
9222static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9223 unsigned long addr, unsigned long len,
9224 unsigned long pgoff, unsigned long flags)
9225{
9226 void *ptr;
9227
9228 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9229 if (IS_ERR(ptr))
9230 return PTR_ERR(ptr);
9231
9232 return (unsigned long) ptr;
9233}
9234
9235#endif /* !CONFIG_MMU */
9236
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009237static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009238{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009239 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009240 DEFINE_WAIT(wait);
9241
9242 do {
9243 if (!io_sqring_full(ctx))
9244 break;
9245
9246 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9247
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009248 if (unlikely(ctx->sqo_dead)) {
9249 ret = -EOWNERDEAD;
9250 goto out;
9251 }
9252
Jens Axboe90554202020-09-03 12:12:41 -06009253 if (!io_sqring_full(ctx))
9254 break;
9255
9256 schedule();
9257 } while (!signal_pending(current));
9258
9259 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009260out:
9261 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009262}
9263
Hao Xuc73ebb62020-11-03 10:54:37 +08009264static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9265 struct __kernel_timespec __user **ts,
9266 const sigset_t __user **sig)
9267{
9268 struct io_uring_getevents_arg arg;
9269
9270 /*
9271 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9272 * is just a pointer to the sigset_t.
9273 */
9274 if (!(flags & IORING_ENTER_EXT_ARG)) {
9275 *sig = (const sigset_t __user *) argp;
9276 *ts = NULL;
9277 return 0;
9278 }
9279
9280 /*
9281 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9282 * timespec and sigset_t pointers if good.
9283 */
9284 if (*argsz != sizeof(arg))
9285 return -EINVAL;
9286 if (copy_from_user(&arg, argp, sizeof(arg)))
9287 return -EFAULT;
9288 *sig = u64_to_user_ptr(arg.sigmask);
9289 *argsz = arg.sigmask_sz;
9290 *ts = u64_to_user_ptr(arg.ts);
9291 return 0;
9292}
9293
Jens Axboe2b188cc2019-01-07 10:46:33 -07009294SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009295 u32, min_complete, u32, flags, const void __user *, argp,
9296 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009297{
9298 struct io_ring_ctx *ctx;
9299 long ret = -EBADF;
9300 int submitted = 0;
9301 struct fd f;
9302
Jens Axboe4c6e2772020-07-01 11:29:10 -06009303 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009304
Jens Axboe90554202020-09-03 12:12:41 -06009305 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009306 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009307 return -EINVAL;
9308
9309 f = fdget(fd);
9310 if (!f.file)
9311 return -EBADF;
9312
9313 ret = -EOPNOTSUPP;
9314 if (f.file->f_op != &io_uring_fops)
9315 goto out_fput;
9316
9317 ret = -ENXIO;
9318 ctx = f.file->private_data;
9319 if (!percpu_ref_tryget(&ctx->refs))
9320 goto out_fput;
9321
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009322 ret = -EBADFD;
9323 if (ctx->flags & IORING_SETUP_R_DISABLED)
9324 goto out;
9325
Jens Axboe6c271ce2019-01-10 11:22:30 -07009326 /*
9327 * For SQ polling, the thread will do all submissions and completions.
9328 * Just return the requested submit count, and wake the thread if
9329 * we were asked to.
9330 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009331 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009332 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009333 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009334
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009335 ret = -EOWNERDEAD;
9336 if (unlikely(ctx->sqo_dead))
9337 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009338 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009339 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009340 if (flags & IORING_ENTER_SQ_WAIT) {
9341 ret = io_sqpoll_wait_sq(ctx);
9342 if (ret)
9343 goto out;
9344 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009345 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009346 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009347 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009348 if (unlikely(ret))
9349 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009350 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009351 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009353
9354 if (submitted != to_submit)
9355 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009356 }
9357 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009358 const sigset_t __user *sig;
9359 struct __kernel_timespec __user *ts;
9360
9361 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9362 if (unlikely(ret))
9363 goto out;
9364
Jens Axboe2b188cc2019-01-07 10:46:33 -07009365 min_complete = min(min_complete, ctx->cq_entries);
9366
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009367 /*
9368 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9369 * space applications don't need to do io completion events
9370 * polling again, they can rely on io_sq_thread to do polling
9371 * work, which can reduce cpu usage and uring_lock contention.
9372 */
9373 if (ctx->flags & IORING_SETUP_IOPOLL &&
9374 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009375 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009376 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009377 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009378 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009379 }
9380
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009381out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009382 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009383out_fput:
9384 fdput(f);
9385 return submitted ? submitted : ret;
9386}
9387
Tobias Klauserbebdb652020-02-26 18:38:32 +01009388#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009389static int io_uring_show_cred(int id, void *p, void *data)
9390{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009391 struct io_identity *iod = p;
9392 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009393 struct seq_file *m = data;
9394 struct user_namespace *uns = seq_user_ns(m);
9395 struct group_info *gi;
9396 kernel_cap_t cap;
9397 unsigned __capi;
9398 int g;
9399
9400 seq_printf(m, "%5d\n", id);
9401 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9402 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9403 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9404 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9405 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9406 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9407 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9408 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9409 seq_puts(m, "\n\tGroups:\t");
9410 gi = cred->group_info;
9411 for (g = 0; g < gi->ngroups; g++) {
9412 seq_put_decimal_ull(m, g ? " " : "",
9413 from_kgid_munged(uns, gi->gid[g]));
9414 }
9415 seq_puts(m, "\n\tCapEff:\t");
9416 cap = cred->cap_effective;
9417 CAP_FOR_EACH_U32(__capi)
9418 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9419 seq_putc(m, '\n');
9420 return 0;
9421}
9422
9423static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9424{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009425 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009426 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009427 int i;
9428
Jens Axboefad8e0d2020-09-28 08:57:48 -06009429 /*
9430 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9431 * since fdinfo case grabs it in the opposite direction of normal use
9432 * cases. If we fail to get the lock, we just don't iterate any
9433 * structures that could be going away outside the io_uring mutex.
9434 */
9435 has_lock = mutex_trylock(&ctx->uring_lock);
9436
Joseph Qidbbe9c62020-09-29 09:01:22 -06009437 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9438 sq = ctx->sq_data;
9439
9440 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9441 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009442 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009443 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009444 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009445
Jens Axboe87ce9552020-01-30 08:25:34 -07009446 if (f)
9447 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9448 else
9449 seq_printf(m, "%5u: <none>\n", i);
9450 }
9451 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009452 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009453 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9454
9455 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9456 (unsigned int) buf->len);
9457 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009458 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009459 seq_printf(m, "Personalities:\n");
9460 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9461 }
Jens Axboed7718a92020-02-14 22:23:12 -07009462 seq_printf(m, "PollList:\n");
9463 spin_lock_irq(&ctx->completion_lock);
9464 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9465 struct hlist_head *list = &ctx->cancel_hash[i];
9466 struct io_kiocb *req;
9467
9468 hlist_for_each_entry(req, list, hash_node)
9469 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9470 req->task->task_works != NULL);
9471 }
9472 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009473 if (has_lock)
9474 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009475}
9476
9477static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9478{
9479 struct io_ring_ctx *ctx = f->private_data;
9480
9481 if (percpu_ref_tryget(&ctx->refs)) {
9482 __io_uring_show_fdinfo(ctx, m);
9483 percpu_ref_put(&ctx->refs);
9484 }
9485}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009486#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009487
Jens Axboe2b188cc2019-01-07 10:46:33 -07009488static const struct file_operations io_uring_fops = {
9489 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009490 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009491 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009492#ifndef CONFIG_MMU
9493 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9494 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9495#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009496 .poll = io_uring_poll,
9497 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009498#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009499 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009500#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009501};
9502
9503static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9504 struct io_uring_params *p)
9505{
Hristo Venev75b28af2019-08-26 17:23:46 +00009506 struct io_rings *rings;
9507 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009508
Jens Axboebd740482020-08-05 12:58:23 -06009509 /* make sure these are sane, as we already accounted them */
9510 ctx->sq_entries = p->sq_entries;
9511 ctx->cq_entries = p->cq_entries;
9512
Hristo Venev75b28af2019-08-26 17:23:46 +00009513 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9514 if (size == SIZE_MAX)
9515 return -EOVERFLOW;
9516
9517 rings = io_mem_alloc(size);
9518 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009519 return -ENOMEM;
9520
Hristo Venev75b28af2019-08-26 17:23:46 +00009521 ctx->rings = rings;
9522 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9523 rings->sq_ring_mask = p->sq_entries - 1;
9524 rings->cq_ring_mask = p->cq_entries - 1;
9525 rings->sq_ring_entries = p->sq_entries;
9526 rings->cq_ring_entries = p->cq_entries;
9527 ctx->sq_mask = rings->sq_ring_mask;
9528 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009529
9530 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009531 if (size == SIZE_MAX) {
9532 io_mem_free(ctx->rings);
9533 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009534 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009535 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009536
9537 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009538 if (!ctx->sq_sqes) {
9539 io_mem_free(ctx->rings);
9540 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009542 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009543
Jens Axboe2b188cc2019-01-07 10:46:33 -07009544 return 0;
9545}
9546
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009547static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9548{
9549 int ret, fd;
9550
9551 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9552 if (fd < 0)
9553 return fd;
9554
9555 ret = io_uring_add_task_file(ctx, file);
9556 if (ret) {
9557 put_unused_fd(fd);
9558 return ret;
9559 }
9560 fd_install(fd, file);
9561 return fd;
9562}
9563
Jens Axboe2b188cc2019-01-07 10:46:33 -07009564/*
9565 * Allocate an anonymous fd, this is what constitutes the application
9566 * visible backing of an io_uring instance. The application mmaps this
9567 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9568 * we have to tie this fd to a socket for file garbage collection purposes.
9569 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009570static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009571{
9572 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009573#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009574 int ret;
9575
Jens Axboe2b188cc2019-01-07 10:46:33 -07009576 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9577 &ctx->ring_sock);
9578 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009579 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009580#endif
9581
Jens Axboe2b188cc2019-01-07 10:46:33 -07009582 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9583 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009584#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009585 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009586 sock_release(ctx->ring_sock);
9587 ctx->ring_sock = NULL;
9588 } else {
9589 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009590 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009591#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009592 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009593}
9594
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009595static int io_uring_create(unsigned entries, struct io_uring_params *p,
9596 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009597{
9598 struct user_struct *user = NULL;
9599 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009600 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009601 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009602 int ret;
9603
Jens Axboe8110c1a2019-12-28 15:39:54 -07009604 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009605 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009606 if (entries > IORING_MAX_ENTRIES) {
9607 if (!(p->flags & IORING_SETUP_CLAMP))
9608 return -EINVAL;
9609 entries = IORING_MAX_ENTRIES;
9610 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009611
9612 /*
9613 * Use twice as many entries for the CQ ring. It's possible for the
9614 * application to drive a higher depth than the size of the SQ ring,
9615 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009616 * some flexibility in overcommitting a bit. If the application has
9617 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9618 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009619 */
9620 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009621 if (p->flags & IORING_SETUP_CQSIZE) {
9622 /*
9623 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9624 * to a power-of-two, if it isn't already. We do NOT impose
9625 * any cq vs sq ring sizing.
9626 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009627 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009628 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009629 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9630 if (!(p->flags & IORING_SETUP_CLAMP))
9631 return -EINVAL;
9632 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9633 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009634 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9635 if (p->cq_entries < p->sq_entries)
9636 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009637 } else {
9638 p->cq_entries = 2 * p->sq_entries;
9639 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009640
9641 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009642 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009643
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009644 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009645 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009646 ring_pages(p->sq_entries, p->cq_entries));
9647 if (ret) {
9648 free_uid(user);
9649 return ret;
9650 }
9651 }
9652
9653 ctx = io_ring_ctx_alloc(p);
9654 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009655 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009656 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009657 p->cq_entries));
9658 free_uid(user);
9659 return -ENOMEM;
9660 }
9661 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009662 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009663 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009664#ifdef CONFIG_AUDIT
9665 ctx->loginuid = current->loginuid;
9666 ctx->sessionid = current->sessionid;
9667#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009668 ctx->sqo_task = get_task_struct(current);
9669
9670 /*
9671 * This is just grabbed for accounting purposes. When a process exits,
9672 * the mm is exited and dropped before the files, hence we need to hang
9673 * on to this mm purely for the purposes of being able to unaccount
9674 * memory (locked/pinned vm). It's not used for anything else.
9675 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009676 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009677 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009678
Dennis Zhou91d8f512020-09-16 13:41:05 -07009679#ifdef CONFIG_BLK_CGROUP
9680 /*
9681 * The sq thread will belong to the original cgroup it was inited in.
9682 * If the cgroup goes offline (e.g. disabling the io controller), then
9683 * issued bios will be associated with the closest cgroup later in the
9684 * block layer.
9685 */
9686 rcu_read_lock();
9687 ctx->sqo_blkcg_css = blkcg_css();
9688 ret = css_tryget_online(ctx->sqo_blkcg_css);
9689 rcu_read_unlock();
9690 if (!ret) {
9691 /* don't init against a dying cgroup, have the user try again */
9692 ctx->sqo_blkcg_css = NULL;
9693 ret = -ENODEV;
9694 goto err;
9695 }
9696#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009697
Jens Axboe2b188cc2019-01-07 10:46:33 -07009698 /*
9699 * Account memory _before_ installing the file descriptor. Once
9700 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009701 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009702 * will un-account as well.
9703 */
9704 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9705 ACCT_LOCKED);
9706 ctx->limit_mem = limit_mem;
9707
9708 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009709 if (ret)
9710 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009711
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009712 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009713 if (ret)
9714 goto err;
9715
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009716 if (!(p->flags & IORING_SETUP_R_DISABLED))
9717 io_sq_offload_start(ctx);
9718
Jens Axboe2b188cc2019-01-07 10:46:33 -07009719 memset(&p->sq_off, 0, sizeof(p->sq_off));
9720 p->sq_off.head = offsetof(struct io_rings, sq.head);
9721 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9722 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9723 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9724 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9725 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9726 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9727
9728 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009729 p->cq_off.head = offsetof(struct io_rings, cq.head);
9730 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9731 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9732 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9733 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9734 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009735 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009736
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009737 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9738 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009739 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009740 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9741 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009742
9743 if (copy_to_user(params, p, sizeof(*p))) {
9744 ret = -EFAULT;
9745 goto err;
9746 }
Jens Axboed1719f72020-07-30 13:43:53 -06009747
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009748 file = io_uring_get_file(ctx);
9749 if (IS_ERR(file)) {
9750 ret = PTR_ERR(file);
9751 goto err;
9752 }
9753
Jens Axboed1719f72020-07-30 13:43:53 -06009754 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009755 * Install ring fd as the very last thing, so we don't risk someone
9756 * having closed it before we finish setup
9757 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009758 ret = io_uring_install_fd(ctx, file);
9759 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009760 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009761 /* fput will clean it up */
9762 fput(file);
9763 return ret;
9764 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009765
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009766 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009767 return ret;
9768err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009769 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009770 io_ring_ctx_wait_and_kill(ctx);
9771 return ret;
9772}
9773
9774/*
9775 * Sets up an aio uring context, and returns the fd. Applications asks for a
9776 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9777 * params structure passed in.
9778 */
9779static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9780{
9781 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009782 int i;
9783
9784 if (copy_from_user(&p, params, sizeof(p)))
9785 return -EFAULT;
9786 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9787 if (p.resv[i])
9788 return -EINVAL;
9789 }
9790
Jens Axboe6c271ce2019-01-10 11:22:30 -07009791 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009792 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009793 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9794 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009795 return -EINVAL;
9796
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009797 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009798}
9799
9800SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9801 struct io_uring_params __user *, params)
9802{
9803 return io_uring_setup(entries, params);
9804}
9805
Jens Axboe66f4af92020-01-16 15:36:52 -07009806static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9807{
9808 struct io_uring_probe *p;
9809 size_t size;
9810 int i, ret;
9811
9812 size = struct_size(p, ops, nr_args);
9813 if (size == SIZE_MAX)
9814 return -EOVERFLOW;
9815 p = kzalloc(size, GFP_KERNEL);
9816 if (!p)
9817 return -ENOMEM;
9818
9819 ret = -EFAULT;
9820 if (copy_from_user(p, arg, size))
9821 goto out;
9822 ret = -EINVAL;
9823 if (memchr_inv(p, 0, size))
9824 goto out;
9825
9826 p->last_op = IORING_OP_LAST - 1;
9827 if (nr_args > IORING_OP_LAST)
9828 nr_args = IORING_OP_LAST;
9829
9830 for (i = 0; i < nr_args; i++) {
9831 p->ops[i].op = i;
9832 if (!io_op_defs[i].not_supported)
9833 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9834 }
9835 p->ops_len = i;
9836
9837 ret = 0;
9838 if (copy_to_user(arg, p, size))
9839 ret = -EFAULT;
9840out:
9841 kfree(p);
9842 return ret;
9843}
9844
Jens Axboe071698e2020-01-28 10:04:42 -07009845static int io_register_personality(struct io_ring_ctx *ctx)
9846{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009847 struct io_identity *id;
9848 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009849
Jens Axboe1e6fa522020-10-15 08:46:24 -06009850 id = kmalloc(sizeof(*id), GFP_KERNEL);
9851 if (unlikely(!id))
9852 return -ENOMEM;
9853
9854 io_init_identity(id);
9855 id->creds = get_current_cred();
9856
9857 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9858 if (ret < 0) {
9859 put_cred(id->creds);
9860 kfree(id);
9861 }
9862 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009863}
9864
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009865static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9866 unsigned int nr_args)
9867{
9868 struct io_uring_restriction *res;
9869 size_t size;
9870 int i, ret;
9871
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009872 /* Restrictions allowed only if rings started disabled */
9873 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9874 return -EBADFD;
9875
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009876 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009877 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009878 return -EBUSY;
9879
9880 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9881 return -EINVAL;
9882
9883 size = array_size(nr_args, sizeof(*res));
9884 if (size == SIZE_MAX)
9885 return -EOVERFLOW;
9886
9887 res = memdup_user(arg, size);
9888 if (IS_ERR(res))
9889 return PTR_ERR(res);
9890
9891 ret = 0;
9892
9893 for (i = 0; i < nr_args; i++) {
9894 switch (res[i].opcode) {
9895 case IORING_RESTRICTION_REGISTER_OP:
9896 if (res[i].register_op >= IORING_REGISTER_LAST) {
9897 ret = -EINVAL;
9898 goto out;
9899 }
9900
9901 __set_bit(res[i].register_op,
9902 ctx->restrictions.register_op);
9903 break;
9904 case IORING_RESTRICTION_SQE_OP:
9905 if (res[i].sqe_op >= IORING_OP_LAST) {
9906 ret = -EINVAL;
9907 goto out;
9908 }
9909
9910 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9911 break;
9912 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9913 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9914 break;
9915 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9916 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9917 break;
9918 default:
9919 ret = -EINVAL;
9920 goto out;
9921 }
9922 }
9923
9924out:
9925 /* Reset all restrictions if an error happened */
9926 if (ret != 0)
9927 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9928 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009929 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009930
9931 kfree(res);
9932 return ret;
9933}
9934
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009935static int io_register_enable_rings(struct io_ring_ctx *ctx)
9936{
9937 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9938 return -EBADFD;
9939
9940 if (ctx->restrictions.registered)
9941 ctx->restricted = 1;
9942
9943 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9944
9945 io_sq_offload_start(ctx);
9946
9947 return 0;
9948}
9949
Jens Axboe071698e2020-01-28 10:04:42 -07009950static bool io_register_op_must_quiesce(int op)
9951{
9952 switch (op) {
9953 case IORING_UNREGISTER_FILES:
9954 case IORING_REGISTER_FILES_UPDATE:
9955 case IORING_REGISTER_PROBE:
9956 case IORING_REGISTER_PERSONALITY:
9957 case IORING_UNREGISTER_PERSONALITY:
9958 return false;
9959 default:
9960 return true;
9961 }
9962}
9963
Jens Axboeedafcce2019-01-09 09:16:05 -07009964static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9965 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009966 __releases(ctx->uring_lock)
9967 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009968{
9969 int ret;
9970
Jens Axboe35fa71a2019-04-22 10:23:23 -06009971 /*
9972 * We're inside the ring mutex, if the ref is already dying, then
9973 * someone else killed the ctx or is already going through
9974 * io_uring_register().
9975 */
9976 if (percpu_ref_is_dying(&ctx->refs))
9977 return -ENXIO;
9978
Jens Axboe071698e2020-01-28 10:04:42 -07009979 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009980 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009981
Jens Axboe05f3fb32019-12-09 11:22:50 -07009982 /*
9983 * Drop uring mutex before waiting for references to exit. If
9984 * another thread is currently inside io_uring_enter() it might
9985 * need to grab the uring_lock to make progress. If we hold it
9986 * here across the drain wait, then we can deadlock. It's safe
9987 * to drop the mutex here, since no new references will come in
9988 * after we've killed the percpu ref.
9989 */
9990 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009991 do {
9992 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9993 if (!ret)
9994 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009995 ret = io_run_task_work_sig();
9996 if (ret < 0)
9997 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009998 } while (1);
9999
Jens Axboe05f3fb32019-12-09 11:22:50 -070010000 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010001
Jens Axboec1503682020-01-08 08:26:07 -070010002 if (ret) {
10003 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010004 goto out_quiesce;
10005 }
10006 }
10007
10008 if (ctx->restricted) {
10009 if (opcode >= IORING_REGISTER_LAST) {
10010 ret = -EINVAL;
10011 goto out;
10012 }
10013
10014 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10015 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010016 goto out;
10017 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010018 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010019
10020 switch (opcode) {
10021 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010022 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010023 break;
10024 case IORING_UNREGISTER_BUFFERS:
10025 ret = -EINVAL;
10026 if (arg || nr_args)
10027 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010028 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010029 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010030 case IORING_REGISTER_FILES:
10031 ret = io_sqe_files_register(ctx, arg, nr_args);
10032 break;
10033 case IORING_UNREGISTER_FILES:
10034 ret = -EINVAL;
10035 if (arg || nr_args)
10036 break;
10037 ret = io_sqe_files_unregister(ctx);
10038 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010039 case IORING_REGISTER_FILES_UPDATE:
10040 ret = io_sqe_files_update(ctx, arg, nr_args);
10041 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010042 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010043 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010044 ret = -EINVAL;
10045 if (nr_args != 1)
10046 break;
10047 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010048 if (ret)
10049 break;
10050 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10051 ctx->eventfd_async = 1;
10052 else
10053 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010054 break;
10055 case IORING_UNREGISTER_EVENTFD:
10056 ret = -EINVAL;
10057 if (arg || nr_args)
10058 break;
10059 ret = io_eventfd_unregister(ctx);
10060 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010061 case IORING_REGISTER_PROBE:
10062 ret = -EINVAL;
10063 if (!arg || nr_args > 256)
10064 break;
10065 ret = io_probe(ctx, arg, nr_args);
10066 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010067 case IORING_REGISTER_PERSONALITY:
10068 ret = -EINVAL;
10069 if (arg || nr_args)
10070 break;
10071 ret = io_register_personality(ctx);
10072 break;
10073 case IORING_UNREGISTER_PERSONALITY:
10074 ret = -EINVAL;
10075 if (arg)
10076 break;
10077 ret = io_unregister_personality(ctx, nr_args);
10078 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010079 case IORING_REGISTER_ENABLE_RINGS:
10080 ret = -EINVAL;
10081 if (arg || nr_args)
10082 break;
10083 ret = io_register_enable_rings(ctx);
10084 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010085 case IORING_REGISTER_RESTRICTIONS:
10086 ret = io_register_restrictions(ctx, arg, nr_args);
10087 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010088 default:
10089 ret = -EINVAL;
10090 break;
10091 }
10092
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010093out:
Jens Axboe071698e2020-01-28 10:04:42 -070010094 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010095 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010096 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010097out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010098 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010099 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010100 return ret;
10101}
10102
10103SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10104 void __user *, arg, unsigned int, nr_args)
10105{
10106 struct io_ring_ctx *ctx;
10107 long ret = -EBADF;
10108 struct fd f;
10109
10110 f = fdget(fd);
10111 if (!f.file)
10112 return -EBADF;
10113
10114 ret = -EOPNOTSUPP;
10115 if (f.file->f_op != &io_uring_fops)
10116 goto out_fput;
10117
10118 ctx = f.file->private_data;
10119
10120 mutex_lock(&ctx->uring_lock);
10121 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10122 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010123 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10124 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010125out_fput:
10126 fdput(f);
10127 return ret;
10128}
10129
Jens Axboe2b188cc2019-01-07 10:46:33 -070010130static int __init io_uring_init(void)
10131{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010132#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10133 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10134 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10135} while (0)
10136
10137#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10138 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10139 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10140 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10141 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10142 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10143 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10144 BUILD_BUG_SQE_ELEM(8, __u64, off);
10145 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10146 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010147 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010148 BUILD_BUG_SQE_ELEM(24, __u32, len);
10149 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10150 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10151 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10152 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010153 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10154 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010155 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10156 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10157 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10158 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10159 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10160 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10161 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10162 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010163 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010164 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10165 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10166 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010167 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010168
Jens Axboed3656342019-12-18 09:50:26 -070010169 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010170 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010171 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10172 return 0;
10173};
10174__initcall(io_uring_init);