blob: fe07af7561868c3f37b6e0c8507773112437cd36 [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 Begunkov6ff119a2021-02-10 00:03:18 +0000269#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000270#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000271
272struct io_comp_state {
273 unsigned int nr;
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000274 struct io_kiocb *reqs[IO_COMPL_BATCH];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000275};
276
277struct io_submit_state {
278 struct blk_plug plug;
279
280 /*
281 * io_kiocb alloc cache
282 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000283 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000284 unsigned int free_reqs;
285
286 bool plug_started;
287
288 /*
289 * Batch completion logic
290 */
291 struct io_comp_state comp;
292
293 /*
294 * File reference cache
295 */
296 struct file *file;
297 unsigned int fd;
298 unsigned int file_refs;
299 unsigned int ios_left;
300};
301
Jens Axboe2b188cc2019-01-07 10:46:33 -0700302struct io_ring_ctx {
303 struct {
304 struct percpu_ref refs;
305 } ____cacheline_aligned_in_smp;
306
307 struct {
308 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800309 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700310 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800311 unsigned int cq_overflow_flushed: 1;
312 unsigned int drain_next: 1;
313 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200314 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000315 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700316
Hristo Venev75b28af2019-08-26 17:23:46 +0000317 /*
318 * Ring buffer of indices into array of io_uring_sqe, which is
319 * mmapped by the application using the IORING_OFF_SQES offset.
320 *
321 * This indirection could e.g. be used to assign fixed
322 * io_uring_sqe entries to operations and only submit them to
323 * the queue when needed.
324 *
325 * The kernel modifies neither the indices array nor the entries
326 * array.
327 */
328 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329 unsigned cached_sq_head;
330 unsigned sq_entries;
331 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700332 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600333 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100334 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700335 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600336
337 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600338 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700339 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700340
Jens Axboead3eb2c2019-12-18 17:12:20 -0700341 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342 } ____cacheline_aligned_in_smp;
343
Hristo Venev75b28af2019-08-26 17:23:46 +0000344 struct io_rings *rings;
345
Jens Axboe2b188cc2019-01-07 10:46:33 -0700346 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600347 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600348
349 /*
350 * For SQPOLL usage - we hold a reference to the parent task, so we
351 * have access to the ->files
352 */
353 struct task_struct *sqo_task;
354
355 /* Only used for accounting purposes */
356 struct mm_struct *mm_account;
357
Dennis Zhou91d8f512020-09-16 13:41:05 -0700358#ifdef CONFIG_BLK_CGROUP
359 struct cgroup_subsys_state *sqo_blkcg_css;
360#endif
361
Jens Axboe534ca6d2020-09-02 13:52:19 -0600362 struct io_sq_data *sq_data; /* if using sq thread polling */
363
Jens Axboe90554202020-09-03 12:12:41 -0600364 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600365 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700366
Jens Axboe6b063142019-01-10 22:13:58 -0700367 /*
368 * If used, fixed file set. Writers must ensure that ->refs is dead,
369 * readers must ensure that ->refs is alive as long as the file* is
370 * used. Only updated through io_uring_register(2).
371 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000372 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700373 unsigned nr_user_files;
374
Jens Axboeedafcce2019-01-09 09:16:05 -0700375 /* if used, fixed mapped user buffers */
376 unsigned nr_user_bufs;
377 struct io_mapped_ubuf *user_bufs;
378
Jens Axboe2b188cc2019-01-07 10:46:33 -0700379 struct user_struct *user;
380
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700381 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700382
Jens Axboe4ea33a92020-10-15 13:46:44 -0600383#ifdef CONFIG_AUDIT
384 kuid_t loginuid;
385 unsigned int sessionid;
386#endif
387
Jens Axboe0f158b42020-05-14 17:18:39 -0600388 struct completion ref_comp;
389 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700390
391#if defined(CONFIG_UNIX)
392 struct socket *ring_sock;
393#endif
394
Jens Axboe5a2e7452020-02-23 16:23:11 -0700395 struct idr io_buffer_idr;
396
Jens Axboe071698e2020-01-28 10:04:42 -0700397 struct idr personality_idr;
398
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct {
400 unsigned cached_cq_tail;
401 unsigned cq_entries;
402 unsigned cq_mask;
403 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500404 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700405 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700406 struct wait_queue_head cq_wait;
407 struct fasync_struct *cq_fasync;
408 struct eventfd_ctx *cq_ev_fd;
409 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410
411 struct {
412 struct mutex uring_lock;
413 wait_queue_head_t wait;
414 } ____cacheline_aligned_in_smp;
415
416 struct {
417 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700418
Jens Axboedef596e2019-01-09 08:59:42 -0700419 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300420 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700421 * io_uring instances that don't use IORING_SETUP_SQPOLL.
422 * For SQPOLL, only the single threaded io_sq_thread() will
423 * manipulate the list, hence no extra locking is needed there.
424 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300425 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700426 struct hlist_head *cancel_hash;
427 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700428 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600429
430 spinlock_t inflight_lock;
431 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600433
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000434 struct delayed_work rsrc_put_work;
435 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000436 struct list_head rsrc_ref_list;
437 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600438
Jens Axboe85faa7b2020-04-09 18:14:00 -0600439 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200440 struct io_restriction restrictions;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000441 struct io_submit_state submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442};
443
Jens Axboe09bb8392019-03-13 12:39:28 -0600444/*
445 * First field must be the file pointer in all the
446 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
447 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700448struct io_poll_iocb {
449 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000450 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700451 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600452 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700453 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700454 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700455};
456
Pavel Begunkov018043b2020-10-27 23:17:18 +0000457struct io_poll_remove {
458 struct file *file;
459 u64 addr;
460};
461
Jens Axboeb5dba592019-12-11 14:02:38 -0700462struct io_close {
463 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700464 int fd;
465};
466
Jens Axboead8a48a2019-11-15 08:49:11 -0700467struct io_timeout_data {
468 struct io_kiocb *req;
469 struct hrtimer timer;
470 struct timespec64 ts;
471 enum hrtimer_mode mode;
472};
473
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700474struct io_accept {
475 struct file *file;
476 struct sockaddr __user *addr;
477 int __user *addr_len;
478 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600479 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700480};
481
482struct io_sync {
483 struct file *file;
484 loff_t len;
485 loff_t off;
486 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700487 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700488};
489
Jens Axboefbf23842019-12-17 18:45:56 -0700490struct io_cancel {
491 struct file *file;
492 u64 addr;
493};
494
Jens Axboeb29472e2019-12-17 18:50:29 -0700495struct io_timeout {
496 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300497 u32 off;
498 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300499 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000500 /* head of the link, used by linked timeouts only */
501 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700502};
503
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100504struct io_timeout_rem {
505 struct file *file;
506 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000507
508 /* timeout update */
509 struct timespec64 ts;
510 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100511};
512
Jens Axboe9adbd452019-12-20 08:45:55 -0700513struct io_rw {
514 /* NOTE: kiocb has the file as the first member, so don't do it here */
515 struct kiocb kiocb;
516 u64 addr;
517 u64 len;
518};
519
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700520struct io_connect {
521 struct file *file;
522 struct sockaddr __user *addr;
523 int addr_len;
524};
525
Jens Axboee47293f2019-12-20 08:58:21 -0700526struct io_sr_msg {
527 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700528 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300529 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700530 void __user *buf;
531 };
Jens Axboee47293f2019-12-20 08:58:21 -0700532 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700533 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700534 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700535 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700536};
537
Jens Axboe15b71ab2019-12-11 11:20:36 -0700538struct io_open {
539 struct file *file;
540 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700541 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700542 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600543 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700544};
545
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000546struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700547 struct file *file;
548 u64 arg;
549 u32 nr_args;
550 u32 offset;
551};
552
Jens Axboe4840e412019-12-25 22:03:45 -0700553struct io_fadvise {
554 struct file *file;
555 u64 offset;
556 u32 len;
557 u32 advice;
558};
559
Jens Axboec1ca7572019-12-25 22:18:28 -0700560struct io_madvise {
561 struct file *file;
562 u64 addr;
563 u32 len;
564 u32 advice;
565};
566
Jens Axboe3e4827b2020-01-08 15:18:09 -0700567struct io_epoll {
568 struct file *file;
569 int epfd;
570 int op;
571 int fd;
572 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573};
574
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300575struct io_splice {
576 struct file *file_out;
577 struct file *file_in;
578 loff_t off_out;
579 loff_t off_in;
580 u64 len;
581 unsigned int flags;
582};
583
Jens Axboeddf0322d2020-02-23 16:41:33 -0700584struct io_provide_buf {
585 struct file *file;
586 __u64 addr;
587 __s32 len;
588 __u32 bgid;
589 __u16 nbufs;
590 __u16 bid;
591};
592
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700593struct io_statx {
594 struct file *file;
595 int dfd;
596 unsigned int mask;
597 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700598 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700599 struct statx __user *buffer;
600};
601
Jens Axboe36f4fa62020-09-05 11:14:22 -0600602struct io_shutdown {
603 struct file *file;
604 int how;
605};
606
Jens Axboe80a261f2020-09-28 14:23:58 -0600607struct io_rename {
608 struct file *file;
609 int old_dfd;
610 int new_dfd;
611 struct filename *oldpath;
612 struct filename *newpath;
613 int flags;
614};
615
Jens Axboe14a11432020-09-28 14:27:37 -0600616struct io_unlink {
617 struct file *file;
618 int dfd;
619 int flags;
620 struct filename *filename;
621};
622
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300623struct io_completion {
624 struct file *file;
625 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300626 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300627};
628
Jens Axboef499a022019-12-02 16:28:46 -0700629struct io_async_connect {
630 struct sockaddr_storage address;
631};
632
Jens Axboe03b12302019-12-02 18:50:25 -0700633struct io_async_msghdr {
634 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000635 /* points to an allocated iov, if NULL we use fast_iov instead */
636 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700637 struct sockaddr __user *uaddr;
638 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700639 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700640};
641
Jens Axboef67676d2019-12-02 11:03:47 -0700642struct io_async_rw {
643 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600644 const struct iovec *free_iovec;
645 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600646 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600647 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700648};
649
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300650enum {
651 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
652 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
653 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
654 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
655 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700656 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300657
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300658 REQ_F_FAIL_LINK_BIT,
659 REQ_F_INFLIGHT_BIT,
660 REQ_F_CUR_POS_BIT,
661 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300662 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300663 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300664 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700665 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700666 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600667 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800668 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100669 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000670 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700671
672 /* not a real bit, just to check we're not overflowing the space */
673 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300674};
675
676enum {
677 /* ctx owns file */
678 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
679 /* drain existing IO first */
680 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
681 /* linked sqes */
682 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
683 /* doesn't sever on completion < 0 */
684 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
685 /* IOSQE_ASYNC */
686 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700687 /* IOSQE_BUFFER_SELECT */
688 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300689
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300690 /* fail rest of links */
691 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
692 /* on inflight list */
693 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
694 /* read/write uses file position */
695 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
696 /* must not punt to workers */
697 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100698 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300699 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300700 /* regular file */
701 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300702 /* needs cleanup */
703 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700704 /* already went through poll handler */
705 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700706 /* buffer already selected */
707 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600708 /* doesn't need file table for this request */
709 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800710 /* io_wq_work is initialized */
711 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100712 /* linked timeout is active, i.e. prepared by link's head */
713 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000714 /* completion is deferred through io_comp_state */
715 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700716};
717
718struct async_poll {
719 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600720 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721};
722
Jens Axboe09bb8392019-03-13 12:39:28 -0600723/*
724 * NOTE! Each of the iocb union members has the file pointer
725 * as the first entry in their struct definition. So you can
726 * access the file pointer through any of the sub-structs,
727 * or directly as just 'ki_filp' in this struct.
728 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700730 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600731 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700732 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700733 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000734 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700735 struct io_accept accept;
736 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700737 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700738 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100739 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700740 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700741 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700742 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700743 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000744 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700745 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700746 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700747 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300748 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700749 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700750 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600751 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600752 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600753 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300754 /* use only after cleaning per-op data, see io_clean_op() */
755 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700756 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* opcode allocated if it needs to store data for async defer */
759 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700760 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800761 /* polled IO has completed */
762 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700763
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700764 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300765 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700766
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300767 struct io_ring_ctx *ctx;
768 unsigned int flags;
769 refcount_t refs;
770 struct task_struct *task;
771 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000773 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000774 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700775
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300776 /*
777 * 1. used with ctx->iopoll_list with reads/writes
778 * 2. to track reqs with ->files (see io_op_def::file_table)
779 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300780 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300781 struct callback_head task_work;
782 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
783 struct hlist_node hash_node;
784 struct async_poll *apoll;
785 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786};
787
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300788struct io_defer_entry {
789 struct list_head list;
790 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300791 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300792};
793
Jens Axboed3656342019-12-18 09:50:26 -0700794struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700795 /* needs req->file assigned */
796 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700797 /* hash wq insertion if file is a regular file */
798 unsigned hash_reg_file : 1;
799 /* unbound wq insertion if file is a non-regular file */
800 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700801 /* opcode is not supported by this kernel */
802 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700803 /* set if opcode supports polled "wait" */
804 unsigned pollin : 1;
805 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700806 /* op supports buffer selection */
807 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700808 /* must always have async data allocated */
809 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600810 /* should block plug */
811 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700812 /* size of async data needed, if any */
813 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600814 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700815};
816
Jens Axboe09186822020-10-13 15:01:40 -0600817static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_NOP] = {},
819 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700820 .needs_file = 1,
821 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700822 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700823 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600825 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700826 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600827 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700830 .needs_file = 1,
831 .hash_reg_file = 1,
832 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700834 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600835 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700836 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600837 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
838 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600842 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700845 .needs_file = 1,
846 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700847 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600848 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700849 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600850 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700853 .needs_file = 1,
854 .hash_reg_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600857 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700858 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600859 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
860 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
864 .unbound_nonreg_file = 1,
865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_POLL_REMOVE] = {},
867 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600869 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700875 .needs_async_data = 1,
876 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000877 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700880 .needs_file = 1,
881 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700882 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700883 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700884 .needs_async_data = 1,
885 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000886 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700889 .needs_async_data = 1,
890 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600891 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700892 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000893 [IORING_OP_TIMEOUT_REMOVE] = {
894 /* used by timeout updates' prep() */
895 .work_flags = IO_WQ_WORK_MM,
896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700898 .needs_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700900 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600901 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_ASYNC_CANCEL] = {},
904 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600907 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700908 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300909 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700910 .needs_file = 1,
911 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700912 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700913 .needs_async_data = 1,
914 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600915 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700918 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600919 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600922 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600923 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700924 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600926 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600929 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700930 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300931 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600932 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
933 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700939 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600942 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700945 .needs_file = 1,
946 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700947 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600948 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700949 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600950 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
951 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700954 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600955 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600958 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700961 .needs_file = 1,
962 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700963 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600964 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700967 .needs_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700969 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700970 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600971 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600974 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600975 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700976 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700977 [IORING_OP_EPOLL_CTL] = {
978 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600979 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700980 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300981 [IORING_OP_SPLICE] = {
982 .needs_file = 1,
983 .hash_reg_file = 1,
984 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600985 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700986 },
987 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700988 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300989 [IORING_OP_TEE] = {
990 .needs_file = 1,
991 .hash_reg_file = 1,
992 .unbound_nonreg_file = 1,
993 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600994 [IORING_OP_SHUTDOWN] = {
995 .needs_file = 1,
996 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600997 [IORING_OP_RENAMEAT] = {
998 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
999 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1000 },
Jens Axboe14a11432020-09-28 14:27:37 -06001001 [IORING_OP_UNLINKAT] = {
1002 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1003 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1004 },
Jens Axboed3656342019-12-18 09:50:26 -07001005};
1006
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001007enum io_mem_account {
1008 ACCT_LOCKED,
1009 ACCT_PINNED,
1010};
1011
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001012static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1013 struct task_struct *task,
1014 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001015static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001016static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001017 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001018static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1019 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001020
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001021static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001022 unsigned int issue_flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001023static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001024static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001025static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001026static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001027static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001028static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001029static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001030static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001031 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001032 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001033static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001034static struct file *io_file_get(struct io_submit_state *state,
1035 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001036static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001037static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001038
Pavel Begunkov847595d2021-02-04 13:52:06 +00001039static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1040 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001041static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1042 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001043 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001044static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001045
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046static struct kmem_cache *req_cachep;
1047
Jens Axboe09186822020-10-13 15:01:40 -06001048static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049
1050struct sock *io_uring_get_socket(struct file *file)
1051{
1052#if defined(CONFIG_UNIX)
1053 if (file->f_op == &io_uring_fops) {
1054 struct io_ring_ctx *ctx = file->private_data;
1055
1056 return ctx->ring_sock->sk;
1057 }
1058#endif
1059 return NULL;
1060}
1061EXPORT_SYMBOL(io_uring_get_socket);
1062
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001063#define io_for_each_link(pos, head) \
1064 for (pos = (head); pos; pos = pos->link)
1065
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001066static inline void io_clean_op(struct io_kiocb *req)
1067{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001068 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001069 __io_clean_op(req);
1070}
1071
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001072static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001073{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001074 struct io_ring_ctx *ctx = req->ctx;
1075
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001076 if (!req->fixed_rsrc_refs) {
1077 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1078 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001079 }
1080}
1081
Pavel Begunkov08d23632020-11-06 13:00:22 +00001082static bool io_match_task(struct io_kiocb *head,
1083 struct task_struct *task,
1084 struct files_struct *files)
1085{
1086 struct io_kiocb *req;
1087
Jens Axboe84965ff2021-01-23 15:51:11 -07001088 if (task && head->task != task) {
1089 /* in terms of cancelation, always match if req task is dead */
1090 if (head->task->flags & PF_EXITING)
1091 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001092 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001093 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001094 if (!files)
1095 return true;
1096
1097 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001098 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1099 continue;
1100 if (req->file && req->file->f_op == &io_uring_fops)
1101 return true;
1102 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001103 req->work.identity->files == files)
1104 return true;
1105 }
1106 return false;
1107}
1108
Jens Axboe28cea78a2020-09-14 10:51:17 -06001109static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001110{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001111 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001112 struct mm_struct *mm = current->mm;
1113
1114 if (mm) {
1115 kthread_unuse_mm(mm);
1116 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001117 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001118 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001119 if (files) {
1120 struct nsproxy *nsproxy = current->nsproxy;
1121
1122 task_lock(current);
1123 current->files = NULL;
1124 current->nsproxy = NULL;
1125 task_unlock(current);
1126 put_files_struct(files);
1127 put_nsproxy(nsproxy);
1128 }
1129}
1130
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001131static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001132{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001133 if (current->flags & PF_EXITING)
1134 return -EFAULT;
1135
Jens Axboe28cea78a2020-09-14 10:51:17 -06001136 if (!current->files) {
1137 struct files_struct *files;
1138 struct nsproxy *nsproxy;
1139
1140 task_lock(ctx->sqo_task);
1141 files = ctx->sqo_task->files;
1142 if (!files) {
1143 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001144 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001145 }
1146 atomic_inc(&files->count);
1147 get_nsproxy(ctx->sqo_task->nsproxy);
1148 nsproxy = ctx->sqo_task->nsproxy;
1149 task_unlock(ctx->sqo_task);
1150
1151 task_lock(current);
1152 current->files = files;
1153 current->nsproxy = nsproxy;
1154 task_unlock(current);
1155 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001156 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001157}
1158
1159static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1160{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001161 struct mm_struct *mm;
1162
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001163 if (current->flags & PF_EXITING)
1164 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001165 if (current->mm)
1166 return 0;
1167
1168 /* Should never happen */
1169 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1170 return -EFAULT;
1171
1172 task_lock(ctx->sqo_task);
1173 mm = ctx->sqo_task->mm;
1174 if (unlikely(!mm || !mmget_not_zero(mm)))
1175 mm = NULL;
1176 task_unlock(ctx->sqo_task);
1177
1178 if (mm) {
1179 kthread_use_mm(mm);
1180 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001181 }
1182
Jens Axboe4b70cf92020-11-02 10:39:05 -07001183 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001184}
1185
Jens Axboe28cea78a2020-09-14 10:51:17 -06001186static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1187 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001188{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001189 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001190 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001191
1192 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001193 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001194 if (unlikely(ret))
1195 return ret;
1196 }
1197
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001198 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1199 ret = __io_sq_thread_acquire_files(ctx);
1200 if (unlikely(ret))
1201 return ret;
1202 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001203
1204 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001205}
1206
Dennis Zhou91d8f512020-09-16 13:41:05 -07001207static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1208 struct cgroup_subsys_state **cur_css)
1209
1210{
1211#ifdef CONFIG_BLK_CGROUP
1212 /* puts the old one when swapping */
1213 if (*cur_css != ctx->sqo_blkcg_css) {
1214 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1215 *cur_css = ctx->sqo_blkcg_css;
1216 }
1217#endif
1218}
1219
1220static void io_sq_thread_unassociate_blkcg(void)
1221{
1222#ifdef CONFIG_BLK_CGROUP
1223 kthread_associate_blkcg(NULL);
1224#endif
1225}
1226
Jens Axboec40f6372020-06-25 15:39:59 -06001227static inline void req_set_fail_links(struct io_kiocb *req)
1228{
1229 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1230 req->flags |= REQ_F_FAIL_LINK;
1231}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001232
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001233/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001234 * None of these are dereferenced, they are simply used to check if any of
1235 * them have changed. If we're under current and check they are still the
1236 * same, we're fine to grab references to them for actual out-of-line use.
1237 */
1238static void io_init_identity(struct io_identity *id)
1239{
1240 id->files = current->files;
1241 id->mm = current->mm;
1242#ifdef CONFIG_BLK_CGROUP
1243 rcu_read_lock();
1244 id->blkcg_css = blkcg_css();
1245 rcu_read_unlock();
1246#endif
1247 id->creds = current_cred();
1248 id->nsproxy = current->nsproxy;
1249 id->fs = current->fs;
1250 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001251#ifdef CONFIG_AUDIT
1252 id->loginuid = current->loginuid;
1253 id->sessionid = current->sessionid;
1254#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001255 refcount_set(&id->count, 1);
1256}
1257
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001258static inline void __io_req_init_async(struct io_kiocb *req)
1259{
1260 memset(&req->work, 0, sizeof(req->work));
1261 req->flags |= REQ_F_WORK_INITIALIZED;
1262}
1263
Jens Axboe1e6fa522020-10-15 08:46:24 -06001264/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001265 * Note: must call io_req_init_async() for the first time you
1266 * touch any members of io_wq_work.
1267 */
1268static inline void io_req_init_async(struct io_kiocb *req)
1269{
Jens Axboe500a3732020-10-15 17:38:03 -06001270 struct io_uring_task *tctx = current->io_uring;
1271
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001272 if (req->flags & REQ_F_WORK_INITIALIZED)
1273 return;
1274
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001275 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001276
1277 /* Grab a ref if this isn't our static identity */
1278 req->work.identity = tctx->identity;
1279 if (tctx->identity != &tctx->__identity)
1280 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001281}
1282
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1284{
1285 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1286
Jens Axboe0f158b42020-05-14 17:18:39 -06001287 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288}
1289
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001290static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1291{
1292 return !req->timeout.off;
1293}
1294
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1296{
1297 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001298 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299
1300 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1301 if (!ctx)
1302 return NULL;
1303
Jens Axboe78076bb2019-12-04 19:56:40 -07001304 /*
1305 * Use 5 bits less than the max cq entries, that should give us around
1306 * 32 entries per hash list if totally full and uniformly spread.
1307 */
1308 hash_bits = ilog2(p->cq_entries);
1309 hash_bits -= 5;
1310 if (hash_bits <= 0)
1311 hash_bits = 1;
1312 ctx->cancel_hash_bits = hash_bits;
1313 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1314 GFP_KERNEL);
1315 if (!ctx->cancel_hash)
1316 goto err;
1317 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1318
Roman Gushchin21482892019-05-07 10:01:48 -07001319 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001320 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1321 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322
1323 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001324 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001325 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001326 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001327 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001328 init_completion(&ctx->ref_comp);
1329 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001330 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001331 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001332 mutex_init(&ctx->uring_lock);
1333 init_waitqueue_head(&ctx->wait);
1334 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001335 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001336 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001337 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001338 spin_lock_init(&ctx->inflight_lock);
1339 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001340 spin_lock_init(&ctx->rsrc_ref_lock);
1341 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001342 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1343 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001344 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001345err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001346 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001347 kfree(ctx);
1348 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349}
1350
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001351static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001352{
Jens Axboe2bc99302020-07-09 09:43:27 -06001353 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1354 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001355
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001356 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001357 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001358 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001359
Bob Liu9d858b22019-11-13 18:06:25 +08001360 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001361}
1362
Jens Axboe5c3462c2020-10-15 09:02:33 -06001363static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001364{
Jens Axboe500a3732020-10-15 17:38:03 -06001365 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001366 return;
1367 if (refcount_dec_and_test(&req->work.identity->count))
1368 kfree(req->work.identity);
1369}
1370
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001371static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001372{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001373 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001374 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001375
Pavel Begunkove86d0042021-02-01 18:59:54 +00001376 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001377 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001378#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001379 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001380 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001381#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001382 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001383 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001384 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001385 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001386
Jens Axboe98447d62020-10-14 10:48:51 -06001387 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001388 if (--fs->users)
1389 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001390 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001391 if (fs)
1392 free_fs_struct(fs);
1393 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001394 if (req->work.flags & IO_WQ_WORK_FILES) {
1395 put_files_struct(req->work.identity->files);
1396 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001397 }
1398 if (req->flags & REQ_F_INFLIGHT) {
1399 struct io_ring_ctx *ctx = req->ctx;
1400 struct io_uring_task *tctx = req->task->io_uring;
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&ctx->inflight_lock, flags);
1404 list_del(&req->inflight_entry);
1405 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1406 req->flags &= ~REQ_F_INFLIGHT;
1407 if (atomic_read(&tctx->in_idle))
1408 wake_up(&tctx->wait);
1409 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001410
Pavel Begunkove86d0042021-02-01 18:59:54 +00001411 req->flags &= ~REQ_F_WORK_INITIALIZED;
1412 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1413 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001414 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001415}
1416
1417/*
1418 * Create a private copy of io_identity, since some fields don't match
1419 * the current context.
1420 */
1421static bool io_identity_cow(struct io_kiocb *req)
1422{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001423 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001424 const struct cred *creds = NULL;
1425 struct io_identity *id;
1426
1427 if (req->work.flags & IO_WQ_WORK_CREDS)
1428 creds = req->work.identity->creds;
1429
1430 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1431 if (unlikely(!id)) {
1432 req->work.flags |= IO_WQ_WORK_CANCEL;
1433 return false;
1434 }
1435
1436 /*
1437 * We can safely just re-init the creds we copied Either the field
1438 * matches the current one, or we haven't grabbed it yet. The only
1439 * exception is ->creds, through registered personalities, so handle
1440 * that one separately.
1441 */
1442 io_init_identity(id);
1443 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001444 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001445
1446 /* add one for this request */
1447 refcount_inc(&id->count);
1448
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001449 /* drop tctx and req identity references, if needed */
1450 if (tctx->identity != &tctx->__identity &&
1451 refcount_dec_and_test(&tctx->identity->count))
1452 kfree(tctx->identity);
1453 if (req->work.identity != &tctx->__identity &&
1454 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001455 kfree(req->work.identity);
1456
1457 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001458 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001459 return true;
1460}
1461
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001462static void io_req_track_inflight(struct io_kiocb *req)
1463{
1464 struct io_ring_ctx *ctx = req->ctx;
1465
1466 if (!(req->flags & REQ_F_INFLIGHT)) {
1467 io_req_init_async(req);
1468 req->flags |= REQ_F_INFLIGHT;
1469
1470 spin_lock_irq(&ctx->inflight_lock);
1471 list_add(&req->inflight_entry, &ctx->inflight_list);
1472 spin_unlock_irq(&ctx->inflight_lock);
1473 }
1474}
1475
Jens Axboe1e6fa522020-10-15 08:46:24 -06001476static bool io_grab_identity(struct io_kiocb *req)
1477{
1478 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001479 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001480
Jens Axboe69228332020-10-20 14:28:41 -06001481 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1482 if (id->fsize != rlimit(RLIMIT_FSIZE))
1483 return false;
1484 req->work.flags |= IO_WQ_WORK_FSIZE;
1485 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001486#ifdef CONFIG_BLK_CGROUP
1487 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1488 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1489 rcu_read_lock();
1490 if (id->blkcg_css != blkcg_css()) {
1491 rcu_read_unlock();
1492 return false;
1493 }
1494 /*
1495 * This should be rare, either the cgroup is dying or the task
1496 * is moving cgroups. Just punt to root for the handful of ios.
1497 */
1498 if (css_tryget_online(id->blkcg_css))
1499 req->work.flags |= IO_WQ_WORK_BLKCG;
1500 rcu_read_unlock();
1501 }
1502#endif
1503 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1504 if (id->creds != current_cred())
1505 return false;
1506 get_cred(id->creds);
1507 req->work.flags |= IO_WQ_WORK_CREDS;
1508 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001509#ifdef CONFIG_AUDIT
1510 if (!uid_eq(current->loginuid, id->loginuid) ||
1511 current->sessionid != id->sessionid)
1512 return false;
1513#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001514 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1515 (def->work_flags & IO_WQ_WORK_FS)) {
1516 if (current->fs != id->fs)
1517 return false;
1518 spin_lock(&id->fs->lock);
1519 if (!id->fs->in_exec) {
1520 id->fs->users++;
1521 req->work.flags |= IO_WQ_WORK_FS;
1522 } else {
1523 req->work.flags |= IO_WQ_WORK_CANCEL;
1524 }
1525 spin_unlock(&current->fs->lock);
1526 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001527 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1528 (def->work_flags & IO_WQ_WORK_FILES) &&
1529 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1530 if (id->files != current->files ||
1531 id->nsproxy != current->nsproxy)
1532 return false;
1533 atomic_inc(&id->files->count);
1534 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001535 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001536 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001537 }
Jens Axboe77788772020-12-29 10:50:46 -07001538 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1539 (def->work_flags & IO_WQ_WORK_MM)) {
1540 if (id->mm != current->mm)
1541 return false;
1542 mmgrab(id->mm);
1543 req->work.flags |= IO_WQ_WORK_MM;
1544 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001545
1546 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001547}
1548
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001549static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001550{
Jens Axboed3656342019-12-18 09:50:26 -07001551 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001552 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001553
Pavel Begunkov16d59802020-07-12 16:16:47 +03001554 io_req_init_async(req);
1555
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001556 if (req->flags & REQ_F_FORCE_ASYNC)
1557 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1558
Jens Axboed3656342019-12-18 09:50:26 -07001559 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001560 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001561 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001562 } else {
1563 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001564 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001565 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001566
Jens Axboe1e6fa522020-10-15 08:46:24 -06001567 /* if we fail grabbing identity, we must COW, regrab, and retry */
1568 if (io_grab_identity(req))
1569 return;
1570
1571 if (!io_identity_cow(req))
1572 return;
1573
1574 /* can't fail at this point */
1575 if (!io_grab_identity(req))
1576 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001577}
1578
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001579static void io_prep_async_link(struct io_kiocb *req)
1580{
1581 struct io_kiocb *cur;
1582
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001583 io_for_each_link(cur, req)
1584 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001585}
1586
Jens Axboe7271ef32020-08-10 09:55:22 -06001587static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001588{
Jackie Liua197f662019-11-08 08:09:12 -07001589 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001590 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001591
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001592 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1593 &req->work, req->flags);
1594 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001595 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001596}
1597
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001598static void io_queue_async_work(struct io_kiocb *req)
1599{
Jens Axboe7271ef32020-08-10 09:55:22 -06001600 struct io_kiocb *link;
1601
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001602 /* init ->work of the whole link before punting */
1603 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001604 link = __io_queue_async_work(req);
1605
1606 if (link)
1607 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001608}
1609
Jens Axboe5262f562019-09-17 12:26:57 -06001610static void io_kill_timeout(struct io_kiocb *req)
1611{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001612 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001613 int ret;
1614
Jens Axboee8c2bc12020-08-15 18:44:09 -07001615 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001616 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001617 atomic_set(&req->ctx->cq_timeouts,
1618 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001619 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001620 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001621 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001622 }
1623}
1624
Jens Axboe76e1b642020-09-26 15:05:03 -06001625/*
1626 * Returns true if we found and killed one or more timeouts
1627 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001628static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1629 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001630{
1631 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001632 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001633
1634 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001635 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001636 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001637 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001638 canceled++;
1639 }
Jens Axboef3606e32020-09-22 08:18:24 -06001640 }
Jens Axboe5262f562019-09-17 12:26:57 -06001641 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001642 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001643}
1644
Pavel Begunkov04518942020-05-26 20:34:05 +03001645static void __io_queue_deferred(struct io_ring_ctx *ctx)
1646{
1647 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001648 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1649 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001650
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001651 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001652 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001653 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001654 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001655 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001656 } while (!list_empty(&ctx->defer_list));
1657}
1658
Pavel Begunkov360428f2020-05-30 14:54:17 +03001659static void io_flush_timeouts(struct io_ring_ctx *ctx)
1660{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001661 u32 seq;
1662
1663 if (list_empty(&ctx->timeout_list))
1664 return;
1665
1666 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1667
1668 do {
1669 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001670 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001671 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001672
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001673 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001674 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001675
1676 /*
1677 * Since seq can easily wrap around over time, subtract
1678 * the last seq at which timeouts were flushed before comparing.
1679 * Assuming not more than 2^31-1 events have happened since,
1680 * these subtractions won't have wrapped, so we can check if
1681 * target is in [last_seq, current_seq] by comparing the two.
1682 */
1683 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1684 events_got = seq - ctx->cq_last_tm_flush;
1685 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001686 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001687
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001688 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001689 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001690 } while (!list_empty(&ctx->timeout_list));
1691
1692 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001693}
1694
Jens Axboede0617e2019-04-06 21:51:27 -06001695static void io_commit_cqring(struct io_ring_ctx *ctx)
1696{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001697 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001698
1699 /* order cqe stores with ring update */
1700 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001701
Pavel Begunkov04518942020-05-26 20:34:05 +03001702 if (unlikely(!list_empty(&ctx->defer_list)))
1703 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001704}
1705
Jens Axboe90554202020-09-03 12:12:41 -06001706static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1707{
1708 struct io_rings *r = ctx->rings;
1709
1710 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1711}
1712
Pavel Begunkov888aae22021-01-19 13:32:39 +00001713static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1714{
1715 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1716}
1717
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1719{
Hristo Venev75b28af2019-08-26 17:23:46 +00001720 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721 unsigned tail;
1722
Stefan Bühler115e12e2019-04-24 23:54:18 +02001723 /*
1724 * writes to the cq entry need to come after reading head; the
1725 * control dependency is enough as we're using WRITE_ONCE to
1726 * fill the cq entry
1727 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001728 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729 return NULL;
1730
Pavel Begunkov888aae22021-01-19 13:32:39 +00001731 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001732 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733}
1734
Jens Axboef2842ab2020-01-08 11:04:00 -07001735static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1736{
Jens Axboef0b493e2020-02-01 21:30:11 -07001737 if (!ctx->cq_ev_fd)
1738 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001739 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1740 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001741 if (!ctx->eventfd_async)
1742 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001743 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001744}
1745
Jens Axboeb41e9852020-02-17 09:52:41 -07001746static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001747{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001748 /* see waitqueue_active() comment */
1749 smp_mb();
1750
Jens Axboe8c838782019-03-12 15:48:16 -06001751 if (waitqueue_active(&ctx->wait))
1752 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001753 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1754 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001755 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001756 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001757 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001758 wake_up_interruptible(&ctx->cq_wait);
1759 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1760 }
Jens Axboe8c838782019-03-12 15:48:16 -06001761}
1762
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001763static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1764{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001765 /* see waitqueue_active() comment */
1766 smp_mb();
1767
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001768 if (ctx->flags & IORING_SETUP_SQPOLL) {
1769 if (waitqueue_active(&ctx->wait))
1770 wake_up(&ctx->wait);
1771 }
1772 if (io_should_trigger_evfd(ctx))
1773 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001774 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001775 wake_up_interruptible(&ctx->cq_wait);
1776 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1777 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001778}
1779
Jens Axboec4a2ed72019-11-21 21:01:26 -07001780/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001781static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1782 struct task_struct *tsk,
1783 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001785 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001786 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001787 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001788 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001789 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001790 LIST_HEAD(list);
1791
Pavel Begunkove23de152020-12-17 00:24:37 +00001792 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1793 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001794
Jens Axboeb18032b2021-01-24 16:58:56 -07001795 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001796 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001797 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001798 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001799 continue;
1800
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001801 cqe = io_get_cqring(ctx);
1802 if (!cqe && !force)
1803 break;
1804
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001805 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001806 if (cqe) {
1807 WRITE_ONCE(cqe->user_data, req->user_data);
1808 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001809 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001810 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001811 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001812 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001813 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001814 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001815 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001816 }
1817
Pavel Begunkov09e88402020-12-17 00:24:38 +00001818 all_flushed = list_empty(&ctx->cq_overflow_list);
1819 if (all_flushed) {
1820 clear_bit(0, &ctx->sq_check_overflow);
1821 clear_bit(0, &ctx->cq_check_overflow);
1822 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1823 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001824
Jens Axboeb18032b2021-01-24 16:58:56 -07001825 if (posted)
1826 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001827 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001828 if (posted)
1829 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001830
1831 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001832 req = list_first_entry(&list, struct io_kiocb, compl.list);
1833 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001834 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001835 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001836
Pavel Begunkov09e88402020-12-17 00:24:38 +00001837 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001838}
1839
Pavel Begunkov6c503152021-01-04 20:36:36 +00001840static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1841 struct task_struct *tsk,
1842 struct files_struct *files)
1843{
1844 if (test_bit(0, &ctx->cq_check_overflow)) {
1845 /* iopoll syncs against uring_lock, not completion_lock */
1846 if (ctx->flags & IORING_SETUP_IOPOLL)
1847 mutex_lock(&ctx->uring_lock);
1848 __io_cqring_overflow_flush(ctx, force, tsk, files);
1849 if (ctx->flags & IORING_SETUP_IOPOLL)
1850 mutex_unlock(&ctx->uring_lock);
1851 }
1852}
1853
Jens Axboebcda7ba2020-02-23 16:42:51 -07001854static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857 struct io_uring_cqe *cqe;
1858
Jens Axboe78e19bb2019-11-06 15:21:34 -07001859 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001860
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861 /*
1862 * If we can't get a cq entry, userspace overflowed the
1863 * submission (by quite a lot). Increment the overflow count in
1864 * the ring.
1865 */
1866 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001867 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001868 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001869 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001870 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001871 } else if (ctx->cq_overflow_flushed ||
1872 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001873 /*
1874 * If we're in ring overflow flush mode, or in task cancel mode,
1875 * then we cannot store the request for later flushing, we need
1876 * to drop it on the floor.
1877 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001878 ctx->cached_cq_overflow++;
1879 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001880 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001881 if (list_empty(&ctx->cq_overflow_list)) {
1882 set_bit(0, &ctx->sq_check_overflow);
1883 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001884 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001885 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001886 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001887 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001888 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001889 refcount_inc(&req->refs);
1890 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891 }
1892}
1893
Jens Axboebcda7ba2020-02-23 16:42:51 -07001894static void io_cqring_fill_event(struct io_kiocb *req, long res)
1895{
1896 __io_cqring_fill_event(req, res, 0);
1897}
1898
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001899static void io_req_complete_post(struct io_kiocb *req, long res,
1900 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001902 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 unsigned long flags;
1904
1905 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001906 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907 io_commit_cqring(ctx);
1908 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1909
Jens Axboe8c838782019-03-12 15:48:16 -06001910 io_cqring_ev_posted(ctx);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00001911}
1912
1913static inline void io_req_complete_nostate(struct io_kiocb *req, long res,
1914 unsigned int cflags)
1915{
1916 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001917 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001918}
1919
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001920static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001921 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001922{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001923 io_clean_op(req);
1924 req->result = res;
1925 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001926 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001927}
1928
Pavel Begunkov889fca72021-02-10 00:03:09 +00001929static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1930 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001931{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001932 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1933 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001934 else
Pavel Begunkov889fca72021-02-10 00:03:09 +00001935 io_req_complete_nostate(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001936}
1937
1938static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001939{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001940 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001941}
1942
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001943static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001945 struct io_submit_state *state = &ctx->submit_state;
1946
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001947 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
1948
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001949 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001950 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001951 int ret;
1952
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001953 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1954 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001955
1956 /*
1957 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1958 * retry single alloc to be on the safe side.
1959 */
1960 if (unlikely(ret <= 0)) {
1961 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1962 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001963 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001964 ret = 1;
1965 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001966 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967 }
1968
Pavel Begunkov291b2822020-09-30 22:57:01 +03001969 state->free_reqs--;
1970 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001971}
1972
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001973static inline void io_put_file(struct io_kiocb *req, struct file *file,
1974 bool fixed)
1975{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001976 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001977 fput(file);
1978}
1979
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001980static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001981{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001982 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001983
Jens Axboee8c2bc12020-08-15 18:44:09 -07001984 if (req->async_data)
1985 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001986 if (req->file)
1987 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001988 if (req->fixed_rsrc_refs)
1989 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001990 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001991}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001992
Pavel Begunkov7c660732021-01-25 11:42:21 +00001993static inline void io_put_task(struct task_struct *task, int nr)
1994{
1995 struct io_uring_task *tctx = task->io_uring;
1996
1997 percpu_counter_sub(&tctx->inflight, nr);
1998 if (unlikely(atomic_read(&tctx->in_idle)))
1999 wake_up(&tctx->wait);
2000 put_task_struct_many(task, nr);
2001}
2002
Pavel Begunkov216578e2020-10-13 09:44:00 +01002003static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002004{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002005 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002006
Pavel Begunkov216578e2020-10-13 09:44:00 +01002007 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002008 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002009
Pavel Begunkov3893f392021-02-10 00:03:15 +00002010 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002011 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002012}
2013
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002014static inline void io_remove_next_linked(struct io_kiocb *req)
2015{
2016 struct io_kiocb *nxt = req->link;
2017
2018 req->link = nxt->link;
2019 nxt->link = NULL;
2020}
2021
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002022static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002023{
Jackie Liua197f662019-11-08 08:09:12 -07002024 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002025 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002026 bool cancelled = false;
2027 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002028
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002029 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002030 link = req->link;
2031
Pavel Begunkov900fad42020-10-19 16:39:16 +01002032 /*
2033 * Can happen if a linked timeout fired and link had been like
2034 * req -> link t-out -> link t-out [-> ...]
2035 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002036 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2037 struct io_timeout_data *io = link->async_data;
2038 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002039
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002040 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002041 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002042 ret = hrtimer_try_to_cancel(&io->timer);
2043 if (ret != -1) {
2044 io_cqring_fill_event(link, -ECANCELED);
2045 io_commit_cqring(ctx);
2046 cancelled = true;
2047 }
2048 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002049 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002050 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002051
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002052 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002053 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002054 io_put_req(link);
2055 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002056}
2057
Jens Axboe4d7dd462019-11-20 13:03:52 -07002058
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002059static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002060{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002061 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002062 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002063 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002064
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002065 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002066 link = req->link;
2067 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002068
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002069 while (link) {
2070 nxt = link->link;
2071 link->link = NULL;
2072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002073 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002074 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002075
2076 /*
2077 * It's ok to free under spinlock as they're not linked anymore,
2078 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2079 * work.fs->lock.
2080 */
2081 if (link->flags & REQ_F_WORK_INITIALIZED)
2082 io_put_req_deferred(link, 2);
2083 else
2084 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002085 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002086 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002087 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002088 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002089
Jens Axboe2665abf2019-11-05 12:40:47 -07002090 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002091}
2092
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002093static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002094{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002095 if (req->flags & REQ_F_LINK_TIMEOUT)
2096 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002097
Jens Axboe9e645e112019-05-10 16:07:28 -06002098 /*
2099 * If LINK is set, we have dependent requests in this chain. If we
2100 * didn't fail this request, queue the first one up, moving any other
2101 * dependencies to the next request. In case of failure, fail the rest
2102 * of the chain.
2103 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002104 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2105 struct io_kiocb *nxt = req->link;
2106
2107 req->link = NULL;
2108 return nxt;
2109 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002110 io_fail_links(req);
2111 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002112}
Jens Axboe2665abf2019-11-05 12:40:47 -07002113
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002114static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002115{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002116 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002117 return NULL;
2118 return __io_req_find_next(req);
2119}
2120
Jens Axboe355fb9e2020-10-22 20:19:35 -06002121static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002122{
2123 struct task_struct *tsk = req->task;
2124 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002125 enum task_work_notify_mode notify;
2126 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002127
Jens Axboe6200b0a2020-09-13 14:38:30 -06002128 if (tsk->flags & PF_EXITING)
2129 return -ESRCH;
2130
Jens Axboec2c4c832020-07-01 15:37:11 -06002131 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002132 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2133 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2134 * processing task_work. There's no reliable way to tell if TWA_RESUME
2135 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002136 */
Jens Axboe91989c72020-10-16 09:02:26 -06002137 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002138 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002139 notify = TWA_SIGNAL;
2140
Jens Axboe87c43112020-09-30 21:00:14 -06002141 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002142 if (!ret)
2143 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002144
Jens Axboec2c4c832020-07-01 15:37:11 -06002145 return ret;
2146}
2147
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002148static void io_req_task_work_add_fallback(struct io_kiocb *req,
2149 void (*cb)(struct callback_head *))
2150{
2151 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2152
2153 init_task_work(&req->task_work, cb);
2154 task_work_add(tsk, &req->task_work, TWA_NONE);
2155 wake_up_process(tsk);
2156}
2157
Jens Axboec40f6372020-06-25 15:39:59 -06002158static void __io_req_task_cancel(struct io_kiocb *req, int error)
2159{
2160 struct io_ring_ctx *ctx = req->ctx;
2161
2162 spin_lock_irq(&ctx->completion_lock);
2163 io_cqring_fill_event(req, error);
2164 io_commit_cqring(ctx);
2165 spin_unlock_irq(&ctx->completion_lock);
2166
2167 io_cqring_ev_posted(ctx);
2168 req_set_fail_links(req);
2169 io_double_put_req(req);
2170}
2171
2172static void io_req_task_cancel(struct callback_head *cb)
2173{
2174 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002175 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002176
2177 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002178 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002179}
2180
2181static void __io_req_task_submit(struct io_kiocb *req)
2182{
2183 struct io_ring_ctx *ctx = req->ctx;
2184
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002185 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002186 if (!ctx->sqo_dead &&
2187 !__io_sq_thread_acquire_mm(ctx) &&
2188 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002189 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002190 else
Jens Axboec40f6372020-06-25 15:39:59 -06002191 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002192 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002193}
2194
Jens Axboec40f6372020-06-25 15:39:59 -06002195static void io_req_task_submit(struct callback_head *cb)
2196{
2197 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002198 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002199
2200 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002201 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002202}
2203
2204static void io_req_task_queue(struct io_kiocb *req)
2205{
Jens Axboec40f6372020-06-25 15:39:59 -06002206 int ret;
2207
2208 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002209 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002210
Jens Axboe355fb9e2020-10-22 20:19:35 -06002211 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002212 if (unlikely(ret))
2213 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002214}
2215
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002216static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002217{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002218 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002219
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002220 if (nxt)
2221 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002222}
2223
Jens Axboe9e645e112019-05-10 16:07:28 -06002224static void io_free_req(struct io_kiocb *req)
2225{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002226 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002227 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002228}
2229
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002230struct req_batch {
2231 void *reqs[IO_IOPOLL_BATCH];
2232 int to_free;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002233 int ctx_refs;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002234
2235 struct task_struct *task;
2236 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002237};
2238
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002239static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002240{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002241 rb->to_free = 0;
2242 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002243 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002244 rb->task = NULL;
2245}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002246
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002247static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2248 struct req_batch *rb)
2249{
2250 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002251 rb->to_free = 0;
2252}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002253
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002254static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2255 struct req_batch *rb)
2256{
2257 if (rb->to_free)
2258 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002259 if (rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002260 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002261 rb->task = NULL;
2262 }
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002263 if (rb->ctx_refs)
2264 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002265}
2266
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002267static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2268 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002269{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002270 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002271
Jens Axboee3bc8e92020-09-24 08:45:57 -06002272 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002273 if (rb->task)
2274 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002275 rb->task = req->task;
2276 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002277 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002278 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002279 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002280
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002281 io_dismantle_req(req);
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002282 if (state->free_reqs != ARRAY_SIZE(state->reqs)) {
2283 state->reqs[state->free_reqs++] = req;
2284 } else {
2285 rb->reqs[rb->to_free++] = req;
2286 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2287 __io_req_free_batch_flush(req->ctx, rb);
2288 }
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002289}
2290
Pavel Begunkov905c1722021-02-10 00:03:14 +00002291static void io_submit_flush_completions(struct io_comp_state *cs,
2292 struct io_ring_ctx *ctx)
2293{
2294 int i, nr = cs->nr;
2295 struct io_kiocb *req;
2296 struct req_batch rb;
2297
2298 io_init_req_batch(&rb);
2299 spin_lock_irq(&ctx->completion_lock);
2300 for (i = 0; i < nr; i++) {
2301 req = cs->reqs[i];
2302 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2303 }
2304 io_commit_cqring(ctx);
2305 spin_unlock_irq(&ctx->completion_lock);
2306
2307 io_cqring_ev_posted(ctx);
2308 for (i = 0; i < nr; i++) {
2309 req = cs->reqs[i];
2310
2311 /* submission and completion refs */
2312 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002313 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002314 }
2315
2316 io_req_free_batch_finish(ctx, &rb);
2317 cs->nr = 0;
2318}
2319
Jens Axboeba816ad2019-09-28 11:36:45 -06002320/*
2321 * Drop reference to request, return next in chain (if there is one) if this
2322 * was the last reference to this request.
2323 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002324static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002325{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002326 struct io_kiocb *nxt = NULL;
2327
Jens Axboe2a44f462020-02-25 13:25:41 -07002328 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002329 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002330 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002331 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002332 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333}
2334
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335static void io_put_req(struct io_kiocb *req)
2336{
Jens Axboedef596e2019-01-09 08:59:42 -07002337 if (refcount_dec_and_test(&req->refs))
2338 io_free_req(req);
2339}
2340
Pavel Begunkov216578e2020-10-13 09:44:00 +01002341static void io_put_req_deferred_cb(struct callback_head *cb)
2342{
2343 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2344
2345 io_free_req(req);
2346}
2347
2348static void io_free_req_deferred(struct io_kiocb *req)
2349{
2350 int ret;
2351
2352 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002353 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002354 if (unlikely(ret))
2355 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002356}
2357
2358static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2359{
2360 if (refcount_sub_and_test(refs, &req->refs))
2361 io_free_req_deferred(req);
2362}
2363
Jens Axboe978db572019-11-14 22:39:04 -07002364static void io_double_put_req(struct io_kiocb *req)
2365{
2366 /* drop both submit and complete references */
2367 if (refcount_sub_and_test(2, &req->refs))
2368 io_free_req(req);
2369}
2370
Pavel Begunkov6c503152021-01-04 20:36:36 +00002371static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002372{
2373 /* See comment at the top of this file */
2374 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002375 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002376}
2377
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002378static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2379{
2380 struct io_rings *rings = ctx->rings;
2381
2382 /* make sure SQ entry isn't read before tail */
2383 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2384}
2385
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002386static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002387{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002388 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002389
Jens Axboebcda7ba2020-02-23 16:42:51 -07002390 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2391 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002392 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002393 kfree(kbuf);
2394 return cflags;
2395}
2396
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002397static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2398{
2399 struct io_buffer *kbuf;
2400
2401 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2402 return io_put_kbuf(req, kbuf);
2403}
2404
Jens Axboe4c6e2772020-07-01 11:29:10 -06002405static inline bool io_run_task_work(void)
2406{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002407 /*
2408 * Not safe to run on exiting task, and the task_work handling will
2409 * not add work to such a task.
2410 */
2411 if (unlikely(current->flags & PF_EXITING))
2412 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002413 if (current->task_works) {
2414 __set_current_state(TASK_RUNNING);
2415 task_work_run();
2416 return true;
2417 }
2418
2419 return false;
2420}
2421
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002422static void io_iopoll_queue(struct list_head *again)
2423{
2424 struct io_kiocb *req;
2425
2426 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002427 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2428 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002429 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002430 } while (!list_empty(again));
2431}
2432
Jens Axboedef596e2019-01-09 08:59:42 -07002433/*
2434 * Find and free completed poll iocbs
2435 */
2436static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2437 struct list_head *done)
2438{
Jens Axboe8237e042019-12-28 10:48:22 -07002439 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002440 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002441 LIST_HEAD(again);
2442
2443 /* order with ->result store in io_complete_rw_iopoll() */
2444 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002445
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002446 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002447 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002448 int cflags = 0;
2449
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002450 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002451 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002452 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002453 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002454 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002455 continue;
2456 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002457 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002458
Jens Axboebcda7ba2020-02-23 16:42:51 -07002459 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002460 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002461
2462 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002463 (*nr_events)++;
2464
Pavel Begunkovc3524382020-06-28 12:52:32 +03002465 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002466 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002467 }
Jens Axboedef596e2019-01-09 08:59:42 -07002468
Jens Axboe09bb8392019-03-13 12:39:28 -06002469 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002470 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002471 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002472
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002473 if (!list_empty(&again))
2474 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002475}
2476
Jens Axboedef596e2019-01-09 08:59:42 -07002477static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2478 long min)
2479{
2480 struct io_kiocb *req, *tmp;
2481 LIST_HEAD(done);
2482 bool spin;
2483 int ret;
2484
2485 /*
2486 * Only spin for completions if we don't have multiple devices hanging
2487 * off our complete list, and we're under the requested amount.
2488 */
2489 spin = !ctx->poll_multi_file && *nr_events < min;
2490
2491 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002492 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002493 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002494
2495 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002496 * Move completed and retryable entries to our local lists.
2497 * If we find a request that requires polling, break out
2498 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002499 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002500 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002501 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002502 continue;
2503 }
2504 if (!list_empty(&done))
2505 break;
2506
2507 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2508 if (ret < 0)
2509 break;
2510
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002511 /* iopoll may have completed current req */
2512 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002513 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002514
Jens Axboedef596e2019-01-09 08:59:42 -07002515 if (ret && spin)
2516 spin = false;
2517 ret = 0;
2518 }
2519
2520 if (!list_empty(&done))
2521 io_iopoll_complete(ctx, nr_events, &done);
2522
2523 return ret;
2524}
2525
2526/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002527 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002528 * non-spinning poll check - we'll still enter the driver poll loop, but only
2529 * as a non-spinning completion check.
2530 */
2531static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2532 long min)
2533{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002534 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002535 int ret;
2536
2537 ret = io_do_iopoll(ctx, nr_events, min);
2538 if (ret < 0)
2539 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002540 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002541 return 0;
2542 }
2543
2544 return 1;
2545}
2546
2547/*
2548 * We can't just wait for polled events to come to us, we have to actively
2549 * find and complete them.
2550 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002551static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002552{
2553 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2554 return;
2555
2556 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002557 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002558 unsigned int nr_events = 0;
2559
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002560 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002561
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002562 /* let it sleep and repeat later if can't complete a request */
2563 if (nr_events == 0)
2564 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002565 /*
2566 * Ensure we allow local-to-the-cpu processing to take place,
2567 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002568 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002569 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002570 if (need_resched()) {
2571 mutex_unlock(&ctx->uring_lock);
2572 cond_resched();
2573 mutex_lock(&ctx->uring_lock);
2574 }
Jens Axboedef596e2019-01-09 08:59:42 -07002575 }
2576 mutex_unlock(&ctx->uring_lock);
2577}
2578
Pavel Begunkov7668b922020-07-07 16:36:21 +03002579static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002580{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002581 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002582 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002583
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002584 /*
2585 * We disallow the app entering submit/complete with polling, but we
2586 * still need to lock the ring to prevent racing with polled issue
2587 * that got punted to a workqueue.
2588 */
2589 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002590 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002591 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002592 * Don't enter poll loop if we already have events pending.
2593 * If we do, we can potentially be spinning for commands that
2594 * already triggered a CQE (eg in error).
2595 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002596 if (test_bit(0, &ctx->cq_check_overflow))
2597 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2598 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002599 break;
2600
2601 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002602 * If a submit got punted to a workqueue, we can have the
2603 * application entering polling for a command before it gets
2604 * issued. That app will hold the uring_lock for the duration
2605 * of the poll right here, so we need to take a breather every
2606 * now and then to ensure that the issue has a chance to add
2607 * the poll to the issued list. Otherwise we can spin here
2608 * forever, while the workqueue is stuck trying to acquire the
2609 * very same mutex.
2610 */
2611 if (!(++iters & 7)) {
2612 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002613 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002614 mutex_lock(&ctx->uring_lock);
2615 }
2616
Pavel Begunkov7668b922020-07-07 16:36:21 +03002617 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002618 if (ret <= 0)
2619 break;
2620 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002621 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002622
Jens Axboe500f9fb2019-08-19 12:15:59 -06002623 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002624 return ret;
2625}
2626
Jens Axboe491381ce2019-10-17 09:20:46 -06002627static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628{
Jens Axboe491381ce2019-10-17 09:20:46 -06002629 /*
2630 * Tell lockdep we inherited freeze protection from submission
2631 * thread.
2632 */
2633 if (req->flags & REQ_F_ISREG) {
2634 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635
Jens Axboe491381ce2019-10-17 09:20:46 -06002636 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002638 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639}
2640
Jens Axboea1d7c392020-06-22 11:09:46 -06002641static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002642 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643{
Jens Axboe9adbd452019-12-20 08:45:55 -07002644 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002645 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646
Jens Axboe491381ce2019-10-17 09:20:46 -06002647 if (kiocb->ki_flags & IOCB_WRITE)
2648 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002650 if (res != req->result)
2651 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002652 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002653 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002654 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002655}
2656
Jens Axboeb63534c2020-06-04 11:28:00 -06002657#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002658static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002659{
2660 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002661 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002662 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002663
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002664 /* already prepared */
2665 if (req->async_data)
2666 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002667
2668 switch (req->opcode) {
2669 case IORING_OP_READV:
2670 case IORING_OP_READ_FIXED:
2671 case IORING_OP_READ:
2672 rw = READ;
2673 break;
2674 case IORING_OP_WRITEV:
2675 case IORING_OP_WRITE_FIXED:
2676 case IORING_OP_WRITE:
2677 rw = WRITE;
2678 break;
2679 default:
2680 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2681 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002682 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002683 }
2684
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002685 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2686 if (ret < 0)
2687 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002688 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002689}
Jens Axboeb63534c2020-06-04 11:28:00 -06002690#endif
2691
2692static bool io_rw_reissue(struct io_kiocb *req, long res)
2693{
2694#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002695 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002696 int ret;
2697
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002698 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002699 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002700 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002701 if (!S_ISBLK(mode) && !S_ISREG(mode))
2702 return false;
2703 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002704 return false;
2705
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002706 lockdep_assert_held(&req->ctx->uring_lock);
2707
Jens Axboe28cea78a2020-09-14 10:51:17 -06002708 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002709
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002710 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002711 refcount_inc(&req->refs);
2712 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002713 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002714 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002715 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002716#endif
2717 return false;
2718}
2719
Jens Axboea1d7c392020-06-22 11:09:46 -06002720static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002721 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002722{
2723 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002724 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002725}
2726
2727static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2728{
Jens Axboe9adbd452019-12-20 08:45:55 -07002729 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002730
Pavel Begunkov889fca72021-02-10 00:03:09 +00002731 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002732}
2733
Jens Axboedef596e2019-01-09 08:59:42 -07002734static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2735{
Jens Axboe9adbd452019-12-20 08:45:55 -07002736 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002737
Jens Axboe491381ce2019-10-17 09:20:46 -06002738 if (kiocb->ki_flags & IOCB_WRITE)
2739 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002740
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002741 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002742 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002743
2744 WRITE_ONCE(req->result, res);
2745 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002746 smp_wmb();
2747 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002748}
2749
2750/*
2751 * After the iocb has been issued, it's safe to be found on the poll list.
2752 * Adding the kiocb to the list AFTER submission ensures that we don't
2753 * find it from a io_iopoll_getevents() thread before the issuer is done
2754 * accessing the kiocb cookie.
2755 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002756static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002757{
2758 struct io_ring_ctx *ctx = req->ctx;
2759
2760 /*
2761 * Track whether we have multiple files in our lists. This will impact
2762 * how we do polling eventually, not spinning if we're on potentially
2763 * different devices.
2764 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002765 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002766 ctx->poll_multi_file = false;
2767 } else if (!ctx->poll_multi_file) {
2768 struct io_kiocb *list_req;
2769
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002770 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002771 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002772 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002773 ctx->poll_multi_file = true;
2774 }
2775
2776 /*
2777 * For fast devices, IO may have already completed. If it has, add
2778 * it to the front so we find it first.
2779 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002780 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002781 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002782 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002783 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002784
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002785 /*
2786 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2787 * task context or in io worker task context. If current task context is
2788 * sq thread, we don't need to check whether should wake up sq thread.
2789 */
2790 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002791 wq_has_sleeper(&ctx->sq_data->wait))
2792 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002793}
2794
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002795static inline void io_state_file_put(struct io_submit_state *state)
2796{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002797 if (state->file_refs) {
2798 fput_many(state->file, state->file_refs);
2799 state->file_refs = 0;
2800 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002801}
2802
2803/*
2804 * Get as many references to a file as we have IOs left in this submission,
2805 * assuming most submissions are for one file, or at least that each file
2806 * has more than one submission.
2807 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002808static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002809{
2810 if (!state)
2811 return fget(fd);
2812
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002813 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002814 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002815 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002816 return state->file;
2817 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002818 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002819 }
2820 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002821 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002822 return NULL;
2823
2824 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002825 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002826 return state->file;
2827}
2828
Jens Axboe4503b762020-06-01 10:00:27 -06002829static bool io_bdev_nowait(struct block_device *bdev)
2830{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002831 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002832}
2833
Jens Axboe2b188cc2019-01-07 10:46:33 -07002834/*
2835 * If we tracked the file through the SCM inflight mechanism, we could support
2836 * any file. For now, just ensure that anything potentially problematic is done
2837 * inline.
2838 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002839static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002840{
2841 umode_t mode = file_inode(file)->i_mode;
2842
Jens Axboe4503b762020-06-01 10:00:27 -06002843 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002844 if (IS_ENABLED(CONFIG_BLOCK) &&
2845 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002846 return true;
2847 return false;
2848 }
2849 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002851 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002852 if (IS_ENABLED(CONFIG_BLOCK) &&
2853 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002854 file->f_op != &io_uring_fops)
2855 return true;
2856 return false;
2857 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002858
Jens Axboec5b85622020-06-09 19:23:05 -06002859 /* any ->read/write should understand O_NONBLOCK */
2860 if (file->f_flags & O_NONBLOCK)
2861 return true;
2862
Jens Axboeaf197f52020-04-28 13:15:06 -06002863 if (!(file->f_mode & FMODE_NOWAIT))
2864 return false;
2865
2866 if (rw == READ)
2867 return file->f_op->read_iter != NULL;
2868
2869 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870}
2871
Pavel Begunkova88fc402020-09-30 22:57:53 +03002872static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002873{
Jens Axboedef596e2019-01-09 08:59:42 -07002874 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002875 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002876 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002877 unsigned ioprio;
2878 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002879
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002880 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002881 req->flags |= REQ_F_ISREG;
2882
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002884 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002885 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002886 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002887 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002889 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2890 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2891 if (unlikely(ret))
2892 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002893
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002894 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2895 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2896 req->flags |= REQ_F_NOWAIT;
2897
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898 ioprio = READ_ONCE(sqe->ioprio);
2899 if (ioprio) {
2900 ret = ioprio_check_cap(ioprio);
2901 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002902 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002903
2904 kiocb->ki_ioprio = ioprio;
2905 } else
2906 kiocb->ki_ioprio = get_current_ioprio();
2907
Jens Axboedef596e2019-01-09 08:59:42 -07002908 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002909 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2910 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002911 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002912
Jens Axboedef596e2019-01-09 08:59:42 -07002913 kiocb->ki_flags |= IOCB_HIPRI;
2914 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002915 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002916 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002917 if (kiocb->ki_flags & IOCB_HIPRI)
2918 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002919 kiocb->ki_complete = io_complete_rw;
2920 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002921
Jens Axboe3529d8c2019-12-19 18:24:38 -07002922 req->rw.addr = READ_ONCE(sqe->addr);
2923 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002924 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002925 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926}
2927
2928static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2929{
2930 switch (ret) {
2931 case -EIOCBQUEUED:
2932 break;
2933 case -ERESTARTSYS:
2934 case -ERESTARTNOINTR:
2935 case -ERESTARTNOHAND:
2936 case -ERESTART_RESTARTBLOCK:
2937 /*
2938 * We can't just restart the syscall, since previously
2939 * submitted sqes may already be in progress. Just fail this
2940 * IO with EINTR.
2941 */
2942 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002943 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944 default:
2945 kiocb->ki_complete(kiocb, ret, 0);
2946 }
2947}
2948
Jens Axboea1d7c392020-06-22 11:09:46 -06002949static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002950 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002951{
Jens Axboeba042912019-12-25 16:33:42 -07002952 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002953 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002954
Jens Axboe227c0c92020-08-13 11:51:40 -06002955 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002956 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002957 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002958 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002959 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002960 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002961 }
2962
Jens Axboeba042912019-12-25 16:33:42 -07002963 if (req->flags & REQ_F_CUR_POS)
2964 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002965 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002966 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002967 else
2968 io_rw_done(kiocb, ret);
2969}
2970
Pavel Begunkov847595d2021-02-04 13:52:06 +00002971static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002972{
Jens Axboe9adbd452019-12-20 08:45:55 -07002973 struct io_ring_ctx *ctx = req->ctx;
2974 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002975 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002976 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002977 size_t offset;
2978 u64 buf_addr;
2979
Jens Axboeedafcce2019-01-09 09:16:05 -07002980 if (unlikely(buf_index >= ctx->nr_user_bufs))
2981 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002982 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2983 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002984 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002985
2986 /* overflow */
2987 if (buf_addr + len < buf_addr)
2988 return -EFAULT;
2989 /* not inside the mapped region */
2990 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2991 return -EFAULT;
2992
2993 /*
2994 * May not be a start of buffer, set size appropriately
2995 * and advance us to the beginning.
2996 */
2997 offset = buf_addr - imu->ubuf;
2998 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002999
3000 if (offset) {
3001 /*
3002 * Don't use iov_iter_advance() here, as it's really slow for
3003 * using the latter parts of a big fixed buffer - it iterates
3004 * over each segment manually. We can cheat a bit here, because
3005 * we know that:
3006 *
3007 * 1) it's a BVEC iter, we set it up
3008 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3009 * first and last bvec
3010 *
3011 * So just find our index, and adjust the iterator afterwards.
3012 * If the offset is within the first bvec (or the whole first
3013 * bvec, just use iov_iter_advance(). This makes it easier
3014 * since we can just skip the first segment, which may not
3015 * be PAGE_SIZE aligned.
3016 */
3017 const struct bio_vec *bvec = imu->bvec;
3018
3019 if (offset <= bvec->bv_len) {
3020 iov_iter_advance(iter, offset);
3021 } else {
3022 unsigned long seg_skip;
3023
3024 /* skip first vec */
3025 offset -= bvec->bv_len;
3026 seg_skip = 1 + (offset >> PAGE_SHIFT);
3027
3028 iter->bvec = bvec + seg_skip;
3029 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003030 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003031 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003032 }
3033 }
3034
Pavel Begunkov847595d2021-02-04 13:52:06 +00003035 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003036}
3037
Jens Axboebcda7ba2020-02-23 16:42:51 -07003038static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3039{
3040 if (needs_lock)
3041 mutex_unlock(&ctx->uring_lock);
3042}
3043
3044static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3045{
3046 /*
3047 * "Normal" inline submissions always hold the uring_lock, since we
3048 * grab it from the system call. Same is true for the SQPOLL offload.
3049 * The only exception is when we've detached the request and issue it
3050 * from an async worker thread, grab the lock for that case.
3051 */
3052 if (needs_lock)
3053 mutex_lock(&ctx->uring_lock);
3054}
3055
3056static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3057 int bgid, struct io_buffer *kbuf,
3058 bool needs_lock)
3059{
3060 struct io_buffer *head;
3061
3062 if (req->flags & REQ_F_BUFFER_SELECTED)
3063 return kbuf;
3064
3065 io_ring_submit_lock(req->ctx, needs_lock);
3066
3067 lockdep_assert_held(&req->ctx->uring_lock);
3068
3069 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3070 if (head) {
3071 if (!list_empty(&head->list)) {
3072 kbuf = list_last_entry(&head->list, struct io_buffer,
3073 list);
3074 list_del(&kbuf->list);
3075 } else {
3076 kbuf = head;
3077 idr_remove(&req->ctx->io_buffer_idr, bgid);
3078 }
3079 if (*len > kbuf->len)
3080 *len = kbuf->len;
3081 } else {
3082 kbuf = ERR_PTR(-ENOBUFS);
3083 }
3084
3085 io_ring_submit_unlock(req->ctx, needs_lock);
3086
3087 return kbuf;
3088}
3089
Jens Axboe4d954c22020-02-27 07:31:19 -07003090static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3091 bool needs_lock)
3092{
3093 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003094 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003095
3096 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003097 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003098 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3099 if (IS_ERR(kbuf))
3100 return kbuf;
3101 req->rw.addr = (u64) (unsigned long) kbuf;
3102 req->flags |= REQ_F_BUFFER_SELECTED;
3103 return u64_to_user_ptr(kbuf->addr);
3104}
3105
3106#ifdef CONFIG_COMPAT
3107static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3108 bool needs_lock)
3109{
3110 struct compat_iovec __user *uiov;
3111 compat_ssize_t clen;
3112 void __user *buf;
3113 ssize_t len;
3114
3115 uiov = u64_to_user_ptr(req->rw.addr);
3116 if (!access_ok(uiov, sizeof(*uiov)))
3117 return -EFAULT;
3118 if (__get_user(clen, &uiov->iov_len))
3119 return -EFAULT;
3120 if (clen < 0)
3121 return -EINVAL;
3122
3123 len = clen;
3124 buf = io_rw_buffer_select(req, &len, needs_lock);
3125 if (IS_ERR(buf))
3126 return PTR_ERR(buf);
3127 iov[0].iov_base = buf;
3128 iov[0].iov_len = (compat_size_t) len;
3129 return 0;
3130}
3131#endif
3132
3133static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3134 bool needs_lock)
3135{
3136 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3137 void __user *buf;
3138 ssize_t len;
3139
3140 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3141 return -EFAULT;
3142
3143 len = iov[0].iov_len;
3144 if (len < 0)
3145 return -EINVAL;
3146 buf = io_rw_buffer_select(req, &len, needs_lock);
3147 if (IS_ERR(buf))
3148 return PTR_ERR(buf);
3149 iov[0].iov_base = buf;
3150 iov[0].iov_len = len;
3151 return 0;
3152}
3153
3154static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3155 bool needs_lock)
3156{
Jens Axboedddb3e22020-06-04 11:27:01 -06003157 if (req->flags & REQ_F_BUFFER_SELECTED) {
3158 struct io_buffer *kbuf;
3159
3160 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3161 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3162 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003163 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003164 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003165 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003166 return -EINVAL;
3167
3168#ifdef CONFIG_COMPAT
3169 if (req->ctx->compat)
3170 return io_compat_import(req, iov, needs_lock);
3171#endif
3172
3173 return __io_iov_buffer_select(req, iov, needs_lock);
3174}
3175
Pavel Begunkov847595d2021-02-04 13:52:06 +00003176static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3177 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003178{
Jens Axboe9adbd452019-12-20 08:45:55 -07003179 void __user *buf = u64_to_user_ptr(req->rw.addr);
3180 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003181 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003182 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003183
Pavel Begunkov7d009162019-11-25 23:14:40 +03003184 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003185 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003186 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003187 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188
Jens Axboebcda7ba2020-02-23 16:42:51 -07003189 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003190 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003191 return -EINVAL;
3192
Jens Axboe3a6820f2019-12-22 15:19:35 -07003193 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003194 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003195 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003196 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003197 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003198 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003199 }
3200
Jens Axboe3a6820f2019-12-22 15:19:35 -07003201 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3202 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003203 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003204 }
3205
Jens Axboe4d954c22020-02-27 07:31:19 -07003206 if (req->flags & REQ_F_BUFFER_SELECT) {
3207 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003208 if (!ret)
3209 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003210 *iovec = NULL;
3211 return ret;
3212 }
3213
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003214 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3215 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003216}
3217
Jens Axboe0fef9482020-08-26 10:36:20 -06003218static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3219{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003220 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003221}
3222
Jens Axboe32960612019-09-23 11:05:34 -06003223/*
3224 * For files that don't have ->read_iter() and ->write_iter(), handle them
3225 * by looping over ->read() or ->write() manually.
3226 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003227static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003228{
Jens Axboe4017eb92020-10-22 14:14:12 -06003229 struct kiocb *kiocb = &req->rw.kiocb;
3230 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003231 ssize_t ret = 0;
3232
3233 /*
3234 * Don't support polled IO through this interface, and we can't
3235 * support non-blocking either. For the latter, this just causes
3236 * the kiocb to be handled from an async context.
3237 */
3238 if (kiocb->ki_flags & IOCB_HIPRI)
3239 return -EOPNOTSUPP;
3240 if (kiocb->ki_flags & IOCB_NOWAIT)
3241 return -EAGAIN;
3242
3243 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003244 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003245 ssize_t nr;
3246
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003247 if (!iov_iter_is_bvec(iter)) {
3248 iovec = iov_iter_iovec(iter);
3249 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003250 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3251 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003252 }
3253
Jens Axboe32960612019-09-23 11:05:34 -06003254 if (rw == READ) {
3255 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003256 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003257 } else {
3258 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003259 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003260 }
3261
3262 if (nr < 0) {
3263 if (!ret)
3264 ret = nr;
3265 break;
3266 }
3267 ret += nr;
3268 if (nr != iovec.iov_len)
3269 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003270 req->rw.len -= nr;
3271 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003272 iov_iter_advance(iter, nr);
3273 }
3274
3275 return ret;
3276}
3277
Jens Axboeff6165b2020-08-13 09:47:43 -06003278static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3279 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003280{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003281 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003282
Jens Axboeff6165b2020-08-13 09:47:43 -06003283 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003284 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003285 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003286 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003287 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003288 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003289 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003290 unsigned iov_off = 0;
3291
3292 rw->iter.iov = rw->fast_iov;
3293 if (iter->iov != fast_iov) {
3294 iov_off = iter->iov - fast_iov;
3295 rw->iter.iov += iov_off;
3296 }
3297 if (rw->fast_iov != fast_iov)
3298 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003299 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003300 } else {
3301 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003302 }
3303}
3304
Jens Axboee8c2bc12020-08-15 18:44:09 -07003305static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003306{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003307 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3308 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3309 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003310}
3311
Jens Axboee8c2bc12020-08-15 18:44:09 -07003312static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003313{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003314 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003315 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003316
Jens Axboee8c2bc12020-08-15 18:44:09 -07003317 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003318}
3319
Jens Axboeff6165b2020-08-13 09:47:43 -06003320static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3321 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003322 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003323{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003324 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003325 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003326 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003327 if (__io_alloc_async_data(req)) {
3328 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003329 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003330 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003331
Jens Axboeff6165b2020-08-13 09:47:43 -06003332 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003333 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003334 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003335}
3336
Pavel Begunkov73debe62020-09-30 22:57:54 +03003337static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003338{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003340 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003341 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003342
Pavel Begunkov2846c482020-11-07 13:16:27 +00003343 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003344 if (unlikely(ret < 0))
3345 return ret;
3346
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003347 iorw->bytes_done = 0;
3348 iorw->free_iovec = iov;
3349 if (iov)
3350 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003351 return 0;
3352}
3353
Pavel Begunkov73debe62020-09-30 22:57:54 +03003354static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003355{
3356 ssize_t ret;
3357
Pavel Begunkova88fc402020-09-30 22:57:53 +03003358 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003359 if (ret)
3360 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003361
Jens Axboe3529d8c2019-12-19 18:24:38 -07003362 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3363 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003364
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003365 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003366 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003367 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003368 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003369}
3370
Jens Axboec1dd91d2020-08-03 16:43:59 -06003371/*
3372 * This is our waitqueue callback handler, registered through lock_page_async()
3373 * when we initially tried to do the IO with the iocb armed our waitqueue.
3374 * This gets called when the page is unlocked, and we generally expect that to
3375 * happen when the page IO is completed and the page is now uptodate. This will
3376 * queue a task_work based retry of the operation, attempting to copy the data
3377 * again. If the latter fails because the page was NOT uptodate, then we will
3378 * do a thread based blocking retry of the operation. That's the unexpected
3379 * slow path.
3380 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003381static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3382 int sync, void *arg)
3383{
3384 struct wait_page_queue *wpq;
3385 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003386 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003387 int ret;
3388
3389 wpq = container_of(wait, struct wait_page_queue, wait);
3390
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003391 if (!wake_page_match(wpq, key))
3392 return 0;
3393
Hao Xuc8d317a2020-09-29 20:00:45 +08003394 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003395 list_del_init(&wait->entry);
3396
Pavel Begunkove7375122020-07-12 20:42:04 +03003397 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003398 percpu_ref_get(&req->ctx->refs);
3399
Jens Axboebcf5a062020-05-22 09:24:42 -06003400 /* submit ref gets dropped, acquire a new one */
3401 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003402 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003403 if (unlikely(ret))
3404 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003405 return 1;
3406}
3407
Jens Axboec1dd91d2020-08-03 16:43:59 -06003408/*
3409 * This controls whether a given IO request should be armed for async page
3410 * based retry. If we return false here, the request is handed to the async
3411 * worker threads for retry. If we're doing buffered reads on a regular file,
3412 * we prepare a private wait_page_queue entry and retry the operation. This
3413 * will either succeed because the page is now uptodate and unlocked, or it
3414 * will register a callback when the page is unlocked at IO completion. Through
3415 * that callback, io_uring uses task_work to setup a retry of the operation.
3416 * That retry will attempt the buffered read again. The retry will generally
3417 * succeed, or in rare cases where it fails, we then fall back to using the
3418 * async worker threads for a blocking retry.
3419 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003420static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003421{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003422 struct io_async_rw *rw = req->async_data;
3423 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003424 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003425
3426 /* never retry for NOWAIT, we just complete with -EAGAIN */
3427 if (req->flags & REQ_F_NOWAIT)
3428 return false;
3429
Jens Axboe227c0c92020-08-13 11:51:40 -06003430 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003431 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003432 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003433
Jens Axboebcf5a062020-05-22 09:24:42 -06003434 /*
3435 * just use poll if we can, and don't attempt if the fs doesn't
3436 * support callback based unlocks
3437 */
3438 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3439 return false;
3440
Jens Axboe3b2a4432020-08-16 10:58:43 -07003441 wait->wait.func = io_async_buf_func;
3442 wait->wait.private = req;
3443 wait->wait.flags = 0;
3444 INIT_LIST_HEAD(&wait->wait.entry);
3445 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003446 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003447 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003448 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003449}
3450
3451static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3452{
3453 if (req->file->f_op->read_iter)
3454 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003455 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003456 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003457 else
3458 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003459}
3460
Pavel Begunkov889fca72021-02-10 00:03:09 +00003461static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462{
3463 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003464 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003465 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003466 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003467 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003468 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003469
Pavel Begunkov2846c482020-11-07 13:16:27 +00003470 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003471 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003472 iovec = NULL;
3473 } else {
3474 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3475 if (ret < 0)
3476 return ret;
3477 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003478 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003479 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003480
Jens Axboefd6c2e42019-12-18 12:19:41 -07003481 /* Ensure we clear previously set non-block flag */
3482 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003483 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003484 else
3485 kiocb->ki_flags |= IOCB_NOWAIT;
3486
Pavel Begunkov24c74672020-06-21 13:09:51 +03003487 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003488 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3489 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003490 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003491 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003492
Pavel Begunkov632546c2020-11-07 13:16:26 +00003493 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003494 if (unlikely(ret)) {
3495 kfree(iovec);
3496 return ret;
3497 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003498
Jens Axboe227c0c92020-08-13 11:51:40 -06003499 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003500
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003501 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003502 /* it's faster to check here then delegate to kfree */
3503 if (iovec)
3504 kfree(iovec);
3505 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003506 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003507 /* IOPOLL retry should happen for io-wq threads */
3508 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003509 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003510 /* no retry on NONBLOCK nor RWF_NOWAIT */
3511 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003512 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003513 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003514 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003515 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003516 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003517 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003518 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003519 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003520 }
3521
Jens Axboe227c0c92020-08-13 11:51:40 -06003522 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003523 if (ret2)
3524 return ret2;
3525
Jens Axboee8c2bc12020-08-15 18:44:09 -07003526 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003527 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003528 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003529
Pavel Begunkovb23df912021-02-04 13:52:04 +00003530 do {
3531 io_size -= ret;
3532 rw->bytes_done += ret;
3533 /* if we can retry, do so with the callbacks armed */
3534 if (!io_rw_should_retry(req)) {
3535 kiocb->ki_flags &= ~IOCB_WAITQ;
3536 return -EAGAIN;
3537 }
3538
3539 /*
3540 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3541 * we get -EIOCBQUEUED, then we'll get a notification when the
3542 * desired page gets unlocked. We can also get a partial read
3543 * here, and if we do, then just retry at the new offset.
3544 */
3545 ret = io_iter_do_read(req, iter);
3546 if (ret == -EIOCBQUEUED)
3547 return 0;
3548 /* we got some bytes, but not all. retry. */
3549 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003550done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003551 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003552 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003553}
3554
Pavel Begunkov73debe62020-09-30 22:57:54 +03003555static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003556{
3557 ssize_t ret;
3558
Pavel Begunkova88fc402020-09-30 22:57:53 +03003559 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003560 if (ret)
3561 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003562
Jens Axboe3529d8c2019-12-19 18:24:38 -07003563 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3564 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003565
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003566 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003567 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003568 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003569 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003570}
3571
Pavel Begunkov889fca72021-02-10 00:03:09 +00003572static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003573{
3574 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003575 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003576 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003577 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003578 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003579 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003580
Pavel Begunkov2846c482020-11-07 13:16:27 +00003581 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003582 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003583 iovec = NULL;
3584 } else {
3585 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3586 if (ret < 0)
3587 return ret;
3588 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003589 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003590 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003591
Jens Axboefd6c2e42019-12-18 12:19:41 -07003592 /* Ensure we clear previously set non-block flag */
3593 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003594 kiocb->ki_flags &= ~IOCB_NOWAIT;
3595 else
3596 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003597
Pavel Begunkov24c74672020-06-21 13:09:51 +03003598 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003599 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003600 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003601
Jens Axboe10d59342019-12-09 20:16:22 -07003602 /* file path doesn't support NOWAIT for non-direct_IO */
3603 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3604 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003605 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003606
Pavel Begunkov632546c2020-11-07 13:16:26 +00003607 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003608 if (unlikely(ret))
3609 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003610
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003611 /*
3612 * Open-code file_start_write here to grab freeze protection,
3613 * which will be released by another thread in
3614 * io_complete_rw(). Fool lockdep by telling it the lock got
3615 * released so that it doesn't complain about the held lock when
3616 * we return to userspace.
3617 */
3618 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003619 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003620 __sb_writers_release(file_inode(req->file)->i_sb,
3621 SB_FREEZE_WRITE);
3622 }
3623 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003624
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003625 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003626 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003627 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003628 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003629 else
3630 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003631
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003632 /*
3633 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3634 * retry them without IOCB_NOWAIT.
3635 */
3636 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3637 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003638 /* no retry on NONBLOCK nor RWF_NOWAIT */
3639 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003640 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003641 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003642 /* IOPOLL retry should happen for io-wq threads */
3643 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3644 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003645done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003646 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003647 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003648copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003649 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003650 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003651 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003652 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003653 }
Jens Axboe31b51512019-01-18 22:56:34 -07003654out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003655 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003656 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003657 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003658 return ret;
3659}
3660
Jens Axboe80a261f2020-09-28 14:23:58 -06003661static int io_renameat_prep(struct io_kiocb *req,
3662 const struct io_uring_sqe *sqe)
3663{
3664 struct io_rename *ren = &req->rename;
3665 const char __user *oldf, *newf;
3666
3667 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3668 return -EBADF;
3669
3670 ren->old_dfd = READ_ONCE(sqe->fd);
3671 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3672 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3673 ren->new_dfd = READ_ONCE(sqe->len);
3674 ren->flags = READ_ONCE(sqe->rename_flags);
3675
3676 ren->oldpath = getname(oldf);
3677 if (IS_ERR(ren->oldpath))
3678 return PTR_ERR(ren->oldpath);
3679
3680 ren->newpath = getname(newf);
3681 if (IS_ERR(ren->newpath)) {
3682 putname(ren->oldpath);
3683 return PTR_ERR(ren->newpath);
3684 }
3685
3686 req->flags |= REQ_F_NEED_CLEANUP;
3687 return 0;
3688}
3689
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003690static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003691{
3692 struct io_rename *ren = &req->rename;
3693 int ret;
3694
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003695 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003696 return -EAGAIN;
3697
3698 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3699 ren->newpath, ren->flags);
3700
3701 req->flags &= ~REQ_F_NEED_CLEANUP;
3702 if (ret < 0)
3703 req_set_fail_links(req);
3704 io_req_complete(req, ret);
3705 return 0;
3706}
3707
Jens Axboe14a11432020-09-28 14:27:37 -06003708static int io_unlinkat_prep(struct io_kiocb *req,
3709 const struct io_uring_sqe *sqe)
3710{
3711 struct io_unlink *un = &req->unlink;
3712 const char __user *fname;
3713
3714 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3715 return -EBADF;
3716
3717 un->dfd = READ_ONCE(sqe->fd);
3718
3719 un->flags = READ_ONCE(sqe->unlink_flags);
3720 if (un->flags & ~AT_REMOVEDIR)
3721 return -EINVAL;
3722
3723 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3724 un->filename = getname(fname);
3725 if (IS_ERR(un->filename))
3726 return PTR_ERR(un->filename);
3727
3728 req->flags |= REQ_F_NEED_CLEANUP;
3729 return 0;
3730}
3731
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003732static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003733{
3734 struct io_unlink *un = &req->unlink;
3735 int ret;
3736
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003737 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003738 return -EAGAIN;
3739
3740 if (un->flags & AT_REMOVEDIR)
3741 ret = do_rmdir(un->dfd, un->filename);
3742 else
3743 ret = do_unlinkat(un->dfd, un->filename);
3744
3745 req->flags &= ~REQ_F_NEED_CLEANUP;
3746 if (ret < 0)
3747 req_set_fail_links(req);
3748 io_req_complete(req, ret);
3749 return 0;
3750}
3751
Jens Axboe36f4fa62020-09-05 11:14:22 -06003752static int io_shutdown_prep(struct io_kiocb *req,
3753 const struct io_uring_sqe *sqe)
3754{
3755#if defined(CONFIG_NET)
3756 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3757 return -EINVAL;
3758 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3759 sqe->buf_index)
3760 return -EINVAL;
3761
3762 req->shutdown.how = READ_ONCE(sqe->len);
3763 return 0;
3764#else
3765 return -EOPNOTSUPP;
3766#endif
3767}
3768
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003769static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003770{
3771#if defined(CONFIG_NET)
3772 struct socket *sock;
3773 int ret;
3774
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003775 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003776 return -EAGAIN;
3777
Linus Torvalds48aba792020-12-16 12:44:05 -08003778 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003779 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003780 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003781
3782 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003783 if (ret < 0)
3784 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003785 io_req_complete(req, ret);
3786 return 0;
3787#else
3788 return -EOPNOTSUPP;
3789#endif
3790}
3791
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003792static int __io_splice_prep(struct io_kiocb *req,
3793 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003794{
3795 struct io_splice* sp = &req->splice;
3796 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003797
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003798 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3799 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003800
3801 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003802 sp->len = READ_ONCE(sqe->len);
3803 sp->flags = READ_ONCE(sqe->splice_flags);
3804
3805 if (unlikely(sp->flags & ~valid_flags))
3806 return -EINVAL;
3807
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003808 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3809 (sp->flags & SPLICE_F_FD_IN_FIXED));
3810 if (!sp->file_in)
3811 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003812 req->flags |= REQ_F_NEED_CLEANUP;
3813
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003814 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3815 /*
3816 * Splice operation will be punted aync, and here need to
3817 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3818 */
3819 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003820 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003821 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003822
3823 return 0;
3824}
3825
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003826static int io_tee_prep(struct io_kiocb *req,
3827 const struct io_uring_sqe *sqe)
3828{
3829 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3830 return -EINVAL;
3831 return __io_splice_prep(req, sqe);
3832}
3833
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003834static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003835{
3836 struct io_splice *sp = &req->splice;
3837 struct file *in = sp->file_in;
3838 struct file *out = sp->file_out;
3839 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3840 long ret = 0;
3841
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003842 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003843 return -EAGAIN;
3844 if (sp->len)
3845 ret = do_tee(in, out, sp->len, flags);
3846
3847 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3848 req->flags &= ~REQ_F_NEED_CLEANUP;
3849
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003850 if (ret != sp->len)
3851 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003852 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003853 return 0;
3854}
3855
3856static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3857{
3858 struct io_splice* sp = &req->splice;
3859
3860 sp->off_in = READ_ONCE(sqe->splice_off_in);
3861 sp->off_out = READ_ONCE(sqe->off);
3862 return __io_splice_prep(req, sqe);
3863}
3864
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003865static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003866{
3867 struct io_splice *sp = &req->splice;
3868 struct file *in = sp->file_in;
3869 struct file *out = sp->file_out;
3870 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3871 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003872 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003873
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003874 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003875 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003876
3877 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3878 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003879
Jens Axboe948a7742020-05-17 14:21:38 -06003880 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003881 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003882
3883 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3884 req->flags &= ~REQ_F_NEED_CLEANUP;
3885
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003886 if (ret != sp->len)
3887 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003888 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003889 return 0;
3890}
3891
Jens Axboe2b188cc2019-01-07 10:46:33 -07003892/*
3893 * IORING_OP_NOP just posts a completion event, nothing else.
3894 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003895static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003896{
3897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003898
Jens Axboedef596e2019-01-09 08:59:42 -07003899 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3900 return -EINVAL;
3901
Pavel Begunkov889fca72021-02-10 00:03:09 +00003902 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003903 return 0;
3904}
3905
Jens Axboe3529d8c2019-12-19 18:24:38 -07003906static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003907{
Jens Axboe6b063142019-01-10 22:13:58 -07003908 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003909
Jens Axboe09bb8392019-03-13 12:39:28 -06003910 if (!req->file)
3911 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003912
Jens Axboe6b063142019-01-10 22:13:58 -07003913 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003914 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003915 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003916 return -EINVAL;
3917
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003918 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3919 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3920 return -EINVAL;
3921
3922 req->sync.off = READ_ONCE(sqe->off);
3923 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003924 return 0;
3925}
3926
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003927static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003928{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003929 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003930 int ret;
3931
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003932 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003933 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003934 return -EAGAIN;
3935
Jens Axboe9adbd452019-12-20 08:45:55 -07003936 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003937 end > 0 ? end : LLONG_MAX,
3938 req->sync.flags & IORING_FSYNC_DATASYNC);
3939 if (ret < 0)
3940 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003941 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003942 return 0;
3943}
3944
Jens Axboed63d1b52019-12-10 10:38:56 -07003945static int io_fallocate_prep(struct io_kiocb *req,
3946 const struct io_uring_sqe *sqe)
3947{
3948 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3949 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003950 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3951 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003952
3953 req->sync.off = READ_ONCE(sqe->off);
3954 req->sync.len = READ_ONCE(sqe->addr);
3955 req->sync.mode = READ_ONCE(sqe->len);
3956 return 0;
3957}
3958
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003959static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003960{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003961 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003962
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003963 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003964 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003965 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003966 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3967 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003968 if (ret < 0)
3969 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003970 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003971 return 0;
3972}
3973
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003974static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003975{
Jens Axboef8748882020-01-08 17:47:02 -07003976 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003977 int ret;
3978
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003979 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003980 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003981 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003982 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003983
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003984 /* open.how should be already initialised */
3985 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003986 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003987
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003988 req->open.dfd = READ_ONCE(sqe->fd);
3989 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003990 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003991 if (IS_ERR(req->open.filename)) {
3992 ret = PTR_ERR(req->open.filename);
3993 req->open.filename = NULL;
3994 return ret;
3995 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003996 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003997 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003998 return 0;
3999}
4000
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004001static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4002{
4003 u64 flags, mode;
4004
Jens Axboe14587a462020-09-05 11:36:08 -06004005 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004006 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004007 mode = READ_ONCE(sqe->len);
4008 flags = READ_ONCE(sqe->open_flags);
4009 req->open.how = build_open_how(flags, mode);
4010 return __io_openat_prep(req, sqe);
4011}
4012
Jens Axboecebdb982020-01-08 17:59:24 -07004013static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4014{
4015 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004016 size_t len;
4017 int ret;
4018
Jens Axboe14587a462020-09-05 11:36:08 -06004019 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004020 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004021 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4022 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004023 if (len < OPEN_HOW_SIZE_VER0)
4024 return -EINVAL;
4025
4026 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4027 len);
4028 if (ret)
4029 return ret;
4030
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004031 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004032}
4033
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004034static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004035{
4036 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004037 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004038 bool nonblock_set;
4039 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004040 int ret;
4041
Jens Axboecebdb982020-01-08 17:59:24 -07004042 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004043 if (ret)
4044 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004045 nonblock_set = op.open_flag & O_NONBLOCK;
4046 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004047 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004048 /*
4049 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4050 * it'll always -EAGAIN
4051 */
4052 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4053 return -EAGAIN;
4054 op.lookup_flags |= LOOKUP_CACHED;
4055 op.open_flag |= O_NONBLOCK;
4056 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004057
Jens Axboe4022e7a2020-03-19 19:23:18 -06004058 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004059 if (ret < 0)
4060 goto err;
4061
4062 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004063 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004064 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4065 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004066 /*
4067 * We could hang on to this 'fd', but seems like marginal
4068 * gain for something that is now known to be a slower path.
4069 * So just put it, and we'll get a new one when we retry.
4070 */
4071 put_unused_fd(ret);
4072 return -EAGAIN;
4073 }
4074
Jens Axboe15b71ab2019-12-11 11:20:36 -07004075 if (IS_ERR(file)) {
4076 put_unused_fd(ret);
4077 ret = PTR_ERR(file);
4078 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004079 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004080 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004081 fsnotify_open(file);
4082 fd_install(ret, file);
4083 }
4084err:
4085 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004086 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004087 if (ret < 0)
4088 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004089 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004090 return 0;
4091}
4092
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004093static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004094{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004095 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004096}
4097
Jens Axboe067524e2020-03-02 16:32:28 -07004098static int io_remove_buffers_prep(struct io_kiocb *req,
4099 const struct io_uring_sqe *sqe)
4100{
4101 struct io_provide_buf *p = &req->pbuf;
4102 u64 tmp;
4103
4104 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4105 return -EINVAL;
4106
4107 tmp = READ_ONCE(sqe->fd);
4108 if (!tmp || tmp > USHRT_MAX)
4109 return -EINVAL;
4110
4111 memset(p, 0, sizeof(*p));
4112 p->nbufs = tmp;
4113 p->bgid = READ_ONCE(sqe->buf_group);
4114 return 0;
4115}
4116
4117static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4118 int bgid, unsigned nbufs)
4119{
4120 unsigned i = 0;
4121
4122 /* shouldn't happen */
4123 if (!nbufs)
4124 return 0;
4125
4126 /* the head kbuf is the list itself */
4127 while (!list_empty(&buf->list)) {
4128 struct io_buffer *nxt;
4129
4130 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4131 list_del(&nxt->list);
4132 kfree(nxt);
4133 if (++i == nbufs)
4134 return i;
4135 }
4136 i++;
4137 kfree(buf);
4138 idr_remove(&ctx->io_buffer_idr, bgid);
4139
4140 return i;
4141}
4142
Pavel Begunkov889fca72021-02-10 00:03:09 +00004143static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004144{
4145 struct io_provide_buf *p = &req->pbuf;
4146 struct io_ring_ctx *ctx = req->ctx;
4147 struct io_buffer *head;
4148 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004149 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004150
4151 io_ring_submit_lock(ctx, !force_nonblock);
4152
4153 lockdep_assert_held(&ctx->uring_lock);
4154
4155 ret = -ENOENT;
4156 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4157 if (head)
4158 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004159 if (ret < 0)
4160 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004161
4162 /* need to hold the lock to complete IOPOLL requests */
4163 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004164 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004165 io_ring_submit_unlock(ctx, !force_nonblock);
4166 } else {
4167 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004168 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004169 }
Jens Axboe067524e2020-03-02 16:32:28 -07004170 return 0;
4171}
4172
Jens Axboeddf0322d2020-02-23 16:41:33 -07004173static int io_provide_buffers_prep(struct io_kiocb *req,
4174 const struct io_uring_sqe *sqe)
4175{
4176 struct io_provide_buf *p = &req->pbuf;
4177 u64 tmp;
4178
4179 if (sqe->ioprio || sqe->rw_flags)
4180 return -EINVAL;
4181
4182 tmp = READ_ONCE(sqe->fd);
4183 if (!tmp || tmp > USHRT_MAX)
4184 return -E2BIG;
4185 p->nbufs = tmp;
4186 p->addr = READ_ONCE(sqe->addr);
4187 p->len = READ_ONCE(sqe->len);
4188
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004189 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004190 return -EFAULT;
4191
4192 p->bgid = READ_ONCE(sqe->buf_group);
4193 tmp = READ_ONCE(sqe->off);
4194 if (tmp > USHRT_MAX)
4195 return -E2BIG;
4196 p->bid = tmp;
4197 return 0;
4198}
4199
4200static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4201{
4202 struct io_buffer *buf;
4203 u64 addr = pbuf->addr;
4204 int i, bid = pbuf->bid;
4205
4206 for (i = 0; i < pbuf->nbufs; i++) {
4207 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4208 if (!buf)
4209 break;
4210
4211 buf->addr = addr;
4212 buf->len = pbuf->len;
4213 buf->bid = bid;
4214 addr += pbuf->len;
4215 bid++;
4216 if (!*head) {
4217 INIT_LIST_HEAD(&buf->list);
4218 *head = buf;
4219 } else {
4220 list_add_tail(&buf->list, &(*head)->list);
4221 }
4222 }
4223
4224 return i ? i : -ENOMEM;
4225}
4226
Pavel Begunkov889fca72021-02-10 00:03:09 +00004227static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004228{
4229 struct io_provide_buf *p = &req->pbuf;
4230 struct io_ring_ctx *ctx = req->ctx;
4231 struct io_buffer *head, *list;
4232 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004233 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004234
4235 io_ring_submit_lock(ctx, !force_nonblock);
4236
4237 lockdep_assert_held(&ctx->uring_lock);
4238
4239 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4240
4241 ret = io_add_buffers(p, &head);
4242 if (ret < 0)
4243 goto out;
4244
4245 if (!list) {
4246 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4247 GFP_KERNEL);
4248 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004249 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004250 goto out;
4251 }
4252 }
4253out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004254 if (ret < 0)
4255 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004256
4257 /* need to hold the lock to complete IOPOLL requests */
4258 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004259 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004260 io_ring_submit_unlock(ctx, !force_nonblock);
4261 } else {
4262 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004263 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004264 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004265 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004266}
4267
Jens Axboe3e4827b2020-01-08 15:18:09 -07004268static int io_epoll_ctl_prep(struct io_kiocb *req,
4269 const struct io_uring_sqe *sqe)
4270{
4271#if defined(CONFIG_EPOLL)
4272 if (sqe->ioprio || sqe->buf_index)
4273 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004274 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004275 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004276
4277 req->epoll.epfd = READ_ONCE(sqe->fd);
4278 req->epoll.op = READ_ONCE(sqe->len);
4279 req->epoll.fd = READ_ONCE(sqe->off);
4280
4281 if (ep_op_has_event(req->epoll.op)) {
4282 struct epoll_event __user *ev;
4283
4284 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4285 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4286 return -EFAULT;
4287 }
4288
4289 return 0;
4290#else
4291 return -EOPNOTSUPP;
4292#endif
4293}
4294
Pavel Begunkov889fca72021-02-10 00:03:09 +00004295static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004296{
4297#if defined(CONFIG_EPOLL)
4298 struct io_epoll *ie = &req->epoll;
4299 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004300 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004301
4302 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4303 if (force_nonblock && ret == -EAGAIN)
4304 return -EAGAIN;
4305
4306 if (ret < 0)
4307 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004308 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004309 return 0;
4310#else
4311 return -EOPNOTSUPP;
4312#endif
4313}
4314
Jens Axboec1ca7572019-12-25 22:18:28 -07004315static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4316{
4317#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4318 if (sqe->ioprio || sqe->buf_index || sqe->off)
4319 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004320 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4321 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004322
4323 req->madvise.addr = READ_ONCE(sqe->addr);
4324 req->madvise.len = READ_ONCE(sqe->len);
4325 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4326 return 0;
4327#else
4328 return -EOPNOTSUPP;
4329#endif
4330}
4331
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004332static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004333{
4334#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4335 struct io_madvise *ma = &req->madvise;
4336 int ret;
4337
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004338 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004339 return -EAGAIN;
4340
Minchan Kim0726b012020-10-17 16:14:50 -07004341 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004342 if (ret < 0)
4343 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004344 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004345 return 0;
4346#else
4347 return -EOPNOTSUPP;
4348#endif
4349}
4350
Jens Axboe4840e412019-12-25 22:03:45 -07004351static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4352{
4353 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4354 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004355 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4356 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004357
4358 req->fadvise.offset = READ_ONCE(sqe->off);
4359 req->fadvise.len = READ_ONCE(sqe->len);
4360 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4361 return 0;
4362}
4363
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004364static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004365{
4366 struct io_fadvise *fa = &req->fadvise;
4367 int ret;
4368
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004369 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004370 switch (fa->advice) {
4371 case POSIX_FADV_NORMAL:
4372 case POSIX_FADV_RANDOM:
4373 case POSIX_FADV_SEQUENTIAL:
4374 break;
4375 default:
4376 return -EAGAIN;
4377 }
4378 }
Jens Axboe4840e412019-12-25 22:03:45 -07004379
4380 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4381 if (ret < 0)
4382 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004383 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004384 return 0;
4385}
4386
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004387static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4388{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004389 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004390 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004391 if (sqe->ioprio || sqe->buf_index)
4392 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004393 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004394 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004395
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004396 req->statx.dfd = READ_ONCE(sqe->fd);
4397 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004398 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004399 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4400 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004401
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004402 return 0;
4403}
4404
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004405static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004406{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004407 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004408 int ret;
4409
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004410 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004411 /* only need file table for an actual valid fd */
4412 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4413 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004414 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004415 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004416
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004417 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4418 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004419
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004420 if (ret < 0)
4421 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004422 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004423 return 0;
4424}
4425
Jens Axboeb5dba592019-12-11 14:02:38 -07004426static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4427{
Jens Axboe14587a462020-09-05 11:36:08 -06004428 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004429 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004430 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4431 sqe->rw_flags || sqe->buf_index)
4432 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004433 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004434 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004435
4436 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004437 return 0;
4438}
4439
Pavel Begunkov889fca72021-02-10 00:03:09 +00004440static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004441{
Jens Axboe9eac1902021-01-19 15:50:37 -07004442 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004443 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004444 struct fdtable *fdt;
4445 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004446 int ret;
4447
Jens Axboe9eac1902021-01-19 15:50:37 -07004448 file = NULL;
4449 ret = -EBADF;
4450 spin_lock(&files->file_lock);
4451 fdt = files_fdtable(files);
4452 if (close->fd >= fdt->max_fds) {
4453 spin_unlock(&files->file_lock);
4454 goto err;
4455 }
4456 file = fdt->fd[close->fd];
4457 if (!file) {
4458 spin_unlock(&files->file_lock);
4459 goto err;
4460 }
4461
4462 if (file->f_op == &io_uring_fops) {
4463 spin_unlock(&files->file_lock);
4464 file = NULL;
4465 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004466 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004467
4468 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004469 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004470 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004471 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004472 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004473
Jens Axboe9eac1902021-01-19 15:50:37 -07004474 ret = __close_fd_get_file(close->fd, &file);
4475 spin_unlock(&files->file_lock);
4476 if (ret < 0) {
4477 if (ret == -ENOENT)
4478 ret = -EBADF;
4479 goto err;
4480 }
4481
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004482 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004483 ret = filp_close(file, current->files);
4484err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004485 if (ret < 0)
4486 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004487 if (file)
4488 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004489 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004490 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004491}
4492
Jens Axboe3529d8c2019-12-19 18:24:38 -07004493static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004494{
4495 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004496
4497 if (!req->file)
4498 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004499
4500 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4501 return -EINVAL;
4502 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4503 return -EINVAL;
4504
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004505 req->sync.off = READ_ONCE(sqe->off);
4506 req->sync.len = READ_ONCE(sqe->len);
4507 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004508 return 0;
4509}
4510
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004511static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004512{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513 int ret;
4514
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004515 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004516 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004517 return -EAGAIN;
4518
Jens Axboe9adbd452019-12-20 08:45:55 -07004519 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004520 req->sync.flags);
4521 if (ret < 0)
4522 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004523 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004524 return 0;
4525}
4526
YueHaibing469956e2020-03-04 15:53:52 +08004527#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004528static int io_setup_async_msg(struct io_kiocb *req,
4529 struct io_async_msghdr *kmsg)
4530{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004531 struct io_async_msghdr *async_msg = req->async_data;
4532
4533 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004534 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004535 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004536 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004537 return -ENOMEM;
4538 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004539 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004540 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004541 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004542 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004543 /* if were using fast_iov, set it to the new one */
4544 if (!async_msg->free_iov)
4545 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4546
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004547 return -EAGAIN;
4548}
4549
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004550static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4551 struct io_async_msghdr *iomsg)
4552{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004553 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004554 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004555 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004556 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004557}
4558
Jens Axboe3529d8c2019-12-19 18:24:38 -07004559static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004560{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004561 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004562 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004563 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004564
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004565 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4566 return -EINVAL;
4567
Jens Axboee47293f2019-12-20 08:58:21 -07004568 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004569 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004570 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004571
Jens Axboed8768362020-02-27 14:17:49 -07004572#ifdef CONFIG_COMPAT
4573 if (req->ctx->compat)
4574 sr->msg_flags |= MSG_CMSG_COMPAT;
4575#endif
4576
Jens Axboee8c2bc12020-08-15 18:44:09 -07004577 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004578 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004579 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004580 if (!ret)
4581 req->flags |= REQ_F_NEED_CLEANUP;
4582 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004583}
4584
Pavel Begunkov889fca72021-02-10 00:03:09 +00004585static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004586{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004587 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004588 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004589 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004590 int ret;
4591
Florent Revestdba4a922020-12-04 12:36:04 +01004592 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004593 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004594 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004595
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004596 kmsg = req->async_data;
4597 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004598 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004599 if (ret)
4600 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004601 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004602 }
4603
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004604 flags = req->sr_msg.msg_flags;
4605 if (flags & MSG_DONTWAIT)
4606 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004607 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004608 flags |= MSG_DONTWAIT;
4609
4610 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004611 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004612 return io_setup_async_msg(req, kmsg);
4613 if (ret == -ERESTARTSYS)
4614 ret = -EINTR;
4615
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004616 /* fast path, check for non-NULL to avoid function call */
4617 if (kmsg->free_iov)
4618 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004619 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004620 if (ret < 0)
4621 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004622 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004623 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004624}
4625
Pavel Begunkov889fca72021-02-10 00:03:09 +00004626static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004627{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 struct io_sr_msg *sr = &req->sr_msg;
4629 struct msghdr msg;
4630 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004631 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004632 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004633 int ret;
4634
Florent Revestdba4a922020-12-04 12:36:04 +01004635 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004636 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004637 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004638
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004639 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4640 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004641 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004642
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004643 msg.msg_name = NULL;
4644 msg.msg_control = NULL;
4645 msg.msg_controllen = 0;
4646 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004647
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004648 flags = req->sr_msg.msg_flags;
4649 if (flags & MSG_DONTWAIT)
4650 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004651 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004652 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004653
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004654 msg.msg_flags = flags;
4655 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004656 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004657 return -EAGAIN;
4658 if (ret == -ERESTARTSYS)
4659 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004660
Jens Axboe03b12302019-12-02 18:50:25 -07004661 if (ret < 0)
4662 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004663 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004664 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004665}
4666
Pavel Begunkov1400e692020-07-12 20:41:05 +03004667static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4668 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004669{
4670 struct io_sr_msg *sr = &req->sr_msg;
4671 struct iovec __user *uiov;
4672 size_t iov_len;
4673 int ret;
4674
Pavel Begunkov1400e692020-07-12 20:41:05 +03004675 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4676 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004677 if (ret)
4678 return ret;
4679
4680 if (req->flags & REQ_F_BUFFER_SELECT) {
4681 if (iov_len > 1)
4682 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004683 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004684 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004685 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004686 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004687 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004688 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004689 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004690 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004691 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004692 if (ret > 0)
4693 ret = 0;
4694 }
4695
4696 return ret;
4697}
4698
4699#ifdef CONFIG_COMPAT
4700static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004701 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004702{
4703 struct compat_msghdr __user *msg_compat;
4704 struct io_sr_msg *sr = &req->sr_msg;
4705 struct compat_iovec __user *uiov;
4706 compat_uptr_t ptr;
4707 compat_size_t len;
4708 int ret;
4709
Pavel Begunkov270a5942020-07-12 20:41:04 +03004710 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004711 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004712 &ptr, &len);
4713 if (ret)
4714 return ret;
4715
4716 uiov = compat_ptr(ptr);
4717 if (req->flags & REQ_F_BUFFER_SELECT) {
4718 compat_ssize_t clen;
4719
4720 if (len > 1)
4721 return -EINVAL;
4722 if (!access_ok(uiov, sizeof(*uiov)))
4723 return -EFAULT;
4724 if (__get_user(clen, &uiov->iov_len))
4725 return -EFAULT;
4726 if (clen < 0)
4727 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004728 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004729 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004730 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004731 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004732 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004733 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004734 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004735 if (ret < 0)
4736 return ret;
4737 }
4738
4739 return 0;
4740}
Jens Axboe03b12302019-12-02 18:50:25 -07004741#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004742
Pavel Begunkov1400e692020-07-12 20:41:05 +03004743static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4744 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004745{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004746 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004747
4748#ifdef CONFIG_COMPAT
4749 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004750 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004751#endif
4752
Pavel Begunkov1400e692020-07-12 20:41:05 +03004753 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004754}
4755
Jens Axboebcda7ba2020-02-23 16:42:51 -07004756static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004757 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004758{
4759 struct io_sr_msg *sr = &req->sr_msg;
4760 struct io_buffer *kbuf;
4761
Jens Axboebcda7ba2020-02-23 16:42:51 -07004762 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4763 if (IS_ERR(kbuf))
4764 return kbuf;
4765
4766 sr->kbuf = kbuf;
4767 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004768 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004769}
4770
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004771static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4772{
4773 return io_put_kbuf(req, req->sr_msg.kbuf);
4774}
4775
Jens Axboe3529d8c2019-12-19 18:24:38 -07004776static int io_recvmsg_prep(struct io_kiocb *req,
4777 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004778{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004779 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004780 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004781 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004782
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004783 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4784 return -EINVAL;
4785
Jens Axboe3529d8c2019-12-19 18:24:38 -07004786 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004787 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004788 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004789 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004790
Jens Axboed8768362020-02-27 14:17:49 -07004791#ifdef CONFIG_COMPAT
4792 if (req->ctx->compat)
4793 sr->msg_flags |= MSG_CMSG_COMPAT;
4794#endif
4795
Jens Axboee8c2bc12020-08-15 18:44:09 -07004796 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004797 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004798 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004799 if (!ret)
4800 req->flags |= REQ_F_NEED_CLEANUP;
4801 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004802}
4803
Pavel Begunkov889fca72021-02-10 00:03:09 +00004804static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004805{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004806 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004807 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004808 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004809 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004810 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004811 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004812
Florent Revestdba4a922020-12-04 12:36:04 +01004813 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004814 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004815 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004816
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004817 kmsg = req->async_data;
4818 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004819 ret = io_recvmsg_copy_hdr(req, &iomsg);
4820 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004821 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004822 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004823 }
4824
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004825 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004826 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004827 if (IS_ERR(kbuf))
4828 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004829 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004830 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4831 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004832 1, req->sr_msg.len);
4833 }
4834
4835 flags = req->sr_msg.msg_flags;
4836 if (flags & MSG_DONTWAIT)
4837 req->flags |= REQ_F_NOWAIT;
4838 else if (force_nonblock)
4839 flags |= MSG_DONTWAIT;
4840
4841 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4842 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004843 if (force_nonblock && ret == -EAGAIN)
4844 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004845 if (ret == -ERESTARTSYS)
4846 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004847
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004848 if (req->flags & REQ_F_BUFFER_SELECTED)
4849 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004850 /* fast path, check for non-NULL to avoid function call */
4851 if (kmsg->free_iov)
4852 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004853 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004854 if (ret < 0)
4855 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004856 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004857 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004858}
4859
Pavel Begunkov889fca72021-02-10 00:03:09 +00004860static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004861{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004862 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004863 struct io_sr_msg *sr = &req->sr_msg;
4864 struct msghdr msg;
4865 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004866 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004867 struct iovec iov;
4868 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004869 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004870 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004871
Florent Revestdba4a922020-12-04 12:36:04 +01004872 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004873 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004874 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004875
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004876 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004877 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004878 if (IS_ERR(kbuf))
4879 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004880 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004881 }
4882
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004883 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004884 if (unlikely(ret))
4885 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004886
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004887 msg.msg_name = NULL;
4888 msg.msg_control = NULL;
4889 msg.msg_controllen = 0;
4890 msg.msg_namelen = 0;
4891 msg.msg_iocb = NULL;
4892 msg.msg_flags = 0;
4893
4894 flags = req->sr_msg.msg_flags;
4895 if (flags & MSG_DONTWAIT)
4896 req->flags |= REQ_F_NOWAIT;
4897 else if (force_nonblock)
4898 flags |= MSG_DONTWAIT;
4899
4900 ret = sock_recvmsg(sock, &msg, flags);
4901 if (force_nonblock && ret == -EAGAIN)
4902 return -EAGAIN;
4903 if (ret == -ERESTARTSYS)
4904 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004905out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004906 if (req->flags & REQ_F_BUFFER_SELECTED)
4907 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004908 if (ret < 0)
4909 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004910 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004911 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004912}
4913
Jens Axboe3529d8c2019-12-19 18:24:38 -07004914static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004915{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004916 struct io_accept *accept = &req->accept;
4917
Jens Axboe14587a462020-09-05 11:36:08 -06004918 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004919 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004920 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004921 return -EINVAL;
4922
Jens Axboed55e5f52019-12-11 16:12:15 -07004923 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4924 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004925 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004926 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004927 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004928}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004929
Pavel Begunkov889fca72021-02-10 00:03:09 +00004930static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004931{
4932 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004933 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004934 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004935 int ret;
4936
Jiufei Xuee697dee2020-06-10 13:41:59 +08004937 if (req->file->f_flags & O_NONBLOCK)
4938 req->flags |= REQ_F_NOWAIT;
4939
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004940 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004941 accept->addr_len, accept->flags,
4942 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004943 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004944 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004945 if (ret < 0) {
4946 if (ret == -ERESTARTSYS)
4947 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004948 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004949 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004950 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004951 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004952}
4953
Jens Axboe3529d8c2019-12-19 18:24:38 -07004954static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004955{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004956 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004957 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004958
Jens Axboe14587a462020-09-05 11:36:08 -06004959 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004960 return -EINVAL;
4961 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4962 return -EINVAL;
4963
Jens Axboe3529d8c2019-12-19 18:24:38 -07004964 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4965 conn->addr_len = READ_ONCE(sqe->addr2);
4966
4967 if (!io)
4968 return 0;
4969
4970 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004971 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004972}
4973
Pavel Begunkov889fca72021-02-10 00:03:09 +00004974static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004975{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004976 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004977 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004978 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004979 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004980
Jens Axboee8c2bc12020-08-15 18:44:09 -07004981 if (req->async_data) {
4982 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004983 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004984 ret = move_addr_to_kernel(req->connect.addr,
4985 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004986 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004987 if (ret)
4988 goto out;
4989 io = &__io;
4990 }
4991
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004992 file_flags = force_nonblock ? O_NONBLOCK : 0;
4993
Jens Axboee8c2bc12020-08-15 18:44:09 -07004994 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004995 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004996 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004997 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004998 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004999 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005000 ret = -ENOMEM;
5001 goto out;
5002 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005003 io = req->async_data;
5004 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005005 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005006 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005007 if (ret == -ERESTARTSYS)
5008 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005009out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005010 if (ret < 0)
5011 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005012 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005013 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005014}
YueHaibing469956e2020-03-04 15:53:52 +08005015#else /* !CONFIG_NET */
5016static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5017{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005018 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005019}
5020
Pavel Begunkov889fca72021-02-10 00:03:09 +00005021static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005022{
YueHaibing469956e2020-03-04 15:53:52 +08005023 return -EOPNOTSUPP;
5024}
5025
Pavel Begunkov889fca72021-02-10 00:03:09 +00005026static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005027{
5028 return -EOPNOTSUPP;
5029}
5030
5031static int io_recvmsg_prep(struct io_kiocb *req,
5032 const struct io_uring_sqe *sqe)
5033{
5034 return -EOPNOTSUPP;
5035}
5036
Pavel Begunkov889fca72021-02-10 00:03:09 +00005037static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005038{
5039 return -EOPNOTSUPP;
5040}
5041
Pavel Begunkov889fca72021-02-10 00:03:09 +00005042static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005043{
5044 return -EOPNOTSUPP;
5045}
5046
5047static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5048{
5049 return -EOPNOTSUPP;
5050}
5051
Pavel Begunkov889fca72021-02-10 00:03:09 +00005052static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005053{
5054 return -EOPNOTSUPP;
5055}
5056
5057static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5058{
5059 return -EOPNOTSUPP;
5060}
5061
Pavel Begunkov889fca72021-02-10 00:03:09 +00005062static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005063{
5064 return -EOPNOTSUPP;
5065}
5066#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005067
Jens Axboed7718a92020-02-14 22:23:12 -07005068struct io_poll_table {
5069 struct poll_table_struct pt;
5070 struct io_kiocb *req;
5071 int error;
5072};
5073
Jens Axboed7718a92020-02-14 22:23:12 -07005074static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5075 __poll_t mask, task_work_func_t func)
5076{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005077 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005078
5079 /* for instances that support it check for an event match first: */
5080 if (mask && !(mask & poll->events))
5081 return 0;
5082
5083 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5084
5085 list_del_init(&poll->wait.entry);
5086
Jens Axboed7718a92020-02-14 22:23:12 -07005087 req->result = mask;
5088 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005089 percpu_ref_get(&req->ctx->refs);
5090
Jens Axboed7718a92020-02-14 22:23:12 -07005091 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005092 * If this fails, then the task is exiting. When a task exits, the
5093 * work gets canceled, so just cancel this request as well instead
5094 * of executing it. We can't safely execute it anyway, as we may not
5095 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005096 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005097 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005098 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005099 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005100 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005101 }
Jens Axboed7718a92020-02-14 22:23:12 -07005102 return 1;
5103}
5104
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005105static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5106 __acquires(&req->ctx->completion_lock)
5107{
5108 struct io_ring_ctx *ctx = req->ctx;
5109
5110 if (!req->result && !READ_ONCE(poll->canceled)) {
5111 struct poll_table_struct pt = { ._key = poll->events };
5112
5113 req->result = vfs_poll(req->file, &pt) & poll->events;
5114 }
5115
5116 spin_lock_irq(&ctx->completion_lock);
5117 if (!req->result && !READ_ONCE(poll->canceled)) {
5118 add_wait_queue(poll->head, &poll->wait);
5119 return true;
5120 }
5121
5122 return false;
5123}
5124
Jens Axboed4e7cd32020-08-15 11:44:50 -07005125static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005126{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005127 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005128 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005129 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005130 return req->apoll->double_poll;
5131}
5132
5133static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5134{
5135 if (req->opcode == IORING_OP_POLL_ADD)
5136 return &req->poll;
5137 return &req->apoll->poll;
5138}
5139
5140static void io_poll_remove_double(struct io_kiocb *req)
5141{
5142 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005143
5144 lockdep_assert_held(&req->ctx->completion_lock);
5145
5146 if (poll && poll->head) {
5147 struct wait_queue_head *head = poll->head;
5148
5149 spin_lock(&head->lock);
5150 list_del_init(&poll->wait.entry);
5151 if (poll->wait.private)
5152 refcount_dec(&req->refs);
5153 poll->head = NULL;
5154 spin_unlock(&head->lock);
5155 }
5156}
5157
5158static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5159{
5160 struct io_ring_ctx *ctx = req->ctx;
5161
Jens Axboed4e7cd32020-08-15 11:44:50 -07005162 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005163 req->poll.done = true;
5164 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5165 io_commit_cqring(ctx);
5166}
5167
Jens Axboe18bceab2020-05-15 11:56:54 -06005168static void io_poll_task_func(struct callback_head *cb)
5169{
5170 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005171 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005172 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005173
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005174 if (io_poll_rewait(req, &req->poll)) {
5175 spin_unlock_irq(&ctx->completion_lock);
5176 } else {
5177 hash_del(&req->hash_node);
5178 io_poll_complete(req, req->result, 0);
5179 spin_unlock_irq(&ctx->completion_lock);
5180
5181 nxt = io_put_req_find_next(req);
5182 io_cqring_ev_posted(ctx);
5183 if (nxt)
5184 __io_req_task_submit(nxt);
5185 }
5186
Jens Axboe6d816e02020-08-11 08:04:14 -06005187 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005188}
5189
5190static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5191 int sync, void *key)
5192{
5193 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005194 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005195 __poll_t mask = key_to_poll(key);
5196
5197 /* for instances that support it check for an event match first: */
5198 if (mask && !(mask & poll->events))
5199 return 0;
5200
Jens Axboe8706e042020-09-28 08:38:54 -06005201 list_del_init(&wait->entry);
5202
Jens Axboe807abcb2020-07-17 17:09:27 -06005203 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005204 bool done;
5205
Jens Axboe807abcb2020-07-17 17:09:27 -06005206 spin_lock(&poll->head->lock);
5207 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005208 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005209 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005210 /* make sure double remove sees this as being gone */
5211 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005212 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005213 if (!done) {
5214 /* use wait func handler, so it matches the rq type */
5215 poll->wait.func(&poll->wait, mode, sync, key);
5216 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005217 }
5218 refcount_dec(&req->refs);
5219 return 1;
5220}
5221
5222static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5223 wait_queue_func_t wake_func)
5224{
5225 poll->head = NULL;
5226 poll->done = false;
5227 poll->canceled = false;
5228 poll->events = events;
5229 INIT_LIST_HEAD(&poll->wait.entry);
5230 init_waitqueue_func_entry(&poll->wait, wake_func);
5231}
5232
5233static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005234 struct wait_queue_head *head,
5235 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005236{
5237 struct io_kiocb *req = pt->req;
5238
5239 /*
5240 * If poll->head is already set, it's because the file being polled
5241 * uses multiple waitqueues for poll handling (eg one for read, one
5242 * for write). Setup a separate io_poll_iocb if this happens.
5243 */
5244 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005245 struct io_poll_iocb *poll_one = poll;
5246
Jens Axboe18bceab2020-05-15 11:56:54 -06005247 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005248 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005249 pt->error = -EINVAL;
5250 return;
5251 }
5252 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5253 if (!poll) {
5254 pt->error = -ENOMEM;
5255 return;
5256 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005257 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005258 refcount_inc(&req->refs);
5259 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005260 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005261 }
5262
5263 pt->error = 0;
5264 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005265
5266 if (poll->events & EPOLLEXCLUSIVE)
5267 add_wait_queue_exclusive(head, &poll->wait);
5268 else
5269 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005270}
5271
5272static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5273 struct poll_table_struct *p)
5274{
5275 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005276 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005277
Jens Axboe807abcb2020-07-17 17:09:27 -06005278 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005279}
5280
Jens Axboed7718a92020-02-14 22:23:12 -07005281static void io_async_task_func(struct callback_head *cb)
5282{
5283 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5284 struct async_poll *apoll = req->apoll;
5285 struct io_ring_ctx *ctx = req->ctx;
5286
5287 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5288
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005289 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005290 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005291 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005292 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005293 }
5294
Jens Axboe31067252020-05-17 17:43:31 -06005295 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005296 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005297 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005298
Jens Axboed4e7cd32020-08-15 11:44:50 -07005299 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005300 spin_unlock_irq(&ctx->completion_lock);
5301
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005302 if (!READ_ONCE(apoll->poll.canceled))
5303 __io_req_task_submit(req);
5304 else
5305 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005306
Jens Axboe6d816e02020-08-11 08:04:14 -06005307 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005308 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005309 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005310}
5311
5312static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5313 void *key)
5314{
5315 struct io_kiocb *req = wait->private;
5316 struct io_poll_iocb *poll = &req->apoll->poll;
5317
5318 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5319 key_to_poll(key));
5320
5321 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5322}
5323
5324static void io_poll_req_insert(struct io_kiocb *req)
5325{
5326 struct io_ring_ctx *ctx = req->ctx;
5327 struct hlist_head *list;
5328
5329 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5330 hlist_add_head(&req->hash_node, list);
5331}
5332
5333static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5334 struct io_poll_iocb *poll,
5335 struct io_poll_table *ipt, __poll_t mask,
5336 wait_queue_func_t wake_func)
5337 __acquires(&ctx->completion_lock)
5338{
5339 struct io_ring_ctx *ctx = req->ctx;
5340 bool cancel = false;
5341
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005342 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005343 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005344 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005345 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005346
5347 ipt->pt._key = mask;
5348 ipt->req = req;
5349 ipt->error = -EINVAL;
5350
Jens Axboed7718a92020-02-14 22:23:12 -07005351 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5352
5353 spin_lock_irq(&ctx->completion_lock);
5354 if (likely(poll->head)) {
5355 spin_lock(&poll->head->lock);
5356 if (unlikely(list_empty(&poll->wait.entry))) {
5357 if (ipt->error)
5358 cancel = true;
5359 ipt->error = 0;
5360 mask = 0;
5361 }
5362 if (mask || ipt->error)
5363 list_del_init(&poll->wait.entry);
5364 else if (cancel)
5365 WRITE_ONCE(poll->canceled, true);
5366 else if (!poll->done) /* actually waiting for an event */
5367 io_poll_req_insert(req);
5368 spin_unlock(&poll->head->lock);
5369 }
5370
5371 return mask;
5372}
5373
5374static bool io_arm_poll_handler(struct io_kiocb *req)
5375{
5376 const struct io_op_def *def = &io_op_defs[req->opcode];
5377 struct io_ring_ctx *ctx = req->ctx;
5378 struct async_poll *apoll;
5379 struct io_poll_table ipt;
5380 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005381 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005382
5383 if (!req->file || !file_can_poll(req->file))
5384 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005385 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005386 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005387 if (def->pollin)
5388 rw = READ;
5389 else if (def->pollout)
5390 rw = WRITE;
5391 else
5392 return false;
5393 /* if we can't nonblock try, then no point in arming a poll handler */
5394 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005395 return false;
5396
5397 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5398 if (unlikely(!apoll))
5399 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005400 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005401
5402 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005403 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005404
Nathan Chancellor8755d972020-03-02 16:01:19 -07005405 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005406 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005407 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005408 if (def->pollout)
5409 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005410
5411 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5412 if ((req->opcode == IORING_OP_RECVMSG) &&
5413 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5414 mask &= ~POLLIN;
5415
Jens Axboed7718a92020-02-14 22:23:12 -07005416 mask |= POLLERR | POLLPRI;
5417
5418 ipt.pt._qproc = io_async_queue_proc;
5419
5420 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5421 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005422 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005423 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005424 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005425 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005426 kfree(apoll);
5427 return false;
5428 }
5429 spin_unlock_irq(&ctx->completion_lock);
5430 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5431 apoll->poll.events);
5432 return true;
5433}
5434
5435static bool __io_poll_remove_one(struct io_kiocb *req,
5436 struct io_poll_iocb *poll)
5437{
Jens Axboeb41e9852020-02-17 09:52:41 -07005438 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005439
5440 spin_lock(&poll->head->lock);
5441 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005442 if (!list_empty(&poll->wait.entry)) {
5443 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005444 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005445 }
5446 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005447 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005448 return do_complete;
5449}
5450
5451static bool io_poll_remove_one(struct io_kiocb *req)
5452{
5453 bool do_complete;
5454
Jens Axboed4e7cd32020-08-15 11:44:50 -07005455 io_poll_remove_double(req);
5456
Jens Axboed7718a92020-02-14 22:23:12 -07005457 if (req->opcode == IORING_OP_POLL_ADD) {
5458 do_complete = __io_poll_remove_one(req, &req->poll);
5459 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005460 struct async_poll *apoll = req->apoll;
5461
Jens Axboed7718a92020-02-14 22:23:12 -07005462 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005463 do_complete = __io_poll_remove_one(req, &apoll->poll);
5464 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005465 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005466 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005467 kfree(apoll);
5468 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005469 }
5470
Jens Axboeb41e9852020-02-17 09:52:41 -07005471 if (do_complete) {
5472 io_cqring_fill_event(req, -ECANCELED);
5473 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005474 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005475 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005476 }
5477
5478 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005479}
5480
Jens Axboe76e1b642020-09-26 15:05:03 -06005481/*
5482 * Returns true if we found and killed one or more poll requests
5483 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005484static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5485 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005486{
Jens Axboe78076bb2019-12-04 19:56:40 -07005487 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005488 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005489 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005490
5491 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005492 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5493 struct hlist_head *list;
5494
5495 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005496 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005497 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005498 posted += io_poll_remove_one(req);
5499 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005500 }
5501 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005502
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005503 if (posted)
5504 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005505
5506 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005507}
5508
Jens Axboe47f46762019-11-09 17:43:02 -07005509static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5510{
Jens Axboe78076bb2019-12-04 19:56:40 -07005511 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005512 struct io_kiocb *req;
5513
Jens Axboe78076bb2019-12-04 19:56:40 -07005514 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5515 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005516 if (sqe_addr != req->user_data)
5517 continue;
5518 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005519 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005520 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005521 }
5522
5523 return -ENOENT;
5524}
5525
Jens Axboe3529d8c2019-12-19 18:24:38 -07005526static int io_poll_remove_prep(struct io_kiocb *req,
5527 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005528{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005529 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5530 return -EINVAL;
5531 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5532 sqe->poll_events)
5533 return -EINVAL;
5534
Pavel Begunkov018043b2020-10-27 23:17:18 +00005535 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005536 return 0;
5537}
5538
5539/*
5540 * Find a running poll command that matches one specified in sqe->addr,
5541 * and remove it if found.
5542 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005543static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005544{
5545 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005546 int ret;
5547
Jens Axboe221c5eb2019-01-17 09:41:58 -07005548 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005549 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005550 spin_unlock_irq(&ctx->completion_lock);
5551
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005552 if (ret < 0)
5553 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005554 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005555 return 0;
5556}
5557
Jens Axboe221c5eb2019-01-17 09:41:58 -07005558static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5559 void *key)
5560{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005561 struct io_kiocb *req = wait->private;
5562 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005563
Jens Axboed7718a92020-02-14 22:23:12 -07005564 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005565}
5566
Jens Axboe221c5eb2019-01-17 09:41:58 -07005567static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5568 struct poll_table_struct *p)
5569{
5570 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5571
Jens Axboee8c2bc12020-08-15 18:44:09 -07005572 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005573}
5574
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005576{
5577 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005578 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005579
5580 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5581 return -EINVAL;
5582 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5583 return -EINVAL;
5584
Jiufei Xue5769a352020-06-17 17:53:55 +08005585 events = READ_ONCE(sqe->poll32_events);
5586#ifdef __BIG_ENDIAN
5587 events = swahw32(events);
5588#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005589 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5590 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005591 return 0;
5592}
5593
Pavel Begunkov61e98202021-02-10 00:03:08 +00005594static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005595{
5596 struct io_poll_iocb *poll = &req->poll;
5597 struct io_ring_ctx *ctx = req->ctx;
5598 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005599 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005600
Jens Axboed7718a92020-02-14 22:23:12 -07005601 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005602
Jens Axboed7718a92020-02-14 22:23:12 -07005603 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5604 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005605
Jens Axboe8c838782019-03-12 15:48:16 -06005606 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005607 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005608 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005609 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005610 spin_unlock_irq(&ctx->completion_lock);
5611
Jens Axboe8c838782019-03-12 15:48:16 -06005612 if (mask) {
5613 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005614 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005615 }
Jens Axboe8c838782019-03-12 15:48:16 -06005616 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005617}
5618
Jens Axboe5262f562019-09-17 12:26:57 -06005619static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5620{
Jens Axboead8a48a2019-11-15 08:49:11 -07005621 struct io_timeout_data *data = container_of(timer,
5622 struct io_timeout_data, timer);
5623 struct io_kiocb *req = data->req;
5624 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005625 unsigned long flags;
5626
Jens Axboe5262f562019-09-17 12:26:57 -06005627 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005628 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005629 atomic_set(&req->ctx->cq_timeouts,
5630 atomic_read(&req->ctx->cq_timeouts) + 1);
5631
Jens Axboe78e19bb2019-11-06 15:21:34 -07005632 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005633 io_commit_cqring(ctx);
5634 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5635
5636 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005637 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005638 io_put_req(req);
5639 return HRTIMER_NORESTART;
5640}
5641
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005642static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5643 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005644{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005645 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005646 struct io_kiocb *req;
5647 int ret = -ENOENT;
5648
5649 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5650 if (user_data == req->user_data) {
5651 ret = 0;
5652 break;
5653 }
5654 }
5655
5656 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005657 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005658
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005659 io = req->async_data;
5660 ret = hrtimer_try_to_cancel(&io->timer);
5661 if (ret == -1)
5662 return ERR_PTR(-EALREADY);
5663 list_del_init(&req->timeout.list);
5664 return req;
5665}
5666
5667static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5668{
5669 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5670
5671 if (IS_ERR(req))
5672 return PTR_ERR(req);
5673
5674 req_set_fail_links(req);
5675 io_cqring_fill_event(req, -ECANCELED);
5676 io_put_req_deferred(req, 1);
5677 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005678}
5679
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005680static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5681 struct timespec64 *ts, enum hrtimer_mode mode)
5682{
5683 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5684 struct io_timeout_data *data;
5685
5686 if (IS_ERR(req))
5687 return PTR_ERR(req);
5688
5689 req->timeout.off = 0; /* noseq */
5690 data = req->async_data;
5691 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5692 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5693 data->timer.function = io_timeout_fn;
5694 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5695 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005696}
5697
Jens Axboe3529d8c2019-12-19 18:24:38 -07005698static int io_timeout_remove_prep(struct io_kiocb *req,
5699 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005700{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005701 struct io_timeout_rem *tr = &req->timeout_rem;
5702
Jens Axboeb29472e2019-12-17 18:50:29 -07005703 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5704 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005705 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5706 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005707 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005708 return -EINVAL;
5709
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005710 tr->addr = READ_ONCE(sqe->addr);
5711 tr->flags = READ_ONCE(sqe->timeout_flags);
5712 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5713 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5714 return -EINVAL;
5715 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5716 return -EFAULT;
5717 } else if (tr->flags) {
5718 /* timeout removal doesn't support flags */
5719 return -EINVAL;
5720 }
5721
Jens Axboeb29472e2019-12-17 18:50:29 -07005722 return 0;
5723}
5724
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005725static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5726{
5727 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5728 : HRTIMER_MODE_REL;
5729}
5730
Jens Axboe11365042019-10-16 09:08:32 -06005731/*
5732 * Remove or update an existing timeout command
5733 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005734static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005735{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005736 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005737 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005738 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005739
Jens Axboe11365042019-10-16 09:08:32 -06005740 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005741 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005742 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005743 else
5744 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5745 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005746
Jens Axboe47f46762019-11-09 17:43:02 -07005747 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005748 io_commit_cqring(ctx);
5749 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005750 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005751 if (ret < 0)
5752 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005753 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005754 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005755}
5756
Jens Axboe3529d8c2019-12-19 18:24:38 -07005757static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005758 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005759{
Jens Axboead8a48a2019-11-15 08:49:11 -07005760 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005761 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005762 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005763
Jens Axboead8a48a2019-11-15 08:49:11 -07005764 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005765 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005766 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005767 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005768 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005769 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005770 flags = READ_ONCE(sqe->timeout_flags);
5771 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005772 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005773
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005774 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005775
Jens Axboee8c2bc12020-08-15 18:44:09 -07005776 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005777 return -ENOMEM;
5778
Jens Axboee8c2bc12020-08-15 18:44:09 -07005779 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005780 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005781
5782 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005783 return -EFAULT;
5784
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005785 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005786 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5787 return 0;
5788}
5789
Pavel Begunkov61e98202021-02-10 00:03:08 +00005790static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005791{
Jens Axboead8a48a2019-11-15 08:49:11 -07005792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005793 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005794 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005795 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005796
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005797 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005798
Jens Axboe5262f562019-09-17 12:26:57 -06005799 /*
5800 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005801 * timeout event to be satisfied. If it isn't set, then this is
5802 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005803 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005804 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005805 entry = ctx->timeout_list.prev;
5806 goto add;
5807 }
Jens Axboe5262f562019-09-17 12:26:57 -06005808
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005809 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5810 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005811
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005812 /* Update the last seq here in case io_flush_timeouts() hasn't.
5813 * This is safe because ->completion_lock is held, and submissions
5814 * and completions are never mixed in the same ->completion_lock section.
5815 */
5816 ctx->cq_last_tm_flush = tail;
5817
Jens Axboe5262f562019-09-17 12:26:57 -06005818 /*
5819 * Insertion sort, ensuring the first entry in the list is always
5820 * the one we need first.
5821 */
Jens Axboe5262f562019-09-17 12:26:57 -06005822 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005823 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5824 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005825
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005826 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005827 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005828 /* nxt.seq is behind @tail, otherwise would've been completed */
5829 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005830 break;
5831 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005832add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005833 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005834 data->timer.function = io_timeout_fn;
5835 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005836 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005837 return 0;
5838}
5839
Jens Axboe62755e32019-10-28 21:49:21 -06005840static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005841{
Jens Axboe62755e32019-10-28 21:49:21 -06005842 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005843
Jens Axboe62755e32019-10-28 21:49:21 -06005844 return req->user_data == (unsigned long) data;
5845}
5846
Jens Axboee977d6d2019-11-05 12:39:45 -07005847static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005848{
Jens Axboe62755e32019-10-28 21:49:21 -06005849 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005850 int ret = 0;
5851
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005852 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005853 switch (cancel_ret) {
5854 case IO_WQ_CANCEL_OK:
5855 ret = 0;
5856 break;
5857 case IO_WQ_CANCEL_RUNNING:
5858 ret = -EALREADY;
5859 break;
5860 case IO_WQ_CANCEL_NOTFOUND:
5861 ret = -ENOENT;
5862 break;
5863 }
5864
Jens Axboee977d6d2019-11-05 12:39:45 -07005865 return ret;
5866}
5867
Jens Axboe47f46762019-11-09 17:43:02 -07005868static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5869 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005870 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005871{
5872 unsigned long flags;
5873 int ret;
5874
5875 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5876 if (ret != -ENOENT) {
5877 spin_lock_irqsave(&ctx->completion_lock, flags);
5878 goto done;
5879 }
5880
5881 spin_lock_irqsave(&ctx->completion_lock, flags);
5882 ret = io_timeout_cancel(ctx, sqe_addr);
5883 if (ret != -ENOENT)
5884 goto done;
5885 ret = io_poll_cancel(ctx, sqe_addr);
5886done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005887 if (!ret)
5888 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005889 io_cqring_fill_event(req, ret);
5890 io_commit_cqring(ctx);
5891 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5892 io_cqring_ev_posted(ctx);
5893
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005894 if (ret < 0)
5895 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005896 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005897}
5898
Jens Axboe3529d8c2019-12-19 18:24:38 -07005899static int io_async_cancel_prep(struct io_kiocb *req,
5900 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005901{
Jens Axboefbf23842019-12-17 18:45:56 -07005902 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005903 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005904 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5905 return -EINVAL;
5906 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005907 return -EINVAL;
5908
Jens Axboefbf23842019-12-17 18:45:56 -07005909 req->cancel.addr = READ_ONCE(sqe->addr);
5910 return 0;
5911}
5912
Pavel Begunkov61e98202021-02-10 00:03:08 +00005913static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005914{
5915 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005916
Pavel Begunkov014db002020-03-03 21:33:12 +03005917 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005918 return 0;
5919}
5920
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005921static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005922 const struct io_uring_sqe *sqe)
5923{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005924 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5925 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005926 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5927 return -EINVAL;
5928 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005929 return -EINVAL;
5930
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005931 req->rsrc_update.offset = READ_ONCE(sqe->off);
5932 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5933 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005934 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005935 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005936 return 0;
5937}
5938
Pavel Begunkov889fca72021-02-10 00:03:09 +00005939static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005940{
5941 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005942 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005943 int ret;
5944
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005945 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005946 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005947
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005948 up.offset = req->rsrc_update.offset;
5949 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005950
5951 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005952 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005953 mutex_unlock(&ctx->uring_lock);
5954
5955 if (ret < 0)
5956 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005957 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005958 return 0;
5959}
5960
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005961static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005962{
Jens Axboed625c6e2019-12-17 19:53:05 -07005963 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005964 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005965 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005966 case IORING_OP_READV:
5967 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005968 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005969 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005970 case IORING_OP_WRITEV:
5971 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005972 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005973 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005974 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005975 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005976 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005977 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005978 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005979 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005980 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005981 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005982 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005983 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005984 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005985 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005986 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005987 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005988 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005989 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005990 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005991 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005992 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005993 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005994 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005995 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005996 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005997 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005998 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005999 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006000 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006001 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006002 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006003 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006004 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006005 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006006 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006007 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006008 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006009 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006010 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006011 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006012 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006013 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006014 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006015 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006016 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006017 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006018 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006019 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006020 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006021 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006022 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006023 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006024 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006025 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006026 case IORING_OP_SHUTDOWN:
6027 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006028 case IORING_OP_RENAMEAT:
6029 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006030 case IORING_OP_UNLINKAT:
6031 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006032 }
6033
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6035 req->opcode);
6036 return-EINVAL;
6037}
6038
Jens Axboedef596e2019-01-09 08:59:42 -07006039static int io_req_defer_prep(struct io_kiocb *req,
6040 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006041{
Jens Axboedef596e2019-01-09 08:59:42 -07006042 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006044 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006045 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006047}
6048
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006049static u32 io_get_sequence(struct io_kiocb *req)
6050{
6051 struct io_kiocb *pos;
6052 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006053 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006054
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006055 io_for_each_link(pos, req)
6056 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006057
6058 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6059 return total_submitted - nr_reqs;
6060}
6061
Jens Axboe3529d8c2019-12-19 18:24:38 -07006062static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063{
6064 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006065 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006067 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068
6069 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006070 if (likely(list_empty_careful(&ctx->defer_list) &&
6071 !(req->flags & REQ_F_IO_DRAIN)))
6072 return 0;
6073
6074 seq = io_get_sequence(req);
6075 /* Still a chance to pass the sequence check */
6076 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006077 return 0;
6078
Jens Axboee8c2bc12020-08-15 18:44:09 -07006079 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006080 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006081 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006082 return ret;
6083 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006084 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006085 de = kmalloc(sizeof(*de), GFP_KERNEL);
6086 if (!de)
6087 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006088
6089 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006090 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006091 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006092 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006093 io_queue_async_work(req);
6094 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006095 }
6096
6097 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006098 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006099 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006100 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006101 spin_unlock_irq(&ctx->completion_lock);
6102 return -EIOCBQUEUED;
6103}
Jens Axboeedafcce2019-01-09 09:16:05 -07006104
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006105static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006106{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006107 if (req->flags & REQ_F_BUFFER_SELECTED) {
6108 switch (req->opcode) {
6109 case IORING_OP_READV:
6110 case IORING_OP_READ_FIXED:
6111 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006112 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006113 break;
6114 case IORING_OP_RECVMSG:
6115 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006116 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006117 break;
6118 }
6119 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006120 }
6121
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006122 if (req->flags & REQ_F_NEED_CLEANUP) {
6123 switch (req->opcode) {
6124 case IORING_OP_READV:
6125 case IORING_OP_READ_FIXED:
6126 case IORING_OP_READ:
6127 case IORING_OP_WRITEV:
6128 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006129 case IORING_OP_WRITE: {
6130 struct io_async_rw *io = req->async_data;
6131 if (io->free_iovec)
6132 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006133 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006134 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006135 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006136 case IORING_OP_SENDMSG: {
6137 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006138
6139 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006140 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006141 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006142 case IORING_OP_SPLICE:
6143 case IORING_OP_TEE:
6144 io_put_file(req, req->splice.file_in,
6145 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6146 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006147 case IORING_OP_OPENAT:
6148 case IORING_OP_OPENAT2:
6149 if (req->open.filename)
6150 putname(req->open.filename);
6151 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006152 case IORING_OP_RENAMEAT:
6153 putname(req->rename.oldpath);
6154 putname(req->rename.newpath);
6155 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006156 case IORING_OP_UNLINKAT:
6157 putname(req->unlink.filename);
6158 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006159 }
6160 req->flags &= ~REQ_F_NEED_CLEANUP;
6161 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006162}
6163
Pavel Begunkov889fca72021-02-10 00:03:09 +00006164static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006165{
Jens Axboeedafcce2019-01-09 09:16:05 -07006166 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006167 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006168
Jens Axboed625c6e2019-12-17 19:53:05 -07006169 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006171 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006172 break;
6173 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006174 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006175 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006176 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177 break;
6178 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006179 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006180 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006181 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006182 break;
6183 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006184 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185 break;
6186 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006187 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006188 break;
6189 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006190 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006191 break;
6192 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006193 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006194 break;
6195 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006196 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006197 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006198 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006199 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006200 break;
6201 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006202 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006203 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006204 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006205 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206 break;
6207 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006208 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 break;
6210 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006211 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212 break;
6213 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006214 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006215 break;
6216 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006217 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218 break;
6219 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006220 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006221 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006222 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006223 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006224 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006225 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006226 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006227 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006228 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006229 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006230 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006231 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006232 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006233 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006234 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006235 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006236 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006237 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006238 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006239 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006240 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006241 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006242 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006243 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006244 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006245 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006246 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006247 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006248 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006249 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006250 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006251 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006252 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006253 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006254 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006255 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006256 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006257 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006258 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006259 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006260 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006261 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006262 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006263 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006264 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006265 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006266 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006267 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006268 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006269 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006270 default:
6271 ret = -EINVAL;
6272 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006273 }
6274
6275 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006276 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006277
Jens Axboeb5325762020-05-19 21:20:27 -06006278 /* If the op doesn't have a file, we're not polling for it */
6279 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006280 const bool in_async = io_wq_current_is_worker();
6281
Jens Axboe11ba8202020-01-15 21:51:17 -07006282 /* workqueue context doesn't hold uring_lock, grab it now */
6283 if (in_async)
6284 mutex_lock(&ctx->uring_lock);
6285
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006286 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006287
6288 if (in_async)
6289 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290 }
6291
6292 return 0;
6293}
6294
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006295static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006296{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006297 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006298 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006299 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006301 timeout = io_prep_linked_timeout(req);
6302 if (timeout)
6303 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006304
Jens Axboe4014d942021-01-19 15:53:54 -07006305 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006306 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006307
Jens Axboe561fb042019-10-24 07:25:42 -06006308 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006309 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006310 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006311 /*
6312 * We can get EAGAIN for polled IO even though we're
6313 * forcing a sync submission from here, since we can't
6314 * wait for request slots on the block side.
6315 */
6316 if (ret != -EAGAIN)
6317 break;
6318 cond_resched();
6319 } while (1);
6320 }
Jens Axboe31b51512019-01-18 22:56:34 -07006321
Jens Axboe561fb042019-10-24 07:25:42 -06006322 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006323 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006324
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006325 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6326 lock_ctx = req->ctx;
6327
6328 /*
6329 * io_iopoll_complete() does not hold completion_lock to
6330 * complete polled io, so here for polled io, we can not call
6331 * io_req_complete() directly, otherwise there maybe concurrent
6332 * access to cqring, defer_list, etc, which is not safe. Given
6333 * that io_iopoll_complete() is always called under uring_lock,
6334 * so here for polled io, we also get uring_lock to complete
6335 * it.
6336 */
6337 if (lock_ctx)
6338 mutex_lock(&lock_ctx->uring_lock);
6339
6340 req_set_fail_links(req);
6341 io_req_complete(req, ret);
6342
6343 if (lock_ctx)
6344 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006345 }
Jens Axboe31b51512019-01-18 22:56:34 -07006346}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006347
Jens Axboe65e19f52019-10-26 07:20:21 -06006348static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6349 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006350{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006351 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006352
Jens Axboe05f3fb32019-12-09 11:22:50 -07006353 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006354 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006355}
6356
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006357static struct file *io_file_get(struct io_submit_state *state,
6358 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006359{
6360 struct io_ring_ctx *ctx = req->ctx;
6361 struct file *file;
6362
6363 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006364 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006365 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006366 fd = array_index_nospec(fd, ctx->nr_user_files);
6367 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006368 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006369 } else {
6370 trace_io_uring_file_get(ctx, fd);
6371 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006372 }
6373
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006374 if (file && unlikely(file->f_op == &io_uring_fops))
6375 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006376 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006377}
6378
Jens Axboe2665abf2019-11-05 12:40:47 -07006379static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6380{
Jens Axboead8a48a2019-11-15 08:49:11 -07006381 struct io_timeout_data *data = container_of(timer,
6382 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006383 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006384 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006385 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006386
6387 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006388 prev = req->timeout.head;
6389 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006390
6391 /*
6392 * We don't expect the list to be empty, that will only happen if we
6393 * race with the completion of the linked work.
6394 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006395 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006396 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006397 else
6398 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006399 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6400
6401 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006402 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006403 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006404 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006405 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006406 io_req_complete_post(req, -ETIME, 0);
6407 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006408 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006409 return HRTIMER_NORESTART;
6410}
6411
Jens Axboe7271ef32020-08-10 09:55:22 -06006412static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006413{
Jens Axboe76a46e02019-11-10 23:34:16 -07006414 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006415 * If the back reference is NULL, then our linked request finished
6416 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006417 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006418 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006419 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006420
Jens Axboead8a48a2019-11-15 08:49:11 -07006421 data->timer.function = io_link_timeout_fn;
6422 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6423 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006424 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006425}
6426
6427static void io_queue_linked_timeout(struct io_kiocb *req)
6428{
6429 struct io_ring_ctx *ctx = req->ctx;
6430
6431 spin_lock_irq(&ctx->completion_lock);
6432 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006433 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006434
Jens Axboe2665abf2019-11-05 12:40:47 -07006435 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006436 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006437}
6438
Jens Axboead8a48a2019-11-15 08:49:11 -07006439static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006440{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006441 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006443 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6444 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006445 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006446
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006447 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006448 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006449 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006450 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006451}
6452
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006453static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006454{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006455 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006456 const struct cred *old_creds = NULL;
Pavel Begunkov889fca72021-02-10 00:03:09 +00006457 int ret, issue_flags = IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006458
Pavel Begunkov889fca72021-02-10 00:03:09 +00006459 if (cs)
6460 issue_flags |= IO_URING_F_COMPLETE_DEFER;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006461again:
6462 linked_timeout = io_prep_linked_timeout(req);
6463
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006464 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6465 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006466 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006467 if (old_creds)
6468 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006469 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006470 old_creds = NULL; /* restored original creds */
6471 else
Jens Axboe98447d62020-10-14 10:48:51 -06006472 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006473 }
6474
Pavel Begunkov889fca72021-02-10 00:03:09 +00006475 ret = io_issue_sqe(req, issue_flags);
Jens Axboe491381ce2019-10-17 09:20:46 -06006476
6477 /*
6478 * We async punt it if the file wasn't marked NOWAIT, or if the file
6479 * doesn't support non-blocking read/write attempts
6480 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006481 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006482 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006483 /*
6484 * Queued up for async execution, worker will release
6485 * submit reference when the iocb is actually submitted.
6486 */
6487 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006488 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006489
Pavel Begunkovf063c542020-07-25 14:41:59 +03006490 if (linked_timeout)
6491 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006492 } else if (likely(!ret)) {
6493 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006494 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006495 cs->reqs[cs->nr++] = req;
6496 if (cs->nr == IO_COMPL_BATCH)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006497 io_submit_flush_completions(cs, req->ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006498 req = NULL;
6499 } else {
6500 req = io_put_req_find_next(req);
6501 }
6502
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006503 if (linked_timeout)
6504 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006505
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006506 if (req) {
6507 if (!(req->flags & REQ_F_FORCE_ASYNC))
6508 goto again;
6509 io_queue_async_work(req);
6510 }
6511 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006512 /* un-prep timeout, so it'll be killed as any other linked */
6513 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006514 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006515 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006516 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006517 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006518
Jens Axboe193155c2020-02-22 23:22:19 -07006519 if (old_creds)
6520 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006521}
6522
Jens Axboef13fad72020-06-22 09:34:30 -06006523static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6524 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006525{
6526 int ret;
6527
Jens Axboe3529d8c2019-12-19 18:24:38 -07006528 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006529 if (ret) {
6530 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006531fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006532 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006533 io_put_req(req);
6534 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006535 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006536 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006537 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006538 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006539 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006540 goto fail_req;
6541 }
Jens Axboece35a472019-12-17 08:04:44 -07006542 io_queue_async_work(req);
6543 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006544 if (sqe) {
6545 ret = io_req_prep(req, sqe);
6546 if (unlikely(ret))
6547 goto fail_req;
6548 }
6549 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006550 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006551}
6552
Jens Axboef13fad72020-06-22 09:34:30 -06006553static inline void io_queue_link_head(struct io_kiocb *req,
6554 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006555{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006556 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006557 io_put_req(req);
6558 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006559 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006560 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006561}
6562
Pavel Begunkov863e0562020-10-27 23:25:35 +00006563struct io_submit_link {
6564 struct io_kiocb *head;
6565 struct io_kiocb *last;
6566};
6567
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006568static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006569 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006570{
Jackie Liua197f662019-11-08 08:09:12 -07006571 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006572 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006573
Jens Axboe9e645e112019-05-10 16:07:28 -06006574 /*
6575 * If we already have a head request, queue this one for async
6576 * submittal once the head completes. If we don't have a head but
6577 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6578 * submitted sync once the chain is complete. If none of those
6579 * conditions are true (normal request), then just queue it.
6580 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006581 if (link->head) {
6582 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006583
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006584 /*
6585 * Taking sequential execution of a link, draining both sides
6586 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6587 * requests in the link. So, it drains the head and the
6588 * next after the link request. The last one is done via
6589 * drain_next flag to persist the effect across calls.
6590 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006591 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006592 head->flags |= REQ_F_IO_DRAIN;
6593 ctx->drain_next = 1;
6594 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006595 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006596 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006597 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006598 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006599 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006600 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006601 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006602 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006603 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006604
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006605 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006606 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006607 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006608 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006609 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006610 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006611 if (unlikely(ctx->drain_next)) {
6612 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006613 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006614 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006615 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006616 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006617 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006618 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006619 link->head = req;
6620 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006621 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006622 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006623 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006624 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006625
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006626 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006627}
6628
Jens Axboe9a56a232019-01-09 09:06:50 -07006629/*
6630 * Batched submission is done, ensure local IO is flushed out.
6631 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006632static void io_submit_state_end(struct io_submit_state *state,
6633 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006634{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006635 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006636 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006637 if (state->plug_started)
6638 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006639 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07006640}
6641
6642/*
6643 * Start submission side cache.
6644 */
6645static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006646 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006647{
Jens Axboe27926b62020-10-28 09:33:23 -06006648 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006649 state->ios_left = max_ios;
6650}
6651
Jens Axboe2b188cc2019-01-07 10:46:33 -07006652static void io_commit_sqring(struct io_ring_ctx *ctx)
6653{
Hristo Venev75b28af2019-08-26 17:23:46 +00006654 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006655
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006656 /*
6657 * Ensure any loads from the SQEs are done at this point,
6658 * since once we write the new head, the application could
6659 * write new data to them.
6660 */
6661 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662}
6663
6664/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006665 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006666 * that is mapped by userspace. This means that care needs to be taken to
6667 * ensure that reads are stable, as we cannot rely on userspace always
6668 * being a good citizen. If members of the sqe are validated and then later
6669 * used, it's important that those reads are done through READ_ONCE() to
6670 * prevent a re-load down the line.
6671 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006672static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673{
Hristo Venev75b28af2019-08-26 17:23:46 +00006674 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006675 unsigned head;
6676
6677 /*
6678 * The cached sq head (or cq tail) serves two purposes:
6679 *
6680 * 1) allows us to batch the cost of updating the user visible
6681 * head updates.
6682 * 2) allows the kernel side to track the head on its own, even
6683 * though the application is the one updating it.
6684 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006685 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006686 if (likely(head < ctx->sq_entries))
6687 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006688
6689 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006690 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006691 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006692 return NULL;
6693}
6694
6695static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6696{
6697 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006698}
6699
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006700/*
6701 * Check SQE restrictions (opcode and flags).
6702 *
6703 * Returns 'true' if SQE is allowed, 'false' otherwise.
6704 */
6705static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6706 struct io_kiocb *req,
6707 unsigned int sqe_flags)
6708{
6709 if (!ctx->restricted)
6710 return true;
6711
6712 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6713 return false;
6714
6715 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6716 ctx->restrictions.sqe_flags_required)
6717 return false;
6718
6719 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6720 ctx->restrictions.sqe_flags_required))
6721 return false;
6722
6723 return true;
6724}
6725
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006726#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6727 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6728 IOSQE_BUFFER_SELECT)
6729
6730static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006731 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006732{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006733 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006734 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006735 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006736
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006737 req->opcode = READ_ONCE(sqe->opcode);
6738 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006739 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006740 req->file = NULL;
6741 req->ctx = ctx;
6742 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006743 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006744 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006745 /* one is dropped after submission, the other at completion */
6746 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006747 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006748 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006749
6750 if (unlikely(req->opcode >= IORING_OP_LAST))
6751 return -EINVAL;
6752
Jens Axboe28cea78a2020-09-14 10:51:17 -06006753 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006754 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006755
6756 sqe_flags = READ_ONCE(sqe->flags);
6757 /* enforce forwards compatibility on users */
6758 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6759 return -EINVAL;
6760
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006761 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6762 return -EACCES;
6763
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006764 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6765 !io_op_defs[req->opcode].buffer_select)
6766 return -EOPNOTSUPP;
6767
6768 id = READ_ONCE(sqe->personality);
6769 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006770 struct io_identity *iod;
6771
Jens Axboe1e6fa522020-10-15 08:46:24 -06006772 iod = idr_find(&ctx->personality_idr, id);
6773 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006774 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006775 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006776
6777 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006778 get_cred(iod->creds);
6779 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006780 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006781 }
6782
6783 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006784 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006785 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006786
Jens Axboe27926b62020-10-28 09:33:23 -06006787 /*
6788 * Plug now if we have more than 1 IO left after this, and the target
6789 * is potentially a read/write to block based storage.
6790 */
6791 if (!state->plug_started && state->ios_left > 1 &&
6792 io_op_defs[req->opcode].plug) {
6793 blk_start_plug(&state->plug);
6794 state->plug_started = true;
6795 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006796
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006797 ret = 0;
6798 if (io_op_defs[req->opcode].needs_file) {
6799 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006800
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006801 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006802 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006803 ret = -EBADF;
6804 }
6805
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006806 state->ios_left--;
6807 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006808}
6809
Jens Axboe0f212202020-09-13 13:09:39 -06006810static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006811{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006812 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006813 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006814
Jens Axboec4a2ed72019-11-21 21:01:26 -07006815 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006816 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006817 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006818 return -EBUSY;
6819 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006820
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006821 /* make sure SQ entry isn't read before tail */
6822 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006823
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006824 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6825 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006826
Jens Axboed8a6df12020-10-15 16:24:45 -06006827 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006828 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006829
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006830 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006831 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006832
Jens Axboe6c271ce2019-01-10 11:22:30 -07006833 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006834 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006835 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006836 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006837
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006838 sqe = io_get_sqe(ctx);
6839 if (unlikely(!sqe)) {
6840 io_consume_sqe(ctx);
6841 break;
6842 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006843 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006844 if (unlikely(!req)) {
6845 if (!submitted)
6846 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006847 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006848 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006849 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006850 /* will complete beyond this point, count as submitted */
6851 submitted++;
6852
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006853 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006854 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006855fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006856 io_put_req(req);
6857 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006858 break;
6859 }
6860
Jens Axboe354420f2020-01-08 18:55:15 -07006861 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006862 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006863 err = io_submit_sqe(req, sqe, &link, &ctx->submit_state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006864 if (err)
6865 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006866 }
6867
Pavel Begunkov9466f432020-01-25 22:34:01 +03006868 if (unlikely(submitted != nr)) {
6869 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006870 struct io_uring_task *tctx = current->io_uring;
6871 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006872
Jens Axboed8a6df12020-10-15 16:24:45 -06006873 percpu_ref_put_many(&ctx->refs, unused);
6874 percpu_counter_sub(&tctx->inflight, unused);
6875 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006876 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006877 if (link.head)
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006878 io_queue_link_head(link.head, &ctx->submit_state.comp);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006879 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006880
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006881 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6882 io_commit_sqring(ctx);
6883
Jens Axboe6c271ce2019-01-10 11:22:30 -07006884 return submitted;
6885}
6886
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006887static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6888{
6889 /* Tell userspace we may need a wakeup call */
6890 spin_lock_irq(&ctx->completion_lock);
6891 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6892 spin_unlock_irq(&ctx->completion_lock);
6893}
6894
6895static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6896{
6897 spin_lock_irq(&ctx->completion_lock);
6898 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6899 spin_unlock_irq(&ctx->completion_lock);
6900}
6901
Xiaoguang Wang08369242020-11-03 14:15:59 +08006902static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006903{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006904 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006905 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006906
Jens Axboec8d1ba52020-09-14 11:07:26 -06006907 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006908 /* if we're handling multiple rings, cap submit size for fairness */
6909 if (cap_entries && to_submit > 8)
6910 to_submit = 8;
6911
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006912 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6913 unsigned nr_events = 0;
6914
Xiaoguang Wang08369242020-11-03 14:15:59 +08006915 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006916 if (!list_empty(&ctx->iopoll_list))
6917 io_do_iopoll(ctx, &nr_events, 0);
6918
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006919 if (to_submit && !ctx->sqo_dead &&
6920 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006921 ret = io_submit_sqes(ctx, to_submit);
6922 mutex_unlock(&ctx->uring_lock);
6923 }
Jens Axboe90554202020-09-03 12:12:41 -06006924
6925 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6926 wake_up(&ctx->sqo_sq_wait);
6927
Xiaoguang Wang08369242020-11-03 14:15:59 +08006928 return ret;
6929}
6930
6931static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6932{
6933 struct io_ring_ctx *ctx;
6934 unsigned sq_thread_idle = 0;
6935
6936 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6937 if (sq_thread_idle < ctx->sq_thread_idle)
6938 sq_thread_idle = ctx->sq_thread_idle;
6939 }
6940
6941 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006942}
6943
Jens Axboe69fb2132020-09-14 11:16:23 -06006944static void io_sqd_init_new(struct io_sq_data *sqd)
6945{
6946 struct io_ring_ctx *ctx;
6947
6948 while (!list_empty(&sqd->ctx_new_list)) {
6949 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006950 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6951 complete(&ctx->sq_thread_comp);
6952 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006953
6954 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006955}
6956
Jens Axboe6c271ce2019-01-10 11:22:30 -07006957static int io_sq_thread(void *data)
6958{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006959 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006960 struct files_struct *old_files = current->files;
6961 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006962 const struct cred *old_cred = NULL;
6963 struct io_sq_data *sqd = data;
6964 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006965 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006966 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006967
Jens Axboe28cea78a2020-09-14 10:51:17 -06006968 task_lock(current);
6969 current->files = NULL;
6970 current->nsproxy = NULL;
6971 task_unlock(current);
6972
Jens Axboe69fb2132020-09-14 11:16:23 -06006973 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006974 int ret;
6975 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006976
6977 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006978 * Any changes to the sqd lists are synchronized through the
6979 * kthread parking. This synchronizes the thread vs users,
6980 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006981 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006982 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006983 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006984 /*
6985 * When sq thread is unparked, in case the previous park operation
6986 * comes from io_put_sq_data(), which means that sq thread is going
6987 * to be stopped, so here needs to have a check.
6988 */
6989 if (kthread_should_stop())
6990 break;
6991 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006992
Xiaoguang Wang08369242020-11-03 14:15:59 +08006993 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006994 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006995 timeout = jiffies + sqd->sq_thread_idle;
6996 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006997
Xiaoguang Wang08369242020-11-03 14:15:59 +08006998 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006999 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007000 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7001 if (current->cred != ctx->creds) {
7002 if (old_cred)
7003 revert_creds(old_cred);
7004 old_cred = override_creds(ctx->creds);
7005 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007006 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007007#ifdef CONFIG_AUDIT
7008 current->loginuid = ctx->loginuid;
7009 current->sessionid = ctx->sessionid;
7010#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007011
Xiaoguang Wang08369242020-11-03 14:15:59 +08007012 ret = __io_sq_thread(ctx, cap_entries);
7013 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7014 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007015
Jens Axboe28cea78a2020-09-14 10:51:17 -06007016 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007017 }
7018
Xiaoguang Wang08369242020-11-03 14:15:59 +08007019 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007020 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007021 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007022 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007023 if (sqt_spin)
7024 timeout = jiffies + sqd->sq_thread_idle;
7025 continue;
7026 }
7027
Xiaoguang Wang08369242020-11-03 14:15:59 +08007028 needs_sched = true;
7029 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7030 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7031 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7032 !list_empty_careful(&ctx->iopoll_list)) {
7033 needs_sched = false;
7034 break;
7035 }
7036 if (io_sqring_entries(ctx)) {
7037 needs_sched = false;
7038 break;
7039 }
7040 }
7041
Hao Xu8b28fdf2021-01-31 22:39:04 +08007042 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007043 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7044 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007045
Jens Axboe69fb2132020-09-14 11:16:23 -06007046 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007047 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7048 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007049 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007050
7051 finish_wait(&sqd->wait, &wait);
7052 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007053 }
7054
Jens Axboe4c6e2772020-07-01 11:29:10 -06007055 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007056 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007057
Dennis Zhou91d8f512020-09-16 13:41:05 -07007058 if (cur_css)
7059 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007060 if (old_cred)
7061 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007062
Jens Axboe28cea78a2020-09-14 10:51:17 -06007063 task_lock(current);
7064 current->files = old_files;
7065 current->nsproxy = old_nsproxy;
7066 task_unlock(current);
7067
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007068 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007069
Jens Axboe6c271ce2019-01-10 11:22:30 -07007070 return 0;
7071}
7072
Jens Axboebda52162019-09-24 13:47:15 -06007073struct io_wait_queue {
7074 struct wait_queue_entry wq;
7075 struct io_ring_ctx *ctx;
7076 unsigned to_wait;
7077 unsigned nr_timeouts;
7078};
7079
Pavel Begunkov6c503152021-01-04 20:36:36 +00007080static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007081{
7082 struct io_ring_ctx *ctx = iowq->ctx;
7083
7084 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007085 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007086 * started waiting. For timeouts, we always want to return to userspace,
7087 * regardless of event count.
7088 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007089 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007090 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7091}
7092
7093static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7094 int wake_flags, void *key)
7095{
7096 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7097 wq);
7098
Pavel Begunkov6c503152021-01-04 20:36:36 +00007099 /*
7100 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7101 * the task, and the next invocation will do it.
7102 */
7103 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7104 return autoremove_wake_function(curr, mode, wake_flags, key);
7105 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007106}
7107
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007108static int io_run_task_work_sig(void)
7109{
7110 if (io_run_task_work())
7111 return 1;
7112 if (!signal_pending(current))
7113 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007114 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7115 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007116 return -EINTR;
7117}
7118
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007119/* when returns >0, the caller should retry */
7120static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7121 struct io_wait_queue *iowq,
7122 signed long *timeout)
7123{
7124 int ret;
7125
7126 /* make sure we run task_work before checking for signals */
7127 ret = io_run_task_work_sig();
7128 if (ret || io_should_wake(iowq))
7129 return ret;
7130 /* let the caller flush overflows, retry */
7131 if (test_bit(0, &ctx->cq_check_overflow))
7132 return 1;
7133
7134 *timeout = schedule_timeout(*timeout);
7135 return !*timeout ? -ETIME : 1;
7136}
7137
Jens Axboe2b188cc2019-01-07 10:46:33 -07007138/*
7139 * Wait until events become available, if we don't already have some. The
7140 * application must reap them itself, as they reside on the shared cq ring.
7141 */
7142static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007143 const sigset_t __user *sig, size_t sigsz,
7144 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007145{
Jens Axboebda52162019-09-24 13:47:15 -06007146 struct io_wait_queue iowq = {
7147 .wq = {
7148 .private = current,
7149 .func = io_wake_function,
7150 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7151 },
7152 .ctx = ctx,
7153 .to_wait = min_events,
7154 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007155 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007156 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7157 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007158
Jens Axboeb41e9852020-02-17 09:52:41 -07007159 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007160 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7161 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007162 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007163 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007164 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007165 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007166
7167 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007168#ifdef CONFIG_COMPAT
7169 if (in_compat_syscall())
7170 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007171 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007172 else
7173#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007174 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007175
Jens Axboe2b188cc2019-01-07 10:46:33 -07007176 if (ret)
7177 return ret;
7178 }
7179
Hao Xuc73ebb62020-11-03 10:54:37 +08007180 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007181 struct timespec64 ts;
7182
Hao Xuc73ebb62020-11-03 10:54:37 +08007183 if (get_timespec64(&ts, uts))
7184 return -EFAULT;
7185 timeout = timespec64_to_jiffies(&ts);
7186 }
7187
Jens Axboebda52162019-09-24 13:47:15 -06007188 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007189 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007190 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007191 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007192 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7193 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007194 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7195 finish_wait(&ctx->wait, &iowq.wq);
7196 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007197
Jens Axboeb7db41c2020-07-04 08:55:50 -06007198 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007199
Hristo Venev75b28af2019-08-26 17:23:46 +00007200 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007201}
7202
Jens Axboe6b063142019-01-10 22:13:58 -07007203static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7204{
7205#if defined(CONFIG_UNIX)
7206 if (ctx->ring_sock) {
7207 struct sock *sock = ctx->ring_sock->sk;
7208 struct sk_buff *skb;
7209
7210 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7211 kfree_skb(skb);
7212 }
7213#else
7214 int i;
7215
Jens Axboe65e19f52019-10-26 07:20:21 -06007216 for (i = 0; i < ctx->nr_user_files; i++) {
7217 struct file *file;
7218
7219 file = io_file_from_index(ctx, i);
7220 if (file)
7221 fput(file);
7222 }
Jens Axboe6b063142019-01-10 22:13:58 -07007223#endif
7224}
7225
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007226static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007227{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007228 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007229
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007230 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007231 complete(&data->done);
7232}
7233
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007234static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7235{
7236 spin_lock_bh(&ctx->rsrc_ref_lock);
7237}
7238
7239static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7240{
7241 spin_unlock_bh(&ctx->rsrc_ref_lock);
7242}
7243
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007244static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7245 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007246 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007247{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007248 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007249 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007250 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007251 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007252 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007253}
7254
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007255static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7256 struct io_ring_ctx *ctx,
7257 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007258{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007259 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007260 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007261
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007262 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007263 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007264 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007265 if (ref_node)
7266 percpu_ref_kill(&ref_node->refs);
7267
7268 percpu_ref_kill(&data->refs);
7269
7270 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007271 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007272 do {
7273 ret = wait_for_completion_interruptible(&data->done);
7274 if (!ret)
7275 break;
7276 ret = io_run_task_work_sig();
7277 if (ret < 0) {
7278 percpu_ref_resurrect(&data->refs);
7279 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007280 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007281 return ret;
7282 }
7283 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007284
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007285 destroy_fixed_rsrc_ref_node(backup_node);
7286 return 0;
7287}
7288
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007289static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7290{
7291 struct fixed_rsrc_data *data;
7292
7293 data = kzalloc(sizeof(*data), GFP_KERNEL);
7294 if (!data)
7295 return NULL;
7296
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007297 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007298 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7299 kfree(data);
7300 return NULL;
7301 }
7302 data->ctx = ctx;
7303 init_completion(&data->done);
7304 return data;
7305}
7306
7307static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7308{
7309 percpu_ref_exit(&data->refs);
7310 kfree(data->table);
7311 kfree(data);
7312}
7313
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007314static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7315{
7316 struct fixed_rsrc_data *data = ctx->file_data;
7317 struct fixed_rsrc_ref_node *backup_node;
7318 unsigned nr_tables, i;
7319 int ret;
7320
7321 if (!data)
7322 return -ENXIO;
7323 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7324 if (!backup_node)
7325 return -ENOMEM;
7326 init_fixed_file_ref_node(ctx, backup_node);
7327
7328 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7329 if (ret)
7330 return ret;
7331
Jens Axboe6b063142019-01-10 22:13:58 -07007332 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007333 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7334 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007335 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007336 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007337 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007338 ctx->nr_user_files = 0;
7339 return 0;
7340}
7341
Jens Axboe534ca6d2020-09-02 13:52:19 -06007342static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007343{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007344 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007345 /*
7346 * The park is a bit of a work-around, without it we get
7347 * warning spews on shutdown with SQPOLL set and affinity
7348 * set to a single CPU.
7349 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007350 if (sqd->thread) {
7351 kthread_park(sqd->thread);
7352 kthread_stop(sqd->thread);
7353 }
7354
7355 kfree(sqd);
7356 }
7357}
7358
Jens Axboeaa061652020-09-02 14:50:27 -06007359static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7360{
7361 struct io_ring_ctx *ctx_attach;
7362 struct io_sq_data *sqd;
7363 struct fd f;
7364
7365 f = fdget(p->wq_fd);
7366 if (!f.file)
7367 return ERR_PTR(-ENXIO);
7368 if (f.file->f_op != &io_uring_fops) {
7369 fdput(f);
7370 return ERR_PTR(-EINVAL);
7371 }
7372
7373 ctx_attach = f.file->private_data;
7374 sqd = ctx_attach->sq_data;
7375 if (!sqd) {
7376 fdput(f);
7377 return ERR_PTR(-EINVAL);
7378 }
7379
7380 refcount_inc(&sqd->refs);
7381 fdput(f);
7382 return sqd;
7383}
7384
Jens Axboe534ca6d2020-09-02 13:52:19 -06007385static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7386{
7387 struct io_sq_data *sqd;
7388
Jens Axboeaa061652020-09-02 14:50:27 -06007389 if (p->flags & IORING_SETUP_ATTACH_WQ)
7390 return io_attach_sq_data(p);
7391
Jens Axboe534ca6d2020-09-02 13:52:19 -06007392 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7393 if (!sqd)
7394 return ERR_PTR(-ENOMEM);
7395
7396 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007397 INIT_LIST_HEAD(&sqd->ctx_list);
7398 INIT_LIST_HEAD(&sqd->ctx_new_list);
7399 mutex_init(&sqd->ctx_lock);
7400 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007401 init_waitqueue_head(&sqd->wait);
7402 return sqd;
7403}
7404
Jens Axboe69fb2132020-09-14 11:16:23 -06007405static void io_sq_thread_unpark(struct io_sq_data *sqd)
7406 __releases(&sqd->lock)
7407{
7408 if (!sqd->thread)
7409 return;
7410 kthread_unpark(sqd->thread);
7411 mutex_unlock(&sqd->lock);
7412}
7413
7414static void io_sq_thread_park(struct io_sq_data *sqd)
7415 __acquires(&sqd->lock)
7416{
7417 if (!sqd->thread)
7418 return;
7419 mutex_lock(&sqd->lock);
7420 kthread_park(sqd->thread);
7421}
7422
Jens Axboe534ca6d2020-09-02 13:52:19 -06007423static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7424{
7425 struct io_sq_data *sqd = ctx->sq_data;
7426
7427 if (sqd) {
7428 if (sqd->thread) {
7429 /*
7430 * We may arrive here from the error branch in
7431 * io_sq_offload_create() where the kthread is created
7432 * without being waked up, thus wake it up now to make
7433 * sure the wait will complete.
7434 */
7435 wake_up_process(sqd->thread);
7436 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007437
7438 io_sq_thread_park(sqd);
7439 }
7440
7441 mutex_lock(&sqd->ctx_lock);
7442 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007443 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007444 mutex_unlock(&sqd->ctx_lock);
7445
Xiaoguang Wang08369242020-11-03 14:15:59 +08007446 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007447 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007448
7449 io_put_sq_data(sqd);
7450 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007451 }
7452}
7453
Jens Axboe6b063142019-01-10 22:13:58 -07007454static void io_finish_async(struct io_ring_ctx *ctx)
7455{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007456 io_sq_thread_stop(ctx);
7457
Jens Axboe561fb042019-10-24 07:25:42 -06007458 if (ctx->io_wq) {
7459 io_wq_destroy(ctx->io_wq);
7460 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007461 }
7462}
7463
7464#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007465/*
7466 * Ensure the UNIX gc is aware of our file set, so we are certain that
7467 * the io_uring can be safely unregistered on process exit, even if we have
7468 * loops in the file referencing.
7469 */
7470static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7471{
7472 struct sock *sk = ctx->ring_sock->sk;
7473 struct scm_fp_list *fpl;
7474 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007475 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007476
Jens Axboe6b063142019-01-10 22:13:58 -07007477 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7478 if (!fpl)
7479 return -ENOMEM;
7480
7481 skb = alloc_skb(0, GFP_KERNEL);
7482 if (!skb) {
7483 kfree(fpl);
7484 return -ENOMEM;
7485 }
7486
7487 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007488
Jens Axboe08a45172019-10-03 08:11:03 -06007489 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007490 fpl->user = get_uid(ctx->user);
7491 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007492 struct file *file = io_file_from_index(ctx, i + offset);
7493
7494 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007495 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007496 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007497 unix_inflight(fpl->user, fpl->fp[nr_files]);
7498 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007499 }
7500
Jens Axboe08a45172019-10-03 08:11:03 -06007501 if (nr_files) {
7502 fpl->max = SCM_MAX_FD;
7503 fpl->count = nr_files;
7504 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007505 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007506 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7507 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007508
Jens Axboe08a45172019-10-03 08:11:03 -06007509 for (i = 0; i < nr_files; i++)
7510 fput(fpl->fp[i]);
7511 } else {
7512 kfree_skb(skb);
7513 kfree(fpl);
7514 }
Jens Axboe6b063142019-01-10 22:13:58 -07007515
7516 return 0;
7517}
7518
7519/*
7520 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7521 * causes regular reference counting to break down. We rely on the UNIX
7522 * garbage collection to take care of this problem for us.
7523 */
7524static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7525{
7526 unsigned left, total;
7527 int ret = 0;
7528
7529 total = 0;
7530 left = ctx->nr_user_files;
7531 while (left) {
7532 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007533
7534 ret = __io_sqe_files_scm(ctx, this_files, total);
7535 if (ret)
7536 break;
7537 left -= this_files;
7538 total += this_files;
7539 }
7540
7541 if (!ret)
7542 return 0;
7543
7544 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007545 struct file *file = io_file_from_index(ctx, total);
7546
7547 if (file)
7548 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007549 total++;
7550 }
7551
7552 return ret;
7553}
7554#else
7555static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7556{
7557 return 0;
7558}
7559#endif
7560
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007561static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007562 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007563{
7564 int i;
7565
7566 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007567 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007568 unsigned this_files;
7569
7570 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7571 table->files = kcalloc(this_files, sizeof(struct file *),
7572 GFP_KERNEL);
7573 if (!table->files)
7574 break;
7575 nr_files -= this_files;
7576 }
7577
7578 if (i == nr_tables)
7579 return 0;
7580
7581 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007582 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007583 kfree(table->files);
7584 }
7585 return 1;
7586}
7587
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007588static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007589{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007590 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007591#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007592 struct sock *sock = ctx->ring_sock->sk;
7593 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7594 struct sk_buff *skb;
7595 int i;
7596
7597 __skb_queue_head_init(&list);
7598
7599 /*
7600 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7601 * remove this entry and rearrange the file array.
7602 */
7603 skb = skb_dequeue(head);
7604 while (skb) {
7605 struct scm_fp_list *fp;
7606
7607 fp = UNIXCB(skb).fp;
7608 for (i = 0; i < fp->count; i++) {
7609 int left;
7610
7611 if (fp->fp[i] != file)
7612 continue;
7613
7614 unix_notinflight(fp->user, fp->fp[i]);
7615 left = fp->count - 1 - i;
7616 if (left) {
7617 memmove(&fp->fp[i], &fp->fp[i + 1],
7618 left * sizeof(struct file *));
7619 }
7620 fp->count--;
7621 if (!fp->count) {
7622 kfree_skb(skb);
7623 skb = NULL;
7624 } else {
7625 __skb_queue_tail(&list, skb);
7626 }
7627 fput(file);
7628 file = NULL;
7629 break;
7630 }
7631
7632 if (!file)
7633 break;
7634
7635 __skb_queue_tail(&list, skb);
7636
7637 skb = skb_dequeue(head);
7638 }
7639
7640 if (skb_peek(&list)) {
7641 spin_lock_irq(&head->lock);
7642 while ((skb = __skb_dequeue(&list)) != NULL)
7643 __skb_queue_tail(head, skb);
7644 spin_unlock_irq(&head->lock);
7645 }
7646#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007647 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007648#endif
7649}
7650
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007651static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007652{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007653 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7654 struct io_ring_ctx *ctx = rsrc_data->ctx;
7655 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007656
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007657 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7658 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007659 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007660 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007661 }
7662
Xiaoguang Wang05589552020-03-31 14:05:18 +08007663 percpu_ref_exit(&ref_node->refs);
7664 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007665 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007666}
7667
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007668static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007669{
7670 struct io_ring_ctx *ctx;
7671 struct llist_node *node;
7672
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007673 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7674 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007675
7676 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007677 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007678 struct llist_node *next = node->next;
7679
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007680 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7681 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007682 node = next;
7683 }
7684}
7685
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007686static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7687 unsigned i)
7688{
7689 struct fixed_rsrc_table *table;
7690
7691 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7692 return &table->files[i & IORING_FILE_TABLE_MASK];
7693}
7694
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007695static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007696{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007697 struct fixed_rsrc_ref_node *ref_node;
7698 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007699 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007700 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007701 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007702
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007703 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7704 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007705 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007706
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007707 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007708 ref_node->done = true;
7709
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007710 while (!list_empty(&ctx->rsrc_ref_list)) {
7711 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007712 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007713 /* recycle ref nodes in order */
7714 if (!ref_node->done)
7715 break;
7716 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007717 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007718 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007719 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007720
7721 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007722 delay = 0;
7723
Jens Axboe4a38aed22020-05-14 17:21:15 -06007724 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007725 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007726 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007727 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007728}
7729
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007730static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007731 struct io_ring_ctx *ctx)
7732{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007733 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007734
7735 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7736 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007737 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007738
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007739 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007740 0, GFP_KERNEL)) {
7741 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007742 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007743 }
7744 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007745 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007746 ref_node->done = false;
7747 return ref_node;
7748}
7749
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007750static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7751 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007752{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007753 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007754 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007755}
7756
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007757static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007758{
7759 percpu_ref_exit(&ref_node->refs);
7760 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007761}
7762
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007763
Jens Axboe05f3fb32019-12-09 11:22:50 -07007764static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7765 unsigned nr_args)
7766{
7767 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007768 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007769 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007770 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007771 struct fixed_rsrc_ref_node *ref_node;
7772 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007773
7774 if (ctx->file_data)
7775 return -EBUSY;
7776 if (!nr_args)
7777 return -EINVAL;
7778 if (nr_args > IORING_MAX_FIXED_FILES)
7779 return -EMFILE;
7780
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007781 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007782 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007783 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007784 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007785
7786 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007787 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007788 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007789 if (!file_data->table)
7790 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007791
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007792 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007793 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007794
7795 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007796 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7797 ret = -EFAULT;
7798 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007799 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007800 /* allow sparse sets */
7801 if (fd == -1)
7802 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007803
Jens Axboe05f3fb32019-12-09 11:22:50 -07007804 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007805 ret = -EBADF;
7806 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007807 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007808
7809 /*
7810 * Don't allow io_uring instances to be registered. If UNIX
7811 * isn't enabled, then this causes a reference cycle and this
7812 * instance can never get freed. If UNIX is enabled we'll
7813 * handle it just fine, but there's still no point in allowing
7814 * a ring fd as it doesn't support regular read/write anyway.
7815 */
7816 if (file->f_op == &io_uring_fops) {
7817 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007818 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007819 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007820 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821 }
7822
Jens Axboe05f3fb32019-12-09 11:22:50 -07007823 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007824 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007825 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007826 return ret;
7827 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007828
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007829 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007830 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007831 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007832 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007833 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007834 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007835
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007836 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007837 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007838out_fput:
7839 for (i = 0; i < ctx->nr_user_files; i++) {
7840 file = io_file_from_index(ctx, i);
7841 if (file)
7842 fput(file);
7843 }
7844 for (i = 0; i < nr_tables; i++)
7845 kfree(file_data->table[i].files);
7846 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007847out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007848 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007849 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850 return ret;
7851}
7852
Jens Axboec3a31e62019-10-03 13:59:56 -06007853static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7854 int index)
7855{
7856#if defined(CONFIG_UNIX)
7857 struct sock *sock = ctx->ring_sock->sk;
7858 struct sk_buff_head *head = &sock->sk_receive_queue;
7859 struct sk_buff *skb;
7860
7861 /*
7862 * See if we can merge this file into an existing skb SCM_RIGHTS
7863 * file set. If there's no room, fall back to allocating a new skb
7864 * and filling it in.
7865 */
7866 spin_lock_irq(&head->lock);
7867 skb = skb_peek(head);
7868 if (skb) {
7869 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7870
7871 if (fpl->count < SCM_MAX_FD) {
7872 __skb_unlink(skb, head);
7873 spin_unlock_irq(&head->lock);
7874 fpl->fp[fpl->count] = get_file(file);
7875 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7876 fpl->count++;
7877 spin_lock_irq(&head->lock);
7878 __skb_queue_head(head, skb);
7879 } else {
7880 skb = NULL;
7881 }
7882 }
7883 spin_unlock_irq(&head->lock);
7884
7885 if (skb) {
7886 fput(file);
7887 return 0;
7888 }
7889
7890 return __io_sqe_files_scm(ctx, 1, index);
7891#else
7892 return 0;
7893#endif
7894}
7895
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007896static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007897{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007898 struct io_rsrc_put *prsrc;
7899 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007901 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7902 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007903 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007904
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007905 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007906 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007907
Hillf Dantona5318d32020-03-23 17:47:15 +08007908 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007909}
7910
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007911static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7912 struct file *file)
7913{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007914 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007915}
7916
Jens Axboe05f3fb32019-12-09 11:22:50 -07007917static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007918 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007919 unsigned nr_args)
7920{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007921 struct fixed_rsrc_data *data = ctx->file_data;
7922 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007923 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007924 __s32 __user *fds;
7925 int fd, i, err;
7926 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007927 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007928
Jens Axboe05f3fb32019-12-09 11:22:50 -07007929 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007930 return -EOVERFLOW;
7931 if (done > ctx->nr_user_files)
7932 return -EINVAL;
7933
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007934 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007935 if (!ref_node)
7936 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007937 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007938
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007939 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007940 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007941 err = 0;
7942 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7943 err = -EFAULT;
7944 break;
7945 }
noah4e0377a2021-01-26 15:23:28 -05007946 if (fd == IORING_REGISTER_FILES_SKIP)
7947 continue;
7948
Pavel Begunkov67973b92021-01-26 13:51:09 +00007949 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007950 file_slot = io_fixed_file_slot(ctx->file_data, i);
7951
7952 if (*file_slot) {
7953 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007954 if (err)
7955 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007956 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007957 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007958 }
7959 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007960 file = fget(fd);
7961 if (!file) {
7962 err = -EBADF;
7963 break;
7964 }
7965 /*
7966 * Don't allow io_uring instances to be registered. If
7967 * UNIX isn't enabled, then this causes a reference
7968 * cycle and this instance can never get freed. If UNIX
7969 * is enabled we'll handle it just fine, but there's
7970 * still no point in allowing a ring fd as it doesn't
7971 * support regular read/write anyway.
7972 */
7973 if (file->f_op == &io_uring_fops) {
7974 fput(file);
7975 err = -EBADF;
7976 break;
7977 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007978 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007979 if (err) {
7980 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007981 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007982 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007983 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007984 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007985 }
7986
Xiaoguang Wang05589552020-03-31 14:05:18 +08007987 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007988 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007989 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007990 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007991 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007992
7993 return done ? done : err;
7994}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007995
Jens Axboe05f3fb32019-12-09 11:22:50 -07007996static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7997 unsigned nr_args)
7998{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007999 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008000
8001 if (!ctx->file_data)
8002 return -ENXIO;
8003 if (!nr_args)
8004 return -EINVAL;
8005 if (copy_from_user(&up, arg, sizeof(up)))
8006 return -EFAULT;
8007 if (up.resv)
8008 return -EINVAL;
8009
8010 return __io_sqe_files_update(ctx, &up, nr_args);
8011}
Jens Axboec3a31e62019-10-03 13:59:56 -06008012
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008013static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008014{
8015 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8016
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008017 req = io_put_req_find_next(req);
8018 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008019}
8020
Pavel Begunkov24369c22020-01-28 03:15:48 +03008021static int io_init_wq_offload(struct io_ring_ctx *ctx,
8022 struct io_uring_params *p)
8023{
8024 struct io_wq_data data;
8025 struct fd f;
8026 struct io_ring_ctx *ctx_attach;
8027 unsigned int concurrency;
8028 int ret = 0;
8029
8030 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008031 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008032 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008033
8034 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8035 /* Do QD, or 4 * CPUS, whatever is smallest */
8036 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8037
8038 ctx->io_wq = io_wq_create(concurrency, &data);
8039 if (IS_ERR(ctx->io_wq)) {
8040 ret = PTR_ERR(ctx->io_wq);
8041 ctx->io_wq = NULL;
8042 }
8043 return ret;
8044 }
8045
8046 f = fdget(p->wq_fd);
8047 if (!f.file)
8048 return -EBADF;
8049
8050 if (f.file->f_op != &io_uring_fops) {
8051 ret = -EINVAL;
8052 goto out_fput;
8053 }
8054
8055 ctx_attach = f.file->private_data;
8056 /* @io_wq is protected by holding the fd */
8057 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8058 ret = -EINVAL;
8059 goto out_fput;
8060 }
8061
8062 ctx->io_wq = ctx_attach->io_wq;
8063out_fput:
8064 fdput(f);
8065 return ret;
8066}
8067
Jens Axboe0f212202020-09-13 13:09:39 -06008068static int io_uring_alloc_task_context(struct task_struct *task)
8069{
8070 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008071 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008072
8073 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8074 if (unlikely(!tctx))
8075 return -ENOMEM;
8076
Jens Axboed8a6df12020-10-15 16:24:45 -06008077 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8078 if (unlikely(ret)) {
8079 kfree(tctx);
8080 return ret;
8081 }
8082
Jens Axboe0f212202020-09-13 13:09:39 -06008083 xa_init(&tctx->xa);
8084 init_waitqueue_head(&tctx->wait);
8085 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008086 atomic_set(&tctx->in_idle, 0);
8087 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008088 io_init_identity(&tctx->__identity);
8089 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008090 task->io_uring = tctx;
8091 return 0;
8092}
8093
8094void __io_uring_free(struct task_struct *tsk)
8095{
8096 struct io_uring_task *tctx = tsk->io_uring;
8097
8098 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008099 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8100 if (tctx->identity != &tctx->__identity)
8101 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008102 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008103 kfree(tctx);
8104 tsk->io_uring = NULL;
8105}
8106
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008107static int io_sq_offload_create(struct io_ring_ctx *ctx,
8108 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008109{
8110 int ret;
8111
Jens Axboe6c271ce2019-01-10 11:22:30 -07008112 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008113 struct io_sq_data *sqd;
8114
Jens Axboe3ec482d2019-04-08 10:51:01 -06008115 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008116 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008117 goto err;
8118
Jens Axboe534ca6d2020-09-02 13:52:19 -06008119 sqd = io_get_sq_data(p);
8120 if (IS_ERR(sqd)) {
8121 ret = PTR_ERR(sqd);
8122 goto err;
8123 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008124
Jens Axboe534ca6d2020-09-02 13:52:19 -06008125 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008126 io_sq_thread_park(sqd);
8127 mutex_lock(&sqd->ctx_lock);
8128 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8129 mutex_unlock(&sqd->ctx_lock);
8130 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008131
Jens Axboe917257d2019-04-13 09:28:55 -06008132 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8133 if (!ctx->sq_thread_idle)
8134 ctx->sq_thread_idle = HZ;
8135
Jens Axboeaa061652020-09-02 14:50:27 -06008136 if (sqd->thread)
8137 goto done;
8138
Jens Axboe6c271ce2019-01-10 11:22:30 -07008139 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008140 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008141
Jens Axboe917257d2019-04-13 09:28:55 -06008142 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008143 if (cpu >= nr_cpu_ids)
8144 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008145 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008146 goto err;
8147
Jens Axboe69fb2132020-09-14 11:16:23 -06008148 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008149 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008150 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008151 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008152 "io_uring-sq");
8153 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008154 if (IS_ERR(sqd->thread)) {
8155 ret = PTR_ERR(sqd->thread);
8156 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008157 goto err;
8158 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008159 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008160 if (ret)
8161 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008162 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8163 /* Can't have SQ_AFF without SQPOLL */
8164 ret = -EINVAL;
8165 goto err;
8166 }
8167
Jens Axboeaa061652020-09-02 14:50:27 -06008168done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008169 ret = io_init_wq_offload(ctx, p);
8170 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008171 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008172
8173 return 0;
8174err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008175 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008176 return ret;
8177}
8178
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008179static void io_sq_offload_start(struct io_ring_ctx *ctx)
8180{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008181 struct io_sq_data *sqd = ctx->sq_data;
8182
8183 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8184 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008185}
8186
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008187static inline void __io_unaccount_mem(struct user_struct *user,
8188 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008189{
8190 atomic_long_sub(nr_pages, &user->locked_vm);
8191}
8192
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008193static inline int __io_account_mem(struct user_struct *user,
8194 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008195{
8196 unsigned long page_limit, cur_pages, new_pages;
8197
8198 /* Don't allow more pages than we can safely lock */
8199 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8200
8201 do {
8202 cur_pages = atomic_long_read(&user->locked_vm);
8203 new_pages = cur_pages + nr_pages;
8204 if (new_pages > page_limit)
8205 return -ENOMEM;
8206 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8207 new_pages) != cur_pages);
8208
8209 return 0;
8210}
8211
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008212static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8213 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008214{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008215 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008216 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008217
Jens Axboe2aede0e2020-09-14 10:45:53 -06008218 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008219 if (acct == ACCT_LOCKED) {
8220 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008221 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008222 mmap_write_unlock(ctx->mm_account);
8223 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008224 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008225 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008226 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008227}
8228
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008229static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8230 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008231{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008232 int ret;
8233
8234 if (ctx->limit_mem) {
8235 ret = __io_account_mem(ctx->user, nr_pages);
8236 if (ret)
8237 return ret;
8238 }
8239
Jens Axboe2aede0e2020-09-14 10:45:53 -06008240 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008241 if (acct == ACCT_LOCKED) {
8242 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008243 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008244 mmap_write_unlock(ctx->mm_account);
8245 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008246 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008247 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008248 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008249
8250 return 0;
8251}
8252
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253static void io_mem_free(void *ptr)
8254{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008255 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256
Mark Rutland52e04ef2019-04-30 17:30:21 +01008257 if (!ptr)
8258 return;
8259
8260 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008261 if (put_page_testzero(page))
8262 free_compound_page(page);
8263}
8264
8265static void *io_mem_alloc(size_t size)
8266{
8267 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8268 __GFP_NORETRY;
8269
8270 return (void *) __get_free_pages(gfp_flags, get_order(size));
8271}
8272
Hristo Venev75b28af2019-08-26 17:23:46 +00008273static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8274 size_t *sq_offset)
8275{
8276 struct io_rings *rings;
8277 size_t off, sq_array_size;
8278
8279 off = struct_size(rings, cqes, cq_entries);
8280 if (off == SIZE_MAX)
8281 return SIZE_MAX;
8282
8283#ifdef CONFIG_SMP
8284 off = ALIGN(off, SMP_CACHE_BYTES);
8285 if (off == 0)
8286 return SIZE_MAX;
8287#endif
8288
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008289 if (sq_offset)
8290 *sq_offset = off;
8291
Hristo Venev75b28af2019-08-26 17:23:46 +00008292 sq_array_size = array_size(sizeof(u32), sq_entries);
8293 if (sq_array_size == SIZE_MAX)
8294 return SIZE_MAX;
8295
8296 if (check_add_overflow(off, sq_array_size, &off))
8297 return SIZE_MAX;
8298
Hristo Venev75b28af2019-08-26 17:23:46 +00008299 return off;
8300}
8301
Jens Axboe2b188cc2019-01-07 10:46:33 -07008302static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8303{
Hristo Venev75b28af2019-08-26 17:23:46 +00008304 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008305
Hristo Venev75b28af2019-08-26 17:23:46 +00008306 pages = (size_t)1 << get_order(
8307 rings_size(sq_entries, cq_entries, NULL));
8308 pages += (size_t)1 << get_order(
8309 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008310
Hristo Venev75b28af2019-08-26 17:23:46 +00008311 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008312}
8313
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008314static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008315{
8316 int i, j;
8317
8318 if (!ctx->user_bufs)
8319 return -ENXIO;
8320
8321 for (i = 0; i < ctx->nr_user_bufs; i++) {
8322 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8323
8324 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008325 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008326
Jens Axboede293932020-09-17 16:19:16 -06008327 if (imu->acct_pages)
8328 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008329 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008330 imu->nr_bvecs = 0;
8331 }
8332
8333 kfree(ctx->user_bufs);
8334 ctx->user_bufs = NULL;
8335 ctx->nr_user_bufs = 0;
8336 return 0;
8337}
8338
8339static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8340 void __user *arg, unsigned index)
8341{
8342 struct iovec __user *src;
8343
8344#ifdef CONFIG_COMPAT
8345 if (ctx->compat) {
8346 struct compat_iovec __user *ciovs;
8347 struct compat_iovec ciov;
8348
8349 ciovs = (struct compat_iovec __user *) arg;
8350 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8351 return -EFAULT;
8352
Jens Axboed55e5f52019-12-11 16:12:15 -07008353 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008354 dst->iov_len = ciov.iov_len;
8355 return 0;
8356 }
8357#endif
8358 src = (struct iovec __user *) arg;
8359 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8360 return -EFAULT;
8361 return 0;
8362}
8363
Jens Axboede293932020-09-17 16:19:16 -06008364/*
8365 * Not super efficient, but this is just a registration time. And we do cache
8366 * the last compound head, so generally we'll only do a full search if we don't
8367 * match that one.
8368 *
8369 * We check if the given compound head page has already been accounted, to
8370 * avoid double accounting it. This allows us to account the full size of the
8371 * page, not just the constituent pages of a huge page.
8372 */
8373static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8374 int nr_pages, struct page *hpage)
8375{
8376 int i, j;
8377
8378 /* check current page array */
8379 for (i = 0; i < nr_pages; i++) {
8380 if (!PageCompound(pages[i]))
8381 continue;
8382 if (compound_head(pages[i]) == hpage)
8383 return true;
8384 }
8385
8386 /* check previously registered pages */
8387 for (i = 0; i < ctx->nr_user_bufs; i++) {
8388 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8389
8390 for (j = 0; j < imu->nr_bvecs; j++) {
8391 if (!PageCompound(imu->bvec[j].bv_page))
8392 continue;
8393 if (compound_head(imu->bvec[j].bv_page) == hpage)
8394 return true;
8395 }
8396 }
8397
8398 return false;
8399}
8400
8401static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8402 int nr_pages, struct io_mapped_ubuf *imu,
8403 struct page **last_hpage)
8404{
8405 int i, ret;
8406
8407 for (i = 0; i < nr_pages; i++) {
8408 if (!PageCompound(pages[i])) {
8409 imu->acct_pages++;
8410 } else {
8411 struct page *hpage;
8412
8413 hpage = compound_head(pages[i]);
8414 if (hpage == *last_hpage)
8415 continue;
8416 *last_hpage = hpage;
8417 if (headpage_already_acct(ctx, pages, i, hpage))
8418 continue;
8419 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8420 }
8421 }
8422
8423 if (!imu->acct_pages)
8424 return 0;
8425
8426 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8427 if (ret)
8428 imu->acct_pages = 0;
8429 return ret;
8430}
8431
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008432static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8433 struct io_mapped_ubuf *imu,
8434 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008435{
8436 struct vm_area_struct **vmas = NULL;
8437 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008438 unsigned long off, start, end, ubuf;
8439 size_t size;
8440 int ret, pret, nr_pages, i;
8441
8442 ubuf = (unsigned long) iov->iov_base;
8443 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8444 start = ubuf >> PAGE_SHIFT;
8445 nr_pages = end - start;
8446
8447 ret = -ENOMEM;
8448
8449 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8450 if (!pages)
8451 goto done;
8452
8453 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8454 GFP_KERNEL);
8455 if (!vmas)
8456 goto done;
8457
8458 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8459 GFP_KERNEL);
8460 if (!imu->bvec)
8461 goto done;
8462
8463 ret = 0;
8464 mmap_read_lock(current->mm);
8465 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8466 pages, vmas);
8467 if (pret == nr_pages) {
8468 /* don't support file backed memory */
8469 for (i = 0; i < nr_pages; i++) {
8470 struct vm_area_struct *vma = vmas[i];
8471
8472 if (vma->vm_file &&
8473 !is_file_hugepages(vma->vm_file)) {
8474 ret = -EOPNOTSUPP;
8475 break;
8476 }
8477 }
8478 } else {
8479 ret = pret < 0 ? pret : -EFAULT;
8480 }
8481 mmap_read_unlock(current->mm);
8482 if (ret) {
8483 /*
8484 * if we did partial map, or found file backed vmas,
8485 * release any pages we did get
8486 */
8487 if (pret > 0)
8488 unpin_user_pages(pages, pret);
8489 kvfree(imu->bvec);
8490 goto done;
8491 }
8492
8493 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8494 if (ret) {
8495 unpin_user_pages(pages, pret);
8496 kvfree(imu->bvec);
8497 goto done;
8498 }
8499
8500 off = ubuf & ~PAGE_MASK;
8501 size = iov->iov_len;
8502 for (i = 0; i < nr_pages; i++) {
8503 size_t vec_len;
8504
8505 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8506 imu->bvec[i].bv_page = pages[i];
8507 imu->bvec[i].bv_len = vec_len;
8508 imu->bvec[i].bv_offset = off;
8509 off = 0;
8510 size -= vec_len;
8511 }
8512 /* store original address for later verification */
8513 imu->ubuf = ubuf;
8514 imu->len = iov->iov_len;
8515 imu->nr_bvecs = nr_pages;
8516 ret = 0;
8517done:
8518 kvfree(pages);
8519 kvfree(vmas);
8520 return ret;
8521}
8522
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008523static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008524{
Jens Axboeedafcce2019-01-09 09:16:05 -07008525 if (ctx->user_bufs)
8526 return -EBUSY;
8527 if (!nr_args || nr_args > UIO_MAXIOV)
8528 return -EINVAL;
8529
8530 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8531 GFP_KERNEL);
8532 if (!ctx->user_bufs)
8533 return -ENOMEM;
8534
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008535 return 0;
8536}
8537
8538static int io_buffer_validate(struct iovec *iov)
8539{
8540 /*
8541 * Don't impose further limits on the size and buffer
8542 * constraints here, we'll -EINVAL later when IO is
8543 * submitted if they are wrong.
8544 */
8545 if (!iov->iov_base || !iov->iov_len)
8546 return -EFAULT;
8547
8548 /* arbitrary limit, but we need something */
8549 if (iov->iov_len > SZ_1G)
8550 return -EFAULT;
8551
8552 return 0;
8553}
8554
8555static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8556 unsigned int nr_args)
8557{
8558 int i, ret;
8559 struct iovec iov;
8560 struct page *last_hpage = NULL;
8561
8562 ret = io_buffers_map_alloc(ctx, nr_args);
8563 if (ret)
8564 return ret;
8565
Jens Axboeedafcce2019-01-09 09:16:05 -07008566 for (i = 0; i < nr_args; i++) {
8567 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008568
8569 ret = io_copy_iov(ctx, &iov, arg, i);
8570 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008571 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008572
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008573 ret = io_buffer_validate(&iov);
8574 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008575 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008576
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008577 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8578 if (ret)
8579 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008580
8581 ctx->nr_user_bufs++;
8582 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008583
8584 if (ret)
8585 io_sqe_buffers_unregister(ctx);
8586
Jens Axboeedafcce2019-01-09 09:16:05 -07008587 return ret;
8588}
8589
Jens Axboe9b402842019-04-11 11:45:41 -06008590static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8591{
8592 __s32 __user *fds = arg;
8593 int fd;
8594
8595 if (ctx->cq_ev_fd)
8596 return -EBUSY;
8597
8598 if (copy_from_user(&fd, fds, sizeof(*fds)))
8599 return -EFAULT;
8600
8601 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8602 if (IS_ERR(ctx->cq_ev_fd)) {
8603 int ret = PTR_ERR(ctx->cq_ev_fd);
8604 ctx->cq_ev_fd = NULL;
8605 return ret;
8606 }
8607
8608 return 0;
8609}
8610
8611static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8612{
8613 if (ctx->cq_ev_fd) {
8614 eventfd_ctx_put(ctx->cq_ev_fd);
8615 ctx->cq_ev_fd = NULL;
8616 return 0;
8617 }
8618
8619 return -ENXIO;
8620}
8621
Jens Axboe5a2e7452020-02-23 16:23:11 -07008622static int __io_destroy_buffers(int id, void *p, void *data)
8623{
8624 struct io_ring_ctx *ctx = data;
8625 struct io_buffer *buf = p;
8626
Jens Axboe067524e2020-03-02 16:32:28 -07008627 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008628 return 0;
8629}
8630
8631static void io_destroy_buffers(struct io_ring_ctx *ctx)
8632{
8633 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8634 idr_destroy(&ctx->io_buffer_idr);
8635}
8636
Jens Axboe2b188cc2019-01-07 10:46:33 -07008637static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8638{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008639 struct io_submit_state *submit_state = &ctx->submit_state;
8640
Jens Axboe6b063142019-01-10 22:13:58 -07008641 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008642 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008643
8644 if (ctx->sqo_task) {
8645 put_task_struct(ctx->sqo_task);
8646 ctx->sqo_task = NULL;
8647 mmdrop(ctx->mm_account);
8648 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008649 }
Jens Axboedef596e2019-01-09 08:59:42 -07008650
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008651 if (submit_state->free_reqs)
8652 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8653 submit_state->reqs);
8654
Dennis Zhou91d8f512020-09-16 13:41:05 -07008655#ifdef CONFIG_BLK_CGROUP
8656 if (ctx->sqo_blkcg_css)
8657 css_put(ctx->sqo_blkcg_css);
8658#endif
8659
Jens Axboe6b063142019-01-10 22:13:58 -07008660 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008661 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008662 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008663 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008664
Jens Axboe2b188cc2019-01-07 10:46:33 -07008665#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008666 if (ctx->ring_sock) {
8667 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008668 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008669 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008670#endif
8671
Hristo Venev75b28af2019-08-26 17:23:46 +00008672 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008673 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008674
8675 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008676 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008677 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008678 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008679 kfree(ctx);
8680}
8681
8682static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8683{
8684 struct io_ring_ctx *ctx = file->private_data;
8685 __poll_t mask = 0;
8686
8687 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008688 /*
8689 * synchronizes with barrier from wq_has_sleeper call in
8690 * io_commit_cqring
8691 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008692 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008693 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008694 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008695 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8696 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008697 mask |= EPOLLIN | EPOLLRDNORM;
8698
8699 return mask;
8700}
8701
8702static int io_uring_fasync(int fd, struct file *file, int on)
8703{
8704 struct io_ring_ctx *ctx = file->private_data;
8705
8706 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8707}
8708
Yejune Deng0bead8c2020-12-24 11:02:20 +08008709static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008710{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008711 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008712
Jens Axboe1e6fa522020-10-15 08:46:24 -06008713 iod = idr_remove(&ctx->personality_idr, id);
8714 if (iod) {
8715 put_cred(iod->creds);
8716 if (refcount_dec_and_test(&iod->count))
8717 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008718 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008719 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008720
8721 return -EINVAL;
8722}
8723
8724static int io_remove_personalities(int id, void *p, void *data)
8725{
8726 struct io_ring_ctx *ctx = data;
8727
8728 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008729 return 0;
8730}
8731
Jens Axboe85faa7b2020-04-09 18:14:00 -06008732static void io_ring_exit_work(struct work_struct *work)
8733{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008734 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8735 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008736
Jens Axboe56952e92020-06-17 15:00:04 -06008737 /*
8738 * If we're doing polled IO and end up having requests being
8739 * submitted async (out-of-line), then completions can come in while
8740 * we're waiting for refs to drop. We need to reap these manually,
8741 * as nobody else will be looking for them.
8742 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008743 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008744 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008745 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008746 io_ring_ctx_free(ctx);
8747}
8748
Jens Axboe00c18642020-12-20 10:45:02 -07008749static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8750{
8751 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8752
8753 return req->ctx == data;
8754}
8755
Jens Axboe2b188cc2019-01-07 10:46:33 -07008756static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8757{
8758 mutex_lock(&ctx->uring_lock);
8759 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008760
8761 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8762 ctx->sqo_dead = 1;
8763
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008764 /* if force is set, the ring is going away. always drop after that */
8765 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008766 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008767 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008768 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008769 mutex_unlock(&ctx->uring_lock);
8770
Pavel Begunkov6b819282020-11-06 13:00:25 +00008771 io_kill_timeouts(ctx, NULL, NULL);
8772 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008773
8774 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008775 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008776
Jens Axboe15dff282019-11-13 09:09:23 -07008777 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008778 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008779
8780 /*
8781 * Do this upfront, so we won't have a grace period where the ring
8782 * is closed but resources aren't reaped yet. This can cause
8783 * spurious failure in setting up a new ring.
8784 */
Jens Axboe760618f2020-07-24 12:53:31 -06008785 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8786 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008787
Jens Axboe85faa7b2020-04-09 18:14:00 -06008788 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008789 /*
8790 * Use system_unbound_wq to avoid spawning tons of event kworkers
8791 * if we're exiting a ton of rings at the same time. It just adds
8792 * noise and overhead, there's no discernable change in runtime
8793 * over using system_wq.
8794 */
8795 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008796}
8797
8798static int io_uring_release(struct inode *inode, struct file *file)
8799{
8800 struct io_ring_ctx *ctx = file->private_data;
8801
8802 file->private_data = NULL;
8803 io_ring_ctx_wait_and_kill(ctx);
8804 return 0;
8805}
8806
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008807struct io_task_cancel {
8808 struct task_struct *task;
8809 struct files_struct *files;
8810};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008811
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008812static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008813{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008814 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008815 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008816 bool ret;
8817
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008818 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008819 unsigned long flags;
8820 struct io_ring_ctx *ctx = req->ctx;
8821
8822 /* protect against races with linked timeouts */
8823 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008824 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008825 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8826 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008827 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008828 }
8829 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008830}
8831
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008832static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008833 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008834 struct files_struct *files)
8835{
8836 struct io_defer_entry *de = NULL;
8837 LIST_HEAD(list);
8838
8839 spin_lock_irq(&ctx->completion_lock);
8840 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008841 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008842 list_cut_position(&list, &ctx->defer_list, &de->list);
8843 break;
8844 }
8845 }
8846 spin_unlock_irq(&ctx->completion_lock);
8847
8848 while (!list_empty(&list)) {
8849 de = list_first_entry(&list, struct io_defer_entry, list);
8850 list_del_init(&de->list);
8851 req_set_fail_links(de->req);
8852 io_put_req(de->req);
8853 io_req_complete(de->req, -ECANCELED);
8854 kfree(de);
8855 }
8856}
8857
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008858static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8859 struct task_struct *task,
8860 struct files_struct *files)
8861{
8862 struct io_task_cancel cancel = { .task = task, .files = files, };
8863
8864 while (1) {
8865 enum io_wq_cancel cret;
8866 bool ret = false;
8867
8868 if (ctx->io_wq) {
8869 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8870 &cancel, true);
8871 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8872 }
8873
8874 /* SQPOLL thread does its own polling */
8875 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8876 while (!list_empty_careful(&ctx->iopoll_list)) {
8877 io_iopoll_try_reap_events(ctx);
8878 ret = true;
8879 }
8880 }
8881
8882 ret |= io_poll_remove_all(ctx, task, files);
8883 ret |= io_kill_timeouts(ctx, task, files);
8884 ret |= io_run_task_work();
8885 io_cqring_overflow_flush(ctx, true, task, files);
8886 if (!ret)
8887 break;
8888 cond_resched();
8889 }
8890}
8891
Pavel Begunkovca70f002021-01-26 15:28:27 +00008892static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8893 struct task_struct *task,
8894 struct files_struct *files)
8895{
8896 struct io_kiocb *req;
8897 int cnt = 0;
8898
8899 spin_lock_irq(&ctx->inflight_lock);
8900 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8901 cnt += io_match_task(req, task, files);
8902 spin_unlock_irq(&ctx->inflight_lock);
8903 return cnt;
8904}
8905
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008906static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008907 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008908 struct files_struct *files)
8909{
Jens Axboefcb323c2019-10-24 12:39:47 -06008910 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008911 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008912 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008913
Pavel Begunkovca70f002021-01-26 15:28:27 +00008914 inflight = io_uring_count_inflight(ctx, task, files);
8915 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008916 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008917
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008918 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008919 prepare_to_wait(&task->io_uring->wait, &wait,
8920 TASK_UNINTERRUPTIBLE);
8921 if (inflight == io_uring_count_inflight(ctx, task, files))
8922 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008923 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008924 }
8925}
8926
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008927static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8928{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008929 mutex_lock(&ctx->uring_lock);
8930 ctx->sqo_dead = 1;
8931 mutex_unlock(&ctx->uring_lock);
8932
8933 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008934 if (ctx->rings)
8935 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008936}
8937
Jens Axboe0f212202020-09-13 13:09:39 -06008938/*
8939 * We need to iteratively cancel requests, in case a request has dependent
8940 * hard links. These persist even for failure of cancelations, hence keep
8941 * looping until none are found.
8942 */
8943static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8944 struct files_struct *files)
8945{
8946 struct task_struct *task = current;
8947
Jens Axboefdaf0832020-10-30 09:37:30 -06008948 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008949 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008950 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008951 atomic_inc(&task->io_uring->in_idle);
8952 io_sq_thread_park(ctx->sq_data);
8953 }
Jens Axboe0f212202020-09-13 13:09:39 -06008954
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008955 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008956
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008957 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008958 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008959 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008960
8961 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8962 atomic_dec(&task->io_uring->in_idle);
8963 /*
8964 * If the files that are going away are the ones in the thread
8965 * identity, clear them out.
8966 */
8967 if (task->io_uring->identity->files == files)
8968 task->io_uring->identity->files = NULL;
8969 io_sq_thread_unpark(ctx->sq_data);
8970 }
Jens Axboe0f212202020-09-13 13:09:39 -06008971}
8972
8973/*
8974 * Note that this task has used io_uring. We use it for cancelation purposes.
8975 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008976static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008977{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008978 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008979 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008980
8981 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008982 ret = io_uring_alloc_task_context(current);
8983 if (unlikely(ret))
8984 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008985 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008986 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008987 if (tctx->last != file) {
8988 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008989
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008990 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008991 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008992 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8993 file, GFP_KERNEL));
8994 if (ret) {
8995 fput(file);
8996 return ret;
8997 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008998
8999 /* one and only SQPOLL file note, held by sqo_task */
9000 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9001 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009002 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009003 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009004 }
9005
Jens Axboefdaf0832020-10-30 09:37:30 -06009006 /*
9007 * This is race safe in that the task itself is doing this, hence it
9008 * cannot be going through the exit/cancel paths at the same time.
9009 * This cannot be modified while exit/cancel is running.
9010 */
9011 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9012 tctx->sqpoll = true;
9013
Jens Axboe0f212202020-09-13 13:09:39 -06009014 return 0;
9015}
9016
9017/*
9018 * Remove this io_uring_file -> task mapping.
9019 */
9020static void io_uring_del_task_file(struct file *file)
9021{
9022 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009023
9024 if (tctx->last == file)
9025 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009026 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009027 if (file)
9028 fput(file);
9029}
9030
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009031static void io_uring_remove_task_files(struct io_uring_task *tctx)
9032{
9033 struct file *file;
9034 unsigned long index;
9035
9036 xa_for_each(&tctx->xa, index, file)
9037 io_uring_del_task_file(file);
9038}
9039
Jens Axboe0f212202020-09-13 13:09:39 -06009040void __io_uring_files_cancel(struct files_struct *files)
9041{
9042 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009043 struct file *file;
9044 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009045
9046 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009047 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009048 xa_for_each(&tctx->xa, index, file)
9049 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009050 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009051
9052 if (files)
9053 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009054}
9055
9056static s64 tctx_inflight(struct io_uring_task *tctx)
9057{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009058 return percpu_counter_sum(&tctx->inflight);
9059}
9060
9061static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9062{
9063 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009064 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009065 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009066
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009067 if (!ctx->sq_data)
9068 return;
9069 tctx = ctx->sq_data->thread->io_uring;
9070 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009071
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009072 atomic_inc(&tctx->in_idle);
9073 do {
9074 /* read completions before cancelations */
9075 inflight = tctx_inflight(tctx);
9076 if (!inflight)
9077 break;
9078 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009079
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009080 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9081 /*
9082 * If we've seen completions, retry without waiting. This
9083 * avoids a race where a completion comes in before we did
9084 * prepare_to_wait().
9085 */
9086 if (inflight == tctx_inflight(tctx))
9087 schedule();
9088 finish_wait(&tctx->wait, &wait);
9089 } while (1);
9090 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009091}
9092
Jens Axboe0f212202020-09-13 13:09:39 -06009093/*
9094 * Find any io_uring fd that this task has registered or done IO on, and cancel
9095 * requests.
9096 */
9097void __io_uring_task_cancel(void)
9098{
9099 struct io_uring_task *tctx = current->io_uring;
9100 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009101 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009102
9103 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009104 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009105
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009106 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009107 if (tctx->sqpoll) {
9108 struct file *file;
9109 unsigned long index;
9110
9111 xa_for_each(&tctx->xa, index, file)
9112 io_uring_cancel_sqpoll(file->private_data);
9113 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009114
Jens Axboed8a6df12020-10-15 16:24:45 -06009115 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009116 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009117 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009118 if (!inflight)
9119 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009120 __io_uring_files_cancel(NULL);
9121
9122 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9123
9124 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009125 * If we've seen completions, retry without waiting. This
9126 * avoids a race where a completion comes in before we did
9127 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009128 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009129 if (inflight == tctx_inflight(tctx))
9130 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009131 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009132 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009133
Jens Axboefdaf0832020-10-30 09:37:30 -06009134 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009135
9136 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009137}
9138
Jens Axboefcb323c2019-10-24 12:39:47 -06009139static int io_uring_flush(struct file *file, void *data)
9140{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009141 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009142 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009143
Jens Axboe84965ff2021-01-23 15:51:11 -07009144 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9145 io_uring_cancel_task_requests(ctx, NULL);
9146
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009147 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009148 return 0;
9149
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009150 /* we should have cancelled and erased it before PF_EXITING */
9151 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9152 xa_load(&tctx->xa, (unsigned long)file));
9153
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009154 /*
9155 * fput() is pending, will be 2 if the only other ref is our potential
9156 * task file note. If the task is exiting, drop regardless of count.
9157 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009158 if (atomic_long_read(&file->f_count) != 2)
9159 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009160
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009161 if (ctx->flags & IORING_SETUP_SQPOLL) {
9162 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009163 WARN_ON_ONCE(ctx->sqo_task != current &&
9164 xa_load(&tctx->xa, (unsigned long)file));
9165 /* sqo_dead check is for when this happens after cancellation */
9166 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009167 !xa_load(&tctx->xa, (unsigned long)file));
9168
9169 io_disable_sqo_submit(ctx);
9170 }
9171
9172 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9173 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009174 return 0;
9175}
9176
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009177static void *io_uring_validate_mmap_request(struct file *file,
9178 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009179{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009180 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009181 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009182 struct page *page;
9183 void *ptr;
9184
9185 switch (offset) {
9186 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009187 case IORING_OFF_CQ_RING:
9188 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009189 break;
9190 case IORING_OFF_SQES:
9191 ptr = ctx->sq_sqes;
9192 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009193 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009194 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009195 }
9196
9197 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009198 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009199 return ERR_PTR(-EINVAL);
9200
9201 return ptr;
9202}
9203
9204#ifdef CONFIG_MMU
9205
9206static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9207{
9208 size_t sz = vma->vm_end - vma->vm_start;
9209 unsigned long pfn;
9210 void *ptr;
9211
9212 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9213 if (IS_ERR(ptr))
9214 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009215
9216 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9217 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9218}
9219
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009220#else /* !CONFIG_MMU */
9221
9222static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9223{
9224 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9225}
9226
9227static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9228{
9229 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9230}
9231
9232static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9233 unsigned long addr, unsigned long len,
9234 unsigned long pgoff, unsigned long flags)
9235{
9236 void *ptr;
9237
9238 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9239 if (IS_ERR(ptr))
9240 return PTR_ERR(ptr);
9241
9242 return (unsigned long) ptr;
9243}
9244
9245#endif /* !CONFIG_MMU */
9246
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009247static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009248{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009249 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009250 DEFINE_WAIT(wait);
9251
9252 do {
9253 if (!io_sqring_full(ctx))
9254 break;
9255
9256 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9257
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009258 if (unlikely(ctx->sqo_dead)) {
9259 ret = -EOWNERDEAD;
9260 goto out;
9261 }
9262
Jens Axboe90554202020-09-03 12:12:41 -06009263 if (!io_sqring_full(ctx))
9264 break;
9265
9266 schedule();
9267 } while (!signal_pending(current));
9268
9269 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009270out:
9271 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009272}
9273
Hao Xuc73ebb62020-11-03 10:54:37 +08009274static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9275 struct __kernel_timespec __user **ts,
9276 const sigset_t __user **sig)
9277{
9278 struct io_uring_getevents_arg arg;
9279
9280 /*
9281 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9282 * is just a pointer to the sigset_t.
9283 */
9284 if (!(flags & IORING_ENTER_EXT_ARG)) {
9285 *sig = (const sigset_t __user *) argp;
9286 *ts = NULL;
9287 return 0;
9288 }
9289
9290 /*
9291 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9292 * timespec and sigset_t pointers if good.
9293 */
9294 if (*argsz != sizeof(arg))
9295 return -EINVAL;
9296 if (copy_from_user(&arg, argp, sizeof(arg)))
9297 return -EFAULT;
9298 *sig = u64_to_user_ptr(arg.sigmask);
9299 *argsz = arg.sigmask_sz;
9300 *ts = u64_to_user_ptr(arg.ts);
9301 return 0;
9302}
9303
Jens Axboe2b188cc2019-01-07 10:46:33 -07009304SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009305 u32, min_complete, u32, flags, const void __user *, argp,
9306 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009307{
9308 struct io_ring_ctx *ctx;
9309 long ret = -EBADF;
9310 int submitted = 0;
9311 struct fd f;
9312
Jens Axboe4c6e2772020-07-01 11:29:10 -06009313 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009314
Jens Axboe90554202020-09-03 12:12:41 -06009315 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009316 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009317 return -EINVAL;
9318
9319 f = fdget(fd);
9320 if (!f.file)
9321 return -EBADF;
9322
9323 ret = -EOPNOTSUPP;
9324 if (f.file->f_op != &io_uring_fops)
9325 goto out_fput;
9326
9327 ret = -ENXIO;
9328 ctx = f.file->private_data;
9329 if (!percpu_ref_tryget(&ctx->refs))
9330 goto out_fput;
9331
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009332 ret = -EBADFD;
9333 if (ctx->flags & IORING_SETUP_R_DISABLED)
9334 goto out;
9335
Jens Axboe6c271ce2019-01-10 11:22:30 -07009336 /*
9337 * For SQ polling, the thread will do all submissions and completions.
9338 * Just return the requested submit count, and wake the thread if
9339 * we were asked to.
9340 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009341 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009342 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009343 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009344
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009345 ret = -EOWNERDEAD;
9346 if (unlikely(ctx->sqo_dead))
9347 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009348 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009349 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009350 if (flags & IORING_ENTER_SQ_WAIT) {
9351 ret = io_sqpoll_wait_sq(ctx);
9352 if (ret)
9353 goto out;
9354 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009355 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009356 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009357 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009358 if (unlikely(ret))
9359 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009360 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009361 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009362 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009363
9364 if (submitted != to_submit)
9365 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009366 }
9367 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009368 const sigset_t __user *sig;
9369 struct __kernel_timespec __user *ts;
9370
9371 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9372 if (unlikely(ret))
9373 goto out;
9374
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375 min_complete = min(min_complete, ctx->cq_entries);
9376
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009377 /*
9378 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9379 * space applications don't need to do io completion events
9380 * polling again, they can rely on io_sq_thread to do polling
9381 * work, which can reduce cpu usage and uring_lock contention.
9382 */
9383 if (ctx->flags & IORING_SETUP_IOPOLL &&
9384 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009385 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009386 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009387 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009388 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009389 }
9390
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009391out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009392 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009393out_fput:
9394 fdput(f);
9395 return submitted ? submitted : ret;
9396}
9397
Tobias Klauserbebdb652020-02-26 18:38:32 +01009398#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009399static int io_uring_show_cred(int id, void *p, void *data)
9400{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009401 struct io_identity *iod = p;
9402 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009403 struct seq_file *m = data;
9404 struct user_namespace *uns = seq_user_ns(m);
9405 struct group_info *gi;
9406 kernel_cap_t cap;
9407 unsigned __capi;
9408 int g;
9409
9410 seq_printf(m, "%5d\n", id);
9411 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9412 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9413 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9414 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9415 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9416 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9417 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9418 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9419 seq_puts(m, "\n\tGroups:\t");
9420 gi = cred->group_info;
9421 for (g = 0; g < gi->ngroups; g++) {
9422 seq_put_decimal_ull(m, g ? " " : "",
9423 from_kgid_munged(uns, gi->gid[g]));
9424 }
9425 seq_puts(m, "\n\tCapEff:\t");
9426 cap = cred->cap_effective;
9427 CAP_FOR_EACH_U32(__capi)
9428 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9429 seq_putc(m, '\n');
9430 return 0;
9431}
9432
9433static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9434{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009435 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009436 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009437 int i;
9438
Jens Axboefad8e0d2020-09-28 08:57:48 -06009439 /*
9440 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9441 * since fdinfo case grabs it in the opposite direction of normal use
9442 * cases. If we fail to get the lock, we just don't iterate any
9443 * structures that could be going away outside the io_uring mutex.
9444 */
9445 has_lock = mutex_trylock(&ctx->uring_lock);
9446
Joseph Qidbbe9c62020-09-29 09:01:22 -06009447 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9448 sq = ctx->sq_data;
9449
9450 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9451 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009452 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009453 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009454 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009455
Jens Axboe87ce9552020-01-30 08:25:34 -07009456 if (f)
9457 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9458 else
9459 seq_printf(m, "%5u: <none>\n", i);
9460 }
9461 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009462 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009463 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9464
9465 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9466 (unsigned int) buf->len);
9467 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009468 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009469 seq_printf(m, "Personalities:\n");
9470 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9471 }
Jens Axboed7718a92020-02-14 22:23:12 -07009472 seq_printf(m, "PollList:\n");
9473 spin_lock_irq(&ctx->completion_lock);
9474 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9475 struct hlist_head *list = &ctx->cancel_hash[i];
9476 struct io_kiocb *req;
9477
9478 hlist_for_each_entry(req, list, hash_node)
9479 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9480 req->task->task_works != NULL);
9481 }
9482 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009483 if (has_lock)
9484 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009485}
9486
9487static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9488{
9489 struct io_ring_ctx *ctx = f->private_data;
9490
9491 if (percpu_ref_tryget(&ctx->refs)) {
9492 __io_uring_show_fdinfo(ctx, m);
9493 percpu_ref_put(&ctx->refs);
9494 }
9495}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009496#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009497
Jens Axboe2b188cc2019-01-07 10:46:33 -07009498static const struct file_operations io_uring_fops = {
9499 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009500 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009501 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009502#ifndef CONFIG_MMU
9503 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9504 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9505#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009506 .poll = io_uring_poll,
9507 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009508#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009509 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009510#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009511};
9512
9513static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9514 struct io_uring_params *p)
9515{
Hristo Venev75b28af2019-08-26 17:23:46 +00009516 struct io_rings *rings;
9517 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009518
Jens Axboebd740482020-08-05 12:58:23 -06009519 /* make sure these are sane, as we already accounted them */
9520 ctx->sq_entries = p->sq_entries;
9521 ctx->cq_entries = p->cq_entries;
9522
Hristo Venev75b28af2019-08-26 17:23:46 +00009523 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9524 if (size == SIZE_MAX)
9525 return -EOVERFLOW;
9526
9527 rings = io_mem_alloc(size);
9528 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009529 return -ENOMEM;
9530
Hristo Venev75b28af2019-08-26 17:23:46 +00009531 ctx->rings = rings;
9532 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9533 rings->sq_ring_mask = p->sq_entries - 1;
9534 rings->cq_ring_mask = p->cq_entries - 1;
9535 rings->sq_ring_entries = p->sq_entries;
9536 rings->cq_ring_entries = p->cq_entries;
9537 ctx->sq_mask = rings->sq_ring_mask;
9538 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009539
9540 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009541 if (size == SIZE_MAX) {
9542 io_mem_free(ctx->rings);
9543 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009544 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009545 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009546
9547 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009548 if (!ctx->sq_sqes) {
9549 io_mem_free(ctx->rings);
9550 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009551 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009552 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553
Jens Axboe2b188cc2019-01-07 10:46:33 -07009554 return 0;
9555}
9556
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009557static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9558{
9559 int ret, fd;
9560
9561 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9562 if (fd < 0)
9563 return fd;
9564
9565 ret = io_uring_add_task_file(ctx, file);
9566 if (ret) {
9567 put_unused_fd(fd);
9568 return ret;
9569 }
9570 fd_install(fd, file);
9571 return fd;
9572}
9573
Jens Axboe2b188cc2019-01-07 10:46:33 -07009574/*
9575 * Allocate an anonymous fd, this is what constitutes the application
9576 * visible backing of an io_uring instance. The application mmaps this
9577 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9578 * we have to tie this fd to a socket for file garbage collection purposes.
9579 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009580static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009581{
9582 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009583#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009584 int ret;
9585
Jens Axboe2b188cc2019-01-07 10:46:33 -07009586 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9587 &ctx->ring_sock);
9588 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009589 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009590#endif
9591
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9593 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009594#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009595 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009596 sock_release(ctx->ring_sock);
9597 ctx->ring_sock = NULL;
9598 } else {
9599 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009601#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009602 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009603}
9604
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009605static int io_uring_create(unsigned entries, struct io_uring_params *p,
9606 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009607{
9608 struct user_struct *user = NULL;
9609 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009610 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009611 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009612 int ret;
9613
Jens Axboe8110c1a2019-12-28 15:39:54 -07009614 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009615 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009616 if (entries > IORING_MAX_ENTRIES) {
9617 if (!(p->flags & IORING_SETUP_CLAMP))
9618 return -EINVAL;
9619 entries = IORING_MAX_ENTRIES;
9620 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009621
9622 /*
9623 * Use twice as many entries for the CQ ring. It's possible for the
9624 * application to drive a higher depth than the size of the SQ ring,
9625 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009626 * some flexibility in overcommitting a bit. If the application has
9627 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9628 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009629 */
9630 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009631 if (p->flags & IORING_SETUP_CQSIZE) {
9632 /*
9633 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9634 * to a power-of-two, if it isn't already. We do NOT impose
9635 * any cq vs sq ring sizing.
9636 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009637 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009638 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009639 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9640 if (!(p->flags & IORING_SETUP_CLAMP))
9641 return -EINVAL;
9642 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9643 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009644 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9645 if (p->cq_entries < p->sq_entries)
9646 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009647 } else {
9648 p->cq_entries = 2 * p->sq_entries;
9649 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009650
9651 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009652 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009653
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009654 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009655 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009656 ring_pages(p->sq_entries, p->cq_entries));
9657 if (ret) {
9658 free_uid(user);
9659 return ret;
9660 }
9661 }
9662
9663 ctx = io_ring_ctx_alloc(p);
9664 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009665 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009666 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009667 p->cq_entries));
9668 free_uid(user);
9669 return -ENOMEM;
9670 }
9671 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009672 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009673 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009674#ifdef CONFIG_AUDIT
9675 ctx->loginuid = current->loginuid;
9676 ctx->sessionid = current->sessionid;
9677#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009678 ctx->sqo_task = get_task_struct(current);
9679
9680 /*
9681 * This is just grabbed for accounting purposes. When a process exits,
9682 * the mm is exited and dropped before the files, hence we need to hang
9683 * on to this mm purely for the purposes of being able to unaccount
9684 * memory (locked/pinned vm). It's not used for anything else.
9685 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009686 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009687 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009688
Dennis Zhou91d8f512020-09-16 13:41:05 -07009689#ifdef CONFIG_BLK_CGROUP
9690 /*
9691 * The sq thread will belong to the original cgroup it was inited in.
9692 * If the cgroup goes offline (e.g. disabling the io controller), then
9693 * issued bios will be associated with the closest cgroup later in the
9694 * block layer.
9695 */
9696 rcu_read_lock();
9697 ctx->sqo_blkcg_css = blkcg_css();
9698 ret = css_tryget_online(ctx->sqo_blkcg_css);
9699 rcu_read_unlock();
9700 if (!ret) {
9701 /* don't init against a dying cgroup, have the user try again */
9702 ctx->sqo_blkcg_css = NULL;
9703 ret = -ENODEV;
9704 goto err;
9705 }
9706#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009707
Jens Axboe2b188cc2019-01-07 10:46:33 -07009708 /*
9709 * Account memory _before_ installing the file descriptor. Once
9710 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009711 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009712 * will un-account as well.
9713 */
9714 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9715 ACCT_LOCKED);
9716 ctx->limit_mem = limit_mem;
9717
9718 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009719 if (ret)
9720 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009721
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009722 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009723 if (ret)
9724 goto err;
9725
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009726 if (!(p->flags & IORING_SETUP_R_DISABLED))
9727 io_sq_offload_start(ctx);
9728
Jens Axboe2b188cc2019-01-07 10:46:33 -07009729 memset(&p->sq_off, 0, sizeof(p->sq_off));
9730 p->sq_off.head = offsetof(struct io_rings, sq.head);
9731 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9732 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9733 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9734 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9735 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9736 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9737
9738 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009739 p->cq_off.head = offsetof(struct io_rings, cq.head);
9740 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9741 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9742 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9743 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9744 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009745 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009746
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009747 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9748 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009749 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009750 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9751 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009752
9753 if (copy_to_user(params, p, sizeof(*p))) {
9754 ret = -EFAULT;
9755 goto err;
9756 }
Jens Axboed1719f72020-07-30 13:43:53 -06009757
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009758 file = io_uring_get_file(ctx);
9759 if (IS_ERR(file)) {
9760 ret = PTR_ERR(file);
9761 goto err;
9762 }
9763
Jens Axboed1719f72020-07-30 13:43:53 -06009764 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009765 * Install ring fd as the very last thing, so we don't risk someone
9766 * having closed it before we finish setup
9767 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009768 ret = io_uring_install_fd(ctx, file);
9769 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009770 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009771 /* fput will clean it up */
9772 fput(file);
9773 return ret;
9774 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009775
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009776 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009777 return ret;
9778err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009779 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009780 io_ring_ctx_wait_and_kill(ctx);
9781 return ret;
9782}
9783
9784/*
9785 * Sets up an aio uring context, and returns the fd. Applications asks for a
9786 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9787 * params structure passed in.
9788 */
9789static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9790{
9791 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009792 int i;
9793
9794 if (copy_from_user(&p, params, sizeof(p)))
9795 return -EFAULT;
9796 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9797 if (p.resv[i])
9798 return -EINVAL;
9799 }
9800
Jens Axboe6c271ce2019-01-10 11:22:30 -07009801 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009802 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009803 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9804 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009805 return -EINVAL;
9806
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009807 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009808}
9809
9810SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9811 struct io_uring_params __user *, params)
9812{
9813 return io_uring_setup(entries, params);
9814}
9815
Jens Axboe66f4af92020-01-16 15:36:52 -07009816static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9817{
9818 struct io_uring_probe *p;
9819 size_t size;
9820 int i, ret;
9821
9822 size = struct_size(p, ops, nr_args);
9823 if (size == SIZE_MAX)
9824 return -EOVERFLOW;
9825 p = kzalloc(size, GFP_KERNEL);
9826 if (!p)
9827 return -ENOMEM;
9828
9829 ret = -EFAULT;
9830 if (copy_from_user(p, arg, size))
9831 goto out;
9832 ret = -EINVAL;
9833 if (memchr_inv(p, 0, size))
9834 goto out;
9835
9836 p->last_op = IORING_OP_LAST - 1;
9837 if (nr_args > IORING_OP_LAST)
9838 nr_args = IORING_OP_LAST;
9839
9840 for (i = 0; i < nr_args; i++) {
9841 p->ops[i].op = i;
9842 if (!io_op_defs[i].not_supported)
9843 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9844 }
9845 p->ops_len = i;
9846
9847 ret = 0;
9848 if (copy_to_user(arg, p, size))
9849 ret = -EFAULT;
9850out:
9851 kfree(p);
9852 return ret;
9853}
9854
Jens Axboe071698e2020-01-28 10:04:42 -07009855static int io_register_personality(struct io_ring_ctx *ctx)
9856{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009857 struct io_identity *id;
9858 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009859
Jens Axboe1e6fa522020-10-15 08:46:24 -06009860 id = kmalloc(sizeof(*id), GFP_KERNEL);
9861 if (unlikely(!id))
9862 return -ENOMEM;
9863
9864 io_init_identity(id);
9865 id->creds = get_current_cred();
9866
9867 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9868 if (ret < 0) {
9869 put_cred(id->creds);
9870 kfree(id);
9871 }
9872 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009873}
9874
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009875static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9876 unsigned int nr_args)
9877{
9878 struct io_uring_restriction *res;
9879 size_t size;
9880 int i, ret;
9881
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009882 /* Restrictions allowed only if rings started disabled */
9883 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9884 return -EBADFD;
9885
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009886 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009887 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009888 return -EBUSY;
9889
9890 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9891 return -EINVAL;
9892
9893 size = array_size(nr_args, sizeof(*res));
9894 if (size == SIZE_MAX)
9895 return -EOVERFLOW;
9896
9897 res = memdup_user(arg, size);
9898 if (IS_ERR(res))
9899 return PTR_ERR(res);
9900
9901 ret = 0;
9902
9903 for (i = 0; i < nr_args; i++) {
9904 switch (res[i].opcode) {
9905 case IORING_RESTRICTION_REGISTER_OP:
9906 if (res[i].register_op >= IORING_REGISTER_LAST) {
9907 ret = -EINVAL;
9908 goto out;
9909 }
9910
9911 __set_bit(res[i].register_op,
9912 ctx->restrictions.register_op);
9913 break;
9914 case IORING_RESTRICTION_SQE_OP:
9915 if (res[i].sqe_op >= IORING_OP_LAST) {
9916 ret = -EINVAL;
9917 goto out;
9918 }
9919
9920 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9921 break;
9922 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9923 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9924 break;
9925 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9926 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9927 break;
9928 default:
9929 ret = -EINVAL;
9930 goto out;
9931 }
9932 }
9933
9934out:
9935 /* Reset all restrictions if an error happened */
9936 if (ret != 0)
9937 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9938 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009939 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009940
9941 kfree(res);
9942 return ret;
9943}
9944
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009945static int io_register_enable_rings(struct io_ring_ctx *ctx)
9946{
9947 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9948 return -EBADFD;
9949
9950 if (ctx->restrictions.registered)
9951 ctx->restricted = 1;
9952
9953 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9954
9955 io_sq_offload_start(ctx);
9956
9957 return 0;
9958}
9959
Jens Axboe071698e2020-01-28 10:04:42 -07009960static bool io_register_op_must_quiesce(int op)
9961{
9962 switch (op) {
9963 case IORING_UNREGISTER_FILES:
9964 case IORING_REGISTER_FILES_UPDATE:
9965 case IORING_REGISTER_PROBE:
9966 case IORING_REGISTER_PERSONALITY:
9967 case IORING_UNREGISTER_PERSONALITY:
9968 return false;
9969 default:
9970 return true;
9971 }
9972}
9973
Jens Axboeedafcce2019-01-09 09:16:05 -07009974static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9975 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009976 __releases(ctx->uring_lock)
9977 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009978{
9979 int ret;
9980
Jens Axboe35fa71a2019-04-22 10:23:23 -06009981 /*
9982 * We're inside the ring mutex, if the ref is already dying, then
9983 * someone else killed the ctx or is already going through
9984 * io_uring_register().
9985 */
9986 if (percpu_ref_is_dying(&ctx->refs))
9987 return -ENXIO;
9988
Jens Axboe071698e2020-01-28 10:04:42 -07009989 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009990 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009991
Jens Axboe05f3fb32019-12-09 11:22:50 -07009992 /*
9993 * Drop uring mutex before waiting for references to exit. If
9994 * another thread is currently inside io_uring_enter() it might
9995 * need to grab the uring_lock to make progress. If we hold it
9996 * here across the drain wait, then we can deadlock. It's safe
9997 * to drop the mutex here, since no new references will come in
9998 * after we've killed the percpu ref.
9999 */
10000 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010001 do {
10002 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10003 if (!ret)
10004 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010005 ret = io_run_task_work_sig();
10006 if (ret < 0)
10007 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010008 } while (1);
10009
Jens Axboe05f3fb32019-12-09 11:22:50 -070010010 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010011
Jens Axboec1503682020-01-08 08:26:07 -070010012 if (ret) {
10013 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010014 goto out_quiesce;
10015 }
10016 }
10017
10018 if (ctx->restricted) {
10019 if (opcode >= IORING_REGISTER_LAST) {
10020 ret = -EINVAL;
10021 goto out;
10022 }
10023
10024 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10025 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010026 goto out;
10027 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010028 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010029
10030 switch (opcode) {
10031 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010032 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010033 break;
10034 case IORING_UNREGISTER_BUFFERS:
10035 ret = -EINVAL;
10036 if (arg || nr_args)
10037 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010038 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010039 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010040 case IORING_REGISTER_FILES:
10041 ret = io_sqe_files_register(ctx, arg, nr_args);
10042 break;
10043 case IORING_UNREGISTER_FILES:
10044 ret = -EINVAL;
10045 if (arg || nr_args)
10046 break;
10047 ret = io_sqe_files_unregister(ctx);
10048 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010049 case IORING_REGISTER_FILES_UPDATE:
10050 ret = io_sqe_files_update(ctx, arg, nr_args);
10051 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010052 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010053 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010054 ret = -EINVAL;
10055 if (nr_args != 1)
10056 break;
10057 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010058 if (ret)
10059 break;
10060 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10061 ctx->eventfd_async = 1;
10062 else
10063 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010064 break;
10065 case IORING_UNREGISTER_EVENTFD:
10066 ret = -EINVAL;
10067 if (arg || nr_args)
10068 break;
10069 ret = io_eventfd_unregister(ctx);
10070 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010071 case IORING_REGISTER_PROBE:
10072 ret = -EINVAL;
10073 if (!arg || nr_args > 256)
10074 break;
10075 ret = io_probe(ctx, arg, nr_args);
10076 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010077 case IORING_REGISTER_PERSONALITY:
10078 ret = -EINVAL;
10079 if (arg || nr_args)
10080 break;
10081 ret = io_register_personality(ctx);
10082 break;
10083 case IORING_UNREGISTER_PERSONALITY:
10084 ret = -EINVAL;
10085 if (arg)
10086 break;
10087 ret = io_unregister_personality(ctx, nr_args);
10088 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010089 case IORING_REGISTER_ENABLE_RINGS:
10090 ret = -EINVAL;
10091 if (arg || nr_args)
10092 break;
10093 ret = io_register_enable_rings(ctx);
10094 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010095 case IORING_REGISTER_RESTRICTIONS:
10096 ret = io_register_restrictions(ctx, arg, nr_args);
10097 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010098 default:
10099 ret = -EINVAL;
10100 break;
10101 }
10102
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010103out:
Jens Axboe071698e2020-01-28 10:04:42 -070010104 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010105 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010106 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010107out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010108 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010109 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010110 return ret;
10111}
10112
10113SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10114 void __user *, arg, unsigned int, nr_args)
10115{
10116 struct io_ring_ctx *ctx;
10117 long ret = -EBADF;
10118 struct fd f;
10119
10120 f = fdget(fd);
10121 if (!f.file)
10122 return -EBADF;
10123
10124 ret = -EOPNOTSUPP;
10125 if (f.file->f_op != &io_uring_fops)
10126 goto out_fput;
10127
10128 ctx = f.file->private_data;
10129
10130 mutex_lock(&ctx->uring_lock);
10131 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10132 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010133 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10134 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010135out_fput:
10136 fdput(f);
10137 return ret;
10138}
10139
Jens Axboe2b188cc2019-01-07 10:46:33 -070010140static int __init io_uring_init(void)
10141{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010142#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10143 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10144 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10145} while (0)
10146
10147#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10148 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10149 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10150 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10151 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10152 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10153 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10154 BUILD_BUG_SQE_ELEM(8, __u64, off);
10155 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10156 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010157 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010158 BUILD_BUG_SQE_ELEM(24, __u32, len);
10159 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10160 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10161 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10162 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010163 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10164 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010165 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10166 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10167 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10168 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10169 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10170 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10171 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10172 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010173 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010174 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10175 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10176 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010177 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010178
Jens Axboed3656342019-12-18 09:50:26 -070010179 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010180 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010181 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10182 return 0;
10183};
10184__initcall(io_uring_init);