blob: a0b5f2c6d8eac47062eb27515d4dc51072b8f9f1 [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 Begunkov5af1d132020-07-18 11:32:52 +03002230
2231 struct task_struct *task;
2232 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002233};
2234
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002235static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002236{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002237 rb->to_free = 0;
2238 rb->task_refs = 0;
2239 rb->task = NULL;
2240}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002241
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002242static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2243 struct req_batch *rb)
2244{
2245 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2246 percpu_ref_put_many(&ctx->refs, rb->to_free);
2247 rb->to_free = 0;
2248}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002249
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002250static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2251 struct req_batch *rb)
2252{
2253 if (rb->to_free)
2254 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002255 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002256 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002257 rb->task = NULL;
2258 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002259}
2260
2261static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2262{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002263 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002264
Jens Axboee3bc8e92020-09-24 08:45:57 -06002265 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002266 if (rb->task)
2267 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002268 rb->task = req->task;
2269 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002270 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002271 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002272
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002273 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002274 rb->reqs[rb->to_free++] = req;
2275 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2276 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002277}
2278
Pavel Begunkov905c1722021-02-10 00:03:14 +00002279static void io_submit_flush_completions(struct io_comp_state *cs,
2280 struct io_ring_ctx *ctx)
2281{
2282 int i, nr = cs->nr;
2283 struct io_kiocb *req;
2284 struct req_batch rb;
2285
2286 io_init_req_batch(&rb);
2287 spin_lock_irq(&ctx->completion_lock);
2288 for (i = 0; i < nr; i++) {
2289 req = cs->reqs[i];
2290 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2291 }
2292 io_commit_cqring(ctx);
2293 spin_unlock_irq(&ctx->completion_lock);
2294
2295 io_cqring_ev_posted(ctx);
2296 for (i = 0; i < nr; i++) {
2297 req = cs->reqs[i];
2298
2299 /* submission and completion refs */
2300 if (refcount_sub_and_test(2, &req->refs))
2301 io_req_free_batch(&rb, req);
2302 }
2303
2304 io_req_free_batch_finish(ctx, &rb);
2305 cs->nr = 0;
2306}
2307
Jens Axboeba816ad2019-09-28 11:36:45 -06002308/*
2309 * Drop reference to request, return next in chain (if there is one) if this
2310 * was the last reference to this request.
2311 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002312static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002313{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002314 struct io_kiocb *nxt = NULL;
2315
Jens Axboe2a44f462020-02-25 13:25:41 -07002316 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002317 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002318 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002319 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002320 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321}
2322
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323static void io_put_req(struct io_kiocb *req)
2324{
Jens Axboedef596e2019-01-09 08:59:42 -07002325 if (refcount_dec_and_test(&req->refs))
2326 io_free_req(req);
2327}
2328
Pavel Begunkov216578e2020-10-13 09:44:00 +01002329static void io_put_req_deferred_cb(struct callback_head *cb)
2330{
2331 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2332
2333 io_free_req(req);
2334}
2335
2336static void io_free_req_deferred(struct io_kiocb *req)
2337{
2338 int ret;
2339
2340 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002341 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002342 if (unlikely(ret))
2343 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002344}
2345
2346static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2347{
2348 if (refcount_sub_and_test(refs, &req->refs))
2349 io_free_req_deferred(req);
2350}
2351
Jens Axboe978db572019-11-14 22:39:04 -07002352static void io_double_put_req(struct io_kiocb *req)
2353{
2354 /* drop both submit and complete references */
2355 if (refcount_sub_and_test(2, &req->refs))
2356 io_free_req(req);
2357}
2358
Pavel Begunkov6c503152021-01-04 20:36:36 +00002359static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002360{
2361 /* See comment at the top of this file */
2362 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002363 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002364}
2365
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002366static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2367{
2368 struct io_rings *rings = ctx->rings;
2369
2370 /* make sure SQ entry isn't read before tail */
2371 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2372}
2373
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002374static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002375{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002376 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002377
Jens Axboebcda7ba2020-02-23 16:42:51 -07002378 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2379 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002380 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002381 kfree(kbuf);
2382 return cflags;
2383}
2384
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002385static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2386{
2387 struct io_buffer *kbuf;
2388
2389 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2390 return io_put_kbuf(req, kbuf);
2391}
2392
Jens Axboe4c6e2772020-07-01 11:29:10 -06002393static inline bool io_run_task_work(void)
2394{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002395 /*
2396 * Not safe to run on exiting task, and the task_work handling will
2397 * not add work to such a task.
2398 */
2399 if (unlikely(current->flags & PF_EXITING))
2400 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002401 if (current->task_works) {
2402 __set_current_state(TASK_RUNNING);
2403 task_work_run();
2404 return true;
2405 }
2406
2407 return false;
2408}
2409
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002410static void io_iopoll_queue(struct list_head *again)
2411{
2412 struct io_kiocb *req;
2413
2414 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002415 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2416 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002417 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002418 } while (!list_empty(again));
2419}
2420
Jens Axboedef596e2019-01-09 08:59:42 -07002421/*
2422 * Find and free completed poll iocbs
2423 */
2424static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2425 struct list_head *done)
2426{
Jens Axboe8237e042019-12-28 10:48:22 -07002427 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002428 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002429 LIST_HEAD(again);
2430
2431 /* order with ->result store in io_complete_rw_iopoll() */
2432 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002433
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002434 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002435 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002436 int cflags = 0;
2437
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002438 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002439 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002440 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002441 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002442 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002443 continue;
2444 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002445 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002446
Jens Axboebcda7ba2020-02-23 16:42:51 -07002447 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002448 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002449
2450 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002451 (*nr_events)++;
2452
Pavel Begunkovc3524382020-06-28 12:52:32 +03002453 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002454 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002455 }
Jens Axboedef596e2019-01-09 08:59:42 -07002456
Jens Axboe09bb8392019-03-13 12:39:28 -06002457 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002458 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002459 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002460
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002461 if (!list_empty(&again))
2462 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002463}
2464
Jens Axboedef596e2019-01-09 08:59:42 -07002465static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2466 long min)
2467{
2468 struct io_kiocb *req, *tmp;
2469 LIST_HEAD(done);
2470 bool spin;
2471 int ret;
2472
2473 /*
2474 * Only spin for completions if we don't have multiple devices hanging
2475 * off our complete list, and we're under the requested amount.
2476 */
2477 spin = !ctx->poll_multi_file && *nr_events < min;
2478
2479 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002480 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002481 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002482
2483 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002484 * Move completed and retryable entries to our local lists.
2485 * If we find a request that requires polling, break out
2486 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002487 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002488 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002489 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002490 continue;
2491 }
2492 if (!list_empty(&done))
2493 break;
2494
2495 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2496 if (ret < 0)
2497 break;
2498
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002499 /* iopoll may have completed current req */
2500 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002501 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002502
Jens Axboedef596e2019-01-09 08:59:42 -07002503 if (ret && spin)
2504 spin = false;
2505 ret = 0;
2506 }
2507
2508 if (!list_empty(&done))
2509 io_iopoll_complete(ctx, nr_events, &done);
2510
2511 return ret;
2512}
2513
2514/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002515 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002516 * non-spinning poll check - we'll still enter the driver poll loop, but only
2517 * as a non-spinning completion check.
2518 */
2519static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2520 long min)
2521{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002522 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002523 int ret;
2524
2525 ret = io_do_iopoll(ctx, nr_events, min);
2526 if (ret < 0)
2527 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002528 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002529 return 0;
2530 }
2531
2532 return 1;
2533}
2534
2535/*
2536 * We can't just wait for polled events to come to us, we have to actively
2537 * find and complete them.
2538 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002539static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002540{
2541 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2542 return;
2543
2544 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002545 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002546 unsigned int nr_events = 0;
2547
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002548 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002549
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002550 /* let it sleep and repeat later if can't complete a request */
2551 if (nr_events == 0)
2552 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002553 /*
2554 * Ensure we allow local-to-the-cpu processing to take place,
2555 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002556 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002557 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002558 if (need_resched()) {
2559 mutex_unlock(&ctx->uring_lock);
2560 cond_resched();
2561 mutex_lock(&ctx->uring_lock);
2562 }
Jens Axboedef596e2019-01-09 08:59:42 -07002563 }
2564 mutex_unlock(&ctx->uring_lock);
2565}
2566
Pavel Begunkov7668b922020-07-07 16:36:21 +03002567static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002568{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002569 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002570 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002571
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002572 /*
2573 * We disallow the app entering submit/complete with polling, but we
2574 * still need to lock the ring to prevent racing with polled issue
2575 * that got punted to a workqueue.
2576 */
2577 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002578 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002579 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002580 * Don't enter poll loop if we already have events pending.
2581 * If we do, we can potentially be spinning for commands that
2582 * already triggered a CQE (eg in error).
2583 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002584 if (test_bit(0, &ctx->cq_check_overflow))
2585 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2586 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002587 break;
2588
2589 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002590 * If a submit got punted to a workqueue, we can have the
2591 * application entering polling for a command before it gets
2592 * issued. That app will hold the uring_lock for the duration
2593 * of the poll right here, so we need to take a breather every
2594 * now and then to ensure that the issue has a chance to add
2595 * the poll to the issued list. Otherwise we can spin here
2596 * forever, while the workqueue is stuck trying to acquire the
2597 * very same mutex.
2598 */
2599 if (!(++iters & 7)) {
2600 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002601 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002602 mutex_lock(&ctx->uring_lock);
2603 }
2604
Pavel Begunkov7668b922020-07-07 16:36:21 +03002605 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002606 if (ret <= 0)
2607 break;
2608 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002609 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002610
Jens Axboe500f9fb2019-08-19 12:15:59 -06002611 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002612 return ret;
2613}
2614
Jens Axboe491381ce2019-10-17 09:20:46 -06002615static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616{
Jens Axboe491381ce2019-10-17 09:20:46 -06002617 /*
2618 * Tell lockdep we inherited freeze protection from submission
2619 * thread.
2620 */
2621 if (req->flags & REQ_F_ISREG) {
2622 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623
Jens Axboe491381ce2019-10-17 09:20:46 -06002624 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002626 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627}
2628
Jens Axboea1d7c392020-06-22 11:09:46 -06002629static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002630 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631{
Jens Axboe9adbd452019-12-20 08:45:55 -07002632 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002633 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634
Jens Axboe491381ce2019-10-17 09:20:46 -06002635 if (kiocb->ki_flags & IOCB_WRITE)
2636 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002638 if (res != req->result)
2639 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002640 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002641 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002642 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002643}
2644
Jens Axboeb63534c2020-06-04 11:28:00 -06002645#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002646static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002647{
2648 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002649 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002650 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002651
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002652 /* already prepared */
2653 if (req->async_data)
2654 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002655
2656 switch (req->opcode) {
2657 case IORING_OP_READV:
2658 case IORING_OP_READ_FIXED:
2659 case IORING_OP_READ:
2660 rw = READ;
2661 break;
2662 case IORING_OP_WRITEV:
2663 case IORING_OP_WRITE_FIXED:
2664 case IORING_OP_WRITE:
2665 rw = WRITE;
2666 break;
2667 default:
2668 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2669 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002670 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002671 }
2672
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002673 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2674 if (ret < 0)
2675 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002676 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002677}
Jens Axboeb63534c2020-06-04 11:28:00 -06002678#endif
2679
2680static bool io_rw_reissue(struct io_kiocb *req, long res)
2681{
2682#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002683 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002684 int ret;
2685
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002686 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002687 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002688 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002689 if (!S_ISBLK(mode) && !S_ISREG(mode))
2690 return false;
2691 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002692 return false;
2693
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002694 lockdep_assert_held(&req->ctx->uring_lock);
2695
Jens Axboe28cea78a2020-09-14 10:51:17 -06002696 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002697
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002698 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002699 refcount_inc(&req->refs);
2700 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002701 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002702 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002703 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002704#endif
2705 return false;
2706}
2707
Jens Axboea1d7c392020-06-22 11:09:46 -06002708static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002709 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002710{
2711 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002712 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002713}
2714
2715static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2716{
Jens Axboe9adbd452019-12-20 08:45:55 -07002717 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002718
Pavel Begunkov889fca72021-02-10 00:03:09 +00002719 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002720}
2721
Jens Axboedef596e2019-01-09 08:59:42 -07002722static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2723{
Jens Axboe9adbd452019-12-20 08:45:55 -07002724 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002725
Jens Axboe491381ce2019-10-17 09:20:46 -06002726 if (kiocb->ki_flags & IOCB_WRITE)
2727 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002728
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002729 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002730 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002731
2732 WRITE_ONCE(req->result, res);
2733 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002734 smp_wmb();
2735 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002736}
2737
2738/*
2739 * After the iocb has been issued, it's safe to be found on the poll list.
2740 * Adding the kiocb to the list AFTER submission ensures that we don't
2741 * find it from a io_iopoll_getevents() thread before the issuer is done
2742 * accessing the kiocb cookie.
2743 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002744static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002745{
2746 struct io_ring_ctx *ctx = req->ctx;
2747
2748 /*
2749 * Track whether we have multiple files in our lists. This will impact
2750 * how we do polling eventually, not spinning if we're on potentially
2751 * different devices.
2752 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002753 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002754 ctx->poll_multi_file = false;
2755 } else if (!ctx->poll_multi_file) {
2756 struct io_kiocb *list_req;
2757
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002758 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002759 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002760 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002761 ctx->poll_multi_file = true;
2762 }
2763
2764 /*
2765 * For fast devices, IO may have already completed. If it has, add
2766 * it to the front so we find it first.
2767 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002768 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002769 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002770 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002771 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002772
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002773 /*
2774 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2775 * task context or in io worker task context. If current task context is
2776 * sq thread, we don't need to check whether should wake up sq thread.
2777 */
2778 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002779 wq_has_sleeper(&ctx->sq_data->wait))
2780 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002781}
2782
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002783static inline void io_state_file_put(struct io_submit_state *state)
2784{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002785 if (state->file_refs) {
2786 fput_many(state->file, state->file_refs);
2787 state->file_refs = 0;
2788 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002789}
2790
2791/*
2792 * Get as many references to a file as we have IOs left in this submission,
2793 * assuming most submissions are for one file, or at least that each file
2794 * has more than one submission.
2795 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002796static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002797{
2798 if (!state)
2799 return fget(fd);
2800
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002801 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002802 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002803 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002804 return state->file;
2805 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002806 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002807 }
2808 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002809 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002810 return NULL;
2811
2812 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002813 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002814 return state->file;
2815}
2816
Jens Axboe4503b762020-06-01 10:00:27 -06002817static bool io_bdev_nowait(struct block_device *bdev)
2818{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002819 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002820}
2821
Jens Axboe2b188cc2019-01-07 10:46:33 -07002822/*
2823 * If we tracked the file through the SCM inflight mechanism, we could support
2824 * any file. For now, just ensure that anything potentially problematic is done
2825 * inline.
2826 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002827static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002828{
2829 umode_t mode = file_inode(file)->i_mode;
2830
Jens Axboe4503b762020-06-01 10:00:27 -06002831 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002832 if (IS_ENABLED(CONFIG_BLOCK) &&
2833 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002834 return true;
2835 return false;
2836 }
2837 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002838 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002839 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002840 if (IS_ENABLED(CONFIG_BLOCK) &&
2841 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002842 file->f_op != &io_uring_fops)
2843 return true;
2844 return false;
2845 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002846
Jens Axboec5b85622020-06-09 19:23:05 -06002847 /* any ->read/write should understand O_NONBLOCK */
2848 if (file->f_flags & O_NONBLOCK)
2849 return true;
2850
Jens Axboeaf197f52020-04-28 13:15:06 -06002851 if (!(file->f_mode & FMODE_NOWAIT))
2852 return false;
2853
2854 if (rw == READ)
2855 return file->f_op->read_iter != NULL;
2856
2857 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002858}
2859
Pavel Begunkova88fc402020-09-30 22:57:53 +03002860static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002861{
Jens Axboedef596e2019-01-09 08:59:42 -07002862 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002863 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002864 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002865 unsigned ioprio;
2866 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002868 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002869 req->flags |= REQ_F_ISREG;
2870
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002872 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002873 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002874 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002875 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002876 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002877 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2878 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2879 if (unlikely(ret))
2880 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002882 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2883 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2884 req->flags |= REQ_F_NOWAIT;
2885
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886 ioprio = READ_ONCE(sqe->ioprio);
2887 if (ioprio) {
2888 ret = ioprio_check_cap(ioprio);
2889 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002890 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002891
2892 kiocb->ki_ioprio = ioprio;
2893 } else
2894 kiocb->ki_ioprio = get_current_ioprio();
2895
Jens Axboedef596e2019-01-09 08:59:42 -07002896 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002897 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2898 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002899 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900
Jens Axboedef596e2019-01-09 08:59:42 -07002901 kiocb->ki_flags |= IOCB_HIPRI;
2902 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002903 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002904 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002905 if (kiocb->ki_flags & IOCB_HIPRI)
2906 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002907 kiocb->ki_complete = io_complete_rw;
2908 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002909
Jens Axboe3529d8c2019-12-19 18:24:38 -07002910 req->rw.addr = READ_ONCE(sqe->addr);
2911 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002912 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002913 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002914}
2915
2916static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2917{
2918 switch (ret) {
2919 case -EIOCBQUEUED:
2920 break;
2921 case -ERESTARTSYS:
2922 case -ERESTARTNOINTR:
2923 case -ERESTARTNOHAND:
2924 case -ERESTART_RESTARTBLOCK:
2925 /*
2926 * We can't just restart the syscall, since previously
2927 * submitted sqes may already be in progress. Just fail this
2928 * IO with EINTR.
2929 */
2930 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002931 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002932 default:
2933 kiocb->ki_complete(kiocb, ret, 0);
2934 }
2935}
2936
Jens Axboea1d7c392020-06-22 11:09:46 -06002937static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002938 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002939{
Jens Axboeba042912019-12-25 16:33:42 -07002940 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002941 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002942
Jens Axboe227c0c92020-08-13 11:51:40 -06002943 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002944 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002945 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002946 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002947 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002948 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002949 }
2950
Jens Axboeba042912019-12-25 16:33:42 -07002951 if (req->flags & REQ_F_CUR_POS)
2952 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002953 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002954 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002955 else
2956 io_rw_done(kiocb, ret);
2957}
2958
Pavel Begunkov847595d2021-02-04 13:52:06 +00002959static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002960{
Jens Axboe9adbd452019-12-20 08:45:55 -07002961 struct io_ring_ctx *ctx = req->ctx;
2962 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002963 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002964 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002965 size_t offset;
2966 u64 buf_addr;
2967
Jens Axboeedafcce2019-01-09 09:16:05 -07002968 if (unlikely(buf_index >= ctx->nr_user_bufs))
2969 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002970 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2971 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002972 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002973
2974 /* overflow */
2975 if (buf_addr + len < buf_addr)
2976 return -EFAULT;
2977 /* not inside the mapped region */
2978 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2979 return -EFAULT;
2980
2981 /*
2982 * May not be a start of buffer, set size appropriately
2983 * and advance us to the beginning.
2984 */
2985 offset = buf_addr - imu->ubuf;
2986 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002987
2988 if (offset) {
2989 /*
2990 * Don't use iov_iter_advance() here, as it's really slow for
2991 * using the latter parts of a big fixed buffer - it iterates
2992 * over each segment manually. We can cheat a bit here, because
2993 * we know that:
2994 *
2995 * 1) it's a BVEC iter, we set it up
2996 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2997 * first and last bvec
2998 *
2999 * So just find our index, and adjust the iterator afterwards.
3000 * If the offset is within the first bvec (or the whole first
3001 * bvec, just use iov_iter_advance(). This makes it easier
3002 * since we can just skip the first segment, which may not
3003 * be PAGE_SIZE aligned.
3004 */
3005 const struct bio_vec *bvec = imu->bvec;
3006
3007 if (offset <= bvec->bv_len) {
3008 iov_iter_advance(iter, offset);
3009 } else {
3010 unsigned long seg_skip;
3011
3012 /* skip first vec */
3013 offset -= bvec->bv_len;
3014 seg_skip = 1 + (offset >> PAGE_SHIFT);
3015
3016 iter->bvec = bvec + seg_skip;
3017 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003018 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003019 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003020 }
3021 }
3022
Pavel Begunkov847595d2021-02-04 13:52:06 +00003023 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003024}
3025
Jens Axboebcda7ba2020-02-23 16:42:51 -07003026static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3027{
3028 if (needs_lock)
3029 mutex_unlock(&ctx->uring_lock);
3030}
3031
3032static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3033{
3034 /*
3035 * "Normal" inline submissions always hold the uring_lock, since we
3036 * grab it from the system call. Same is true for the SQPOLL offload.
3037 * The only exception is when we've detached the request and issue it
3038 * from an async worker thread, grab the lock for that case.
3039 */
3040 if (needs_lock)
3041 mutex_lock(&ctx->uring_lock);
3042}
3043
3044static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3045 int bgid, struct io_buffer *kbuf,
3046 bool needs_lock)
3047{
3048 struct io_buffer *head;
3049
3050 if (req->flags & REQ_F_BUFFER_SELECTED)
3051 return kbuf;
3052
3053 io_ring_submit_lock(req->ctx, needs_lock);
3054
3055 lockdep_assert_held(&req->ctx->uring_lock);
3056
3057 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3058 if (head) {
3059 if (!list_empty(&head->list)) {
3060 kbuf = list_last_entry(&head->list, struct io_buffer,
3061 list);
3062 list_del(&kbuf->list);
3063 } else {
3064 kbuf = head;
3065 idr_remove(&req->ctx->io_buffer_idr, bgid);
3066 }
3067 if (*len > kbuf->len)
3068 *len = kbuf->len;
3069 } else {
3070 kbuf = ERR_PTR(-ENOBUFS);
3071 }
3072
3073 io_ring_submit_unlock(req->ctx, needs_lock);
3074
3075 return kbuf;
3076}
3077
Jens Axboe4d954c22020-02-27 07:31:19 -07003078static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3079 bool needs_lock)
3080{
3081 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003082 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003083
3084 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003085 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003086 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3087 if (IS_ERR(kbuf))
3088 return kbuf;
3089 req->rw.addr = (u64) (unsigned long) kbuf;
3090 req->flags |= REQ_F_BUFFER_SELECTED;
3091 return u64_to_user_ptr(kbuf->addr);
3092}
3093
3094#ifdef CONFIG_COMPAT
3095static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3096 bool needs_lock)
3097{
3098 struct compat_iovec __user *uiov;
3099 compat_ssize_t clen;
3100 void __user *buf;
3101 ssize_t len;
3102
3103 uiov = u64_to_user_ptr(req->rw.addr);
3104 if (!access_ok(uiov, sizeof(*uiov)))
3105 return -EFAULT;
3106 if (__get_user(clen, &uiov->iov_len))
3107 return -EFAULT;
3108 if (clen < 0)
3109 return -EINVAL;
3110
3111 len = clen;
3112 buf = io_rw_buffer_select(req, &len, needs_lock);
3113 if (IS_ERR(buf))
3114 return PTR_ERR(buf);
3115 iov[0].iov_base = buf;
3116 iov[0].iov_len = (compat_size_t) len;
3117 return 0;
3118}
3119#endif
3120
3121static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3122 bool needs_lock)
3123{
3124 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3125 void __user *buf;
3126 ssize_t len;
3127
3128 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3129 return -EFAULT;
3130
3131 len = iov[0].iov_len;
3132 if (len < 0)
3133 return -EINVAL;
3134 buf = io_rw_buffer_select(req, &len, needs_lock);
3135 if (IS_ERR(buf))
3136 return PTR_ERR(buf);
3137 iov[0].iov_base = buf;
3138 iov[0].iov_len = len;
3139 return 0;
3140}
3141
3142static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3143 bool needs_lock)
3144{
Jens Axboedddb3e22020-06-04 11:27:01 -06003145 if (req->flags & REQ_F_BUFFER_SELECTED) {
3146 struct io_buffer *kbuf;
3147
3148 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3149 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3150 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003151 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003152 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003153 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003154 return -EINVAL;
3155
3156#ifdef CONFIG_COMPAT
3157 if (req->ctx->compat)
3158 return io_compat_import(req, iov, needs_lock);
3159#endif
3160
3161 return __io_iov_buffer_select(req, iov, needs_lock);
3162}
3163
Pavel Begunkov847595d2021-02-04 13:52:06 +00003164static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3165 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003166{
Jens Axboe9adbd452019-12-20 08:45:55 -07003167 void __user *buf = u64_to_user_ptr(req->rw.addr);
3168 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003169 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003170 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003171
Pavel Begunkov7d009162019-11-25 23:14:40 +03003172 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003173 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003174 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003175 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003176
Jens Axboebcda7ba2020-02-23 16:42:51 -07003177 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003178 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003179 return -EINVAL;
3180
Jens Axboe3a6820f2019-12-22 15:19:35 -07003181 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003182 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003183 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003184 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003185 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003186 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003187 }
3188
Jens Axboe3a6820f2019-12-22 15:19:35 -07003189 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3190 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003191 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003192 }
3193
Jens Axboe4d954c22020-02-27 07:31:19 -07003194 if (req->flags & REQ_F_BUFFER_SELECT) {
3195 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003196 if (!ret)
3197 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003198 *iovec = NULL;
3199 return ret;
3200 }
3201
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003202 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3203 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003204}
3205
Jens Axboe0fef9482020-08-26 10:36:20 -06003206static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3207{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003208 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003209}
3210
Jens Axboe32960612019-09-23 11:05:34 -06003211/*
3212 * For files that don't have ->read_iter() and ->write_iter(), handle them
3213 * by looping over ->read() or ->write() manually.
3214 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003215static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003216{
Jens Axboe4017eb92020-10-22 14:14:12 -06003217 struct kiocb *kiocb = &req->rw.kiocb;
3218 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003219 ssize_t ret = 0;
3220
3221 /*
3222 * Don't support polled IO through this interface, and we can't
3223 * support non-blocking either. For the latter, this just causes
3224 * the kiocb to be handled from an async context.
3225 */
3226 if (kiocb->ki_flags & IOCB_HIPRI)
3227 return -EOPNOTSUPP;
3228 if (kiocb->ki_flags & IOCB_NOWAIT)
3229 return -EAGAIN;
3230
3231 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003232 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003233 ssize_t nr;
3234
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003235 if (!iov_iter_is_bvec(iter)) {
3236 iovec = iov_iter_iovec(iter);
3237 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003238 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3239 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003240 }
3241
Jens Axboe32960612019-09-23 11:05:34 -06003242 if (rw == READ) {
3243 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003244 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003245 } else {
3246 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003247 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003248 }
3249
3250 if (nr < 0) {
3251 if (!ret)
3252 ret = nr;
3253 break;
3254 }
3255 ret += nr;
3256 if (nr != iovec.iov_len)
3257 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003258 req->rw.len -= nr;
3259 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003260 iov_iter_advance(iter, nr);
3261 }
3262
3263 return ret;
3264}
3265
Jens Axboeff6165b2020-08-13 09:47:43 -06003266static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3267 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003268{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003269 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003270
Jens Axboeff6165b2020-08-13 09:47:43 -06003271 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003272 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003273 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003274 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003275 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003276 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003277 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003278 unsigned iov_off = 0;
3279
3280 rw->iter.iov = rw->fast_iov;
3281 if (iter->iov != fast_iov) {
3282 iov_off = iter->iov - fast_iov;
3283 rw->iter.iov += iov_off;
3284 }
3285 if (rw->fast_iov != fast_iov)
3286 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003287 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003288 } else {
3289 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003290 }
3291}
3292
Jens Axboee8c2bc12020-08-15 18:44:09 -07003293static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003294{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003295 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3296 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3297 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003298}
3299
Jens Axboee8c2bc12020-08-15 18:44:09 -07003300static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003301{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003302 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003303 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003304
Jens Axboee8c2bc12020-08-15 18:44:09 -07003305 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003306}
3307
Jens Axboeff6165b2020-08-13 09:47:43 -06003308static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3309 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003310 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003311{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003312 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003313 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003314 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003315 if (__io_alloc_async_data(req)) {
3316 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003317 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003318 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003319
Jens Axboeff6165b2020-08-13 09:47:43 -06003320 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003321 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003322 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003323}
3324
Pavel Begunkov73debe62020-09-30 22:57:54 +03003325static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003326{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003327 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003328 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003329 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003330
Pavel Begunkov2846c482020-11-07 13:16:27 +00003331 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003332 if (unlikely(ret < 0))
3333 return ret;
3334
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003335 iorw->bytes_done = 0;
3336 iorw->free_iovec = iov;
3337 if (iov)
3338 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003339 return 0;
3340}
3341
Pavel Begunkov73debe62020-09-30 22:57:54 +03003342static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003343{
3344 ssize_t ret;
3345
Pavel Begunkova88fc402020-09-30 22:57:53 +03003346 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003347 if (ret)
3348 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003349
Jens Axboe3529d8c2019-12-19 18:24:38 -07003350 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3351 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003352
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003353 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003354 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003355 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003356 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003357}
3358
Jens Axboec1dd91d2020-08-03 16:43:59 -06003359/*
3360 * This is our waitqueue callback handler, registered through lock_page_async()
3361 * when we initially tried to do the IO with the iocb armed our waitqueue.
3362 * This gets called when the page is unlocked, and we generally expect that to
3363 * happen when the page IO is completed and the page is now uptodate. This will
3364 * queue a task_work based retry of the operation, attempting to copy the data
3365 * again. If the latter fails because the page was NOT uptodate, then we will
3366 * do a thread based blocking retry of the operation. That's the unexpected
3367 * slow path.
3368 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003369static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3370 int sync, void *arg)
3371{
3372 struct wait_page_queue *wpq;
3373 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003374 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003375 int ret;
3376
3377 wpq = container_of(wait, struct wait_page_queue, wait);
3378
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003379 if (!wake_page_match(wpq, key))
3380 return 0;
3381
Hao Xuc8d317a2020-09-29 20:00:45 +08003382 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003383 list_del_init(&wait->entry);
3384
Pavel Begunkove7375122020-07-12 20:42:04 +03003385 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003386 percpu_ref_get(&req->ctx->refs);
3387
Jens Axboebcf5a062020-05-22 09:24:42 -06003388 /* submit ref gets dropped, acquire a new one */
3389 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003390 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003391 if (unlikely(ret))
3392 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003393 return 1;
3394}
3395
Jens Axboec1dd91d2020-08-03 16:43:59 -06003396/*
3397 * This controls whether a given IO request should be armed for async page
3398 * based retry. If we return false here, the request is handed to the async
3399 * worker threads for retry. If we're doing buffered reads on a regular file,
3400 * we prepare a private wait_page_queue entry and retry the operation. This
3401 * will either succeed because the page is now uptodate and unlocked, or it
3402 * will register a callback when the page is unlocked at IO completion. Through
3403 * that callback, io_uring uses task_work to setup a retry of the operation.
3404 * That retry will attempt the buffered read again. The retry will generally
3405 * succeed, or in rare cases where it fails, we then fall back to using the
3406 * async worker threads for a blocking retry.
3407 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003408static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003409{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003410 struct io_async_rw *rw = req->async_data;
3411 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003412 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003413
3414 /* never retry for NOWAIT, we just complete with -EAGAIN */
3415 if (req->flags & REQ_F_NOWAIT)
3416 return false;
3417
Jens Axboe227c0c92020-08-13 11:51:40 -06003418 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003419 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003420 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003421
Jens Axboebcf5a062020-05-22 09:24:42 -06003422 /*
3423 * just use poll if we can, and don't attempt if the fs doesn't
3424 * support callback based unlocks
3425 */
3426 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3427 return false;
3428
Jens Axboe3b2a4432020-08-16 10:58:43 -07003429 wait->wait.func = io_async_buf_func;
3430 wait->wait.private = req;
3431 wait->wait.flags = 0;
3432 INIT_LIST_HEAD(&wait->wait.entry);
3433 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003434 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003435 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003436 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003437}
3438
3439static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3440{
3441 if (req->file->f_op->read_iter)
3442 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003443 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003444 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003445 else
3446 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003447}
3448
Pavel Begunkov889fca72021-02-10 00:03:09 +00003449static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450{
3451 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003452 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003453 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003454 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003455 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003456 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003457
Pavel Begunkov2846c482020-11-07 13:16:27 +00003458 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003459 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003460 iovec = NULL;
3461 } else {
3462 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3463 if (ret < 0)
3464 return ret;
3465 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003466 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003467 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003468
Jens Axboefd6c2e42019-12-18 12:19:41 -07003469 /* Ensure we clear previously set non-block flag */
3470 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003471 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003472 else
3473 kiocb->ki_flags |= IOCB_NOWAIT;
3474
Pavel Begunkov24c74672020-06-21 13:09:51 +03003475 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003476 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3477 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003478 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003479 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003480
Pavel Begunkov632546c2020-11-07 13:16:26 +00003481 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003482 if (unlikely(ret)) {
3483 kfree(iovec);
3484 return ret;
3485 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486
Jens Axboe227c0c92020-08-13 11:51:40 -06003487 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003488
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003489 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003490 /* it's faster to check here then delegate to kfree */
3491 if (iovec)
3492 kfree(iovec);
3493 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003494 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003495 /* IOPOLL retry should happen for io-wq threads */
3496 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003497 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003498 /* no retry on NONBLOCK nor RWF_NOWAIT */
3499 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003500 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003501 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003502 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003503 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003504 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003505 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003506 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003507 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003508 }
3509
Jens Axboe227c0c92020-08-13 11:51:40 -06003510 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003511 if (ret2)
3512 return ret2;
3513
Jens Axboee8c2bc12020-08-15 18:44:09 -07003514 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003515 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003516 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003517
Pavel Begunkovb23df912021-02-04 13:52:04 +00003518 do {
3519 io_size -= ret;
3520 rw->bytes_done += ret;
3521 /* if we can retry, do so with the callbacks armed */
3522 if (!io_rw_should_retry(req)) {
3523 kiocb->ki_flags &= ~IOCB_WAITQ;
3524 return -EAGAIN;
3525 }
3526
3527 /*
3528 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3529 * we get -EIOCBQUEUED, then we'll get a notification when the
3530 * desired page gets unlocked. We can also get a partial read
3531 * here, and if we do, then just retry at the new offset.
3532 */
3533 ret = io_iter_do_read(req, iter);
3534 if (ret == -EIOCBQUEUED)
3535 return 0;
3536 /* we got some bytes, but not all. retry. */
3537 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003538done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003539 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003540 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541}
3542
Pavel Begunkov73debe62020-09-30 22:57:54 +03003543static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003544{
3545 ssize_t ret;
3546
Pavel Begunkova88fc402020-09-30 22:57:53 +03003547 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003548 if (ret)
3549 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003550
Jens Axboe3529d8c2019-12-19 18:24:38 -07003551 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3552 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003553
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003554 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003555 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003556 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003557 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003558}
3559
Pavel Begunkov889fca72021-02-10 00:03:09 +00003560static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003561{
3562 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003563 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003564 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003565 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003566 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003567 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003568
Pavel Begunkov2846c482020-11-07 13:16:27 +00003569 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003570 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003571 iovec = NULL;
3572 } else {
3573 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3574 if (ret < 0)
3575 return ret;
3576 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003577 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003578 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003579
Jens Axboefd6c2e42019-12-18 12:19:41 -07003580 /* Ensure we clear previously set non-block flag */
3581 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003582 kiocb->ki_flags &= ~IOCB_NOWAIT;
3583 else
3584 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003585
Pavel Begunkov24c74672020-06-21 13:09:51 +03003586 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003587 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003588 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003589
Jens Axboe10d59342019-12-09 20:16:22 -07003590 /* file path doesn't support NOWAIT for non-direct_IO */
3591 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3592 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003593 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003594
Pavel Begunkov632546c2020-11-07 13:16:26 +00003595 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003596 if (unlikely(ret))
3597 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003598
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003599 /*
3600 * Open-code file_start_write here to grab freeze protection,
3601 * which will be released by another thread in
3602 * io_complete_rw(). Fool lockdep by telling it the lock got
3603 * released so that it doesn't complain about the held lock when
3604 * we return to userspace.
3605 */
3606 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003607 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003608 __sb_writers_release(file_inode(req->file)->i_sb,
3609 SB_FREEZE_WRITE);
3610 }
3611 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003612
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003613 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003614 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003615 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003616 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003617 else
3618 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003619
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003620 /*
3621 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3622 * retry them without IOCB_NOWAIT.
3623 */
3624 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3625 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003626 /* no retry on NONBLOCK nor RWF_NOWAIT */
3627 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003628 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003629 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003630 /* IOPOLL retry should happen for io-wq threads */
3631 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3632 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003633done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003634 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003635 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003636copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003637 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003638 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003639 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003640 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003641 }
Jens Axboe31b51512019-01-18 22:56:34 -07003642out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003643 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003644 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003645 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003646 return ret;
3647}
3648
Jens Axboe80a261f2020-09-28 14:23:58 -06003649static int io_renameat_prep(struct io_kiocb *req,
3650 const struct io_uring_sqe *sqe)
3651{
3652 struct io_rename *ren = &req->rename;
3653 const char __user *oldf, *newf;
3654
3655 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3656 return -EBADF;
3657
3658 ren->old_dfd = READ_ONCE(sqe->fd);
3659 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3660 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3661 ren->new_dfd = READ_ONCE(sqe->len);
3662 ren->flags = READ_ONCE(sqe->rename_flags);
3663
3664 ren->oldpath = getname(oldf);
3665 if (IS_ERR(ren->oldpath))
3666 return PTR_ERR(ren->oldpath);
3667
3668 ren->newpath = getname(newf);
3669 if (IS_ERR(ren->newpath)) {
3670 putname(ren->oldpath);
3671 return PTR_ERR(ren->newpath);
3672 }
3673
3674 req->flags |= REQ_F_NEED_CLEANUP;
3675 return 0;
3676}
3677
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003678static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003679{
3680 struct io_rename *ren = &req->rename;
3681 int ret;
3682
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003683 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003684 return -EAGAIN;
3685
3686 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3687 ren->newpath, ren->flags);
3688
3689 req->flags &= ~REQ_F_NEED_CLEANUP;
3690 if (ret < 0)
3691 req_set_fail_links(req);
3692 io_req_complete(req, ret);
3693 return 0;
3694}
3695
Jens Axboe14a11432020-09-28 14:27:37 -06003696static int io_unlinkat_prep(struct io_kiocb *req,
3697 const struct io_uring_sqe *sqe)
3698{
3699 struct io_unlink *un = &req->unlink;
3700 const char __user *fname;
3701
3702 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3703 return -EBADF;
3704
3705 un->dfd = READ_ONCE(sqe->fd);
3706
3707 un->flags = READ_ONCE(sqe->unlink_flags);
3708 if (un->flags & ~AT_REMOVEDIR)
3709 return -EINVAL;
3710
3711 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3712 un->filename = getname(fname);
3713 if (IS_ERR(un->filename))
3714 return PTR_ERR(un->filename);
3715
3716 req->flags |= REQ_F_NEED_CLEANUP;
3717 return 0;
3718}
3719
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003720static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003721{
3722 struct io_unlink *un = &req->unlink;
3723 int ret;
3724
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003725 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003726 return -EAGAIN;
3727
3728 if (un->flags & AT_REMOVEDIR)
3729 ret = do_rmdir(un->dfd, un->filename);
3730 else
3731 ret = do_unlinkat(un->dfd, un->filename);
3732
3733 req->flags &= ~REQ_F_NEED_CLEANUP;
3734 if (ret < 0)
3735 req_set_fail_links(req);
3736 io_req_complete(req, ret);
3737 return 0;
3738}
3739
Jens Axboe36f4fa62020-09-05 11:14:22 -06003740static int io_shutdown_prep(struct io_kiocb *req,
3741 const struct io_uring_sqe *sqe)
3742{
3743#if defined(CONFIG_NET)
3744 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3745 return -EINVAL;
3746 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3747 sqe->buf_index)
3748 return -EINVAL;
3749
3750 req->shutdown.how = READ_ONCE(sqe->len);
3751 return 0;
3752#else
3753 return -EOPNOTSUPP;
3754#endif
3755}
3756
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003757static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003758{
3759#if defined(CONFIG_NET)
3760 struct socket *sock;
3761 int ret;
3762
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003763 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003764 return -EAGAIN;
3765
Linus Torvalds48aba792020-12-16 12:44:05 -08003766 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003767 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003768 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003769
3770 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003771 if (ret < 0)
3772 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003773 io_req_complete(req, ret);
3774 return 0;
3775#else
3776 return -EOPNOTSUPP;
3777#endif
3778}
3779
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003780static int __io_splice_prep(struct io_kiocb *req,
3781 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003782{
3783 struct io_splice* sp = &req->splice;
3784 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003785
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003786 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3787 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003788
3789 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003790 sp->len = READ_ONCE(sqe->len);
3791 sp->flags = READ_ONCE(sqe->splice_flags);
3792
3793 if (unlikely(sp->flags & ~valid_flags))
3794 return -EINVAL;
3795
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003796 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3797 (sp->flags & SPLICE_F_FD_IN_FIXED));
3798 if (!sp->file_in)
3799 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003800 req->flags |= REQ_F_NEED_CLEANUP;
3801
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003802 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3803 /*
3804 * Splice operation will be punted aync, and here need to
3805 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3806 */
3807 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003808 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003809 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003810
3811 return 0;
3812}
3813
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003814static int io_tee_prep(struct io_kiocb *req,
3815 const struct io_uring_sqe *sqe)
3816{
3817 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3818 return -EINVAL;
3819 return __io_splice_prep(req, sqe);
3820}
3821
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003822static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003823{
3824 struct io_splice *sp = &req->splice;
3825 struct file *in = sp->file_in;
3826 struct file *out = sp->file_out;
3827 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3828 long ret = 0;
3829
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003830 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003831 return -EAGAIN;
3832 if (sp->len)
3833 ret = do_tee(in, out, sp->len, flags);
3834
3835 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3836 req->flags &= ~REQ_F_NEED_CLEANUP;
3837
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003838 if (ret != sp->len)
3839 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003840 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003841 return 0;
3842}
3843
3844static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3845{
3846 struct io_splice* sp = &req->splice;
3847
3848 sp->off_in = READ_ONCE(sqe->splice_off_in);
3849 sp->off_out = READ_ONCE(sqe->off);
3850 return __io_splice_prep(req, sqe);
3851}
3852
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003853static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003854{
3855 struct io_splice *sp = &req->splice;
3856 struct file *in = sp->file_in;
3857 struct file *out = sp->file_out;
3858 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3859 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003860 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003861
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003862 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003863 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003864
3865 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3866 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003867
Jens Axboe948a7742020-05-17 14:21:38 -06003868 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003869 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003870
3871 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3872 req->flags &= ~REQ_F_NEED_CLEANUP;
3873
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003874 if (ret != sp->len)
3875 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003876 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003877 return 0;
3878}
3879
Jens Axboe2b188cc2019-01-07 10:46:33 -07003880/*
3881 * IORING_OP_NOP just posts a completion event, nothing else.
3882 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003883static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884{
3885 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003886
Jens Axboedef596e2019-01-09 08:59:42 -07003887 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3888 return -EINVAL;
3889
Pavel Begunkov889fca72021-02-10 00:03:09 +00003890 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003891 return 0;
3892}
3893
Jens Axboe3529d8c2019-12-19 18:24:38 -07003894static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003895{
Jens Axboe6b063142019-01-10 22:13:58 -07003896 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003897
Jens Axboe09bb8392019-03-13 12:39:28 -06003898 if (!req->file)
3899 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003900
Jens Axboe6b063142019-01-10 22:13:58 -07003901 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003902 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003903 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003904 return -EINVAL;
3905
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003906 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3907 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3908 return -EINVAL;
3909
3910 req->sync.off = READ_ONCE(sqe->off);
3911 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003912 return 0;
3913}
3914
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003915static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003916{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003917 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003918 int ret;
3919
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003920 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003921 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003922 return -EAGAIN;
3923
Jens Axboe9adbd452019-12-20 08:45:55 -07003924 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003925 end > 0 ? end : LLONG_MAX,
3926 req->sync.flags & IORING_FSYNC_DATASYNC);
3927 if (ret < 0)
3928 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003929 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003930 return 0;
3931}
3932
Jens Axboed63d1b52019-12-10 10:38:56 -07003933static int io_fallocate_prep(struct io_kiocb *req,
3934 const struct io_uring_sqe *sqe)
3935{
3936 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3937 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003938 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3939 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003940
3941 req->sync.off = READ_ONCE(sqe->off);
3942 req->sync.len = READ_ONCE(sqe->addr);
3943 req->sync.mode = READ_ONCE(sqe->len);
3944 return 0;
3945}
3946
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003947static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003948{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003949 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003950
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003951 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003952 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003953 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003954 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3955 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003956 if (ret < 0)
3957 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003958 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003959 return 0;
3960}
3961
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003962static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003963{
Jens Axboef8748882020-01-08 17:47:02 -07003964 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003965 int ret;
3966
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003967 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003968 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003969 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003970 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003971
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003972 /* open.how should be already initialised */
3973 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003974 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003975
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003976 req->open.dfd = READ_ONCE(sqe->fd);
3977 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003978 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003979 if (IS_ERR(req->open.filename)) {
3980 ret = PTR_ERR(req->open.filename);
3981 req->open.filename = NULL;
3982 return ret;
3983 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003984 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003985 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003986 return 0;
3987}
3988
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003989static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3990{
3991 u64 flags, mode;
3992
Jens Axboe14587a462020-09-05 11:36:08 -06003993 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003994 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003995 mode = READ_ONCE(sqe->len);
3996 flags = READ_ONCE(sqe->open_flags);
3997 req->open.how = build_open_how(flags, mode);
3998 return __io_openat_prep(req, sqe);
3999}
4000
Jens Axboecebdb982020-01-08 17:59:24 -07004001static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4002{
4003 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004004 size_t len;
4005 int ret;
4006
Jens Axboe14587a462020-09-05 11:36:08 -06004007 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004008 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004009 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4010 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004011 if (len < OPEN_HOW_SIZE_VER0)
4012 return -EINVAL;
4013
4014 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4015 len);
4016 if (ret)
4017 return ret;
4018
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004019 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004020}
4021
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004022static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004023{
4024 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004025 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004026 bool nonblock_set;
4027 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004028 int ret;
4029
Jens Axboecebdb982020-01-08 17:59:24 -07004030 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004031 if (ret)
4032 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004033 nonblock_set = op.open_flag & O_NONBLOCK;
4034 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004035 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004036 /*
4037 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4038 * it'll always -EAGAIN
4039 */
4040 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4041 return -EAGAIN;
4042 op.lookup_flags |= LOOKUP_CACHED;
4043 op.open_flag |= O_NONBLOCK;
4044 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004045
Jens Axboe4022e7a2020-03-19 19:23:18 -06004046 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004047 if (ret < 0)
4048 goto err;
4049
4050 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004051 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004052 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4053 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004054 /*
4055 * We could hang on to this 'fd', but seems like marginal
4056 * gain for something that is now known to be a slower path.
4057 * So just put it, and we'll get a new one when we retry.
4058 */
4059 put_unused_fd(ret);
4060 return -EAGAIN;
4061 }
4062
Jens Axboe15b71ab2019-12-11 11:20:36 -07004063 if (IS_ERR(file)) {
4064 put_unused_fd(ret);
4065 ret = PTR_ERR(file);
4066 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004067 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004068 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004069 fsnotify_open(file);
4070 fd_install(ret, file);
4071 }
4072err:
4073 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004074 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004075 if (ret < 0)
4076 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004077 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004078 return 0;
4079}
4080
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004081static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004082{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004083 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004084}
4085
Jens Axboe067524e2020-03-02 16:32:28 -07004086static int io_remove_buffers_prep(struct io_kiocb *req,
4087 const struct io_uring_sqe *sqe)
4088{
4089 struct io_provide_buf *p = &req->pbuf;
4090 u64 tmp;
4091
4092 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4093 return -EINVAL;
4094
4095 tmp = READ_ONCE(sqe->fd);
4096 if (!tmp || tmp > USHRT_MAX)
4097 return -EINVAL;
4098
4099 memset(p, 0, sizeof(*p));
4100 p->nbufs = tmp;
4101 p->bgid = READ_ONCE(sqe->buf_group);
4102 return 0;
4103}
4104
4105static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4106 int bgid, unsigned nbufs)
4107{
4108 unsigned i = 0;
4109
4110 /* shouldn't happen */
4111 if (!nbufs)
4112 return 0;
4113
4114 /* the head kbuf is the list itself */
4115 while (!list_empty(&buf->list)) {
4116 struct io_buffer *nxt;
4117
4118 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4119 list_del(&nxt->list);
4120 kfree(nxt);
4121 if (++i == nbufs)
4122 return i;
4123 }
4124 i++;
4125 kfree(buf);
4126 idr_remove(&ctx->io_buffer_idr, bgid);
4127
4128 return i;
4129}
4130
Pavel Begunkov889fca72021-02-10 00:03:09 +00004131static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004132{
4133 struct io_provide_buf *p = &req->pbuf;
4134 struct io_ring_ctx *ctx = req->ctx;
4135 struct io_buffer *head;
4136 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004137 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004138
4139 io_ring_submit_lock(ctx, !force_nonblock);
4140
4141 lockdep_assert_held(&ctx->uring_lock);
4142
4143 ret = -ENOENT;
4144 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4145 if (head)
4146 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004147 if (ret < 0)
4148 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004149
4150 /* need to hold the lock to complete IOPOLL requests */
4151 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004152 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004153 io_ring_submit_unlock(ctx, !force_nonblock);
4154 } else {
4155 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004156 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004157 }
Jens Axboe067524e2020-03-02 16:32:28 -07004158 return 0;
4159}
4160
Jens Axboeddf0322d2020-02-23 16:41:33 -07004161static int io_provide_buffers_prep(struct io_kiocb *req,
4162 const struct io_uring_sqe *sqe)
4163{
4164 struct io_provide_buf *p = &req->pbuf;
4165 u64 tmp;
4166
4167 if (sqe->ioprio || sqe->rw_flags)
4168 return -EINVAL;
4169
4170 tmp = READ_ONCE(sqe->fd);
4171 if (!tmp || tmp > USHRT_MAX)
4172 return -E2BIG;
4173 p->nbufs = tmp;
4174 p->addr = READ_ONCE(sqe->addr);
4175 p->len = READ_ONCE(sqe->len);
4176
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004177 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004178 return -EFAULT;
4179
4180 p->bgid = READ_ONCE(sqe->buf_group);
4181 tmp = READ_ONCE(sqe->off);
4182 if (tmp > USHRT_MAX)
4183 return -E2BIG;
4184 p->bid = tmp;
4185 return 0;
4186}
4187
4188static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4189{
4190 struct io_buffer *buf;
4191 u64 addr = pbuf->addr;
4192 int i, bid = pbuf->bid;
4193
4194 for (i = 0; i < pbuf->nbufs; i++) {
4195 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4196 if (!buf)
4197 break;
4198
4199 buf->addr = addr;
4200 buf->len = pbuf->len;
4201 buf->bid = bid;
4202 addr += pbuf->len;
4203 bid++;
4204 if (!*head) {
4205 INIT_LIST_HEAD(&buf->list);
4206 *head = buf;
4207 } else {
4208 list_add_tail(&buf->list, &(*head)->list);
4209 }
4210 }
4211
4212 return i ? i : -ENOMEM;
4213}
4214
Pavel Begunkov889fca72021-02-10 00:03:09 +00004215static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004216{
4217 struct io_provide_buf *p = &req->pbuf;
4218 struct io_ring_ctx *ctx = req->ctx;
4219 struct io_buffer *head, *list;
4220 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004221 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004222
4223 io_ring_submit_lock(ctx, !force_nonblock);
4224
4225 lockdep_assert_held(&ctx->uring_lock);
4226
4227 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4228
4229 ret = io_add_buffers(p, &head);
4230 if (ret < 0)
4231 goto out;
4232
4233 if (!list) {
4234 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4235 GFP_KERNEL);
4236 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004237 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004238 goto out;
4239 }
4240 }
4241out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004242 if (ret < 0)
4243 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004244
4245 /* need to hold the lock to complete IOPOLL requests */
4246 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004247 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004248 io_ring_submit_unlock(ctx, !force_nonblock);
4249 } else {
4250 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004251 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004252 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004253 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004254}
4255
Jens Axboe3e4827b2020-01-08 15:18:09 -07004256static int io_epoll_ctl_prep(struct io_kiocb *req,
4257 const struct io_uring_sqe *sqe)
4258{
4259#if defined(CONFIG_EPOLL)
4260 if (sqe->ioprio || sqe->buf_index)
4261 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004262 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004263 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004264
4265 req->epoll.epfd = READ_ONCE(sqe->fd);
4266 req->epoll.op = READ_ONCE(sqe->len);
4267 req->epoll.fd = READ_ONCE(sqe->off);
4268
4269 if (ep_op_has_event(req->epoll.op)) {
4270 struct epoll_event __user *ev;
4271
4272 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4273 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4274 return -EFAULT;
4275 }
4276
4277 return 0;
4278#else
4279 return -EOPNOTSUPP;
4280#endif
4281}
4282
Pavel Begunkov889fca72021-02-10 00:03:09 +00004283static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004284{
4285#if defined(CONFIG_EPOLL)
4286 struct io_epoll *ie = &req->epoll;
4287 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004288 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004289
4290 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4291 if (force_nonblock && ret == -EAGAIN)
4292 return -EAGAIN;
4293
4294 if (ret < 0)
4295 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004296 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004297 return 0;
4298#else
4299 return -EOPNOTSUPP;
4300#endif
4301}
4302
Jens Axboec1ca7572019-12-25 22:18:28 -07004303static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4304{
4305#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4306 if (sqe->ioprio || sqe->buf_index || sqe->off)
4307 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004308 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4309 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004310
4311 req->madvise.addr = READ_ONCE(sqe->addr);
4312 req->madvise.len = READ_ONCE(sqe->len);
4313 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4314 return 0;
4315#else
4316 return -EOPNOTSUPP;
4317#endif
4318}
4319
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004320static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004321{
4322#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4323 struct io_madvise *ma = &req->madvise;
4324 int ret;
4325
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004326 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004327 return -EAGAIN;
4328
Minchan Kim0726b012020-10-17 16:14:50 -07004329 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004330 if (ret < 0)
4331 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004332 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004333 return 0;
4334#else
4335 return -EOPNOTSUPP;
4336#endif
4337}
4338
Jens Axboe4840e412019-12-25 22:03:45 -07004339static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4340{
4341 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4342 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004343 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4344 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004345
4346 req->fadvise.offset = READ_ONCE(sqe->off);
4347 req->fadvise.len = READ_ONCE(sqe->len);
4348 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4349 return 0;
4350}
4351
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004352static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004353{
4354 struct io_fadvise *fa = &req->fadvise;
4355 int ret;
4356
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004357 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004358 switch (fa->advice) {
4359 case POSIX_FADV_NORMAL:
4360 case POSIX_FADV_RANDOM:
4361 case POSIX_FADV_SEQUENTIAL:
4362 break;
4363 default:
4364 return -EAGAIN;
4365 }
4366 }
Jens Axboe4840e412019-12-25 22:03:45 -07004367
4368 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4369 if (ret < 0)
4370 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004371 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004372 return 0;
4373}
4374
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004375static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4376{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004377 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004378 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004379 if (sqe->ioprio || sqe->buf_index)
4380 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004381 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004382 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004383
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004384 req->statx.dfd = READ_ONCE(sqe->fd);
4385 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004386 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004387 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4388 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004389
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004390 return 0;
4391}
4392
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004393static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004394{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004395 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004396 int ret;
4397
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004398 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004399 /* only need file table for an actual valid fd */
4400 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4401 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004402 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004403 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004404
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004405 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4406 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004407
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004408 if (ret < 0)
4409 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004410 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004411 return 0;
4412}
4413
Jens Axboeb5dba592019-12-11 14:02:38 -07004414static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4415{
Jens Axboe14587a462020-09-05 11:36:08 -06004416 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004417 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004418 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4419 sqe->rw_flags || sqe->buf_index)
4420 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004421 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004422 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004423
4424 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004425 return 0;
4426}
4427
Pavel Begunkov889fca72021-02-10 00:03:09 +00004428static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004429{
Jens Axboe9eac1902021-01-19 15:50:37 -07004430 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004431 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004432 struct fdtable *fdt;
4433 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004434 int ret;
4435
Jens Axboe9eac1902021-01-19 15:50:37 -07004436 file = NULL;
4437 ret = -EBADF;
4438 spin_lock(&files->file_lock);
4439 fdt = files_fdtable(files);
4440 if (close->fd >= fdt->max_fds) {
4441 spin_unlock(&files->file_lock);
4442 goto err;
4443 }
4444 file = fdt->fd[close->fd];
4445 if (!file) {
4446 spin_unlock(&files->file_lock);
4447 goto err;
4448 }
4449
4450 if (file->f_op == &io_uring_fops) {
4451 spin_unlock(&files->file_lock);
4452 file = NULL;
4453 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004454 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004455
4456 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004457 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004458 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004459 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004460 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004461
Jens Axboe9eac1902021-01-19 15:50:37 -07004462 ret = __close_fd_get_file(close->fd, &file);
4463 spin_unlock(&files->file_lock);
4464 if (ret < 0) {
4465 if (ret == -ENOENT)
4466 ret = -EBADF;
4467 goto err;
4468 }
4469
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004470 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004471 ret = filp_close(file, current->files);
4472err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004473 if (ret < 0)
4474 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004475 if (file)
4476 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004477 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004478 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004479}
4480
Jens Axboe3529d8c2019-12-19 18:24:38 -07004481static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004482{
4483 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004484
4485 if (!req->file)
4486 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004487
4488 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4489 return -EINVAL;
4490 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4491 return -EINVAL;
4492
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004493 req->sync.off = READ_ONCE(sqe->off);
4494 req->sync.len = READ_ONCE(sqe->len);
4495 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004496 return 0;
4497}
4498
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004499static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004500{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004501 int ret;
4502
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004503 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004504 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004505 return -EAGAIN;
4506
Jens Axboe9adbd452019-12-20 08:45:55 -07004507 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004508 req->sync.flags);
4509 if (ret < 0)
4510 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004511 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004512 return 0;
4513}
4514
YueHaibing469956e2020-03-04 15:53:52 +08004515#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004516static int io_setup_async_msg(struct io_kiocb *req,
4517 struct io_async_msghdr *kmsg)
4518{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004519 struct io_async_msghdr *async_msg = req->async_data;
4520
4521 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004522 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004523 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004524 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004525 return -ENOMEM;
4526 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004527 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004528 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004529 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004530 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004531 /* if were using fast_iov, set it to the new one */
4532 if (!async_msg->free_iov)
4533 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4534
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004535 return -EAGAIN;
4536}
4537
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004538static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4539 struct io_async_msghdr *iomsg)
4540{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004541 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004542 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004543 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004544 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004545}
4546
Jens Axboe3529d8c2019-12-19 18:24:38 -07004547static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004548{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004549 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004550 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004551 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004552
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004553 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4554 return -EINVAL;
4555
Jens Axboee47293f2019-12-20 08:58:21 -07004556 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004557 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004558 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004559
Jens Axboed8768362020-02-27 14:17:49 -07004560#ifdef CONFIG_COMPAT
4561 if (req->ctx->compat)
4562 sr->msg_flags |= MSG_CMSG_COMPAT;
4563#endif
4564
Jens Axboee8c2bc12020-08-15 18:44:09 -07004565 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004566 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004567 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004568 if (!ret)
4569 req->flags |= REQ_F_NEED_CLEANUP;
4570 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004571}
4572
Pavel Begunkov889fca72021-02-10 00:03:09 +00004573static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004574{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004575 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004576 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004577 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004578 int ret;
4579
Florent Revestdba4a922020-12-04 12:36:04 +01004580 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004581 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004582 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004583
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004584 kmsg = req->async_data;
4585 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004586 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004587 if (ret)
4588 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004589 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004590 }
4591
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004592 flags = req->sr_msg.msg_flags;
4593 if (flags & MSG_DONTWAIT)
4594 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004595 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004596 flags |= MSG_DONTWAIT;
4597
4598 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004599 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004600 return io_setup_async_msg(req, kmsg);
4601 if (ret == -ERESTARTSYS)
4602 ret = -EINTR;
4603
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004604 /* fast path, check for non-NULL to avoid function call */
4605 if (kmsg->free_iov)
4606 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004607 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004608 if (ret < 0)
4609 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004610 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004611 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004612}
4613
Pavel Begunkov889fca72021-02-10 00:03:09 +00004614static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004615{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004616 struct io_sr_msg *sr = &req->sr_msg;
4617 struct msghdr msg;
4618 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004619 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004620 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004621 int ret;
4622
Florent Revestdba4a922020-12-04 12:36:04 +01004623 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004624 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004625 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004626
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004627 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4628 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004629 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004630
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 msg.msg_name = NULL;
4632 msg.msg_control = NULL;
4633 msg.msg_controllen = 0;
4634 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004635
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004636 flags = req->sr_msg.msg_flags;
4637 if (flags & MSG_DONTWAIT)
4638 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004639 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004640 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004641
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004642 msg.msg_flags = flags;
4643 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004644 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004645 return -EAGAIN;
4646 if (ret == -ERESTARTSYS)
4647 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004648
Jens Axboe03b12302019-12-02 18:50:25 -07004649 if (ret < 0)
4650 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004651 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004652 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004653}
4654
Pavel Begunkov1400e692020-07-12 20:41:05 +03004655static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4656 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004657{
4658 struct io_sr_msg *sr = &req->sr_msg;
4659 struct iovec __user *uiov;
4660 size_t iov_len;
4661 int ret;
4662
Pavel Begunkov1400e692020-07-12 20:41:05 +03004663 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4664 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004665 if (ret)
4666 return ret;
4667
4668 if (req->flags & REQ_F_BUFFER_SELECT) {
4669 if (iov_len > 1)
4670 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004671 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004672 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004673 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004674 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004675 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004676 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004677 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004678 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004679 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004680 if (ret > 0)
4681 ret = 0;
4682 }
4683
4684 return ret;
4685}
4686
4687#ifdef CONFIG_COMPAT
4688static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004689 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004690{
4691 struct compat_msghdr __user *msg_compat;
4692 struct io_sr_msg *sr = &req->sr_msg;
4693 struct compat_iovec __user *uiov;
4694 compat_uptr_t ptr;
4695 compat_size_t len;
4696 int ret;
4697
Pavel Begunkov270a5942020-07-12 20:41:04 +03004698 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004699 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004700 &ptr, &len);
4701 if (ret)
4702 return ret;
4703
4704 uiov = compat_ptr(ptr);
4705 if (req->flags & REQ_F_BUFFER_SELECT) {
4706 compat_ssize_t clen;
4707
4708 if (len > 1)
4709 return -EINVAL;
4710 if (!access_ok(uiov, sizeof(*uiov)))
4711 return -EFAULT;
4712 if (__get_user(clen, &uiov->iov_len))
4713 return -EFAULT;
4714 if (clen < 0)
4715 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004716 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004717 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004718 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004719 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004720 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004721 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004722 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004723 if (ret < 0)
4724 return ret;
4725 }
4726
4727 return 0;
4728}
Jens Axboe03b12302019-12-02 18:50:25 -07004729#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004730
Pavel Begunkov1400e692020-07-12 20:41:05 +03004731static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4732 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004733{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004734 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004735
4736#ifdef CONFIG_COMPAT
4737 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004738 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004739#endif
4740
Pavel Begunkov1400e692020-07-12 20:41:05 +03004741 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004742}
4743
Jens Axboebcda7ba2020-02-23 16:42:51 -07004744static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004745 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004746{
4747 struct io_sr_msg *sr = &req->sr_msg;
4748 struct io_buffer *kbuf;
4749
Jens Axboebcda7ba2020-02-23 16:42:51 -07004750 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4751 if (IS_ERR(kbuf))
4752 return kbuf;
4753
4754 sr->kbuf = kbuf;
4755 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004756 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004757}
4758
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004759static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4760{
4761 return io_put_kbuf(req, req->sr_msg.kbuf);
4762}
4763
Jens Axboe3529d8c2019-12-19 18:24:38 -07004764static int io_recvmsg_prep(struct io_kiocb *req,
4765 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004766{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004767 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004768 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004769 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004770
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004771 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4772 return -EINVAL;
4773
Jens Axboe3529d8c2019-12-19 18:24:38 -07004774 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004775 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004776 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004777 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004778
Jens Axboed8768362020-02-27 14:17:49 -07004779#ifdef CONFIG_COMPAT
4780 if (req->ctx->compat)
4781 sr->msg_flags |= MSG_CMSG_COMPAT;
4782#endif
4783
Jens Axboee8c2bc12020-08-15 18:44:09 -07004784 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004785 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004786 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004787 if (!ret)
4788 req->flags |= REQ_F_NEED_CLEANUP;
4789 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004790}
4791
Pavel Begunkov889fca72021-02-10 00:03:09 +00004792static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004793{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004794 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004795 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004796 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004797 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004798 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004799 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004800
Florent Revestdba4a922020-12-04 12:36:04 +01004801 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004802 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004803 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004804
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004805 kmsg = req->async_data;
4806 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004807 ret = io_recvmsg_copy_hdr(req, &iomsg);
4808 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004809 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004810 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004811 }
4812
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004813 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004814 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004815 if (IS_ERR(kbuf))
4816 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004817 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004818 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4819 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004820 1, req->sr_msg.len);
4821 }
4822
4823 flags = req->sr_msg.msg_flags;
4824 if (flags & MSG_DONTWAIT)
4825 req->flags |= REQ_F_NOWAIT;
4826 else if (force_nonblock)
4827 flags |= MSG_DONTWAIT;
4828
4829 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4830 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004831 if (force_nonblock && ret == -EAGAIN)
4832 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004833 if (ret == -ERESTARTSYS)
4834 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004835
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004836 if (req->flags & REQ_F_BUFFER_SELECTED)
4837 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004838 /* fast path, check for non-NULL to avoid function call */
4839 if (kmsg->free_iov)
4840 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004841 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004842 if (ret < 0)
4843 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004844 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004845 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004846}
4847
Pavel Begunkov889fca72021-02-10 00:03:09 +00004848static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004849{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004850 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004851 struct io_sr_msg *sr = &req->sr_msg;
4852 struct msghdr msg;
4853 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004854 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004855 struct iovec iov;
4856 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004857 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004858 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004859
Florent Revestdba4a922020-12-04 12:36:04 +01004860 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004861 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004862 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004863
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004864 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004865 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004866 if (IS_ERR(kbuf))
4867 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004868 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004869 }
4870
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004871 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004872 if (unlikely(ret))
4873 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004874
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004875 msg.msg_name = NULL;
4876 msg.msg_control = NULL;
4877 msg.msg_controllen = 0;
4878 msg.msg_namelen = 0;
4879 msg.msg_iocb = NULL;
4880 msg.msg_flags = 0;
4881
4882 flags = req->sr_msg.msg_flags;
4883 if (flags & MSG_DONTWAIT)
4884 req->flags |= REQ_F_NOWAIT;
4885 else if (force_nonblock)
4886 flags |= MSG_DONTWAIT;
4887
4888 ret = sock_recvmsg(sock, &msg, flags);
4889 if (force_nonblock && ret == -EAGAIN)
4890 return -EAGAIN;
4891 if (ret == -ERESTARTSYS)
4892 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004893out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004894 if (req->flags & REQ_F_BUFFER_SELECTED)
4895 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004896 if (ret < 0)
4897 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004898 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004899 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004900}
4901
Jens Axboe3529d8c2019-12-19 18:24:38 -07004902static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004903{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004904 struct io_accept *accept = &req->accept;
4905
Jens Axboe14587a462020-09-05 11:36:08 -06004906 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004907 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004908 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004909 return -EINVAL;
4910
Jens Axboed55e5f52019-12-11 16:12:15 -07004911 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4912 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004913 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004914 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004915 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004916}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004917
Pavel Begunkov889fca72021-02-10 00:03:09 +00004918static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004919{
4920 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004921 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004922 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004923 int ret;
4924
Jiufei Xuee697dee2020-06-10 13:41:59 +08004925 if (req->file->f_flags & O_NONBLOCK)
4926 req->flags |= REQ_F_NOWAIT;
4927
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004928 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004929 accept->addr_len, accept->flags,
4930 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004931 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004932 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004933 if (ret < 0) {
4934 if (ret == -ERESTARTSYS)
4935 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004936 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004937 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004938 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004939 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004940}
4941
Jens Axboe3529d8c2019-12-19 18:24:38 -07004942static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004943{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004944 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004945 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004946
Jens Axboe14587a462020-09-05 11:36:08 -06004947 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004948 return -EINVAL;
4949 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4950 return -EINVAL;
4951
Jens Axboe3529d8c2019-12-19 18:24:38 -07004952 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4953 conn->addr_len = READ_ONCE(sqe->addr2);
4954
4955 if (!io)
4956 return 0;
4957
4958 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004959 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004960}
4961
Pavel Begunkov889fca72021-02-10 00:03:09 +00004962static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004963{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004964 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004965 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004966 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004967 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004968
Jens Axboee8c2bc12020-08-15 18:44:09 -07004969 if (req->async_data) {
4970 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004971 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004972 ret = move_addr_to_kernel(req->connect.addr,
4973 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004974 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004975 if (ret)
4976 goto out;
4977 io = &__io;
4978 }
4979
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004980 file_flags = force_nonblock ? O_NONBLOCK : 0;
4981
Jens Axboee8c2bc12020-08-15 18:44:09 -07004982 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004983 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004984 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004985 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004986 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004987 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004988 ret = -ENOMEM;
4989 goto out;
4990 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004991 io = req->async_data;
4992 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004993 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004994 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004995 if (ret == -ERESTARTSYS)
4996 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004997out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004998 if (ret < 0)
4999 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005000 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005001 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005002}
YueHaibing469956e2020-03-04 15:53:52 +08005003#else /* !CONFIG_NET */
5004static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5005{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005006 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005007}
5008
Pavel Begunkov889fca72021-02-10 00:03:09 +00005009static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005010{
YueHaibing469956e2020-03-04 15:53:52 +08005011 return -EOPNOTSUPP;
5012}
5013
Pavel Begunkov889fca72021-02-10 00:03:09 +00005014static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005015{
5016 return -EOPNOTSUPP;
5017}
5018
5019static int io_recvmsg_prep(struct io_kiocb *req,
5020 const struct io_uring_sqe *sqe)
5021{
5022 return -EOPNOTSUPP;
5023}
5024
Pavel Begunkov889fca72021-02-10 00:03:09 +00005025static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005026{
5027 return -EOPNOTSUPP;
5028}
5029
Pavel Begunkov889fca72021-02-10 00:03:09 +00005030static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005031{
5032 return -EOPNOTSUPP;
5033}
5034
5035static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5036{
5037 return -EOPNOTSUPP;
5038}
5039
Pavel Begunkov889fca72021-02-10 00:03:09 +00005040static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005041{
5042 return -EOPNOTSUPP;
5043}
5044
5045static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5046{
5047 return -EOPNOTSUPP;
5048}
5049
Pavel Begunkov889fca72021-02-10 00:03:09 +00005050static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005051{
5052 return -EOPNOTSUPP;
5053}
5054#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005055
Jens Axboed7718a92020-02-14 22:23:12 -07005056struct io_poll_table {
5057 struct poll_table_struct pt;
5058 struct io_kiocb *req;
5059 int error;
5060};
5061
Jens Axboed7718a92020-02-14 22:23:12 -07005062static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5063 __poll_t mask, task_work_func_t func)
5064{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005065 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005066
5067 /* for instances that support it check for an event match first: */
5068 if (mask && !(mask & poll->events))
5069 return 0;
5070
5071 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5072
5073 list_del_init(&poll->wait.entry);
5074
Jens Axboed7718a92020-02-14 22:23:12 -07005075 req->result = mask;
5076 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005077 percpu_ref_get(&req->ctx->refs);
5078
Jens Axboed7718a92020-02-14 22:23:12 -07005079 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005080 * If this fails, then the task is exiting. When a task exits, the
5081 * work gets canceled, so just cancel this request as well instead
5082 * of executing it. We can't safely execute it anyway, as we may not
5083 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005084 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005085 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005086 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005087 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005088 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005089 }
Jens Axboed7718a92020-02-14 22:23:12 -07005090 return 1;
5091}
5092
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005093static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5094 __acquires(&req->ctx->completion_lock)
5095{
5096 struct io_ring_ctx *ctx = req->ctx;
5097
5098 if (!req->result && !READ_ONCE(poll->canceled)) {
5099 struct poll_table_struct pt = { ._key = poll->events };
5100
5101 req->result = vfs_poll(req->file, &pt) & poll->events;
5102 }
5103
5104 spin_lock_irq(&ctx->completion_lock);
5105 if (!req->result && !READ_ONCE(poll->canceled)) {
5106 add_wait_queue(poll->head, &poll->wait);
5107 return true;
5108 }
5109
5110 return false;
5111}
5112
Jens Axboed4e7cd32020-08-15 11:44:50 -07005113static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005114{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005115 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005116 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005117 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005118 return req->apoll->double_poll;
5119}
5120
5121static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5122{
5123 if (req->opcode == IORING_OP_POLL_ADD)
5124 return &req->poll;
5125 return &req->apoll->poll;
5126}
5127
5128static void io_poll_remove_double(struct io_kiocb *req)
5129{
5130 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005131
5132 lockdep_assert_held(&req->ctx->completion_lock);
5133
5134 if (poll && poll->head) {
5135 struct wait_queue_head *head = poll->head;
5136
5137 spin_lock(&head->lock);
5138 list_del_init(&poll->wait.entry);
5139 if (poll->wait.private)
5140 refcount_dec(&req->refs);
5141 poll->head = NULL;
5142 spin_unlock(&head->lock);
5143 }
5144}
5145
5146static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5147{
5148 struct io_ring_ctx *ctx = req->ctx;
5149
Jens Axboed4e7cd32020-08-15 11:44:50 -07005150 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005151 req->poll.done = true;
5152 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5153 io_commit_cqring(ctx);
5154}
5155
Jens Axboe18bceab2020-05-15 11:56:54 -06005156static void io_poll_task_func(struct callback_head *cb)
5157{
5158 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005159 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005160 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005161
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005162 if (io_poll_rewait(req, &req->poll)) {
5163 spin_unlock_irq(&ctx->completion_lock);
5164 } else {
5165 hash_del(&req->hash_node);
5166 io_poll_complete(req, req->result, 0);
5167 spin_unlock_irq(&ctx->completion_lock);
5168
5169 nxt = io_put_req_find_next(req);
5170 io_cqring_ev_posted(ctx);
5171 if (nxt)
5172 __io_req_task_submit(nxt);
5173 }
5174
Jens Axboe6d816e02020-08-11 08:04:14 -06005175 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005176}
5177
5178static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5179 int sync, void *key)
5180{
5181 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005182 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005183 __poll_t mask = key_to_poll(key);
5184
5185 /* for instances that support it check for an event match first: */
5186 if (mask && !(mask & poll->events))
5187 return 0;
5188
Jens Axboe8706e042020-09-28 08:38:54 -06005189 list_del_init(&wait->entry);
5190
Jens Axboe807abcb2020-07-17 17:09:27 -06005191 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005192 bool done;
5193
Jens Axboe807abcb2020-07-17 17:09:27 -06005194 spin_lock(&poll->head->lock);
5195 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005196 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005197 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005198 /* make sure double remove sees this as being gone */
5199 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005200 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005201 if (!done) {
5202 /* use wait func handler, so it matches the rq type */
5203 poll->wait.func(&poll->wait, mode, sync, key);
5204 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005205 }
5206 refcount_dec(&req->refs);
5207 return 1;
5208}
5209
5210static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5211 wait_queue_func_t wake_func)
5212{
5213 poll->head = NULL;
5214 poll->done = false;
5215 poll->canceled = false;
5216 poll->events = events;
5217 INIT_LIST_HEAD(&poll->wait.entry);
5218 init_waitqueue_func_entry(&poll->wait, wake_func);
5219}
5220
5221static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005222 struct wait_queue_head *head,
5223 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005224{
5225 struct io_kiocb *req = pt->req;
5226
5227 /*
5228 * If poll->head is already set, it's because the file being polled
5229 * uses multiple waitqueues for poll handling (eg one for read, one
5230 * for write). Setup a separate io_poll_iocb if this happens.
5231 */
5232 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005233 struct io_poll_iocb *poll_one = poll;
5234
Jens Axboe18bceab2020-05-15 11:56:54 -06005235 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005236 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005237 pt->error = -EINVAL;
5238 return;
5239 }
5240 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5241 if (!poll) {
5242 pt->error = -ENOMEM;
5243 return;
5244 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005245 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005246 refcount_inc(&req->refs);
5247 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005248 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005249 }
5250
5251 pt->error = 0;
5252 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005253
5254 if (poll->events & EPOLLEXCLUSIVE)
5255 add_wait_queue_exclusive(head, &poll->wait);
5256 else
5257 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005258}
5259
5260static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5261 struct poll_table_struct *p)
5262{
5263 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005264 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005265
Jens Axboe807abcb2020-07-17 17:09:27 -06005266 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005267}
5268
Jens Axboed7718a92020-02-14 22:23:12 -07005269static void io_async_task_func(struct callback_head *cb)
5270{
5271 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5272 struct async_poll *apoll = req->apoll;
5273 struct io_ring_ctx *ctx = req->ctx;
5274
5275 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5276
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005277 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005278 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005279 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005280 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005281 }
5282
Jens Axboe31067252020-05-17 17:43:31 -06005283 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005284 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005285 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005286
Jens Axboed4e7cd32020-08-15 11:44:50 -07005287 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005288 spin_unlock_irq(&ctx->completion_lock);
5289
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005290 if (!READ_ONCE(apoll->poll.canceled))
5291 __io_req_task_submit(req);
5292 else
5293 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005294
Jens Axboe6d816e02020-08-11 08:04:14 -06005295 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005296 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005297 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005298}
5299
5300static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5301 void *key)
5302{
5303 struct io_kiocb *req = wait->private;
5304 struct io_poll_iocb *poll = &req->apoll->poll;
5305
5306 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5307 key_to_poll(key));
5308
5309 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5310}
5311
5312static void io_poll_req_insert(struct io_kiocb *req)
5313{
5314 struct io_ring_ctx *ctx = req->ctx;
5315 struct hlist_head *list;
5316
5317 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5318 hlist_add_head(&req->hash_node, list);
5319}
5320
5321static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5322 struct io_poll_iocb *poll,
5323 struct io_poll_table *ipt, __poll_t mask,
5324 wait_queue_func_t wake_func)
5325 __acquires(&ctx->completion_lock)
5326{
5327 struct io_ring_ctx *ctx = req->ctx;
5328 bool cancel = false;
5329
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005330 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005331 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005332 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005333 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005334
5335 ipt->pt._key = mask;
5336 ipt->req = req;
5337 ipt->error = -EINVAL;
5338
Jens Axboed7718a92020-02-14 22:23:12 -07005339 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5340
5341 spin_lock_irq(&ctx->completion_lock);
5342 if (likely(poll->head)) {
5343 spin_lock(&poll->head->lock);
5344 if (unlikely(list_empty(&poll->wait.entry))) {
5345 if (ipt->error)
5346 cancel = true;
5347 ipt->error = 0;
5348 mask = 0;
5349 }
5350 if (mask || ipt->error)
5351 list_del_init(&poll->wait.entry);
5352 else if (cancel)
5353 WRITE_ONCE(poll->canceled, true);
5354 else if (!poll->done) /* actually waiting for an event */
5355 io_poll_req_insert(req);
5356 spin_unlock(&poll->head->lock);
5357 }
5358
5359 return mask;
5360}
5361
5362static bool io_arm_poll_handler(struct io_kiocb *req)
5363{
5364 const struct io_op_def *def = &io_op_defs[req->opcode];
5365 struct io_ring_ctx *ctx = req->ctx;
5366 struct async_poll *apoll;
5367 struct io_poll_table ipt;
5368 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005369 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005370
5371 if (!req->file || !file_can_poll(req->file))
5372 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005373 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005374 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005375 if (def->pollin)
5376 rw = READ;
5377 else if (def->pollout)
5378 rw = WRITE;
5379 else
5380 return false;
5381 /* if we can't nonblock try, then no point in arming a poll handler */
5382 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005383 return false;
5384
5385 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5386 if (unlikely(!apoll))
5387 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005388 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005389
5390 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005391 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005392
Nathan Chancellor8755d972020-03-02 16:01:19 -07005393 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005394 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005395 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005396 if (def->pollout)
5397 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005398
5399 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5400 if ((req->opcode == IORING_OP_RECVMSG) &&
5401 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5402 mask &= ~POLLIN;
5403
Jens Axboed7718a92020-02-14 22:23:12 -07005404 mask |= POLLERR | POLLPRI;
5405
5406 ipt.pt._qproc = io_async_queue_proc;
5407
5408 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5409 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005410 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005411 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005412 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005413 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005414 kfree(apoll);
5415 return false;
5416 }
5417 spin_unlock_irq(&ctx->completion_lock);
5418 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5419 apoll->poll.events);
5420 return true;
5421}
5422
5423static bool __io_poll_remove_one(struct io_kiocb *req,
5424 struct io_poll_iocb *poll)
5425{
Jens Axboeb41e9852020-02-17 09:52:41 -07005426 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005427
5428 spin_lock(&poll->head->lock);
5429 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005430 if (!list_empty(&poll->wait.entry)) {
5431 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005432 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005433 }
5434 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005435 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005436 return do_complete;
5437}
5438
5439static bool io_poll_remove_one(struct io_kiocb *req)
5440{
5441 bool do_complete;
5442
Jens Axboed4e7cd32020-08-15 11:44:50 -07005443 io_poll_remove_double(req);
5444
Jens Axboed7718a92020-02-14 22:23:12 -07005445 if (req->opcode == IORING_OP_POLL_ADD) {
5446 do_complete = __io_poll_remove_one(req, &req->poll);
5447 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005448 struct async_poll *apoll = req->apoll;
5449
Jens Axboed7718a92020-02-14 22:23:12 -07005450 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005451 do_complete = __io_poll_remove_one(req, &apoll->poll);
5452 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005453 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005454 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005455 kfree(apoll);
5456 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005457 }
5458
Jens Axboeb41e9852020-02-17 09:52:41 -07005459 if (do_complete) {
5460 io_cqring_fill_event(req, -ECANCELED);
5461 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005462 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005463 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005464 }
5465
5466 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005467}
5468
Jens Axboe76e1b642020-09-26 15:05:03 -06005469/*
5470 * Returns true if we found and killed one or more poll requests
5471 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005472static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5473 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005474{
Jens Axboe78076bb2019-12-04 19:56:40 -07005475 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005476 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005477 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005478
5479 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005480 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5481 struct hlist_head *list;
5482
5483 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005484 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005485 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005486 posted += io_poll_remove_one(req);
5487 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005488 }
5489 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005490
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005491 if (posted)
5492 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005493
5494 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005495}
5496
Jens Axboe47f46762019-11-09 17:43:02 -07005497static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5498{
Jens Axboe78076bb2019-12-04 19:56:40 -07005499 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005500 struct io_kiocb *req;
5501
Jens Axboe78076bb2019-12-04 19:56:40 -07005502 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5503 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005504 if (sqe_addr != req->user_data)
5505 continue;
5506 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005507 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005508 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005509 }
5510
5511 return -ENOENT;
5512}
5513
Jens Axboe3529d8c2019-12-19 18:24:38 -07005514static int io_poll_remove_prep(struct io_kiocb *req,
5515 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005516{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005517 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5518 return -EINVAL;
5519 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5520 sqe->poll_events)
5521 return -EINVAL;
5522
Pavel Begunkov018043b2020-10-27 23:17:18 +00005523 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005524 return 0;
5525}
5526
5527/*
5528 * Find a running poll command that matches one specified in sqe->addr,
5529 * and remove it if found.
5530 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005531static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005532{
5533 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005534 int ret;
5535
Jens Axboe221c5eb2019-01-17 09:41:58 -07005536 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005537 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005538 spin_unlock_irq(&ctx->completion_lock);
5539
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005540 if (ret < 0)
5541 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005542 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005543 return 0;
5544}
5545
Jens Axboe221c5eb2019-01-17 09:41:58 -07005546static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5547 void *key)
5548{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005549 struct io_kiocb *req = wait->private;
5550 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005551
Jens Axboed7718a92020-02-14 22:23:12 -07005552 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005553}
5554
Jens Axboe221c5eb2019-01-17 09:41:58 -07005555static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5556 struct poll_table_struct *p)
5557{
5558 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5559
Jens Axboee8c2bc12020-08-15 18:44:09 -07005560 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005561}
5562
Jens Axboe3529d8c2019-12-19 18:24:38 -07005563static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005564{
5565 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005566 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005567
5568 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5569 return -EINVAL;
5570 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5571 return -EINVAL;
5572
Jiufei Xue5769a352020-06-17 17:53:55 +08005573 events = READ_ONCE(sqe->poll32_events);
5574#ifdef __BIG_ENDIAN
5575 events = swahw32(events);
5576#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005577 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5578 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005579 return 0;
5580}
5581
Pavel Begunkov61e98202021-02-10 00:03:08 +00005582static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005583{
5584 struct io_poll_iocb *poll = &req->poll;
5585 struct io_ring_ctx *ctx = req->ctx;
5586 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005587 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005588
Jens Axboed7718a92020-02-14 22:23:12 -07005589 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005590
Jens Axboed7718a92020-02-14 22:23:12 -07005591 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5592 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005593
Jens Axboe8c838782019-03-12 15:48:16 -06005594 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005595 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005596 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005597 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005598 spin_unlock_irq(&ctx->completion_lock);
5599
Jens Axboe8c838782019-03-12 15:48:16 -06005600 if (mask) {
5601 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005602 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005603 }
Jens Axboe8c838782019-03-12 15:48:16 -06005604 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005605}
5606
Jens Axboe5262f562019-09-17 12:26:57 -06005607static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5608{
Jens Axboead8a48a2019-11-15 08:49:11 -07005609 struct io_timeout_data *data = container_of(timer,
5610 struct io_timeout_data, timer);
5611 struct io_kiocb *req = data->req;
5612 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005613 unsigned long flags;
5614
Jens Axboe5262f562019-09-17 12:26:57 -06005615 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005616 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005617 atomic_set(&req->ctx->cq_timeouts,
5618 atomic_read(&req->ctx->cq_timeouts) + 1);
5619
Jens Axboe78e19bb2019-11-06 15:21:34 -07005620 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005621 io_commit_cqring(ctx);
5622 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5623
5624 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005625 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005626 io_put_req(req);
5627 return HRTIMER_NORESTART;
5628}
5629
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005630static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5631 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005632{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005633 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005634 struct io_kiocb *req;
5635 int ret = -ENOENT;
5636
5637 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5638 if (user_data == req->user_data) {
5639 ret = 0;
5640 break;
5641 }
5642 }
5643
5644 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005645 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005646
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005647 io = req->async_data;
5648 ret = hrtimer_try_to_cancel(&io->timer);
5649 if (ret == -1)
5650 return ERR_PTR(-EALREADY);
5651 list_del_init(&req->timeout.list);
5652 return req;
5653}
5654
5655static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5656{
5657 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5658
5659 if (IS_ERR(req))
5660 return PTR_ERR(req);
5661
5662 req_set_fail_links(req);
5663 io_cqring_fill_event(req, -ECANCELED);
5664 io_put_req_deferred(req, 1);
5665 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005666}
5667
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005668static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5669 struct timespec64 *ts, enum hrtimer_mode mode)
5670{
5671 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5672 struct io_timeout_data *data;
5673
5674 if (IS_ERR(req))
5675 return PTR_ERR(req);
5676
5677 req->timeout.off = 0; /* noseq */
5678 data = req->async_data;
5679 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5680 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5681 data->timer.function = io_timeout_fn;
5682 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5683 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005684}
5685
Jens Axboe3529d8c2019-12-19 18:24:38 -07005686static int io_timeout_remove_prep(struct io_kiocb *req,
5687 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005688{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005689 struct io_timeout_rem *tr = &req->timeout_rem;
5690
Jens Axboeb29472e2019-12-17 18:50:29 -07005691 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5692 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005693 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5694 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005695 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005696 return -EINVAL;
5697
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005698 tr->addr = READ_ONCE(sqe->addr);
5699 tr->flags = READ_ONCE(sqe->timeout_flags);
5700 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5701 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5702 return -EINVAL;
5703 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5704 return -EFAULT;
5705 } else if (tr->flags) {
5706 /* timeout removal doesn't support flags */
5707 return -EINVAL;
5708 }
5709
Jens Axboeb29472e2019-12-17 18:50:29 -07005710 return 0;
5711}
5712
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005713static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5714{
5715 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5716 : HRTIMER_MODE_REL;
5717}
5718
Jens Axboe11365042019-10-16 09:08:32 -06005719/*
5720 * Remove or update an existing timeout command
5721 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005722static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005723{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005724 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005725 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005726 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005727
Jens Axboe11365042019-10-16 09:08:32 -06005728 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005729 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005730 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005731 else
5732 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5733 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005734
Jens Axboe47f46762019-11-09 17:43:02 -07005735 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005736 io_commit_cqring(ctx);
5737 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005738 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005739 if (ret < 0)
5740 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005741 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005742 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005743}
5744
Jens Axboe3529d8c2019-12-19 18:24:38 -07005745static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005746 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005747{
Jens Axboead8a48a2019-11-15 08:49:11 -07005748 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005749 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005750 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005751
Jens Axboead8a48a2019-11-15 08:49:11 -07005752 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005753 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005754 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005755 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005756 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005757 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005758 flags = READ_ONCE(sqe->timeout_flags);
5759 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005760 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005761
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005762 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005763
Jens Axboee8c2bc12020-08-15 18:44:09 -07005764 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005765 return -ENOMEM;
5766
Jens Axboee8c2bc12020-08-15 18:44:09 -07005767 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005768 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005769
5770 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005771 return -EFAULT;
5772
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005773 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005774 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5775 return 0;
5776}
5777
Pavel Begunkov61e98202021-02-10 00:03:08 +00005778static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005779{
Jens Axboead8a48a2019-11-15 08:49:11 -07005780 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005781 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005782 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005783 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005784
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005785 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005786
Jens Axboe5262f562019-09-17 12:26:57 -06005787 /*
5788 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005789 * timeout event to be satisfied. If it isn't set, then this is
5790 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005791 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005792 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005793 entry = ctx->timeout_list.prev;
5794 goto add;
5795 }
Jens Axboe5262f562019-09-17 12:26:57 -06005796
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005797 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5798 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005799
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005800 /* Update the last seq here in case io_flush_timeouts() hasn't.
5801 * This is safe because ->completion_lock is held, and submissions
5802 * and completions are never mixed in the same ->completion_lock section.
5803 */
5804 ctx->cq_last_tm_flush = tail;
5805
Jens Axboe5262f562019-09-17 12:26:57 -06005806 /*
5807 * Insertion sort, ensuring the first entry in the list is always
5808 * the one we need first.
5809 */
Jens Axboe5262f562019-09-17 12:26:57 -06005810 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005811 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5812 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005813
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005814 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005815 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005816 /* nxt.seq is behind @tail, otherwise would've been completed */
5817 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005818 break;
5819 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005820add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005821 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005822 data->timer.function = io_timeout_fn;
5823 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005824 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005825 return 0;
5826}
5827
Jens Axboe62755e32019-10-28 21:49:21 -06005828static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005829{
Jens Axboe62755e32019-10-28 21:49:21 -06005830 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005831
Jens Axboe62755e32019-10-28 21:49:21 -06005832 return req->user_data == (unsigned long) data;
5833}
5834
Jens Axboee977d6d2019-11-05 12:39:45 -07005835static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005836{
Jens Axboe62755e32019-10-28 21:49:21 -06005837 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005838 int ret = 0;
5839
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005840 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005841 switch (cancel_ret) {
5842 case IO_WQ_CANCEL_OK:
5843 ret = 0;
5844 break;
5845 case IO_WQ_CANCEL_RUNNING:
5846 ret = -EALREADY;
5847 break;
5848 case IO_WQ_CANCEL_NOTFOUND:
5849 ret = -ENOENT;
5850 break;
5851 }
5852
Jens Axboee977d6d2019-11-05 12:39:45 -07005853 return ret;
5854}
5855
Jens Axboe47f46762019-11-09 17:43:02 -07005856static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5857 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005858 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005859{
5860 unsigned long flags;
5861 int ret;
5862
5863 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5864 if (ret != -ENOENT) {
5865 spin_lock_irqsave(&ctx->completion_lock, flags);
5866 goto done;
5867 }
5868
5869 spin_lock_irqsave(&ctx->completion_lock, flags);
5870 ret = io_timeout_cancel(ctx, sqe_addr);
5871 if (ret != -ENOENT)
5872 goto done;
5873 ret = io_poll_cancel(ctx, sqe_addr);
5874done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005875 if (!ret)
5876 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005877 io_cqring_fill_event(req, ret);
5878 io_commit_cqring(ctx);
5879 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5880 io_cqring_ev_posted(ctx);
5881
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005882 if (ret < 0)
5883 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005884 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005885}
5886
Jens Axboe3529d8c2019-12-19 18:24:38 -07005887static int io_async_cancel_prep(struct io_kiocb *req,
5888 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005889{
Jens Axboefbf23842019-12-17 18:45:56 -07005890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005891 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005892 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5893 return -EINVAL;
5894 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005895 return -EINVAL;
5896
Jens Axboefbf23842019-12-17 18:45:56 -07005897 req->cancel.addr = READ_ONCE(sqe->addr);
5898 return 0;
5899}
5900
Pavel Begunkov61e98202021-02-10 00:03:08 +00005901static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005902{
5903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005904
Pavel Begunkov014db002020-03-03 21:33:12 +03005905 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005906 return 0;
5907}
5908
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005909static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005910 const struct io_uring_sqe *sqe)
5911{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005912 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5913 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005914 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5915 return -EINVAL;
5916 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005917 return -EINVAL;
5918
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005919 req->rsrc_update.offset = READ_ONCE(sqe->off);
5920 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5921 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005922 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005923 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005924 return 0;
5925}
5926
Pavel Begunkov889fca72021-02-10 00:03:09 +00005927static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005928{
5929 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005930 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005931 int ret;
5932
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005933 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005934 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005935
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005936 up.offset = req->rsrc_update.offset;
5937 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005938
5939 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005940 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005941 mutex_unlock(&ctx->uring_lock);
5942
5943 if (ret < 0)
5944 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005945 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005946 return 0;
5947}
5948
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005949static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005950{
Jens Axboed625c6e2019-12-17 19:53:05 -07005951 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005952 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005953 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005954 case IORING_OP_READV:
5955 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005956 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005957 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005958 case IORING_OP_WRITEV:
5959 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005960 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005961 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005962 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005963 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005964 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005965 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005966 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005967 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005968 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005969 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005970 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005971 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005972 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005973 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005974 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005975 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005976 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005977 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005978 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005979 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005980 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005981 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005982 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005983 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005984 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005985 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005986 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005987 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005988 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005989 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005990 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005991 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005992 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005993 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005994 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005995 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005996 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005997 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005998 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005999 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006000 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006001 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006002 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006003 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006004 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006005 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006006 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006007 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006008 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006009 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006010 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006011 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006012 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006013 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006014 case IORING_OP_SHUTDOWN:
6015 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006016 case IORING_OP_RENAMEAT:
6017 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006018 case IORING_OP_UNLINKAT:
6019 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006020 }
6021
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006022 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6023 req->opcode);
6024 return-EINVAL;
6025}
6026
Jens Axboedef596e2019-01-09 08:59:42 -07006027static int io_req_defer_prep(struct io_kiocb *req,
6028 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006029{
Jens Axboedef596e2019-01-09 08:59:42 -07006030 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006032 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006033 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006035}
6036
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006037static u32 io_get_sequence(struct io_kiocb *req)
6038{
6039 struct io_kiocb *pos;
6040 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006041 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006042
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006043 io_for_each_link(pos, req)
6044 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006045
6046 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6047 return total_submitted - nr_reqs;
6048}
6049
Jens Axboe3529d8c2019-12-19 18:24:38 -07006050static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006051{
6052 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006053 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006054 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006055 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006056
6057 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006058 if (likely(list_empty_careful(&ctx->defer_list) &&
6059 !(req->flags & REQ_F_IO_DRAIN)))
6060 return 0;
6061
6062 seq = io_get_sequence(req);
6063 /* Still a chance to pass the sequence check */
6064 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065 return 0;
6066
Jens Axboee8c2bc12020-08-15 18:44:09 -07006067 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006068 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006069 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006070 return ret;
6071 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006072 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006073 de = kmalloc(sizeof(*de), GFP_KERNEL);
6074 if (!de)
6075 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006076
6077 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006078 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006079 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006080 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006081 io_queue_async_work(req);
6082 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006083 }
6084
6085 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006086 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006087 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006088 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006089 spin_unlock_irq(&ctx->completion_lock);
6090 return -EIOCBQUEUED;
6091}
Jens Axboeedafcce2019-01-09 09:16:05 -07006092
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006093static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006094{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006095 if (req->flags & REQ_F_BUFFER_SELECTED) {
6096 switch (req->opcode) {
6097 case IORING_OP_READV:
6098 case IORING_OP_READ_FIXED:
6099 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006100 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006101 break;
6102 case IORING_OP_RECVMSG:
6103 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006104 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006105 break;
6106 }
6107 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006108 }
6109
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006110 if (req->flags & REQ_F_NEED_CLEANUP) {
6111 switch (req->opcode) {
6112 case IORING_OP_READV:
6113 case IORING_OP_READ_FIXED:
6114 case IORING_OP_READ:
6115 case IORING_OP_WRITEV:
6116 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006117 case IORING_OP_WRITE: {
6118 struct io_async_rw *io = req->async_data;
6119 if (io->free_iovec)
6120 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006121 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006122 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006123 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006124 case IORING_OP_SENDMSG: {
6125 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006126
6127 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006128 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006129 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006130 case IORING_OP_SPLICE:
6131 case IORING_OP_TEE:
6132 io_put_file(req, req->splice.file_in,
6133 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6134 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006135 case IORING_OP_OPENAT:
6136 case IORING_OP_OPENAT2:
6137 if (req->open.filename)
6138 putname(req->open.filename);
6139 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006140 case IORING_OP_RENAMEAT:
6141 putname(req->rename.oldpath);
6142 putname(req->rename.newpath);
6143 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006144 case IORING_OP_UNLINKAT:
6145 putname(req->unlink.filename);
6146 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006147 }
6148 req->flags &= ~REQ_F_NEED_CLEANUP;
6149 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006150}
6151
Pavel Begunkov889fca72021-02-10 00:03:09 +00006152static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006153{
Jens Axboeedafcce2019-01-09 09:16:05 -07006154 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006155 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006156
Jens Axboed625c6e2019-12-17 19:53:05 -07006157 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006158 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006159 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006160 break;
6161 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006162 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006163 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006164 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006165 break;
6166 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006167 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006168 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006169 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170 break;
6171 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006172 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173 break;
6174 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006175 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006176 break;
6177 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006178 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006179 break;
6180 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006181 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006182 break;
6183 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006184 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006185 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006186 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006187 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006188 break;
6189 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006190 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006191 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006192 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006193 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194 break;
6195 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006196 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 break;
6198 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006199 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006200 break;
6201 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006202 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203 break;
6204 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006205 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206 break;
6207 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006208 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006210 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006211 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006212 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006213 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006214 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006215 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006216 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006217 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006218 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006219 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006220 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006221 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006222 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006223 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006224 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006225 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006226 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006227 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006228 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006229 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006230 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006231 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006232 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006233 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006234 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006235 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006236 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006237 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006238 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006239 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006240 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006241 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006242 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006243 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006244 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006246 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006247 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006248 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006249 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006250 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006251 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006252 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006253 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006254 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006255 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006256 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006257 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006258 default:
6259 ret = -EINVAL;
6260 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006261 }
6262
6263 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006264 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265
Jens Axboeb5325762020-05-19 21:20:27 -06006266 /* If the op doesn't have a file, we're not polling for it */
6267 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006268 const bool in_async = io_wq_current_is_worker();
6269
Jens Axboe11ba8202020-01-15 21:51:17 -07006270 /* workqueue context doesn't hold uring_lock, grab it now */
6271 if (in_async)
6272 mutex_lock(&ctx->uring_lock);
6273
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006274 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006275
6276 if (in_async)
6277 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006278 }
6279
6280 return 0;
6281}
6282
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006283static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006284{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006285 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006286 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006287 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006288
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006289 timeout = io_prep_linked_timeout(req);
6290 if (timeout)
6291 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006292
Jens Axboe4014d942021-01-19 15:53:54 -07006293 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006294 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006295
Jens Axboe561fb042019-10-24 07:25:42 -06006296 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006297 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006298 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006299 /*
6300 * We can get EAGAIN for polled IO even though we're
6301 * forcing a sync submission from here, since we can't
6302 * wait for request slots on the block side.
6303 */
6304 if (ret != -EAGAIN)
6305 break;
6306 cond_resched();
6307 } while (1);
6308 }
Jens Axboe31b51512019-01-18 22:56:34 -07006309
Jens Axboe561fb042019-10-24 07:25:42 -06006310 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006311 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006312
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006313 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6314 lock_ctx = req->ctx;
6315
6316 /*
6317 * io_iopoll_complete() does not hold completion_lock to
6318 * complete polled io, so here for polled io, we can not call
6319 * io_req_complete() directly, otherwise there maybe concurrent
6320 * access to cqring, defer_list, etc, which is not safe. Given
6321 * that io_iopoll_complete() is always called under uring_lock,
6322 * so here for polled io, we also get uring_lock to complete
6323 * it.
6324 */
6325 if (lock_ctx)
6326 mutex_lock(&lock_ctx->uring_lock);
6327
6328 req_set_fail_links(req);
6329 io_req_complete(req, ret);
6330
6331 if (lock_ctx)
6332 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006333 }
Jens Axboe31b51512019-01-18 22:56:34 -07006334}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006335
Jens Axboe65e19f52019-10-26 07:20:21 -06006336static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6337 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006338{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006339 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006340
Jens Axboe05f3fb32019-12-09 11:22:50 -07006341 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006342 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006343}
6344
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006345static struct file *io_file_get(struct io_submit_state *state,
6346 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006347{
6348 struct io_ring_ctx *ctx = req->ctx;
6349 struct file *file;
6350
6351 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006352 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006353 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006354 fd = array_index_nospec(fd, ctx->nr_user_files);
6355 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006356 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006357 } else {
6358 trace_io_uring_file_get(ctx, fd);
6359 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006360 }
6361
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006362 if (file && unlikely(file->f_op == &io_uring_fops))
6363 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006364 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006365}
6366
Jens Axboe2665abf2019-11-05 12:40:47 -07006367static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6368{
Jens Axboead8a48a2019-11-15 08:49:11 -07006369 struct io_timeout_data *data = container_of(timer,
6370 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006371 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006372 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006373 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006374
6375 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006376 prev = req->timeout.head;
6377 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006378
6379 /*
6380 * We don't expect the list to be empty, that will only happen if we
6381 * race with the completion of the linked work.
6382 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006383 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006384 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006385 else
6386 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006387 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6388
6389 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006390 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006391 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006392 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006393 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006394 io_req_complete_post(req, -ETIME, 0);
6395 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006396 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006397 return HRTIMER_NORESTART;
6398}
6399
Jens Axboe7271ef32020-08-10 09:55:22 -06006400static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006401{
Jens Axboe76a46e02019-11-10 23:34:16 -07006402 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006403 * If the back reference is NULL, then our linked request finished
6404 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006405 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006406 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006407 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006408
Jens Axboead8a48a2019-11-15 08:49:11 -07006409 data->timer.function = io_link_timeout_fn;
6410 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6411 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006412 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006413}
6414
6415static void io_queue_linked_timeout(struct io_kiocb *req)
6416{
6417 struct io_ring_ctx *ctx = req->ctx;
6418
6419 spin_lock_irq(&ctx->completion_lock);
6420 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006421 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006422
Jens Axboe2665abf2019-11-05 12:40:47 -07006423 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006424 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006425}
6426
Jens Axboead8a48a2019-11-15 08:49:11 -07006427static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006428{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006429 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006430
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006431 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6432 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006433 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006434
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006435 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006436 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006437 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006438 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006439}
6440
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006441static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006443 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006444 const struct cred *old_creds = NULL;
Pavel Begunkov889fca72021-02-10 00:03:09 +00006445 int ret, issue_flags = IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006446
Pavel Begunkov889fca72021-02-10 00:03:09 +00006447 if (cs)
6448 issue_flags |= IO_URING_F_COMPLETE_DEFER;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006449again:
6450 linked_timeout = io_prep_linked_timeout(req);
6451
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006452 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6453 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006454 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006455 if (old_creds)
6456 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006457 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006458 old_creds = NULL; /* restored original creds */
6459 else
Jens Axboe98447d62020-10-14 10:48:51 -06006460 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006461 }
6462
Pavel Begunkov889fca72021-02-10 00:03:09 +00006463 ret = io_issue_sqe(req, issue_flags);
Jens Axboe491381ce2019-10-17 09:20:46 -06006464
6465 /*
6466 * We async punt it if the file wasn't marked NOWAIT, or if the file
6467 * doesn't support non-blocking read/write attempts
6468 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006469 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006470 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006471 /*
6472 * Queued up for async execution, worker will release
6473 * submit reference when the iocb is actually submitted.
6474 */
6475 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006477
Pavel Begunkovf063c542020-07-25 14:41:59 +03006478 if (linked_timeout)
6479 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006480 } else if (likely(!ret)) {
6481 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006482 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006483 cs->reqs[cs->nr++] = req;
6484 if (cs->nr == IO_COMPL_BATCH)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006485 io_submit_flush_completions(cs, req->ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006486 req = NULL;
6487 } else {
6488 req = io_put_req_find_next(req);
6489 }
6490
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006491 if (linked_timeout)
6492 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006493
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006494 if (req) {
6495 if (!(req->flags & REQ_F_FORCE_ASYNC))
6496 goto again;
6497 io_queue_async_work(req);
6498 }
6499 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006500 /* un-prep timeout, so it'll be killed as any other linked */
6501 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006502 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006503 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006504 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006505 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006506
Jens Axboe193155c2020-02-22 23:22:19 -07006507 if (old_creds)
6508 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006509}
6510
Jens Axboef13fad72020-06-22 09:34:30 -06006511static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6512 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006513{
6514 int ret;
6515
Jens Axboe3529d8c2019-12-19 18:24:38 -07006516 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006517 if (ret) {
6518 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006519fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006520 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006521 io_put_req(req);
6522 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006523 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006524 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006525 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006526 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006527 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006528 goto fail_req;
6529 }
Jens Axboece35a472019-12-17 08:04:44 -07006530 io_queue_async_work(req);
6531 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006532 if (sqe) {
6533 ret = io_req_prep(req, sqe);
6534 if (unlikely(ret))
6535 goto fail_req;
6536 }
6537 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006538 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006539}
6540
Jens Axboef13fad72020-06-22 09:34:30 -06006541static inline void io_queue_link_head(struct io_kiocb *req,
6542 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006543{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006544 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006545 io_put_req(req);
6546 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006547 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006548 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006549}
6550
Pavel Begunkov863e0562020-10-27 23:25:35 +00006551struct io_submit_link {
6552 struct io_kiocb *head;
6553 struct io_kiocb *last;
6554};
6555
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006556static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006557 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006558{
Jackie Liua197f662019-11-08 08:09:12 -07006559 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006560 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006561
Jens Axboe9e645e112019-05-10 16:07:28 -06006562 /*
6563 * If we already have a head request, queue this one for async
6564 * submittal once the head completes. If we don't have a head but
6565 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6566 * submitted sync once the chain is complete. If none of those
6567 * conditions are true (normal request), then just queue it.
6568 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006569 if (link->head) {
6570 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006571
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006572 /*
6573 * Taking sequential execution of a link, draining both sides
6574 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6575 * requests in the link. So, it drains the head and the
6576 * next after the link request. The last one is done via
6577 * drain_next flag to persist the effect across calls.
6578 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006579 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006580 head->flags |= REQ_F_IO_DRAIN;
6581 ctx->drain_next = 1;
6582 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006583 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006584 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006585 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006586 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006587 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006588 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006589 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006590 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006591 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006592
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006593 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006594 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006595 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006596 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006597 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006598 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006599 if (unlikely(ctx->drain_next)) {
6600 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006601 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006602 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006603 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006604 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006605 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006606 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006607 link->head = req;
6608 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006609 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006610 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006611 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006612 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006613
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006614 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006615}
6616
Jens Axboe9a56a232019-01-09 09:06:50 -07006617/*
6618 * Batched submission is done, ensure local IO is flushed out.
6619 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006620static void io_submit_state_end(struct io_submit_state *state,
6621 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006622{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006623 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006624 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006625 if (state->plug_started)
6626 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006627 io_state_file_put(state);
Pavel Begunkov50872752021-02-10 00:03:12 +00006628 if (state->free_reqs) {
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006629 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Pavel Begunkov50872752021-02-10 00:03:12 +00006630 state->free_reqs = 0;
6631 }
Jens Axboe9a56a232019-01-09 09:06:50 -07006632}
6633
6634/*
6635 * Start submission side cache.
6636 */
6637static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006638 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006639{
Jens Axboe27926b62020-10-28 09:33:23 -06006640 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006641 state->ios_left = max_ios;
6642}
6643
Jens Axboe2b188cc2019-01-07 10:46:33 -07006644static void io_commit_sqring(struct io_ring_ctx *ctx)
6645{
Hristo Venev75b28af2019-08-26 17:23:46 +00006646 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006648 /*
6649 * Ensure any loads from the SQEs are done at this point,
6650 * since once we write the new head, the application could
6651 * write new data to them.
6652 */
6653 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006654}
6655
6656/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006657 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006658 * that is mapped by userspace. This means that care needs to be taken to
6659 * ensure that reads are stable, as we cannot rely on userspace always
6660 * being a good citizen. If members of the sqe are validated and then later
6661 * used, it's important that those reads are done through READ_ONCE() to
6662 * prevent a re-load down the line.
6663 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006664static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006665{
Hristo Venev75b28af2019-08-26 17:23:46 +00006666 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006667 unsigned head;
6668
6669 /*
6670 * The cached sq head (or cq tail) serves two purposes:
6671 *
6672 * 1) allows us to batch the cost of updating the user visible
6673 * head updates.
6674 * 2) allows the kernel side to track the head on its own, even
6675 * though the application is the one updating it.
6676 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006677 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006678 if (likely(head < ctx->sq_entries))
6679 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006680
6681 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006682 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006683 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006684 return NULL;
6685}
6686
6687static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6688{
6689 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006690}
6691
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006692/*
6693 * Check SQE restrictions (opcode and flags).
6694 *
6695 * Returns 'true' if SQE is allowed, 'false' otherwise.
6696 */
6697static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6698 struct io_kiocb *req,
6699 unsigned int sqe_flags)
6700{
6701 if (!ctx->restricted)
6702 return true;
6703
6704 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6705 return false;
6706
6707 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6708 ctx->restrictions.sqe_flags_required)
6709 return false;
6710
6711 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6712 ctx->restrictions.sqe_flags_required))
6713 return false;
6714
6715 return true;
6716}
6717
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006718#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6719 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6720 IOSQE_BUFFER_SELECT)
6721
6722static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006723 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006724{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006725 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006726 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006727 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006728
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006729 req->opcode = READ_ONCE(sqe->opcode);
6730 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006731 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006732 req->file = NULL;
6733 req->ctx = ctx;
6734 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006735 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006736 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006737 /* one is dropped after submission, the other at completion */
6738 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006739 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006740 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006741
6742 if (unlikely(req->opcode >= IORING_OP_LAST))
6743 return -EINVAL;
6744
Jens Axboe28cea78a2020-09-14 10:51:17 -06006745 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006746 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006747
6748 sqe_flags = READ_ONCE(sqe->flags);
6749 /* enforce forwards compatibility on users */
6750 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6751 return -EINVAL;
6752
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006753 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6754 return -EACCES;
6755
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006756 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6757 !io_op_defs[req->opcode].buffer_select)
6758 return -EOPNOTSUPP;
6759
6760 id = READ_ONCE(sqe->personality);
6761 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006762 struct io_identity *iod;
6763
Jens Axboe1e6fa522020-10-15 08:46:24 -06006764 iod = idr_find(&ctx->personality_idr, id);
6765 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006766 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006767 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006768
6769 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006770 get_cred(iod->creds);
6771 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006772 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006773 }
6774
6775 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006776 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006777 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006778
Jens Axboe27926b62020-10-28 09:33:23 -06006779 /*
6780 * Plug now if we have more than 1 IO left after this, and the target
6781 * is potentially a read/write to block based storage.
6782 */
6783 if (!state->plug_started && state->ios_left > 1 &&
6784 io_op_defs[req->opcode].plug) {
6785 blk_start_plug(&state->plug);
6786 state->plug_started = true;
6787 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006788
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006789 ret = 0;
6790 if (io_op_defs[req->opcode].needs_file) {
6791 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006792
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006793 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006794 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006795 ret = -EBADF;
6796 }
6797
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006798 state->ios_left--;
6799 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006800}
6801
Jens Axboe0f212202020-09-13 13:09:39 -06006802static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006803{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006804 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006805 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006806
Jens Axboec4a2ed72019-11-21 21:01:26 -07006807 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006808 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006809 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006810 return -EBUSY;
6811 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006812
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006813 /* make sure SQ entry isn't read before tail */
6814 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006815
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006816 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6817 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006818
Jens Axboed8a6df12020-10-15 16:24:45 -06006819 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006820 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006821
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006822 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006823 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006824
Jens Axboe6c271ce2019-01-10 11:22:30 -07006825 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006826 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006827 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006828 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006829
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006830 sqe = io_get_sqe(ctx);
6831 if (unlikely(!sqe)) {
6832 io_consume_sqe(ctx);
6833 break;
6834 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006835 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006836 if (unlikely(!req)) {
6837 if (!submitted)
6838 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006839 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006840 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006841 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006842 /* will complete beyond this point, count as submitted */
6843 submitted++;
6844
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006845 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006846 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006847fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006848 io_put_req(req);
6849 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006850 break;
6851 }
6852
Jens Axboe354420f2020-01-08 18:55:15 -07006853 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006854 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006855 err = io_submit_sqe(req, sqe, &link, &ctx->submit_state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006856 if (err)
6857 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006858 }
6859
Pavel Begunkov9466f432020-01-25 22:34:01 +03006860 if (unlikely(submitted != nr)) {
6861 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006862 struct io_uring_task *tctx = current->io_uring;
6863 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006864
Jens Axboed8a6df12020-10-15 16:24:45 -06006865 percpu_ref_put_many(&ctx->refs, unused);
6866 percpu_counter_sub(&tctx->inflight, unused);
6867 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006868 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006869 if (link.head)
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006870 io_queue_link_head(link.head, &ctx->submit_state.comp);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006871 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006872
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006873 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6874 io_commit_sqring(ctx);
6875
Jens Axboe6c271ce2019-01-10 11:22:30 -07006876 return submitted;
6877}
6878
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006879static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6880{
6881 /* Tell userspace we may need a wakeup call */
6882 spin_lock_irq(&ctx->completion_lock);
6883 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6884 spin_unlock_irq(&ctx->completion_lock);
6885}
6886
6887static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6888{
6889 spin_lock_irq(&ctx->completion_lock);
6890 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6891 spin_unlock_irq(&ctx->completion_lock);
6892}
6893
Xiaoguang Wang08369242020-11-03 14:15:59 +08006894static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006895{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006896 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006897 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006898
Jens Axboec8d1ba52020-09-14 11:07:26 -06006899 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006900 /* if we're handling multiple rings, cap submit size for fairness */
6901 if (cap_entries && to_submit > 8)
6902 to_submit = 8;
6903
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006904 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6905 unsigned nr_events = 0;
6906
Xiaoguang Wang08369242020-11-03 14:15:59 +08006907 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006908 if (!list_empty(&ctx->iopoll_list))
6909 io_do_iopoll(ctx, &nr_events, 0);
6910
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006911 if (to_submit && !ctx->sqo_dead &&
6912 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006913 ret = io_submit_sqes(ctx, to_submit);
6914 mutex_unlock(&ctx->uring_lock);
6915 }
Jens Axboe90554202020-09-03 12:12:41 -06006916
6917 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6918 wake_up(&ctx->sqo_sq_wait);
6919
Xiaoguang Wang08369242020-11-03 14:15:59 +08006920 return ret;
6921}
6922
6923static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6924{
6925 struct io_ring_ctx *ctx;
6926 unsigned sq_thread_idle = 0;
6927
6928 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6929 if (sq_thread_idle < ctx->sq_thread_idle)
6930 sq_thread_idle = ctx->sq_thread_idle;
6931 }
6932
6933 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006934}
6935
Jens Axboe69fb2132020-09-14 11:16:23 -06006936static void io_sqd_init_new(struct io_sq_data *sqd)
6937{
6938 struct io_ring_ctx *ctx;
6939
6940 while (!list_empty(&sqd->ctx_new_list)) {
6941 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006942 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6943 complete(&ctx->sq_thread_comp);
6944 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006945
6946 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006947}
6948
Jens Axboe6c271ce2019-01-10 11:22:30 -07006949static int io_sq_thread(void *data)
6950{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006951 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006952 struct files_struct *old_files = current->files;
6953 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006954 const struct cred *old_cred = NULL;
6955 struct io_sq_data *sqd = data;
6956 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006957 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006958 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006959
Jens Axboe28cea78a2020-09-14 10:51:17 -06006960 task_lock(current);
6961 current->files = NULL;
6962 current->nsproxy = NULL;
6963 task_unlock(current);
6964
Jens Axboe69fb2132020-09-14 11:16:23 -06006965 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006966 int ret;
6967 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006968
6969 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006970 * Any changes to the sqd lists are synchronized through the
6971 * kthread parking. This synchronizes the thread vs users,
6972 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006973 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006974 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006975 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006976 /*
6977 * When sq thread is unparked, in case the previous park operation
6978 * comes from io_put_sq_data(), which means that sq thread is going
6979 * to be stopped, so here needs to have a check.
6980 */
6981 if (kthread_should_stop())
6982 break;
6983 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006984
Xiaoguang Wang08369242020-11-03 14:15:59 +08006985 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006986 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006987 timeout = jiffies + sqd->sq_thread_idle;
6988 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006989
Xiaoguang Wang08369242020-11-03 14:15:59 +08006990 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006991 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006992 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6993 if (current->cred != ctx->creds) {
6994 if (old_cred)
6995 revert_creds(old_cred);
6996 old_cred = override_creds(ctx->creds);
6997 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006998 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006999#ifdef CONFIG_AUDIT
7000 current->loginuid = ctx->loginuid;
7001 current->sessionid = ctx->sessionid;
7002#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007003
Xiaoguang Wang08369242020-11-03 14:15:59 +08007004 ret = __io_sq_thread(ctx, cap_entries);
7005 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7006 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007007
Jens Axboe28cea78a2020-09-14 10:51:17 -06007008 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007009 }
7010
Xiaoguang Wang08369242020-11-03 14:15:59 +08007011 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007012 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007013 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007014 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007015 if (sqt_spin)
7016 timeout = jiffies + sqd->sq_thread_idle;
7017 continue;
7018 }
7019
Xiaoguang Wang08369242020-11-03 14:15:59 +08007020 needs_sched = true;
7021 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7022 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7023 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7024 !list_empty_careful(&ctx->iopoll_list)) {
7025 needs_sched = false;
7026 break;
7027 }
7028 if (io_sqring_entries(ctx)) {
7029 needs_sched = false;
7030 break;
7031 }
7032 }
7033
Hao Xu8b28fdf2021-01-31 22:39:04 +08007034 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007035 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7036 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007037
Jens Axboe69fb2132020-09-14 11:16:23 -06007038 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007039 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7040 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007041 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007042
7043 finish_wait(&sqd->wait, &wait);
7044 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007045 }
7046
Jens Axboe4c6e2772020-07-01 11:29:10 -06007047 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007048 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007049
Dennis Zhou91d8f512020-09-16 13:41:05 -07007050 if (cur_css)
7051 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007052 if (old_cred)
7053 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007054
Jens Axboe28cea78a2020-09-14 10:51:17 -06007055 task_lock(current);
7056 current->files = old_files;
7057 current->nsproxy = old_nsproxy;
7058 task_unlock(current);
7059
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007060 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007061
Jens Axboe6c271ce2019-01-10 11:22:30 -07007062 return 0;
7063}
7064
Jens Axboebda52162019-09-24 13:47:15 -06007065struct io_wait_queue {
7066 struct wait_queue_entry wq;
7067 struct io_ring_ctx *ctx;
7068 unsigned to_wait;
7069 unsigned nr_timeouts;
7070};
7071
Pavel Begunkov6c503152021-01-04 20:36:36 +00007072static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007073{
7074 struct io_ring_ctx *ctx = iowq->ctx;
7075
7076 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007077 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007078 * started waiting. For timeouts, we always want to return to userspace,
7079 * regardless of event count.
7080 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007081 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007082 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7083}
7084
7085static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7086 int wake_flags, void *key)
7087{
7088 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7089 wq);
7090
Pavel Begunkov6c503152021-01-04 20:36:36 +00007091 /*
7092 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7093 * the task, and the next invocation will do it.
7094 */
7095 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7096 return autoremove_wake_function(curr, mode, wake_flags, key);
7097 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007098}
7099
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007100static int io_run_task_work_sig(void)
7101{
7102 if (io_run_task_work())
7103 return 1;
7104 if (!signal_pending(current))
7105 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007106 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7107 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007108 return -EINTR;
7109}
7110
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007111/* when returns >0, the caller should retry */
7112static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7113 struct io_wait_queue *iowq,
7114 signed long *timeout)
7115{
7116 int ret;
7117
7118 /* make sure we run task_work before checking for signals */
7119 ret = io_run_task_work_sig();
7120 if (ret || io_should_wake(iowq))
7121 return ret;
7122 /* let the caller flush overflows, retry */
7123 if (test_bit(0, &ctx->cq_check_overflow))
7124 return 1;
7125
7126 *timeout = schedule_timeout(*timeout);
7127 return !*timeout ? -ETIME : 1;
7128}
7129
Jens Axboe2b188cc2019-01-07 10:46:33 -07007130/*
7131 * Wait until events become available, if we don't already have some. The
7132 * application must reap them itself, as they reside on the shared cq ring.
7133 */
7134static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007135 const sigset_t __user *sig, size_t sigsz,
7136 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007137{
Jens Axboebda52162019-09-24 13:47:15 -06007138 struct io_wait_queue iowq = {
7139 .wq = {
7140 .private = current,
7141 .func = io_wake_function,
7142 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7143 },
7144 .ctx = ctx,
7145 .to_wait = min_events,
7146 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007147 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007148 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7149 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007150
Jens Axboeb41e9852020-02-17 09:52:41 -07007151 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007152 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7153 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007154 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007155 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007156 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007157 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007158
7159 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007160#ifdef CONFIG_COMPAT
7161 if (in_compat_syscall())
7162 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007163 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007164 else
7165#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007166 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007167
Jens Axboe2b188cc2019-01-07 10:46:33 -07007168 if (ret)
7169 return ret;
7170 }
7171
Hao Xuc73ebb62020-11-03 10:54:37 +08007172 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007173 struct timespec64 ts;
7174
Hao Xuc73ebb62020-11-03 10:54:37 +08007175 if (get_timespec64(&ts, uts))
7176 return -EFAULT;
7177 timeout = timespec64_to_jiffies(&ts);
7178 }
7179
Jens Axboebda52162019-09-24 13:47:15 -06007180 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007181 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007182 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007183 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007184 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7185 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007186 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7187 finish_wait(&ctx->wait, &iowq.wq);
7188 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007189
Jens Axboeb7db41c2020-07-04 08:55:50 -06007190 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007191
Hristo Venev75b28af2019-08-26 17:23:46 +00007192 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007193}
7194
Jens Axboe6b063142019-01-10 22:13:58 -07007195static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7196{
7197#if defined(CONFIG_UNIX)
7198 if (ctx->ring_sock) {
7199 struct sock *sock = ctx->ring_sock->sk;
7200 struct sk_buff *skb;
7201
7202 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7203 kfree_skb(skb);
7204 }
7205#else
7206 int i;
7207
Jens Axboe65e19f52019-10-26 07:20:21 -06007208 for (i = 0; i < ctx->nr_user_files; i++) {
7209 struct file *file;
7210
7211 file = io_file_from_index(ctx, i);
7212 if (file)
7213 fput(file);
7214 }
Jens Axboe6b063142019-01-10 22:13:58 -07007215#endif
7216}
7217
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007218static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007219{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007220 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007221
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007222 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007223 complete(&data->done);
7224}
7225
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007226static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7227{
7228 spin_lock_bh(&ctx->rsrc_ref_lock);
7229}
7230
7231static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7232{
7233 spin_unlock_bh(&ctx->rsrc_ref_lock);
7234}
7235
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007236static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7237 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007238 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007239{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007240 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007241 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007242 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007243 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007244 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007245}
7246
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007247static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7248 struct io_ring_ctx *ctx,
7249 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007250{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007251 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007252 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007253
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007254 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007255 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007256 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007257 if (ref_node)
7258 percpu_ref_kill(&ref_node->refs);
7259
7260 percpu_ref_kill(&data->refs);
7261
7262 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007263 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007264 do {
7265 ret = wait_for_completion_interruptible(&data->done);
7266 if (!ret)
7267 break;
7268 ret = io_run_task_work_sig();
7269 if (ret < 0) {
7270 percpu_ref_resurrect(&data->refs);
7271 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007272 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007273 return ret;
7274 }
7275 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007276
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007277 destroy_fixed_rsrc_ref_node(backup_node);
7278 return 0;
7279}
7280
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007281static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7282{
7283 struct fixed_rsrc_data *data;
7284
7285 data = kzalloc(sizeof(*data), GFP_KERNEL);
7286 if (!data)
7287 return NULL;
7288
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007289 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007290 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7291 kfree(data);
7292 return NULL;
7293 }
7294 data->ctx = ctx;
7295 init_completion(&data->done);
7296 return data;
7297}
7298
7299static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7300{
7301 percpu_ref_exit(&data->refs);
7302 kfree(data->table);
7303 kfree(data);
7304}
7305
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007306static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7307{
7308 struct fixed_rsrc_data *data = ctx->file_data;
7309 struct fixed_rsrc_ref_node *backup_node;
7310 unsigned nr_tables, i;
7311 int ret;
7312
7313 if (!data)
7314 return -ENXIO;
7315 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7316 if (!backup_node)
7317 return -ENOMEM;
7318 init_fixed_file_ref_node(ctx, backup_node);
7319
7320 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7321 if (ret)
7322 return ret;
7323
Jens Axboe6b063142019-01-10 22:13:58 -07007324 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007325 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7326 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007327 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007328 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007329 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007330 ctx->nr_user_files = 0;
7331 return 0;
7332}
7333
Jens Axboe534ca6d2020-09-02 13:52:19 -06007334static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007335{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007336 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007337 /*
7338 * The park is a bit of a work-around, without it we get
7339 * warning spews on shutdown with SQPOLL set and affinity
7340 * set to a single CPU.
7341 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007342 if (sqd->thread) {
7343 kthread_park(sqd->thread);
7344 kthread_stop(sqd->thread);
7345 }
7346
7347 kfree(sqd);
7348 }
7349}
7350
Jens Axboeaa061652020-09-02 14:50:27 -06007351static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7352{
7353 struct io_ring_ctx *ctx_attach;
7354 struct io_sq_data *sqd;
7355 struct fd f;
7356
7357 f = fdget(p->wq_fd);
7358 if (!f.file)
7359 return ERR_PTR(-ENXIO);
7360 if (f.file->f_op != &io_uring_fops) {
7361 fdput(f);
7362 return ERR_PTR(-EINVAL);
7363 }
7364
7365 ctx_attach = f.file->private_data;
7366 sqd = ctx_attach->sq_data;
7367 if (!sqd) {
7368 fdput(f);
7369 return ERR_PTR(-EINVAL);
7370 }
7371
7372 refcount_inc(&sqd->refs);
7373 fdput(f);
7374 return sqd;
7375}
7376
Jens Axboe534ca6d2020-09-02 13:52:19 -06007377static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7378{
7379 struct io_sq_data *sqd;
7380
Jens Axboeaa061652020-09-02 14:50:27 -06007381 if (p->flags & IORING_SETUP_ATTACH_WQ)
7382 return io_attach_sq_data(p);
7383
Jens Axboe534ca6d2020-09-02 13:52:19 -06007384 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7385 if (!sqd)
7386 return ERR_PTR(-ENOMEM);
7387
7388 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007389 INIT_LIST_HEAD(&sqd->ctx_list);
7390 INIT_LIST_HEAD(&sqd->ctx_new_list);
7391 mutex_init(&sqd->ctx_lock);
7392 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007393 init_waitqueue_head(&sqd->wait);
7394 return sqd;
7395}
7396
Jens Axboe69fb2132020-09-14 11:16:23 -06007397static void io_sq_thread_unpark(struct io_sq_data *sqd)
7398 __releases(&sqd->lock)
7399{
7400 if (!sqd->thread)
7401 return;
7402 kthread_unpark(sqd->thread);
7403 mutex_unlock(&sqd->lock);
7404}
7405
7406static void io_sq_thread_park(struct io_sq_data *sqd)
7407 __acquires(&sqd->lock)
7408{
7409 if (!sqd->thread)
7410 return;
7411 mutex_lock(&sqd->lock);
7412 kthread_park(sqd->thread);
7413}
7414
Jens Axboe534ca6d2020-09-02 13:52:19 -06007415static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7416{
7417 struct io_sq_data *sqd = ctx->sq_data;
7418
7419 if (sqd) {
7420 if (sqd->thread) {
7421 /*
7422 * We may arrive here from the error branch in
7423 * io_sq_offload_create() where the kthread is created
7424 * without being waked up, thus wake it up now to make
7425 * sure the wait will complete.
7426 */
7427 wake_up_process(sqd->thread);
7428 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007429
7430 io_sq_thread_park(sqd);
7431 }
7432
7433 mutex_lock(&sqd->ctx_lock);
7434 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007435 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007436 mutex_unlock(&sqd->ctx_lock);
7437
Xiaoguang Wang08369242020-11-03 14:15:59 +08007438 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007439 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007440
7441 io_put_sq_data(sqd);
7442 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007443 }
7444}
7445
Jens Axboe6b063142019-01-10 22:13:58 -07007446static void io_finish_async(struct io_ring_ctx *ctx)
7447{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007448 io_sq_thread_stop(ctx);
7449
Jens Axboe561fb042019-10-24 07:25:42 -06007450 if (ctx->io_wq) {
7451 io_wq_destroy(ctx->io_wq);
7452 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007453 }
7454}
7455
7456#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007457/*
7458 * Ensure the UNIX gc is aware of our file set, so we are certain that
7459 * the io_uring can be safely unregistered on process exit, even if we have
7460 * loops in the file referencing.
7461 */
7462static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7463{
7464 struct sock *sk = ctx->ring_sock->sk;
7465 struct scm_fp_list *fpl;
7466 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007467 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007468
Jens Axboe6b063142019-01-10 22:13:58 -07007469 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7470 if (!fpl)
7471 return -ENOMEM;
7472
7473 skb = alloc_skb(0, GFP_KERNEL);
7474 if (!skb) {
7475 kfree(fpl);
7476 return -ENOMEM;
7477 }
7478
7479 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007480
Jens Axboe08a45172019-10-03 08:11:03 -06007481 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007482 fpl->user = get_uid(ctx->user);
7483 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007484 struct file *file = io_file_from_index(ctx, i + offset);
7485
7486 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007487 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007488 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007489 unix_inflight(fpl->user, fpl->fp[nr_files]);
7490 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007491 }
7492
Jens Axboe08a45172019-10-03 08:11:03 -06007493 if (nr_files) {
7494 fpl->max = SCM_MAX_FD;
7495 fpl->count = nr_files;
7496 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007497 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007498 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7499 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007500
Jens Axboe08a45172019-10-03 08:11:03 -06007501 for (i = 0; i < nr_files; i++)
7502 fput(fpl->fp[i]);
7503 } else {
7504 kfree_skb(skb);
7505 kfree(fpl);
7506 }
Jens Axboe6b063142019-01-10 22:13:58 -07007507
7508 return 0;
7509}
7510
7511/*
7512 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7513 * causes regular reference counting to break down. We rely on the UNIX
7514 * garbage collection to take care of this problem for us.
7515 */
7516static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7517{
7518 unsigned left, total;
7519 int ret = 0;
7520
7521 total = 0;
7522 left = ctx->nr_user_files;
7523 while (left) {
7524 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007525
7526 ret = __io_sqe_files_scm(ctx, this_files, total);
7527 if (ret)
7528 break;
7529 left -= this_files;
7530 total += this_files;
7531 }
7532
7533 if (!ret)
7534 return 0;
7535
7536 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007537 struct file *file = io_file_from_index(ctx, total);
7538
7539 if (file)
7540 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007541 total++;
7542 }
7543
7544 return ret;
7545}
7546#else
7547static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7548{
7549 return 0;
7550}
7551#endif
7552
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007553static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007554 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007555{
7556 int i;
7557
7558 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007559 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007560 unsigned this_files;
7561
7562 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7563 table->files = kcalloc(this_files, sizeof(struct file *),
7564 GFP_KERNEL);
7565 if (!table->files)
7566 break;
7567 nr_files -= this_files;
7568 }
7569
7570 if (i == nr_tables)
7571 return 0;
7572
7573 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007574 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007575 kfree(table->files);
7576 }
7577 return 1;
7578}
7579
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007580static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007581{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007582 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007583#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007584 struct sock *sock = ctx->ring_sock->sk;
7585 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7586 struct sk_buff *skb;
7587 int i;
7588
7589 __skb_queue_head_init(&list);
7590
7591 /*
7592 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7593 * remove this entry and rearrange the file array.
7594 */
7595 skb = skb_dequeue(head);
7596 while (skb) {
7597 struct scm_fp_list *fp;
7598
7599 fp = UNIXCB(skb).fp;
7600 for (i = 0; i < fp->count; i++) {
7601 int left;
7602
7603 if (fp->fp[i] != file)
7604 continue;
7605
7606 unix_notinflight(fp->user, fp->fp[i]);
7607 left = fp->count - 1 - i;
7608 if (left) {
7609 memmove(&fp->fp[i], &fp->fp[i + 1],
7610 left * sizeof(struct file *));
7611 }
7612 fp->count--;
7613 if (!fp->count) {
7614 kfree_skb(skb);
7615 skb = NULL;
7616 } else {
7617 __skb_queue_tail(&list, skb);
7618 }
7619 fput(file);
7620 file = NULL;
7621 break;
7622 }
7623
7624 if (!file)
7625 break;
7626
7627 __skb_queue_tail(&list, skb);
7628
7629 skb = skb_dequeue(head);
7630 }
7631
7632 if (skb_peek(&list)) {
7633 spin_lock_irq(&head->lock);
7634 while ((skb = __skb_dequeue(&list)) != NULL)
7635 __skb_queue_tail(head, skb);
7636 spin_unlock_irq(&head->lock);
7637 }
7638#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007639 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007640#endif
7641}
7642
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007643static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007644{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007645 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7646 struct io_ring_ctx *ctx = rsrc_data->ctx;
7647 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007648
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007649 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7650 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007651 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007652 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007653 }
7654
Xiaoguang Wang05589552020-03-31 14:05:18 +08007655 percpu_ref_exit(&ref_node->refs);
7656 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007657 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007658}
7659
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007660static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007661{
7662 struct io_ring_ctx *ctx;
7663 struct llist_node *node;
7664
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007665 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7666 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007667
7668 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007669 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007670 struct llist_node *next = node->next;
7671
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007672 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7673 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007674 node = next;
7675 }
7676}
7677
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007678static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7679 unsigned i)
7680{
7681 struct fixed_rsrc_table *table;
7682
7683 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7684 return &table->files[i & IORING_FILE_TABLE_MASK];
7685}
7686
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007687static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007688{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007689 struct fixed_rsrc_ref_node *ref_node;
7690 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007691 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007692 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007693 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007694
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007695 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7696 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007697 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007698
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007699 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007700 ref_node->done = true;
7701
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007702 while (!list_empty(&ctx->rsrc_ref_list)) {
7703 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007704 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007705 /* recycle ref nodes in order */
7706 if (!ref_node->done)
7707 break;
7708 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007709 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007710 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007711 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007712
7713 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007714 delay = 0;
7715
Jens Axboe4a38aed22020-05-14 17:21:15 -06007716 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007717 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007718 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007719 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007720}
7721
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007722static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007723 struct io_ring_ctx *ctx)
7724{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007725 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007726
7727 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7728 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007729 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007730
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007731 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007732 0, GFP_KERNEL)) {
7733 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007734 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007735 }
7736 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007737 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007738 ref_node->done = false;
7739 return ref_node;
7740}
7741
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007742static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7743 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007744{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007745 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007746 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007747}
7748
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007749static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007750{
7751 percpu_ref_exit(&ref_node->refs);
7752 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007753}
7754
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007755
Jens Axboe05f3fb32019-12-09 11:22:50 -07007756static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7757 unsigned nr_args)
7758{
7759 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007760 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007761 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007762 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007763 struct fixed_rsrc_ref_node *ref_node;
7764 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007765
7766 if (ctx->file_data)
7767 return -EBUSY;
7768 if (!nr_args)
7769 return -EINVAL;
7770 if (nr_args > IORING_MAX_FIXED_FILES)
7771 return -EMFILE;
7772
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007773 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007774 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007775 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007776 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007777
7778 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007779 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007780 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007781 if (!file_data->table)
7782 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007783
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007784 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007785 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007786
7787 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007788 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7789 ret = -EFAULT;
7790 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007791 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007792 /* allow sparse sets */
7793 if (fd == -1)
7794 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007795
Jens Axboe05f3fb32019-12-09 11:22:50 -07007796 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007797 ret = -EBADF;
7798 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007799 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007800
7801 /*
7802 * Don't allow io_uring instances to be registered. If UNIX
7803 * isn't enabled, then this causes a reference cycle and this
7804 * instance can never get freed. If UNIX is enabled we'll
7805 * handle it just fine, but there's still no point in allowing
7806 * a ring fd as it doesn't support regular read/write anyway.
7807 */
7808 if (file->f_op == &io_uring_fops) {
7809 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007810 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007811 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007812 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007813 }
7814
Jens Axboe05f3fb32019-12-09 11:22:50 -07007815 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007816 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007817 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007818 return ret;
7819 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007820
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007821 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007822 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007823 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007824 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007825 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007826 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007827
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007828 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007829 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007830out_fput:
7831 for (i = 0; i < ctx->nr_user_files; i++) {
7832 file = io_file_from_index(ctx, i);
7833 if (file)
7834 fput(file);
7835 }
7836 for (i = 0; i < nr_tables; i++)
7837 kfree(file_data->table[i].files);
7838 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007839out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007840 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007841 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007842 return ret;
7843}
7844
Jens Axboec3a31e62019-10-03 13:59:56 -06007845static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7846 int index)
7847{
7848#if defined(CONFIG_UNIX)
7849 struct sock *sock = ctx->ring_sock->sk;
7850 struct sk_buff_head *head = &sock->sk_receive_queue;
7851 struct sk_buff *skb;
7852
7853 /*
7854 * See if we can merge this file into an existing skb SCM_RIGHTS
7855 * file set. If there's no room, fall back to allocating a new skb
7856 * and filling it in.
7857 */
7858 spin_lock_irq(&head->lock);
7859 skb = skb_peek(head);
7860 if (skb) {
7861 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7862
7863 if (fpl->count < SCM_MAX_FD) {
7864 __skb_unlink(skb, head);
7865 spin_unlock_irq(&head->lock);
7866 fpl->fp[fpl->count] = get_file(file);
7867 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7868 fpl->count++;
7869 spin_lock_irq(&head->lock);
7870 __skb_queue_head(head, skb);
7871 } else {
7872 skb = NULL;
7873 }
7874 }
7875 spin_unlock_irq(&head->lock);
7876
7877 if (skb) {
7878 fput(file);
7879 return 0;
7880 }
7881
7882 return __io_sqe_files_scm(ctx, 1, index);
7883#else
7884 return 0;
7885#endif
7886}
7887
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007888static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007889{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007890 struct io_rsrc_put *prsrc;
7891 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007892
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007893 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7894 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007895 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007896
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007897 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007898 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007899
Hillf Dantona5318d32020-03-23 17:47:15 +08007900 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007901}
7902
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007903static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7904 struct file *file)
7905{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007906 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007907}
7908
Jens Axboe05f3fb32019-12-09 11:22:50 -07007909static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007910 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007911 unsigned nr_args)
7912{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007913 struct fixed_rsrc_data *data = ctx->file_data;
7914 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007915 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007916 __s32 __user *fds;
7917 int fd, i, err;
7918 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007919 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007920
Jens Axboe05f3fb32019-12-09 11:22:50 -07007921 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007922 return -EOVERFLOW;
7923 if (done > ctx->nr_user_files)
7924 return -EINVAL;
7925
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007926 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007927 if (!ref_node)
7928 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007929 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007930
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007931 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007932 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007933 err = 0;
7934 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7935 err = -EFAULT;
7936 break;
7937 }
noah4e0377a2021-01-26 15:23:28 -05007938 if (fd == IORING_REGISTER_FILES_SKIP)
7939 continue;
7940
Pavel Begunkov67973b92021-01-26 13:51:09 +00007941 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007942 file_slot = io_fixed_file_slot(ctx->file_data, i);
7943
7944 if (*file_slot) {
7945 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007946 if (err)
7947 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007948 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007949 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007950 }
7951 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007952 file = fget(fd);
7953 if (!file) {
7954 err = -EBADF;
7955 break;
7956 }
7957 /*
7958 * Don't allow io_uring instances to be registered. If
7959 * UNIX isn't enabled, then this causes a reference
7960 * cycle and this instance can never get freed. If UNIX
7961 * is enabled we'll handle it just fine, but there's
7962 * still no point in allowing a ring fd as it doesn't
7963 * support regular read/write anyway.
7964 */
7965 if (file->f_op == &io_uring_fops) {
7966 fput(file);
7967 err = -EBADF;
7968 break;
7969 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007970 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007971 if (err) {
7972 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007973 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007974 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007975 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007976 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007977 }
7978
Xiaoguang Wang05589552020-03-31 14:05:18 +08007979 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007980 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007981 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007982 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007983 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007984
7985 return done ? done : err;
7986}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007987
Jens Axboe05f3fb32019-12-09 11:22:50 -07007988static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7989 unsigned nr_args)
7990{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007991 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007992
7993 if (!ctx->file_data)
7994 return -ENXIO;
7995 if (!nr_args)
7996 return -EINVAL;
7997 if (copy_from_user(&up, arg, sizeof(up)))
7998 return -EFAULT;
7999 if (up.resv)
8000 return -EINVAL;
8001
8002 return __io_sqe_files_update(ctx, &up, nr_args);
8003}
Jens Axboec3a31e62019-10-03 13:59:56 -06008004
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008005static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008006{
8007 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8008
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008009 req = io_put_req_find_next(req);
8010 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008011}
8012
Pavel Begunkov24369c22020-01-28 03:15:48 +03008013static int io_init_wq_offload(struct io_ring_ctx *ctx,
8014 struct io_uring_params *p)
8015{
8016 struct io_wq_data data;
8017 struct fd f;
8018 struct io_ring_ctx *ctx_attach;
8019 unsigned int concurrency;
8020 int ret = 0;
8021
8022 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008023 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008024 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008025
8026 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8027 /* Do QD, or 4 * CPUS, whatever is smallest */
8028 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8029
8030 ctx->io_wq = io_wq_create(concurrency, &data);
8031 if (IS_ERR(ctx->io_wq)) {
8032 ret = PTR_ERR(ctx->io_wq);
8033 ctx->io_wq = NULL;
8034 }
8035 return ret;
8036 }
8037
8038 f = fdget(p->wq_fd);
8039 if (!f.file)
8040 return -EBADF;
8041
8042 if (f.file->f_op != &io_uring_fops) {
8043 ret = -EINVAL;
8044 goto out_fput;
8045 }
8046
8047 ctx_attach = f.file->private_data;
8048 /* @io_wq is protected by holding the fd */
8049 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8050 ret = -EINVAL;
8051 goto out_fput;
8052 }
8053
8054 ctx->io_wq = ctx_attach->io_wq;
8055out_fput:
8056 fdput(f);
8057 return ret;
8058}
8059
Jens Axboe0f212202020-09-13 13:09:39 -06008060static int io_uring_alloc_task_context(struct task_struct *task)
8061{
8062 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008063 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008064
8065 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8066 if (unlikely(!tctx))
8067 return -ENOMEM;
8068
Jens Axboed8a6df12020-10-15 16:24:45 -06008069 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8070 if (unlikely(ret)) {
8071 kfree(tctx);
8072 return ret;
8073 }
8074
Jens Axboe0f212202020-09-13 13:09:39 -06008075 xa_init(&tctx->xa);
8076 init_waitqueue_head(&tctx->wait);
8077 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008078 atomic_set(&tctx->in_idle, 0);
8079 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008080 io_init_identity(&tctx->__identity);
8081 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008082 task->io_uring = tctx;
8083 return 0;
8084}
8085
8086void __io_uring_free(struct task_struct *tsk)
8087{
8088 struct io_uring_task *tctx = tsk->io_uring;
8089
8090 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008091 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8092 if (tctx->identity != &tctx->__identity)
8093 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008094 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008095 kfree(tctx);
8096 tsk->io_uring = NULL;
8097}
8098
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008099static int io_sq_offload_create(struct io_ring_ctx *ctx,
8100 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008101{
8102 int ret;
8103
Jens Axboe6c271ce2019-01-10 11:22:30 -07008104 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008105 struct io_sq_data *sqd;
8106
Jens Axboe3ec482d2019-04-08 10:51:01 -06008107 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008108 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008109 goto err;
8110
Jens Axboe534ca6d2020-09-02 13:52:19 -06008111 sqd = io_get_sq_data(p);
8112 if (IS_ERR(sqd)) {
8113 ret = PTR_ERR(sqd);
8114 goto err;
8115 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008116
Jens Axboe534ca6d2020-09-02 13:52:19 -06008117 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008118 io_sq_thread_park(sqd);
8119 mutex_lock(&sqd->ctx_lock);
8120 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8121 mutex_unlock(&sqd->ctx_lock);
8122 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008123
Jens Axboe917257d2019-04-13 09:28:55 -06008124 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8125 if (!ctx->sq_thread_idle)
8126 ctx->sq_thread_idle = HZ;
8127
Jens Axboeaa061652020-09-02 14:50:27 -06008128 if (sqd->thread)
8129 goto done;
8130
Jens Axboe6c271ce2019-01-10 11:22:30 -07008131 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008132 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008133
Jens Axboe917257d2019-04-13 09:28:55 -06008134 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008135 if (cpu >= nr_cpu_ids)
8136 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008137 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008138 goto err;
8139
Jens Axboe69fb2132020-09-14 11:16:23 -06008140 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008141 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008142 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008143 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008144 "io_uring-sq");
8145 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008146 if (IS_ERR(sqd->thread)) {
8147 ret = PTR_ERR(sqd->thread);
8148 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008149 goto err;
8150 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008151 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008152 if (ret)
8153 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008154 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8155 /* Can't have SQ_AFF without SQPOLL */
8156 ret = -EINVAL;
8157 goto err;
8158 }
8159
Jens Axboeaa061652020-09-02 14:50:27 -06008160done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008161 ret = io_init_wq_offload(ctx, p);
8162 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008163 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008164
8165 return 0;
8166err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008167 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008168 return ret;
8169}
8170
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008171static void io_sq_offload_start(struct io_ring_ctx *ctx)
8172{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008173 struct io_sq_data *sqd = ctx->sq_data;
8174
8175 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8176 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008177}
8178
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008179static inline void __io_unaccount_mem(struct user_struct *user,
8180 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008181{
8182 atomic_long_sub(nr_pages, &user->locked_vm);
8183}
8184
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008185static inline int __io_account_mem(struct user_struct *user,
8186 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008187{
8188 unsigned long page_limit, cur_pages, new_pages;
8189
8190 /* Don't allow more pages than we can safely lock */
8191 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8192
8193 do {
8194 cur_pages = atomic_long_read(&user->locked_vm);
8195 new_pages = cur_pages + nr_pages;
8196 if (new_pages > page_limit)
8197 return -ENOMEM;
8198 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8199 new_pages) != cur_pages);
8200
8201 return 0;
8202}
8203
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008204static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8205 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008206{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008207 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008208 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008209
Jens Axboe2aede0e2020-09-14 10:45:53 -06008210 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008211 if (acct == ACCT_LOCKED) {
8212 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008213 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008214 mmap_write_unlock(ctx->mm_account);
8215 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008216 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008217 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008218 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008219}
8220
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008221static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8222 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008223{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008224 int ret;
8225
8226 if (ctx->limit_mem) {
8227 ret = __io_account_mem(ctx->user, nr_pages);
8228 if (ret)
8229 return ret;
8230 }
8231
Jens Axboe2aede0e2020-09-14 10:45:53 -06008232 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008233 if (acct == ACCT_LOCKED) {
8234 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008235 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008236 mmap_write_unlock(ctx->mm_account);
8237 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008238 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008239 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008240 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008241
8242 return 0;
8243}
8244
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245static void io_mem_free(void *ptr)
8246{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008247 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008248
Mark Rutland52e04ef2019-04-30 17:30:21 +01008249 if (!ptr)
8250 return;
8251
8252 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253 if (put_page_testzero(page))
8254 free_compound_page(page);
8255}
8256
8257static void *io_mem_alloc(size_t size)
8258{
8259 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8260 __GFP_NORETRY;
8261
8262 return (void *) __get_free_pages(gfp_flags, get_order(size));
8263}
8264
Hristo Venev75b28af2019-08-26 17:23:46 +00008265static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8266 size_t *sq_offset)
8267{
8268 struct io_rings *rings;
8269 size_t off, sq_array_size;
8270
8271 off = struct_size(rings, cqes, cq_entries);
8272 if (off == SIZE_MAX)
8273 return SIZE_MAX;
8274
8275#ifdef CONFIG_SMP
8276 off = ALIGN(off, SMP_CACHE_BYTES);
8277 if (off == 0)
8278 return SIZE_MAX;
8279#endif
8280
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008281 if (sq_offset)
8282 *sq_offset = off;
8283
Hristo Venev75b28af2019-08-26 17:23:46 +00008284 sq_array_size = array_size(sizeof(u32), sq_entries);
8285 if (sq_array_size == SIZE_MAX)
8286 return SIZE_MAX;
8287
8288 if (check_add_overflow(off, sq_array_size, &off))
8289 return SIZE_MAX;
8290
Hristo Venev75b28af2019-08-26 17:23:46 +00008291 return off;
8292}
8293
Jens Axboe2b188cc2019-01-07 10:46:33 -07008294static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8295{
Hristo Venev75b28af2019-08-26 17:23:46 +00008296 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008297
Hristo Venev75b28af2019-08-26 17:23:46 +00008298 pages = (size_t)1 << get_order(
8299 rings_size(sq_entries, cq_entries, NULL));
8300 pages += (size_t)1 << get_order(
8301 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008302
Hristo Venev75b28af2019-08-26 17:23:46 +00008303 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008304}
8305
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008306static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008307{
8308 int i, j;
8309
8310 if (!ctx->user_bufs)
8311 return -ENXIO;
8312
8313 for (i = 0; i < ctx->nr_user_bufs; i++) {
8314 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8315
8316 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008317 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008318
Jens Axboede293932020-09-17 16:19:16 -06008319 if (imu->acct_pages)
8320 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008321 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008322 imu->nr_bvecs = 0;
8323 }
8324
8325 kfree(ctx->user_bufs);
8326 ctx->user_bufs = NULL;
8327 ctx->nr_user_bufs = 0;
8328 return 0;
8329}
8330
8331static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8332 void __user *arg, unsigned index)
8333{
8334 struct iovec __user *src;
8335
8336#ifdef CONFIG_COMPAT
8337 if (ctx->compat) {
8338 struct compat_iovec __user *ciovs;
8339 struct compat_iovec ciov;
8340
8341 ciovs = (struct compat_iovec __user *) arg;
8342 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8343 return -EFAULT;
8344
Jens Axboed55e5f52019-12-11 16:12:15 -07008345 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008346 dst->iov_len = ciov.iov_len;
8347 return 0;
8348 }
8349#endif
8350 src = (struct iovec __user *) arg;
8351 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8352 return -EFAULT;
8353 return 0;
8354}
8355
Jens Axboede293932020-09-17 16:19:16 -06008356/*
8357 * Not super efficient, but this is just a registration time. And we do cache
8358 * the last compound head, so generally we'll only do a full search if we don't
8359 * match that one.
8360 *
8361 * We check if the given compound head page has already been accounted, to
8362 * avoid double accounting it. This allows us to account the full size of the
8363 * page, not just the constituent pages of a huge page.
8364 */
8365static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8366 int nr_pages, struct page *hpage)
8367{
8368 int i, j;
8369
8370 /* check current page array */
8371 for (i = 0; i < nr_pages; i++) {
8372 if (!PageCompound(pages[i]))
8373 continue;
8374 if (compound_head(pages[i]) == hpage)
8375 return true;
8376 }
8377
8378 /* check previously registered pages */
8379 for (i = 0; i < ctx->nr_user_bufs; i++) {
8380 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8381
8382 for (j = 0; j < imu->nr_bvecs; j++) {
8383 if (!PageCompound(imu->bvec[j].bv_page))
8384 continue;
8385 if (compound_head(imu->bvec[j].bv_page) == hpage)
8386 return true;
8387 }
8388 }
8389
8390 return false;
8391}
8392
8393static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8394 int nr_pages, struct io_mapped_ubuf *imu,
8395 struct page **last_hpage)
8396{
8397 int i, ret;
8398
8399 for (i = 0; i < nr_pages; i++) {
8400 if (!PageCompound(pages[i])) {
8401 imu->acct_pages++;
8402 } else {
8403 struct page *hpage;
8404
8405 hpage = compound_head(pages[i]);
8406 if (hpage == *last_hpage)
8407 continue;
8408 *last_hpage = hpage;
8409 if (headpage_already_acct(ctx, pages, i, hpage))
8410 continue;
8411 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8412 }
8413 }
8414
8415 if (!imu->acct_pages)
8416 return 0;
8417
8418 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8419 if (ret)
8420 imu->acct_pages = 0;
8421 return ret;
8422}
8423
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008424static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8425 struct io_mapped_ubuf *imu,
8426 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008427{
8428 struct vm_area_struct **vmas = NULL;
8429 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008430 unsigned long off, start, end, ubuf;
8431 size_t size;
8432 int ret, pret, nr_pages, i;
8433
8434 ubuf = (unsigned long) iov->iov_base;
8435 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8436 start = ubuf >> PAGE_SHIFT;
8437 nr_pages = end - start;
8438
8439 ret = -ENOMEM;
8440
8441 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8442 if (!pages)
8443 goto done;
8444
8445 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8446 GFP_KERNEL);
8447 if (!vmas)
8448 goto done;
8449
8450 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8451 GFP_KERNEL);
8452 if (!imu->bvec)
8453 goto done;
8454
8455 ret = 0;
8456 mmap_read_lock(current->mm);
8457 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8458 pages, vmas);
8459 if (pret == nr_pages) {
8460 /* don't support file backed memory */
8461 for (i = 0; i < nr_pages; i++) {
8462 struct vm_area_struct *vma = vmas[i];
8463
8464 if (vma->vm_file &&
8465 !is_file_hugepages(vma->vm_file)) {
8466 ret = -EOPNOTSUPP;
8467 break;
8468 }
8469 }
8470 } else {
8471 ret = pret < 0 ? pret : -EFAULT;
8472 }
8473 mmap_read_unlock(current->mm);
8474 if (ret) {
8475 /*
8476 * if we did partial map, or found file backed vmas,
8477 * release any pages we did get
8478 */
8479 if (pret > 0)
8480 unpin_user_pages(pages, pret);
8481 kvfree(imu->bvec);
8482 goto done;
8483 }
8484
8485 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8486 if (ret) {
8487 unpin_user_pages(pages, pret);
8488 kvfree(imu->bvec);
8489 goto done;
8490 }
8491
8492 off = ubuf & ~PAGE_MASK;
8493 size = iov->iov_len;
8494 for (i = 0; i < nr_pages; i++) {
8495 size_t vec_len;
8496
8497 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8498 imu->bvec[i].bv_page = pages[i];
8499 imu->bvec[i].bv_len = vec_len;
8500 imu->bvec[i].bv_offset = off;
8501 off = 0;
8502 size -= vec_len;
8503 }
8504 /* store original address for later verification */
8505 imu->ubuf = ubuf;
8506 imu->len = iov->iov_len;
8507 imu->nr_bvecs = nr_pages;
8508 ret = 0;
8509done:
8510 kvfree(pages);
8511 kvfree(vmas);
8512 return ret;
8513}
8514
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008515static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008516{
Jens Axboeedafcce2019-01-09 09:16:05 -07008517 if (ctx->user_bufs)
8518 return -EBUSY;
8519 if (!nr_args || nr_args > UIO_MAXIOV)
8520 return -EINVAL;
8521
8522 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8523 GFP_KERNEL);
8524 if (!ctx->user_bufs)
8525 return -ENOMEM;
8526
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008527 return 0;
8528}
8529
8530static int io_buffer_validate(struct iovec *iov)
8531{
8532 /*
8533 * Don't impose further limits on the size and buffer
8534 * constraints here, we'll -EINVAL later when IO is
8535 * submitted if they are wrong.
8536 */
8537 if (!iov->iov_base || !iov->iov_len)
8538 return -EFAULT;
8539
8540 /* arbitrary limit, but we need something */
8541 if (iov->iov_len > SZ_1G)
8542 return -EFAULT;
8543
8544 return 0;
8545}
8546
8547static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8548 unsigned int nr_args)
8549{
8550 int i, ret;
8551 struct iovec iov;
8552 struct page *last_hpage = NULL;
8553
8554 ret = io_buffers_map_alloc(ctx, nr_args);
8555 if (ret)
8556 return ret;
8557
Jens Axboeedafcce2019-01-09 09:16:05 -07008558 for (i = 0; i < nr_args; i++) {
8559 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008560
8561 ret = io_copy_iov(ctx, &iov, arg, i);
8562 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008563 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008564
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008565 ret = io_buffer_validate(&iov);
8566 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008567 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008568
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008569 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8570 if (ret)
8571 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008572
8573 ctx->nr_user_bufs++;
8574 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008575
8576 if (ret)
8577 io_sqe_buffers_unregister(ctx);
8578
Jens Axboeedafcce2019-01-09 09:16:05 -07008579 return ret;
8580}
8581
Jens Axboe9b402842019-04-11 11:45:41 -06008582static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8583{
8584 __s32 __user *fds = arg;
8585 int fd;
8586
8587 if (ctx->cq_ev_fd)
8588 return -EBUSY;
8589
8590 if (copy_from_user(&fd, fds, sizeof(*fds)))
8591 return -EFAULT;
8592
8593 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8594 if (IS_ERR(ctx->cq_ev_fd)) {
8595 int ret = PTR_ERR(ctx->cq_ev_fd);
8596 ctx->cq_ev_fd = NULL;
8597 return ret;
8598 }
8599
8600 return 0;
8601}
8602
8603static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8604{
8605 if (ctx->cq_ev_fd) {
8606 eventfd_ctx_put(ctx->cq_ev_fd);
8607 ctx->cq_ev_fd = NULL;
8608 return 0;
8609 }
8610
8611 return -ENXIO;
8612}
8613
Jens Axboe5a2e7452020-02-23 16:23:11 -07008614static int __io_destroy_buffers(int id, void *p, void *data)
8615{
8616 struct io_ring_ctx *ctx = data;
8617 struct io_buffer *buf = p;
8618
Jens Axboe067524e2020-03-02 16:32:28 -07008619 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008620 return 0;
8621}
8622
8623static void io_destroy_buffers(struct io_ring_ctx *ctx)
8624{
8625 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8626 idr_destroy(&ctx->io_buffer_idr);
8627}
8628
Jens Axboe2b188cc2019-01-07 10:46:33 -07008629static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8630{
Jens Axboe6b063142019-01-10 22:13:58 -07008631 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008632 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008633
8634 if (ctx->sqo_task) {
8635 put_task_struct(ctx->sqo_task);
8636 ctx->sqo_task = NULL;
8637 mmdrop(ctx->mm_account);
8638 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008639 }
Jens Axboedef596e2019-01-09 08:59:42 -07008640
Dennis Zhou91d8f512020-09-16 13:41:05 -07008641#ifdef CONFIG_BLK_CGROUP
8642 if (ctx->sqo_blkcg_css)
8643 css_put(ctx->sqo_blkcg_css);
8644#endif
8645
Jens Axboe6b063142019-01-10 22:13:58 -07008646 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008647 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008648 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008649 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008650
Jens Axboe2b188cc2019-01-07 10:46:33 -07008651#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008652 if (ctx->ring_sock) {
8653 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008654 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008655 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008656#endif
8657
Hristo Venev75b28af2019-08-26 17:23:46 +00008658 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008659 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008660
8661 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008662 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008663 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008664 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008665 kfree(ctx);
8666}
8667
8668static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8669{
8670 struct io_ring_ctx *ctx = file->private_data;
8671 __poll_t mask = 0;
8672
8673 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008674 /*
8675 * synchronizes with barrier from wq_has_sleeper call in
8676 * io_commit_cqring
8677 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008678 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008679 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008680 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008681 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8682 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008683 mask |= EPOLLIN | EPOLLRDNORM;
8684
8685 return mask;
8686}
8687
8688static int io_uring_fasync(int fd, struct file *file, int on)
8689{
8690 struct io_ring_ctx *ctx = file->private_data;
8691
8692 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8693}
8694
Yejune Deng0bead8c2020-12-24 11:02:20 +08008695static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008696{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008697 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008698
Jens Axboe1e6fa522020-10-15 08:46:24 -06008699 iod = idr_remove(&ctx->personality_idr, id);
8700 if (iod) {
8701 put_cred(iod->creds);
8702 if (refcount_dec_and_test(&iod->count))
8703 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008704 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008705 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008706
8707 return -EINVAL;
8708}
8709
8710static int io_remove_personalities(int id, void *p, void *data)
8711{
8712 struct io_ring_ctx *ctx = data;
8713
8714 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008715 return 0;
8716}
8717
Jens Axboe85faa7b2020-04-09 18:14:00 -06008718static void io_ring_exit_work(struct work_struct *work)
8719{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008720 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8721 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008722
Jens Axboe56952e92020-06-17 15:00:04 -06008723 /*
8724 * If we're doing polled IO and end up having requests being
8725 * submitted async (out-of-line), then completions can come in while
8726 * we're waiting for refs to drop. We need to reap these manually,
8727 * as nobody else will be looking for them.
8728 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008729 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008730 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008731 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008732 io_ring_ctx_free(ctx);
8733}
8734
Jens Axboe00c18642020-12-20 10:45:02 -07008735static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8736{
8737 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8738
8739 return req->ctx == data;
8740}
8741
Jens Axboe2b188cc2019-01-07 10:46:33 -07008742static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8743{
8744 mutex_lock(&ctx->uring_lock);
8745 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008746
8747 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8748 ctx->sqo_dead = 1;
8749
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008750 /* if force is set, the ring is going away. always drop after that */
8751 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008752 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008753 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008754 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755 mutex_unlock(&ctx->uring_lock);
8756
Pavel Begunkov6b819282020-11-06 13:00:25 +00008757 io_kill_timeouts(ctx, NULL, NULL);
8758 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008759
8760 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008761 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008762
Jens Axboe15dff282019-11-13 09:09:23 -07008763 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008764 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008765
8766 /*
8767 * Do this upfront, so we won't have a grace period where the ring
8768 * is closed but resources aren't reaped yet. This can cause
8769 * spurious failure in setting up a new ring.
8770 */
Jens Axboe760618f2020-07-24 12:53:31 -06008771 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8772 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008773
Jens Axboe85faa7b2020-04-09 18:14:00 -06008774 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008775 /*
8776 * Use system_unbound_wq to avoid spawning tons of event kworkers
8777 * if we're exiting a ton of rings at the same time. It just adds
8778 * noise and overhead, there's no discernable change in runtime
8779 * over using system_wq.
8780 */
8781 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782}
8783
8784static int io_uring_release(struct inode *inode, struct file *file)
8785{
8786 struct io_ring_ctx *ctx = file->private_data;
8787
8788 file->private_data = NULL;
8789 io_ring_ctx_wait_and_kill(ctx);
8790 return 0;
8791}
8792
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008793struct io_task_cancel {
8794 struct task_struct *task;
8795 struct files_struct *files;
8796};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008797
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008798static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008799{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008800 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008801 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008802 bool ret;
8803
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008804 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008805 unsigned long flags;
8806 struct io_ring_ctx *ctx = req->ctx;
8807
8808 /* protect against races with linked timeouts */
8809 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008810 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008811 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8812 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008813 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008814 }
8815 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008816}
8817
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008818static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008819 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008820 struct files_struct *files)
8821{
8822 struct io_defer_entry *de = NULL;
8823 LIST_HEAD(list);
8824
8825 spin_lock_irq(&ctx->completion_lock);
8826 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008827 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008828 list_cut_position(&list, &ctx->defer_list, &de->list);
8829 break;
8830 }
8831 }
8832 spin_unlock_irq(&ctx->completion_lock);
8833
8834 while (!list_empty(&list)) {
8835 de = list_first_entry(&list, struct io_defer_entry, list);
8836 list_del_init(&de->list);
8837 req_set_fail_links(de->req);
8838 io_put_req(de->req);
8839 io_req_complete(de->req, -ECANCELED);
8840 kfree(de);
8841 }
8842}
8843
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008844static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8845 struct task_struct *task,
8846 struct files_struct *files)
8847{
8848 struct io_task_cancel cancel = { .task = task, .files = files, };
8849
8850 while (1) {
8851 enum io_wq_cancel cret;
8852 bool ret = false;
8853
8854 if (ctx->io_wq) {
8855 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8856 &cancel, true);
8857 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8858 }
8859
8860 /* SQPOLL thread does its own polling */
8861 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8862 while (!list_empty_careful(&ctx->iopoll_list)) {
8863 io_iopoll_try_reap_events(ctx);
8864 ret = true;
8865 }
8866 }
8867
8868 ret |= io_poll_remove_all(ctx, task, files);
8869 ret |= io_kill_timeouts(ctx, task, files);
8870 ret |= io_run_task_work();
8871 io_cqring_overflow_flush(ctx, true, task, files);
8872 if (!ret)
8873 break;
8874 cond_resched();
8875 }
8876}
8877
Pavel Begunkovca70f002021-01-26 15:28:27 +00008878static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8879 struct task_struct *task,
8880 struct files_struct *files)
8881{
8882 struct io_kiocb *req;
8883 int cnt = 0;
8884
8885 spin_lock_irq(&ctx->inflight_lock);
8886 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8887 cnt += io_match_task(req, task, files);
8888 spin_unlock_irq(&ctx->inflight_lock);
8889 return cnt;
8890}
8891
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008892static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008893 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008894 struct files_struct *files)
8895{
Jens Axboefcb323c2019-10-24 12:39:47 -06008896 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008897 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008898 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008899
Pavel Begunkovca70f002021-01-26 15:28:27 +00008900 inflight = io_uring_count_inflight(ctx, task, files);
8901 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008902 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008903
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008904 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008905 prepare_to_wait(&task->io_uring->wait, &wait,
8906 TASK_UNINTERRUPTIBLE);
8907 if (inflight == io_uring_count_inflight(ctx, task, files))
8908 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008909 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008910 }
8911}
8912
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008913static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8914{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008915 mutex_lock(&ctx->uring_lock);
8916 ctx->sqo_dead = 1;
8917 mutex_unlock(&ctx->uring_lock);
8918
8919 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008920 if (ctx->rings)
8921 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008922}
8923
Jens Axboe0f212202020-09-13 13:09:39 -06008924/*
8925 * We need to iteratively cancel requests, in case a request has dependent
8926 * hard links. These persist even for failure of cancelations, hence keep
8927 * looping until none are found.
8928 */
8929static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8930 struct files_struct *files)
8931{
8932 struct task_struct *task = current;
8933
Jens Axboefdaf0832020-10-30 09:37:30 -06008934 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008935 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008936 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008937 atomic_inc(&task->io_uring->in_idle);
8938 io_sq_thread_park(ctx->sq_data);
8939 }
Jens Axboe0f212202020-09-13 13:09:39 -06008940
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008941 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008942
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008943 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008944 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008945 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008946
8947 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8948 atomic_dec(&task->io_uring->in_idle);
8949 /*
8950 * If the files that are going away are the ones in the thread
8951 * identity, clear them out.
8952 */
8953 if (task->io_uring->identity->files == files)
8954 task->io_uring->identity->files = NULL;
8955 io_sq_thread_unpark(ctx->sq_data);
8956 }
Jens Axboe0f212202020-09-13 13:09:39 -06008957}
8958
8959/*
8960 * Note that this task has used io_uring. We use it for cancelation purposes.
8961 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008962static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008963{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008964 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008965 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008966
8967 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008968 ret = io_uring_alloc_task_context(current);
8969 if (unlikely(ret))
8970 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008971 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008972 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008973 if (tctx->last != file) {
8974 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008975
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008976 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008977 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008978 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8979 file, GFP_KERNEL));
8980 if (ret) {
8981 fput(file);
8982 return ret;
8983 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008984
8985 /* one and only SQPOLL file note, held by sqo_task */
8986 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8987 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008988 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008989 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008990 }
8991
Jens Axboefdaf0832020-10-30 09:37:30 -06008992 /*
8993 * This is race safe in that the task itself is doing this, hence it
8994 * cannot be going through the exit/cancel paths at the same time.
8995 * This cannot be modified while exit/cancel is running.
8996 */
8997 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8998 tctx->sqpoll = true;
8999
Jens Axboe0f212202020-09-13 13:09:39 -06009000 return 0;
9001}
9002
9003/*
9004 * Remove this io_uring_file -> task mapping.
9005 */
9006static void io_uring_del_task_file(struct file *file)
9007{
9008 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009009
9010 if (tctx->last == file)
9011 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009012 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009013 if (file)
9014 fput(file);
9015}
9016
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009017static void io_uring_remove_task_files(struct io_uring_task *tctx)
9018{
9019 struct file *file;
9020 unsigned long index;
9021
9022 xa_for_each(&tctx->xa, index, file)
9023 io_uring_del_task_file(file);
9024}
9025
Jens Axboe0f212202020-09-13 13:09:39 -06009026void __io_uring_files_cancel(struct files_struct *files)
9027{
9028 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009029 struct file *file;
9030 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009031
9032 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009033 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009034 xa_for_each(&tctx->xa, index, file)
9035 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009036 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009037
9038 if (files)
9039 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009040}
9041
9042static s64 tctx_inflight(struct io_uring_task *tctx)
9043{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009044 return percpu_counter_sum(&tctx->inflight);
9045}
9046
9047static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9048{
9049 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009050 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009051 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009052
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009053 if (!ctx->sq_data)
9054 return;
9055 tctx = ctx->sq_data->thread->io_uring;
9056 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009057
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009058 atomic_inc(&tctx->in_idle);
9059 do {
9060 /* read completions before cancelations */
9061 inflight = tctx_inflight(tctx);
9062 if (!inflight)
9063 break;
9064 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009065
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009066 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9067 /*
9068 * If we've seen completions, retry without waiting. This
9069 * avoids a race where a completion comes in before we did
9070 * prepare_to_wait().
9071 */
9072 if (inflight == tctx_inflight(tctx))
9073 schedule();
9074 finish_wait(&tctx->wait, &wait);
9075 } while (1);
9076 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009077}
9078
Jens Axboe0f212202020-09-13 13:09:39 -06009079/*
9080 * Find any io_uring fd that this task has registered or done IO on, and cancel
9081 * requests.
9082 */
9083void __io_uring_task_cancel(void)
9084{
9085 struct io_uring_task *tctx = current->io_uring;
9086 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009087 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009088
9089 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009090 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009091
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009092 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009093 if (tctx->sqpoll) {
9094 struct file *file;
9095 unsigned long index;
9096
9097 xa_for_each(&tctx->xa, index, file)
9098 io_uring_cancel_sqpoll(file->private_data);
9099 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009100
Jens Axboed8a6df12020-10-15 16:24:45 -06009101 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009102 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009103 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009104 if (!inflight)
9105 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009106 __io_uring_files_cancel(NULL);
9107
9108 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9109
9110 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009111 * If we've seen completions, retry without waiting. This
9112 * avoids a race where a completion comes in before we did
9113 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009114 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009115 if (inflight == tctx_inflight(tctx))
9116 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009117 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009118 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009119
Jens Axboefdaf0832020-10-30 09:37:30 -06009120 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009121
9122 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009123}
9124
Jens Axboefcb323c2019-10-24 12:39:47 -06009125static int io_uring_flush(struct file *file, void *data)
9126{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009127 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009128 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009129
Jens Axboe84965ff2021-01-23 15:51:11 -07009130 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9131 io_uring_cancel_task_requests(ctx, NULL);
9132
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009133 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009134 return 0;
9135
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009136 /* we should have cancelled and erased it before PF_EXITING */
9137 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9138 xa_load(&tctx->xa, (unsigned long)file));
9139
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009140 /*
9141 * fput() is pending, will be 2 if the only other ref is our potential
9142 * task file note. If the task is exiting, drop regardless of count.
9143 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009144 if (atomic_long_read(&file->f_count) != 2)
9145 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009146
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009147 if (ctx->flags & IORING_SETUP_SQPOLL) {
9148 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009149 WARN_ON_ONCE(ctx->sqo_task != current &&
9150 xa_load(&tctx->xa, (unsigned long)file));
9151 /* sqo_dead check is for when this happens after cancellation */
9152 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009153 !xa_load(&tctx->xa, (unsigned long)file));
9154
9155 io_disable_sqo_submit(ctx);
9156 }
9157
9158 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9159 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009160 return 0;
9161}
9162
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009163static void *io_uring_validate_mmap_request(struct file *file,
9164 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009165{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009166 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009167 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009168 struct page *page;
9169 void *ptr;
9170
9171 switch (offset) {
9172 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009173 case IORING_OFF_CQ_RING:
9174 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009175 break;
9176 case IORING_OFF_SQES:
9177 ptr = ctx->sq_sqes;
9178 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009179 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009180 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009181 }
9182
9183 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009184 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009185 return ERR_PTR(-EINVAL);
9186
9187 return ptr;
9188}
9189
9190#ifdef CONFIG_MMU
9191
9192static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9193{
9194 size_t sz = vma->vm_end - vma->vm_start;
9195 unsigned long pfn;
9196 void *ptr;
9197
9198 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9199 if (IS_ERR(ptr))
9200 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009201
9202 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9203 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9204}
9205
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009206#else /* !CONFIG_MMU */
9207
9208static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9209{
9210 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9211}
9212
9213static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9214{
9215 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9216}
9217
9218static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9219 unsigned long addr, unsigned long len,
9220 unsigned long pgoff, unsigned long flags)
9221{
9222 void *ptr;
9223
9224 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9225 if (IS_ERR(ptr))
9226 return PTR_ERR(ptr);
9227
9228 return (unsigned long) ptr;
9229}
9230
9231#endif /* !CONFIG_MMU */
9232
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009233static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009234{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009235 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009236 DEFINE_WAIT(wait);
9237
9238 do {
9239 if (!io_sqring_full(ctx))
9240 break;
9241
9242 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9243
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009244 if (unlikely(ctx->sqo_dead)) {
9245 ret = -EOWNERDEAD;
9246 goto out;
9247 }
9248
Jens Axboe90554202020-09-03 12:12:41 -06009249 if (!io_sqring_full(ctx))
9250 break;
9251
9252 schedule();
9253 } while (!signal_pending(current));
9254
9255 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009256out:
9257 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009258}
9259
Hao Xuc73ebb62020-11-03 10:54:37 +08009260static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9261 struct __kernel_timespec __user **ts,
9262 const sigset_t __user **sig)
9263{
9264 struct io_uring_getevents_arg arg;
9265
9266 /*
9267 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9268 * is just a pointer to the sigset_t.
9269 */
9270 if (!(flags & IORING_ENTER_EXT_ARG)) {
9271 *sig = (const sigset_t __user *) argp;
9272 *ts = NULL;
9273 return 0;
9274 }
9275
9276 /*
9277 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9278 * timespec and sigset_t pointers if good.
9279 */
9280 if (*argsz != sizeof(arg))
9281 return -EINVAL;
9282 if (copy_from_user(&arg, argp, sizeof(arg)))
9283 return -EFAULT;
9284 *sig = u64_to_user_ptr(arg.sigmask);
9285 *argsz = arg.sigmask_sz;
9286 *ts = u64_to_user_ptr(arg.ts);
9287 return 0;
9288}
9289
Jens Axboe2b188cc2019-01-07 10:46:33 -07009290SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009291 u32, min_complete, u32, flags, const void __user *, argp,
9292 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009293{
9294 struct io_ring_ctx *ctx;
9295 long ret = -EBADF;
9296 int submitted = 0;
9297 struct fd f;
9298
Jens Axboe4c6e2772020-07-01 11:29:10 -06009299 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009300
Jens Axboe90554202020-09-03 12:12:41 -06009301 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009302 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303 return -EINVAL;
9304
9305 f = fdget(fd);
9306 if (!f.file)
9307 return -EBADF;
9308
9309 ret = -EOPNOTSUPP;
9310 if (f.file->f_op != &io_uring_fops)
9311 goto out_fput;
9312
9313 ret = -ENXIO;
9314 ctx = f.file->private_data;
9315 if (!percpu_ref_tryget(&ctx->refs))
9316 goto out_fput;
9317
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009318 ret = -EBADFD;
9319 if (ctx->flags & IORING_SETUP_R_DISABLED)
9320 goto out;
9321
Jens Axboe6c271ce2019-01-10 11:22:30 -07009322 /*
9323 * For SQ polling, the thread will do all submissions and completions.
9324 * Just return the requested submit count, and wake the thread if
9325 * we were asked to.
9326 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009327 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009328 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009329 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009330
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009331 ret = -EOWNERDEAD;
9332 if (unlikely(ctx->sqo_dead))
9333 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009334 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009335 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009336 if (flags & IORING_ENTER_SQ_WAIT) {
9337 ret = io_sqpoll_wait_sq(ctx);
9338 if (ret)
9339 goto out;
9340 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009341 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009342 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009343 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009344 if (unlikely(ret))
9345 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009346 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009347 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009348 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009349
9350 if (submitted != to_submit)
9351 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352 }
9353 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009354 const sigset_t __user *sig;
9355 struct __kernel_timespec __user *ts;
9356
9357 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9358 if (unlikely(ret))
9359 goto out;
9360
Jens Axboe2b188cc2019-01-07 10:46:33 -07009361 min_complete = min(min_complete, ctx->cq_entries);
9362
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009363 /*
9364 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9365 * space applications don't need to do io completion events
9366 * polling again, they can rely on io_sq_thread to do polling
9367 * work, which can reduce cpu usage and uring_lock contention.
9368 */
9369 if (ctx->flags & IORING_SETUP_IOPOLL &&
9370 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009371 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009372 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009373 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009374 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375 }
9376
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009377out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009378 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009379out_fput:
9380 fdput(f);
9381 return submitted ? submitted : ret;
9382}
9383
Tobias Klauserbebdb652020-02-26 18:38:32 +01009384#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009385static int io_uring_show_cred(int id, void *p, void *data)
9386{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009387 struct io_identity *iod = p;
9388 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009389 struct seq_file *m = data;
9390 struct user_namespace *uns = seq_user_ns(m);
9391 struct group_info *gi;
9392 kernel_cap_t cap;
9393 unsigned __capi;
9394 int g;
9395
9396 seq_printf(m, "%5d\n", id);
9397 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9398 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9399 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9400 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9401 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9402 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9403 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9404 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9405 seq_puts(m, "\n\tGroups:\t");
9406 gi = cred->group_info;
9407 for (g = 0; g < gi->ngroups; g++) {
9408 seq_put_decimal_ull(m, g ? " " : "",
9409 from_kgid_munged(uns, gi->gid[g]));
9410 }
9411 seq_puts(m, "\n\tCapEff:\t");
9412 cap = cred->cap_effective;
9413 CAP_FOR_EACH_U32(__capi)
9414 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9415 seq_putc(m, '\n');
9416 return 0;
9417}
9418
9419static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9420{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009421 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009422 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009423 int i;
9424
Jens Axboefad8e0d2020-09-28 08:57:48 -06009425 /*
9426 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9427 * since fdinfo case grabs it in the opposite direction of normal use
9428 * cases. If we fail to get the lock, we just don't iterate any
9429 * structures that could be going away outside the io_uring mutex.
9430 */
9431 has_lock = mutex_trylock(&ctx->uring_lock);
9432
Joseph Qidbbe9c62020-09-29 09:01:22 -06009433 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9434 sq = ctx->sq_data;
9435
9436 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9437 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009438 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009439 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009440 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009441
Jens Axboe87ce9552020-01-30 08:25:34 -07009442 if (f)
9443 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9444 else
9445 seq_printf(m, "%5u: <none>\n", i);
9446 }
9447 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009448 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009449 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9450
9451 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9452 (unsigned int) buf->len);
9453 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009454 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009455 seq_printf(m, "Personalities:\n");
9456 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9457 }
Jens Axboed7718a92020-02-14 22:23:12 -07009458 seq_printf(m, "PollList:\n");
9459 spin_lock_irq(&ctx->completion_lock);
9460 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9461 struct hlist_head *list = &ctx->cancel_hash[i];
9462 struct io_kiocb *req;
9463
9464 hlist_for_each_entry(req, list, hash_node)
9465 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9466 req->task->task_works != NULL);
9467 }
9468 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009469 if (has_lock)
9470 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009471}
9472
9473static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9474{
9475 struct io_ring_ctx *ctx = f->private_data;
9476
9477 if (percpu_ref_tryget(&ctx->refs)) {
9478 __io_uring_show_fdinfo(ctx, m);
9479 percpu_ref_put(&ctx->refs);
9480 }
9481}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009482#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009483
Jens Axboe2b188cc2019-01-07 10:46:33 -07009484static const struct file_operations io_uring_fops = {
9485 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009486 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009487 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009488#ifndef CONFIG_MMU
9489 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9490 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9491#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009492 .poll = io_uring_poll,
9493 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009494#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009495 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009496#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009497};
9498
9499static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9500 struct io_uring_params *p)
9501{
Hristo Venev75b28af2019-08-26 17:23:46 +00009502 struct io_rings *rings;
9503 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009504
Jens Axboebd740482020-08-05 12:58:23 -06009505 /* make sure these are sane, as we already accounted them */
9506 ctx->sq_entries = p->sq_entries;
9507 ctx->cq_entries = p->cq_entries;
9508
Hristo Venev75b28af2019-08-26 17:23:46 +00009509 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9510 if (size == SIZE_MAX)
9511 return -EOVERFLOW;
9512
9513 rings = io_mem_alloc(size);
9514 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009515 return -ENOMEM;
9516
Hristo Venev75b28af2019-08-26 17:23:46 +00009517 ctx->rings = rings;
9518 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9519 rings->sq_ring_mask = p->sq_entries - 1;
9520 rings->cq_ring_mask = p->cq_entries - 1;
9521 rings->sq_ring_entries = p->sq_entries;
9522 rings->cq_ring_entries = p->cq_entries;
9523 ctx->sq_mask = rings->sq_ring_mask;
9524 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009525
9526 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009527 if (size == SIZE_MAX) {
9528 io_mem_free(ctx->rings);
9529 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009530 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009531 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009532
9533 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009534 if (!ctx->sq_sqes) {
9535 io_mem_free(ctx->rings);
9536 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009537 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009538 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009539
Jens Axboe2b188cc2019-01-07 10:46:33 -07009540 return 0;
9541}
9542
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009543static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9544{
9545 int ret, fd;
9546
9547 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9548 if (fd < 0)
9549 return fd;
9550
9551 ret = io_uring_add_task_file(ctx, file);
9552 if (ret) {
9553 put_unused_fd(fd);
9554 return ret;
9555 }
9556 fd_install(fd, file);
9557 return fd;
9558}
9559
Jens Axboe2b188cc2019-01-07 10:46:33 -07009560/*
9561 * Allocate an anonymous fd, this is what constitutes the application
9562 * visible backing of an io_uring instance. The application mmaps this
9563 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9564 * we have to tie this fd to a socket for file garbage collection purposes.
9565 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009566static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009567{
9568 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009569#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009570 int ret;
9571
Jens Axboe2b188cc2019-01-07 10:46:33 -07009572 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9573 &ctx->ring_sock);
9574 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009575 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009576#endif
9577
Jens Axboe2b188cc2019-01-07 10:46:33 -07009578 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9579 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009580#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009581 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009582 sock_release(ctx->ring_sock);
9583 ctx->ring_sock = NULL;
9584 } else {
9585 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009586 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009587#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009588 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009589}
9590
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009591static int io_uring_create(unsigned entries, struct io_uring_params *p,
9592 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009593{
9594 struct user_struct *user = NULL;
9595 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009596 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009597 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009598 int ret;
9599
Jens Axboe8110c1a2019-12-28 15:39:54 -07009600 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009601 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009602 if (entries > IORING_MAX_ENTRIES) {
9603 if (!(p->flags & IORING_SETUP_CLAMP))
9604 return -EINVAL;
9605 entries = IORING_MAX_ENTRIES;
9606 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009607
9608 /*
9609 * Use twice as many entries for the CQ ring. It's possible for the
9610 * application to drive a higher depth than the size of the SQ ring,
9611 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009612 * some flexibility in overcommitting a bit. If the application has
9613 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9614 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009615 */
9616 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009617 if (p->flags & IORING_SETUP_CQSIZE) {
9618 /*
9619 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9620 * to a power-of-two, if it isn't already. We do NOT impose
9621 * any cq vs sq ring sizing.
9622 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009623 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009624 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009625 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9626 if (!(p->flags & IORING_SETUP_CLAMP))
9627 return -EINVAL;
9628 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9629 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009630 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9631 if (p->cq_entries < p->sq_entries)
9632 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009633 } else {
9634 p->cq_entries = 2 * p->sq_entries;
9635 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009636
9637 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009638 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009639
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009640 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009641 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009642 ring_pages(p->sq_entries, p->cq_entries));
9643 if (ret) {
9644 free_uid(user);
9645 return ret;
9646 }
9647 }
9648
9649 ctx = io_ring_ctx_alloc(p);
9650 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009651 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009652 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009653 p->cq_entries));
9654 free_uid(user);
9655 return -ENOMEM;
9656 }
9657 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009658 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009659 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009660#ifdef CONFIG_AUDIT
9661 ctx->loginuid = current->loginuid;
9662 ctx->sessionid = current->sessionid;
9663#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009664 ctx->sqo_task = get_task_struct(current);
9665
9666 /*
9667 * This is just grabbed for accounting purposes. When a process exits,
9668 * the mm is exited and dropped before the files, hence we need to hang
9669 * on to this mm purely for the purposes of being able to unaccount
9670 * memory (locked/pinned vm). It's not used for anything else.
9671 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009672 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009673 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009674
Dennis Zhou91d8f512020-09-16 13:41:05 -07009675#ifdef CONFIG_BLK_CGROUP
9676 /*
9677 * The sq thread will belong to the original cgroup it was inited in.
9678 * If the cgroup goes offline (e.g. disabling the io controller), then
9679 * issued bios will be associated with the closest cgroup later in the
9680 * block layer.
9681 */
9682 rcu_read_lock();
9683 ctx->sqo_blkcg_css = blkcg_css();
9684 ret = css_tryget_online(ctx->sqo_blkcg_css);
9685 rcu_read_unlock();
9686 if (!ret) {
9687 /* don't init against a dying cgroup, have the user try again */
9688 ctx->sqo_blkcg_css = NULL;
9689 ret = -ENODEV;
9690 goto err;
9691 }
9692#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009693
Jens Axboe2b188cc2019-01-07 10:46:33 -07009694 /*
9695 * Account memory _before_ installing the file descriptor. Once
9696 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009697 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009698 * will un-account as well.
9699 */
9700 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9701 ACCT_LOCKED);
9702 ctx->limit_mem = limit_mem;
9703
9704 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009705 if (ret)
9706 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009707
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009708 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009709 if (ret)
9710 goto err;
9711
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009712 if (!(p->flags & IORING_SETUP_R_DISABLED))
9713 io_sq_offload_start(ctx);
9714
Jens Axboe2b188cc2019-01-07 10:46:33 -07009715 memset(&p->sq_off, 0, sizeof(p->sq_off));
9716 p->sq_off.head = offsetof(struct io_rings, sq.head);
9717 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9718 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9719 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9720 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9721 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9722 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9723
9724 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009725 p->cq_off.head = offsetof(struct io_rings, cq.head);
9726 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9727 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9728 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9729 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9730 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009731 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009732
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009733 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9734 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009735 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009736 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9737 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009738
9739 if (copy_to_user(params, p, sizeof(*p))) {
9740 ret = -EFAULT;
9741 goto err;
9742 }
Jens Axboed1719f72020-07-30 13:43:53 -06009743
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009744 file = io_uring_get_file(ctx);
9745 if (IS_ERR(file)) {
9746 ret = PTR_ERR(file);
9747 goto err;
9748 }
9749
Jens Axboed1719f72020-07-30 13:43:53 -06009750 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009751 * Install ring fd as the very last thing, so we don't risk someone
9752 * having closed it before we finish setup
9753 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009754 ret = io_uring_install_fd(ctx, file);
9755 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009756 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009757 /* fput will clean it up */
9758 fput(file);
9759 return ret;
9760 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009761
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009762 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009763 return ret;
9764err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009765 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009766 io_ring_ctx_wait_and_kill(ctx);
9767 return ret;
9768}
9769
9770/*
9771 * Sets up an aio uring context, and returns the fd. Applications asks for a
9772 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9773 * params structure passed in.
9774 */
9775static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9776{
9777 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009778 int i;
9779
9780 if (copy_from_user(&p, params, sizeof(p)))
9781 return -EFAULT;
9782 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9783 if (p.resv[i])
9784 return -EINVAL;
9785 }
9786
Jens Axboe6c271ce2019-01-10 11:22:30 -07009787 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009788 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009789 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9790 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009791 return -EINVAL;
9792
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009793 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009794}
9795
9796SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9797 struct io_uring_params __user *, params)
9798{
9799 return io_uring_setup(entries, params);
9800}
9801
Jens Axboe66f4af92020-01-16 15:36:52 -07009802static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9803{
9804 struct io_uring_probe *p;
9805 size_t size;
9806 int i, ret;
9807
9808 size = struct_size(p, ops, nr_args);
9809 if (size == SIZE_MAX)
9810 return -EOVERFLOW;
9811 p = kzalloc(size, GFP_KERNEL);
9812 if (!p)
9813 return -ENOMEM;
9814
9815 ret = -EFAULT;
9816 if (copy_from_user(p, arg, size))
9817 goto out;
9818 ret = -EINVAL;
9819 if (memchr_inv(p, 0, size))
9820 goto out;
9821
9822 p->last_op = IORING_OP_LAST - 1;
9823 if (nr_args > IORING_OP_LAST)
9824 nr_args = IORING_OP_LAST;
9825
9826 for (i = 0; i < nr_args; i++) {
9827 p->ops[i].op = i;
9828 if (!io_op_defs[i].not_supported)
9829 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9830 }
9831 p->ops_len = i;
9832
9833 ret = 0;
9834 if (copy_to_user(arg, p, size))
9835 ret = -EFAULT;
9836out:
9837 kfree(p);
9838 return ret;
9839}
9840
Jens Axboe071698e2020-01-28 10:04:42 -07009841static int io_register_personality(struct io_ring_ctx *ctx)
9842{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009843 struct io_identity *id;
9844 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009845
Jens Axboe1e6fa522020-10-15 08:46:24 -06009846 id = kmalloc(sizeof(*id), GFP_KERNEL);
9847 if (unlikely(!id))
9848 return -ENOMEM;
9849
9850 io_init_identity(id);
9851 id->creds = get_current_cred();
9852
9853 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9854 if (ret < 0) {
9855 put_cred(id->creds);
9856 kfree(id);
9857 }
9858 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009859}
9860
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009861static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9862 unsigned int nr_args)
9863{
9864 struct io_uring_restriction *res;
9865 size_t size;
9866 int i, ret;
9867
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009868 /* Restrictions allowed only if rings started disabled */
9869 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9870 return -EBADFD;
9871
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009872 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009873 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009874 return -EBUSY;
9875
9876 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9877 return -EINVAL;
9878
9879 size = array_size(nr_args, sizeof(*res));
9880 if (size == SIZE_MAX)
9881 return -EOVERFLOW;
9882
9883 res = memdup_user(arg, size);
9884 if (IS_ERR(res))
9885 return PTR_ERR(res);
9886
9887 ret = 0;
9888
9889 for (i = 0; i < nr_args; i++) {
9890 switch (res[i].opcode) {
9891 case IORING_RESTRICTION_REGISTER_OP:
9892 if (res[i].register_op >= IORING_REGISTER_LAST) {
9893 ret = -EINVAL;
9894 goto out;
9895 }
9896
9897 __set_bit(res[i].register_op,
9898 ctx->restrictions.register_op);
9899 break;
9900 case IORING_RESTRICTION_SQE_OP:
9901 if (res[i].sqe_op >= IORING_OP_LAST) {
9902 ret = -EINVAL;
9903 goto out;
9904 }
9905
9906 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9907 break;
9908 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9909 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9910 break;
9911 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9912 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9913 break;
9914 default:
9915 ret = -EINVAL;
9916 goto out;
9917 }
9918 }
9919
9920out:
9921 /* Reset all restrictions if an error happened */
9922 if (ret != 0)
9923 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9924 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009925 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009926
9927 kfree(res);
9928 return ret;
9929}
9930
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009931static int io_register_enable_rings(struct io_ring_ctx *ctx)
9932{
9933 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9934 return -EBADFD;
9935
9936 if (ctx->restrictions.registered)
9937 ctx->restricted = 1;
9938
9939 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9940
9941 io_sq_offload_start(ctx);
9942
9943 return 0;
9944}
9945
Jens Axboe071698e2020-01-28 10:04:42 -07009946static bool io_register_op_must_quiesce(int op)
9947{
9948 switch (op) {
9949 case IORING_UNREGISTER_FILES:
9950 case IORING_REGISTER_FILES_UPDATE:
9951 case IORING_REGISTER_PROBE:
9952 case IORING_REGISTER_PERSONALITY:
9953 case IORING_UNREGISTER_PERSONALITY:
9954 return false;
9955 default:
9956 return true;
9957 }
9958}
9959
Jens Axboeedafcce2019-01-09 09:16:05 -07009960static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9961 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009962 __releases(ctx->uring_lock)
9963 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009964{
9965 int ret;
9966
Jens Axboe35fa71a2019-04-22 10:23:23 -06009967 /*
9968 * We're inside the ring mutex, if the ref is already dying, then
9969 * someone else killed the ctx or is already going through
9970 * io_uring_register().
9971 */
9972 if (percpu_ref_is_dying(&ctx->refs))
9973 return -ENXIO;
9974
Jens Axboe071698e2020-01-28 10:04:42 -07009975 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009976 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009977
Jens Axboe05f3fb32019-12-09 11:22:50 -07009978 /*
9979 * Drop uring mutex before waiting for references to exit. If
9980 * another thread is currently inside io_uring_enter() it might
9981 * need to grab the uring_lock to make progress. If we hold it
9982 * here across the drain wait, then we can deadlock. It's safe
9983 * to drop the mutex here, since no new references will come in
9984 * after we've killed the percpu ref.
9985 */
9986 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009987 do {
9988 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9989 if (!ret)
9990 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009991 ret = io_run_task_work_sig();
9992 if (ret < 0)
9993 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009994 } while (1);
9995
Jens Axboe05f3fb32019-12-09 11:22:50 -07009996 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009997
Jens Axboec1503682020-01-08 08:26:07 -07009998 if (ret) {
9999 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010000 goto out_quiesce;
10001 }
10002 }
10003
10004 if (ctx->restricted) {
10005 if (opcode >= IORING_REGISTER_LAST) {
10006 ret = -EINVAL;
10007 goto out;
10008 }
10009
10010 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10011 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010012 goto out;
10013 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010014 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010015
10016 switch (opcode) {
10017 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010018 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010019 break;
10020 case IORING_UNREGISTER_BUFFERS:
10021 ret = -EINVAL;
10022 if (arg || nr_args)
10023 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010024 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010025 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010026 case IORING_REGISTER_FILES:
10027 ret = io_sqe_files_register(ctx, arg, nr_args);
10028 break;
10029 case IORING_UNREGISTER_FILES:
10030 ret = -EINVAL;
10031 if (arg || nr_args)
10032 break;
10033 ret = io_sqe_files_unregister(ctx);
10034 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010035 case IORING_REGISTER_FILES_UPDATE:
10036 ret = io_sqe_files_update(ctx, arg, nr_args);
10037 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010038 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010039 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010040 ret = -EINVAL;
10041 if (nr_args != 1)
10042 break;
10043 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010044 if (ret)
10045 break;
10046 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10047 ctx->eventfd_async = 1;
10048 else
10049 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010050 break;
10051 case IORING_UNREGISTER_EVENTFD:
10052 ret = -EINVAL;
10053 if (arg || nr_args)
10054 break;
10055 ret = io_eventfd_unregister(ctx);
10056 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010057 case IORING_REGISTER_PROBE:
10058 ret = -EINVAL;
10059 if (!arg || nr_args > 256)
10060 break;
10061 ret = io_probe(ctx, arg, nr_args);
10062 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010063 case IORING_REGISTER_PERSONALITY:
10064 ret = -EINVAL;
10065 if (arg || nr_args)
10066 break;
10067 ret = io_register_personality(ctx);
10068 break;
10069 case IORING_UNREGISTER_PERSONALITY:
10070 ret = -EINVAL;
10071 if (arg)
10072 break;
10073 ret = io_unregister_personality(ctx, nr_args);
10074 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010075 case IORING_REGISTER_ENABLE_RINGS:
10076 ret = -EINVAL;
10077 if (arg || nr_args)
10078 break;
10079 ret = io_register_enable_rings(ctx);
10080 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010081 case IORING_REGISTER_RESTRICTIONS:
10082 ret = io_register_restrictions(ctx, arg, nr_args);
10083 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010084 default:
10085 ret = -EINVAL;
10086 break;
10087 }
10088
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010089out:
Jens Axboe071698e2020-01-28 10:04:42 -070010090 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010091 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010092 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010093out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010094 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010095 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010096 return ret;
10097}
10098
10099SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10100 void __user *, arg, unsigned int, nr_args)
10101{
10102 struct io_ring_ctx *ctx;
10103 long ret = -EBADF;
10104 struct fd f;
10105
10106 f = fdget(fd);
10107 if (!f.file)
10108 return -EBADF;
10109
10110 ret = -EOPNOTSUPP;
10111 if (f.file->f_op != &io_uring_fops)
10112 goto out_fput;
10113
10114 ctx = f.file->private_data;
10115
10116 mutex_lock(&ctx->uring_lock);
10117 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10118 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010119 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10120 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010121out_fput:
10122 fdput(f);
10123 return ret;
10124}
10125
Jens Axboe2b188cc2019-01-07 10:46:33 -070010126static int __init io_uring_init(void)
10127{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010128#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10129 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10130 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10131} while (0)
10132
10133#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10134 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10135 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10136 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10137 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10138 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10139 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10140 BUILD_BUG_SQE_ELEM(8, __u64, off);
10141 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10142 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010143 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010144 BUILD_BUG_SQE_ELEM(24, __u32, len);
10145 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10146 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10147 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10148 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010149 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10150 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010151 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10152 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10153 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10154 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10155 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10156 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10157 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10158 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010159 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010160 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10161 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10162 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010163 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010164
Jens Axboed3656342019-12-18 09:50:26 -070010165 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010166 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010167 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10168 return 0;
10169};
10170__initcall(io_uring_init);