blob: 1f0b3b332d3253cd6ee7ac12c089b8cb71fdd9d9 [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 Begunkovbf019da2021-02-10 00:03:17 +0000269#define IO_REQ_CACHE_SIZE 8
270#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
2267static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2268{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002269 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002270
Jens Axboee3bc8e92020-09-24 08:45:57 -06002271 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002272 if (rb->task)
2273 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002274 rb->task = req->task;
2275 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002276 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002277 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002278 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002279
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002280 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002281 rb->reqs[rb->to_free++] = req;
2282 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2283 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002284}
2285
Pavel Begunkov905c1722021-02-10 00:03:14 +00002286static void io_submit_flush_completions(struct io_comp_state *cs,
2287 struct io_ring_ctx *ctx)
2288{
2289 int i, nr = cs->nr;
2290 struct io_kiocb *req;
2291 struct req_batch rb;
2292
2293 io_init_req_batch(&rb);
2294 spin_lock_irq(&ctx->completion_lock);
2295 for (i = 0; i < nr; i++) {
2296 req = cs->reqs[i];
2297 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2298 }
2299 io_commit_cqring(ctx);
2300 spin_unlock_irq(&ctx->completion_lock);
2301
2302 io_cqring_ev_posted(ctx);
2303 for (i = 0; i < nr; i++) {
2304 req = cs->reqs[i];
2305
2306 /* submission and completion refs */
2307 if (refcount_sub_and_test(2, &req->refs))
2308 io_req_free_batch(&rb, req);
2309 }
2310
2311 io_req_free_batch_finish(ctx, &rb);
2312 cs->nr = 0;
2313}
2314
Jens Axboeba816ad2019-09-28 11:36:45 -06002315/*
2316 * Drop reference to request, return next in chain (if there is one) if this
2317 * was the last reference to this request.
2318 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002319static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002320{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002321 struct io_kiocb *nxt = NULL;
2322
Jens Axboe2a44f462020-02-25 13:25:41 -07002323 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002324 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002325 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002326 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002327 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002328}
2329
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330static void io_put_req(struct io_kiocb *req)
2331{
Jens Axboedef596e2019-01-09 08:59:42 -07002332 if (refcount_dec_and_test(&req->refs))
2333 io_free_req(req);
2334}
2335
Pavel Begunkov216578e2020-10-13 09:44:00 +01002336static void io_put_req_deferred_cb(struct callback_head *cb)
2337{
2338 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2339
2340 io_free_req(req);
2341}
2342
2343static void io_free_req_deferred(struct io_kiocb *req)
2344{
2345 int ret;
2346
2347 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002348 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002349 if (unlikely(ret))
2350 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002351}
2352
2353static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2354{
2355 if (refcount_sub_and_test(refs, &req->refs))
2356 io_free_req_deferred(req);
2357}
2358
Jens Axboe978db572019-11-14 22:39:04 -07002359static void io_double_put_req(struct io_kiocb *req)
2360{
2361 /* drop both submit and complete references */
2362 if (refcount_sub_and_test(2, &req->refs))
2363 io_free_req(req);
2364}
2365
Pavel Begunkov6c503152021-01-04 20:36:36 +00002366static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002367{
2368 /* See comment at the top of this file */
2369 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002370 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002371}
2372
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002373static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2374{
2375 struct io_rings *rings = ctx->rings;
2376
2377 /* make sure SQ entry isn't read before tail */
2378 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2379}
2380
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002381static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002382{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002383 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002384
Jens Axboebcda7ba2020-02-23 16:42:51 -07002385 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2386 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002387 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002388 kfree(kbuf);
2389 return cflags;
2390}
2391
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002392static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2393{
2394 struct io_buffer *kbuf;
2395
2396 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2397 return io_put_kbuf(req, kbuf);
2398}
2399
Jens Axboe4c6e2772020-07-01 11:29:10 -06002400static inline bool io_run_task_work(void)
2401{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002402 /*
2403 * Not safe to run on exiting task, and the task_work handling will
2404 * not add work to such a task.
2405 */
2406 if (unlikely(current->flags & PF_EXITING))
2407 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002408 if (current->task_works) {
2409 __set_current_state(TASK_RUNNING);
2410 task_work_run();
2411 return true;
2412 }
2413
2414 return false;
2415}
2416
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002417static void io_iopoll_queue(struct list_head *again)
2418{
2419 struct io_kiocb *req;
2420
2421 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002422 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2423 list_del(&req->inflight_entry);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002424 __io_complete_rw(req, -EAGAIN, 0, 0);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002425 } while (!list_empty(again));
2426}
2427
Jens Axboedef596e2019-01-09 08:59:42 -07002428/*
2429 * Find and free completed poll iocbs
2430 */
2431static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2432 struct list_head *done)
2433{
Jens Axboe8237e042019-12-28 10:48:22 -07002434 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002435 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002436 LIST_HEAD(again);
2437
2438 /* order with ->result store in io_complete_rw_iopoll() */
2439 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002440
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002441 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002442 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002443 int cflags = 0;
2444
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002445 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002446 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002447 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002448 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002449 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002450 continue;
2451 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002452 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002453
Jens Axboebcda7ba2020-02-23 16:42:51 -07002454 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002455 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002456
2457 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002458 (*nr_events)++;
2459
Pavel Begunkovc3524382020-06-28 12:52:32 +03002460 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002461 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002462 }
Jens Axboedef596e2019-01-09 08:59:42 -07002463
Jens Axboe09bb8392019-03-13 12:39:28 -06002464 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002465 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002466 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002467
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002468 if (!list_empty(&again))
2469 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002470}
2471
Jens Axboedef596e2019-01-09 08:59:42 -07002472static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2473 long min)
2474{
2475 struct io_kiocb *req, *tmp;
2476 LIST_HEAD(done);
2477 bool spin;
2478 int ret;
2479
2480 /*
2481 * Only spin for completions if we don't have multiple devices hanging
2482 * off our complete list, and we're under the requested amount.
2483 */
2484 spin = !ctx->poll_multi_file && *nr_events < min;
2485
2486 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002487 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002488 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002489
2490 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002491 * Move completed and retryable entries to our local lists.
2492 * If we find a request that requires polling, break out
2493 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002494 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002495 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002496 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002497 continue;
2498 }
2499 if (!list_empty(&done))
2500 break;
2501
2502 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2503 if (ret < 0)
2504 break;
2505
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002506 /* iopoll may have completed current req */
2507 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002508 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002509
Jens Axboedef596e2019-01-09 08:59:42 -07002510 if (ret && spin)
2511 spin = false;
2512 ret = 0;
2513 }
2514
2515 if (!list_empty(&done))
2516 io_iopoll_complete(ctx, nr_events, &done);
2517
2518 return ret;
2519}
2520
2521/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002522 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002523 * non-spinning poll check - we'll still enter the driver poll loop, but only
2524 * as a non-spinning completion check.
2525 */
2526static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2527 long min)
2528{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002529 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002530 int ret;
2531
2532 ret = io_do_iopoll(ctx, nr_events, min);
2533 if (ret < 0)
2534 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002535 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002536 return 0;
2537 }
2538
2539 return 1;
2540}
2541
2542/*
2543 * We can't just wait for polled events to come to us, we have to actively
2544 * find and complete them.
2545 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002546static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002547{
2548 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2549 return;
2550
2551 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002552 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002553 unsigned int nr_events = 0;
2554
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002555 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002556
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002557 /* let it sleep and repeat later if can't complete a request */
2558 if (nr_events == 0)
2559 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002560 /*
2561 * Ensure we allow local-to-the-cpu processing to take place,
2562 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002563 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002564 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002565 if (need_resched()) {
2566 mutex_unlock(&ctx->uring_lock);
2567 cond_resched();
2568 mutex_lock(&ctx->uring_lock);
2569 }
Jens Axboedef596e2019-01-09 08:59:42 -07002570 }
2571 mutex_unlock(&ctx->uring_lock);
2572}
2573
Pavel Begunkov7668b922020-07-07 16:36:21 +03002574static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002575{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002576 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002577 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002578
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002579 /*
2580 * We disallow the app entering submit/complete with polling, but we
2581 * still need to lock the ring to prevent racing with polled issue
2582 * that got punted to a workqueue.
2583 */
2584 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002585 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002586 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002587 * Don't enter poll loop if we already have events pending.
2588 * If we do, we can potentially be spinning for commands that
2589 * already triggered a CQE (eg in error).
2590 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002591 if (test_bit(0, &ctx->cq_check_overflow))
2592 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2593 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002594 break;
2595
2596 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002597 * If a submit got punted to a workqueue, we can have the
2598 * application entering polling for a command before it gets
2599 * issued. That app will hold the uring_lock for the duration
2600 * of the poll right here, so we need to take a breather every
2601 * now and then to ensure that the issue has a chance to add
2602 * the poll to the issued list. Otherwise we can spin here
2603 * forever, while the workqueue is stuck trying to acquire the
2604 * very same mutex.
2605 */
2606 if (!(++iters & 7)) {
2607 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002608 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002609 mutex_lock(&ctx->uring_lock);
2610 }
2611
Pavel Begunkov7668b922020-07-07 16:36:21 +03002612 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002613 if (ret <= 0)
2614 break;
2615 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002616 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002617
Jens Axboe500f9fb2019-08-19 12:15:59 -06002618 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002619 return ret;
2620}
2621
Jens Axboe491381ce2019-10-17 09:20:46 -06002622static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623{
Jens Axboe491381ce2019-10-17 09:20:46 -06002624 /*
2625 * Tell lockdep we inherited freeze protection from submission
2626 * thread.
2627 */
2628 if (req->flags & REQ_F_ISREG) {
2629 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630
Jens Axboe491381ce2019-10-17 09:20:46 -06002631 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002632 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002633 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634}
2635
Jens Axboea1d7c392020-06-22 11:09:46 -06002636static void io_complete_rw_common(struct kiocb *kiocb, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002637 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638{
Jens Axboe9adbd452019-12-20 08:45:55 -07002639 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002640 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641
Jens Axboe491381ce2019-10-17 09:20:46 -06002642 if (kiocb->ki_flags & IOCB_WRITE)
2643 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002645 if (res != req->result)
2646 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002647 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002648 cflags = io_put_rw_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00002649 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002650}
2651
Jens Axboeb63534c2020-06-04 11:28:00 -06002652#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002653static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002654{
2655 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002656 int rw, ret = -ECANCELED;
Jens Axboeb63534c2020-06-04 11:28:00 -06002657 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002658
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002659 /* already prepared */
2660 if (req->async_data)
2661 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002662
2663 switch (req->opcode) {
2664 case IORING_OP_READV:
2665 case IORING_OP_READ_FIXED:
2666 case IORING_OP_READ:
2667 rw = READ;
2668 break;
2669 case IORING_OP_WRITEV:
2670 case IORING_OP_WRITE_FIXED:
2671 case IORING_OP_WRITE:
2672 rw = WRITE;
2673 break;
2674 default:
2675 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2676 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002677 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002678 }
2679
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002680 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2681 if (ret < 0)
2682 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002683 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002684}
Jens Axboeb63534c2020-06-04 11:28:00 -06002685#endif
2686
2687static bool io_rw_reissue(struct io_kiocb *req, long res)
2688{
2689#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002690 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002691 int ret;
2692
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002693 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002694 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002695 mode = file_inode(req->file)->i_mode;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002696 if (!S_ISBLK(mode) && !S_ISREG(mode))
2697 return false;
2698 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002699 return false;
2700
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002701 lockdep_assert_held(&req->ctx->uring_lock);
2702
Jens Axboe28cea78a2020-09-14 10:51:17 -06002703 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002704
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002705 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002706 refcount_inc(&req->refs);
2707 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002708 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002709 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002710 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002711#endif
2712 return false;
2713}
2714
Jens Axboea1d7c392020-06-22 11:09:46 -06002715static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002716 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002717{
2718 if (!io_rw_reissue(req, res))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002719 io_complete_rw_common(&req->rw.kiocb, res, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002720}
2721
2722static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2723{
Jens Axboe9adbd452019-12-20 08:45:55 -07002724 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002725
Pavel Begunkov889fca72021-02-10 00:03:09 +00002726 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002727}
2728
Jens Axboedef596e2019-01-09 08:59:42 -07002729static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2730{
Jens Axboe9adbd452019-12-20 08:45:55 -07002731 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002732
Jens Axboe491381ce2019-10-17 09:20:46 -06002733 if (kiocb->ki_flags & IOCB_WRITE)
2734 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002735
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002736 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002737 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002738
2739 WRITE_ONCE(req->result, res);
2740 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002741 smp_wmb();
2742 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002743}
2744
2745/*
2746 * After the iocb has been issued, it's safe to be found on the poll list.
2747 * Adding the kiocb to the list AFTER submission ensures that we don't
2748 * find it from a io_iopoll_getevents() thread before the issuer is done
2749 * accessing the kiocb cookie.
2750 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002751static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002752{
2753 struct io_ring_ctx *ctx = req->ctx;
2754
2755 /*
2756 * Track whether we have multiple files in our lists. This will impact
2757 * how we do polling eventually, not spinning if we're on potentially
2758 * different devices.
2759 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002760 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002761 ctx->poll_multi_file = false;
2762 } else if (!ctx->poll_multi_file) {
2763 struct io_kiocb *list_req;
2764
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002765 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002766 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002767 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002768 ctx->poll_multi_file = true;
2769 }
2770
2771 /*
2772 * For fast devices, IO may have already completed. If it has, add
2773 * it to the front so we find it first.
2774 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002775 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002776 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002777 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002778 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002779
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002780 /*
2781 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2782 * task context or in io worker task context. If current task context is
2783 * sq thread, we don't need to check whether should wake up sq thread.
2784 */
2785 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002786 wq_has_sleeper(&ctx->sq_data->wait))
2787 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002788}
2789
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002790static inline void io_state_file_put(struct io_submit_state *state)
2791{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002792 if (state->file_refs) {
2793 fput_many(state->file, state->file_refs);
2794 state->file_refs = 0;
2795 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002796}
2797
2798/*
2799 * Get as many references to a file as we have IOs left in this submission,
2800 * assuming most submissions are for one file, or at least that each file
2801 * has more than one submission.
2802 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002803static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002804{
2805 if (!state)
2806 return fget(fd);
2807
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002808 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002809 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002810 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002811 return state->file;
2812 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002813 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002814 }
2815 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002816 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002817 return NULL;
2818
2819 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002820 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002821 return state->file;
2822}
2823
Jens Axboe4503b762020-06-01 10:00:27 -06002824static bool io_bdev_nowait(struct block_device *bdev)
2825{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002826 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002827}
2828
Jens Axboe2b188cc2019-01-07 10:46:33 -07002829/*
2830 * If we tracked the file through the SCM inflight mechanism, we could support
2831 * any file. For now, just ensure that anything potentially problematic is done
2832 * inline.
2833 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002834static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002835{
2836 umode_t mode = file_inode(file)->i_mode;
2837
Jens Axboe4503b762020-06-01 10:00:27 -06002838 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002839 if (IS_ENABLED(CONFIG_BLOCK) &&
2840 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002841 return true;
2842 return false;
2843 }
2844 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002845 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002846 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002847 if (IS_ENABLED(CONFIG_BLOCK) &&
2848 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002849 file->f_op != &io_uring_fops)
2850 return true;
2851 return false;
2852 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853
Jens Axboec5b85622020-06-09 19:23:05 -06002854 /* any ->read/write should understand O_NONBLOCK */
2855 if (file->f_flags & O_NONBLOCK)
2856 return true;
2857
Jens Axboeaf197f52020-04-28 13:15:06 -06002858 if (!(file->f_mode & FMODE_NOWAIT))
2859 return false;
2860
2861 if (rw == READ)
2862 return file->f_op->read_iter != NULL;
2863
2864 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002865}
2866
Pavel Begunkova88fc402020-09-30 22:57:53 +03002867static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002868{
Jens Axboedef596e2019-01-09 08:59:42 -07002869 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002870 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002871 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002872 unsigned ioprio;
2873 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002874
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002875 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002876 req->flags |= REQ_F_ISREG;
2877
Jens Axboe2b188cc2019-01-07 10:46:33 -07002878 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002879 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002880 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002881 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002882 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002884 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2885 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2886 if (unlikely(ret))
2887 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002889 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2890 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2891 req->flags |= REQ_F_NOWAIT;
2892
Jens Axboe2b188cc2019-01-07 10:46:33 -07002893 ioprio = READ_ONCE(sqe->ioprio);
2894 if (ioprio) {
2895 ret = ioprio_check_cap(ioprio);
2896 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002897 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898
2899 kiocb->ki_ioprio = ioprio;
2900 } else
2901 kiocb->ki_ioprio = get_current_ioprio();
2902
Jens Axboedef596e2019-01-09 08:59:42 -07002903 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002904 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2905 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002906 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002907
Jens Axboedef596e2019-01-09 08:59:42 -07002908 kiocb->ki_flags |= IOCB_HIPRI;
2909 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002910 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002911 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002912 if (kiocb->ki_flags & IOCB_HIPRI)
2913 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002914 kiocb->ki_complete = io_complete_rw;
2915 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002916
Jens Axboe3529d8c2019-12-19 18:24:38 -07002917 req->rw.addr = READ_ONCE(sqe->addr);
2918 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002919 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921}
2922
2923static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2924{
2925 switch (ret) {
2926 case -EIOCBQUEUED:
2927 break;
2928 case -ERESTARTSYS:
2929 case -ERESTARTNOINTR:
2930 case -ERESTARTNOHAND:
2931 case -ERESTART_RESTARTBLOCK:
2932 /*
2933 * We can't just restart the syscall, since previously
2934 * submitted sqes may already be in progress. Just fail this
2935 * IO with EINTR.
2936 */
2937 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002938 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939 default:
2940 kiocb->ki_complete(kiocb, ret, 0);
2941 }
2942}
2943
Jens Axboea1d7c392020-06-22 11:09:46 -06002944static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002945 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002946{
Jens Axboeba042912019-12-25 16:33:42 -07002947 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002948 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002949
Jens Axboe227c0c92020-08-13 11:51:40 -06002950 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002951 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002952 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002953 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002954 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002955 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002956 }
2957
Jens Axboeba042912019-12-25 16:33:42 -07002958 if (req->flags & REQ_F_CUR_POS)
2959 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002960 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002961 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002962 else
2963 io_rw_done(kiocb, ret);
2964}
2965
Pavel Begunkov847595d2021-02-04 13:52:06 +00002966static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002967{
Jens Axboe9adbd452019-12-20 08:45:55 -07002968 struct io_ring_ctx *ctx = req->ctx;
2969 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002970 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002971 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002972 size_t offset;
2973 u64 buf_addr;
2974
Jens Axboeedafcce2019-01-09 09:16:05 -07002975 if (unlikely(buf_index >= ctx->nr_user_bufs))
2976 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002977 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2978 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002979 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002980
2981 /* overflow */
2982 if (buf_addr + len < buf_addr)
2983 return -EFAULT;
2984 /* not inside the mapped region */
2985 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2986 return -EFAULT;
2987
2988 /*
2989 * May not be a start of buffer, set size appropriately
2990 * and advance us to the beginning.
2991 */
2992 offset = buf_addr - imu->ubuf;
2993 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002994
2995 if (offset) {
2996 /*
2997 * Don't use iov_iter_advance() here, as it's really slow for
2998 * using the latter parts of a big fixed buffer - it iterates
2999 * over each segment manually. We can cheat a bit here, because
3000 * we know that:
3001 *
3002 * 1) it's a BVEC iter, we set it up
3003 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3004 * first and last bvec
3005 *
3006 * So just find our index, and adjust the iterator afterwards.
3007 * If the offset is within the first bvec (or the whole first
3008 * bvec, just use iov_iter_advance(). This makes it easier
3009 * since we can just skip the first segment, which may not
3010 * be PAGE_SIZE aligned.
3011 */
3012 const struct bio_vec *bvec = imu->bvec;
3013
3014 if (offset <= bvec->bv_len) {
3015 iov_iter_advance(iter, offset);
3016 } else {
3017 unsigned long seg_skip;
3018
3019 /* skip first vec */
3020 offset -= bvec->bv_len;
3021 seg_skip = 1 + (offset >> PAGE_SHIFT);
3022
3023 iter->bvec = bvec + seg_skip;
3024 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003025 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003026 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003027 }
3028 }
3029
Pavel Begunkov847595d2021-02-04 13:52:06 +00003030 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003031}
3032
Jens Axboebcda7ba2020-02-23 16:42:51 -07003033static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3034{
3035 if (needs_lock)
3036 mutex_unlock(&ctx->uring_lock);
3037}
3038
3039static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3040{
3041 /*
3042 * "Normal" inline submissions always hold the uring_lock, since we
3043 * grab it from the system call. Same is true for the SQPOLL offload.
3044 * The only exception is when we've detached the request and issue it
3045 * from an async worker thread, grab the lock for that case.
3046 */
3047 if (needs_lock)
3048 mutex_lock(&ctx->uring_lock);
3049}
3050
3051static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3052 int bgid, struct io_buffer *kbuf,
3053 bool needs_lock)
3054{
3055 struct io_buffer *head;
3056
3057 if (req->flags & REQ_F_BUFFER_SELECTED)
3058 return kbuf;
3059
3060 io_ring_submit_lock(req->ctx, needs_lock);
3061
3062 lockdep_assert_held(&req->ctx->uring_lock);
3063
3064 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3065 if (head) {
3066 if (!list_empty(&head->list)) {
3067 kbuf = list_last_entry(&head->list, struct io_buffer,
3068 list);
3069 list_del(&kbuf->list);
3070 } else {
3071 kbuf = head;
3072 idr_remove(&req->ctx->io_buffer_idr, bgid);
3073 }
3074 if (*len > kbuf->len)
3075 *len = kbuf->len;
3076 } else {
3077 kbuf = ERR_PTR(-ENOBUFS);
3078 }
3079
3080 io_ring_submit_unlock(req->ctx, needs_lock);
3081
3082 return kbuf;
3083}
3084
Jens Axboe4d954c22020-02-27 07:31:19 -07003085static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3086 bool needs_lock)
3087{
3088 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003089 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003090
3091 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003092 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003093 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3094 if (IS_ERR(kbuf))
3095 return kbuf;
3096 req->rw.addr = (u64) (unsigned long) kbuf;
3097 req->flags |= REQ_F_BUFFER_SELECTED;
3098 return u64_to_user_ptr(kbuf->addr);
3099}
3100
3101#ifdef CONFIG_COMPAT
3102static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3103 bool needs_lock)
3104{
3105 struct compat_iovec __user *uiov;
3106 compat_ssize_t clen;
3107 void __user *buf;
3108 ssize_t len;
3109
3110 uiov = u64_to_user_ptr(req->rw.addr);
3111 if (!access_ok(uiov, sizeof(*uiov)))
3112 return -EFAULT;
3113 if (__get_user(clen, &uiov->iov_len))
3114 return -EFAULT;
3115 if (clen < 0)
3116 return -EINVAL;
3117
3118 len = clen;
3119 buf = io_rw_buffer_select(req, &len, needs_lock);
3120 if (IS_ERR(buf))
3121 return PTR_ERR(buf);
3122 iov[0].iov_base = buf;
3123 iov[0].iov_len = (compat_size_t) len;
3124 return 0;
3125}
3126#endif
3127
3128static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3129 bool needs_lock)
3130{
3131 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3132 void __user *buf;
3133 ssize_t len;
3134
3135 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3136 return -EFAULT;
3137
3138 len = iov[0].iov_len;
3139 if (len < 0)
3140 return -EINVAL;
3141 buf = io_rw_buffer_select(req, &len, needs_lock);
3142 if (IS_ERR(buf))
3143 return PTR_ERR(buf);
3144 iov[0].iov_base = buf;
3145 iov[0].iov_len = len;
3146 return 0;
3147}
3148
3149static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3150 bool needs_lock)
3151{
Jens Axboedddb3e22020-06-04 11:27:01 -06003152 if (req->flags & REQ_F_BUFFER_SELECTED) {
3153 struct io_buffer *kbuf;
3154
3155 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3156 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3157 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003158 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003159 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003160 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003161 return -EINVAL;
3162
3163#ifdef CONFIG_COMPAT
3164 if (req->ctx->compat)
3165 return io_compat_import(req, iov, needs_lock);
3166#endif
3167
3168 return __io_iov_buffer_select(req, iov, needs_lock);
3169}
3170
Pavel Begunkov847595d2021-02-04 13:52:06 +00003171static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3172 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173{
Jens Axboe9adbd452019-12-20 08:45:55 -07003174 void __user *buf = u64_to_user_ptr(req->rw.addr);
3175 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003176 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003177 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003178
Pavel Begunkov7d009162019-11-25 23:14:40 +03003179 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003180 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003181 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003182 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183
Jens Axboebcda7ba2020-02-23 16:42:51 -07003184 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003185 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003186 return -EINVAL;
3187
Jens Axboe3a6820f2019-12-22 15:19:35 -07003188 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003189 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003190 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003191 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003192 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003193 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003194 }
3195
Jens Axboe3a6820f2019-12-22 15:19:35 -07003196 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3197 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003198 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003199 }
3200
Jens Axboe4d954c22020-02-27 07:31:19 -07003201 if (req->flags & REQ_F_BUFFER_SELECT) {
3202 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003203 if (!ret)
3204 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003205 *iovec = NULL;
3206 return ret;
3207 }
3208
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003209 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3210 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003211}
3212
Jens Axboe0fef9482020-08-26 10:36:20 -06003213static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3214{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003215 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003216}
3217
Jens Axboe32960612019-09-23 11:05:34 -06003218/*
3219 * For files that don't have ->read_iter() and ->write_iter(), handle them
3220 * by looping over ->read() or ->write() manually.
3221 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003222static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003223{
Jens Axboe4017eb92020-10-22 14:14:12 -06003224 struct kiocb *kiocb = &req->rw.kiocb;
3225 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003226 ssize_t ret = 0;
3227
3228 /*
3229 * Don't support polled IO through this interface, and we can't
3230 * support non-blocking either. For the latter, this just causes
3231 * the kiocb to be handled from an async context.
3232 */
3233 if (kiocb->ki_flags & IOCB_HIPRI)
3234 return -EOPNOTSUPP;
3235 if (kiocb->ki_flags & IOCB_NOWAIT)
3236 return -EAGAIN;
3237
3238 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003239 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003240 ssize_t nr;
3241
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003242 if (!iov_iter_is_bvec(iter)) {
3243 iovec = iov_iter_iovec(iter);
3244 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003245 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3246 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003247 }
3248
Jens Axboe32960612019-09-23 11:05:34 -06003249 if (rw == READ) {
3250 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003251 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003252 } else {
3253 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003254 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003255 }
3256
3257 if (nr < 0) {
3258 if (!ret)
3259 ret = nr;
3260 break;
3261 }
3262 ret += nr;
3263 if (nr != iovec.iov_len)
3264 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003265 req->rw.len -= nr;
3266 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003267 iov_iter_advance(iter, nr);
3268 }
3269
3270 return ret;
3271}
3272
Jens Axboeff6165b2020-08-13 09:47:43 -06003273static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3274 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003275{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003276 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003277
Jens Axboeff6165b2020-08-13 09:47:43 -06003278 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003279 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003280 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003281 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003282 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003283 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003284 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003285 unsigned iov_off = 0;
3286
3287 rw->iter.iov = rw->fast_iov;
3288 if (iter->iov != fast_iov) {
3289 iov_off = iter->iov - fast_iov;
3290 rw->iter.iov += iov_off;
3291 }
3292 if (rw->fast_iov != fast_iov)
3293 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003294 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003295 } else {
3296 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003297 }
3298}
3299
Jens Axboee8c2bc12020-08-15 18:44:09 -07003300static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003301{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003302 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3303 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3304 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003305}
3306
Jens Axboee8c2bc12020-08-15 18:44:09 -07003307static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003308{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003309 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003310 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003311
Jens Axboee8c2bc12020-08-15 18:44:09 -07003312 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003313}
3314
Jens Axboeff6165b2020-08-13 09:47:43 -06003315static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3316 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003317 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003318{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003319 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003320 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003321 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003322 if (__io_alloc_async_data(req)) {
3323 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003324 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003325 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003326
Jens Axboeff6165b2020-08-13 09:47:43 -06003327 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003328 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003329 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003330}
3331
Pavel Begunkov73debe62020-09-30 22:57:54 +03003332static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003333{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003334 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003335 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003336 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003337
Pavel Begunkov2846c482020-11-07 13:16:27 +00003338 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003339 if (unlikely(ret < 0))
3340 return ret;
3341
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003342 iorw->bytes_done = 0;
3343 iorw->free_iovec = iov;
3344 if (iov)
3345 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003346 return 0;
3347}
3348
Pavel Begunkov73debe62020-09-30 22:57:54 +03003349static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003350{
3351 ssize_t ret;
3352
Pavel Begunkova88fc402020-09-30 22:57:53 +03003353 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003354 if (ret)
3355 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003356
Jens Axboe3529d8c2019-12-19 18:24:38 -07003357 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3358 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003359
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003360 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003361 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003362 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003363 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003364}
3365
Jens Axboec1dd91d2020-08-03 16:43:59 -06003366/*
3367 * This is our waitqueue callback handler, registered through lock_page_async()
3368 * when we initially tried to do the IO with the iocb armed our waitqueue.
3369 * This gets called when the page is unlocked, and we generally expect that to
3370 * happen when the page IO is completed and the page is now uptodate. This will
3371 * queue a task_work based retry of the operation, attempting to copy the data
3372 * again. If the latter fails because the page was NOT uptodate, then we will
3373 * do a thread based blocking retry of the operation. That's the unexpected
3374 * slow path.
3375 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003376static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3377 int sync, void *arg)
3378{
3379 struct wait_page_queue *wpq;
3380 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003381 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003382 int ret;
3383
3384 wpq = container_of(wait, struct wait_page_queue, wait);
3385
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003386 if (!wake_page_match(wpq, key))
3387 return 0;
3388
Hao Xuc8d317a2020-09-29 20:00:45 +08003389 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003390 list_del_init(&wait->entry);
3391
Pavel Begunkove7375122020-07-12 20:42:04 +03003392 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003393 percpu_ref_get(&req->ctx->refs);
3394
Jens Axboebcf5a062020-05-22 09:24:42 -06003395 /* submit ref gets dropped, acquire a new one */
3396 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003397 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00003398 if (unlikely(ret))
3399 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003400 return 1;
3401}
3402
Jens Axboec1dd91d2020-08-03 16:43:59 -06003403/*
3404 * This controls whether a given IO request should be armed for async page
3405 * based retry. If we return false here, the request is handed to the async
3406 * worker threads for retry. If we're doing buffered reads on a regular file,
3407 * we prepare a private wait_page_queue entry and retry the operation. This
3408 * will either succeed because the page is now uptodate and unlocked, or it
3409 * will register a callback when the page is unlocked at IO completion. Through
3410 * that callback, io_uring uses task_work to setup a retry of the operation.
3411 * That retry will attempt the buffered read again. The retry will generally
3412 * succeed, or in rare cases where it fails, we then fall back to using the
3413 * async worker threads for a blocking retry.
3414 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003415static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003416{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003417 struct io_async_rw *rw = req->async_data;
3418 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003419 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003420
3421 /* never retry for NOWAIT, we just complete with -EAGAIN */
3422 if (req->flags & REQ_F_NOWAIT)
3423 return false;
3424
Jens Axboe227c0c92020-08-13 11:51:40 -06003425 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003426 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003427 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003428
Jens Axboebcf5a062020-05-22 09:24:42 -06003429 /*
3430 * just use poll if we can, and don't attempt if the fs doesn't
3431 * support callback based unlocks
3432 */
3433 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3434 return false;
3435
Jens Axboe3b2a4432020-08-16 10:58:43 -07003436 wait->wait.func = io_async_buf_func;
3437 wait->wait.private = req;
3438 wait->wait.flags = 0;
3439 INIT_LIST_HEAD(&wait->wait.entry);
3440 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003441 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003442 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003443 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003444}
3445
3446static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3447{
3448 if (req->file->f_op->read_iter)
3449 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003450 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003451 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003452 else
3453 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003454}
3455
Pavel Begunkov889fca72021-02-10 00:03:09 +00003456static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003457{
3458 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003459 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003460 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003461 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003462 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003463 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003464
Pavel Begunkov2846c482020-11-07 13:16:27 +00003465 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003466 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003467 iovec = NULL;
3468 } else {
3469 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3470 if (ret < 0)
3471 return ret;
3472 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003473 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003474 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475
Jens Axboefd6c2e42019-12-18 12:19:41 -07003476 /* Ensure we clear previously set non-block flag */
3477 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003478 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003479 else
3480 kiocb->ki_flags |= IOCB_NOWAIT;
3481
Pavel Begunkov24c74672020-06-21 13:09:51 +03003482 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003483 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3484 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003485 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003486 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003487
Pavel Begunkov632546c2020-11-07 13:16:26 +00003488 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003489 if (unlikely(ret)) {
3490 kfree(iovec);
3491 return ret;
3492 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493
Jens Axboe227c0c92020-08-13 11:51:40 -06003494 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003495
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003496 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003497 /* it's faster to check here then delegate to kfree */
3498 if (iovec)
3499 kfree(iovec);
3500 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003501 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003502 /* IOPOLL retry should happen for io-wq threads */
3503 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003504 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003505 /* no retry on NONBLOCK nor RWF_NOWAIT */
3506 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003507 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003508 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003509 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003510 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003511 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003512 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003513 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003514 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003515 }
3516
Jens Axboe227c0c92020-08-13 11:51:40 -06003517 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003518 if (ret2)
3519 return ret2;
3520
Jens Axboee8c2bc12020-08-15 18:44:09 -07003521 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003522 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003523 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003524
Pavel Begunkovb23df912021-02-04 13:52:04 +00003525 do {
3526 io_size -= ret;
3527 rw->bytes_done += ret;
3528 /* if we can retry, do so with the callbacks armed */
3529 if (!io_rw_should_retry(req)) {
3530 kiocb->ki_flags &= ~IOCB_WAITQ;
3531 return -EAGAIN;
3532 }
3533
3534 /*
3535 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3536 * we get -EIOCBQUEUED, then we'll get a notification when the
3537 * desired page gets unlocked. We can also get a partial read
3538 * here, and if we do, then just retry at the new offset.
3539 */
3540 ret = io_iter_do_read(req, iter);
3541 if (ret == -EIOCBQUEUED)
3542 return 0;
3543 /* we got some bytes, but not all. retry. */
3544 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003545done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003546 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003547 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003548}
3549
Pavel Begunkov73debe62020-09-30 22:57:54 +03003550static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003551{
3552 ssize_t ret;
3553
Pavel Begunkova88fc402020-09-30 22:57:53 +03003554 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003555 if (ret)
3556 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003557
Jens Axboe3529d8c2019-12-19 18:24:38 -07003558 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3559 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003560
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003561 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003562 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003563 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003564 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003565}
3566
Pavel Begunkov889fca72021-02-10 00:03:09 +00003567static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003568{
3569 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003570 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003571 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003572 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003573 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003574 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003575
Pavel Begunkov2846c482020-11-07 13:16:27 +00003576 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003577 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003578 iovec = NULL;
3579 } else {
3580 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3581 if (ret < 0)
3582 return ret;
3583 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003584 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003585 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003586
Jens Axboefd6c2e42019-12-18 12:19:41 -07003587 /* Ensure we clear previously set non-block flag */
3588 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003589 kiocb->ki_flags &= ~IOCB_NOWAIT;
3590 else
3591 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003592
Pavel Begunkov24c74672020-06-21 13:09:51 +03003593 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003594 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003595 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003596
Jens Axboe10d59342019-12-09 20:16:22 -07003597 /* file path doesn't support NOWAIT for non-direct_IO */
3598 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3599 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003600 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003601
Pavel Begunkov632546c2020-11-07 13:16:26 +00003602 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003603 if (unlikely(ret))
3604 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003605
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003606 /*
3607 * Open-code file_start_write here to grab freeze protection,
3608 * which will be released by another thread in
3609 * io_complete_rw(). Fool lockdep by telling it the lock got
3610 * released so that it doesn't complain about the held lock when
3611 * we return to userspace.
3612 */
3613 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003614 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003615 __sb_writers_release(file_inode(req->file)->i_sb,
3616 SB_FREEZE_WRITE);
3617 }
3618 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003619
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003620 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003621 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003622 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003623 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003624 else
3625 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003626
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003627 /*
3628 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3629 * retry them without IOCB_NOWAIT.
3630 */
3631 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3632 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003633 /* no retry on NONBLOCK nor RWF_NOWAIT */
3634 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003635 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003636 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003637 /* IOPOLL retry should happen for io-wq threads */
3638 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3639 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003640done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003641 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003642 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003643copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003644 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003645 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003646 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003647 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003648 }
Jens Axboe31b51512019-01-18 22:56:34 -07003649out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003650 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003651 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003652 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003653 return ret;
3654}
3655
Jens Axboe80a261f2020-09-28 14:23:58 -06003656static int io_renameat_prep(struct io_kiocb *req,
3657 const struct io_uring_sqe *sqe)
3658{
3659 struct io_rename *ren = &req->rename;
3660 const char __user *oldf, *newf;
3661
3662 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3663 return -EBADF;
3664
3665 ren->old_dfd = READ_ONCE(sqe->fd);
3666 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3667 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3668 ren->new_dfd = READ_ONCE(sqe->len);
3669 ren->flags = READ_ONCE(sqe->rename_flags);
3670
3671 ren->oldpath = getname(oldf);
3672 if (IS_ERR(ren->oldpath))
3673 return PTR_ERR(ren->oldpath);
3674
3675 ren->newpath = getname(newf);
3676 if (IS_ERR(ren->newpath)) {
3677 putname(ren->oldpath);
3678 return PTR_ERR(ren->newpath);
3679 }
3680
3681 req->flags |= REQ_F_NEED_CLEANUP;
3682 return 0;
3683}
3684
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003685static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003686{
3687 struct io_rename *ren = &req->rename;
3688 int ret;
3689
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003690 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003691 return -EAGAIN;
3692
3693 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3694 ren->newpath, ren->flags);
3695
3696 req->flags &= ~REQ_F_NEED_CLEANUP;
3697 if (ret < 0)
3698 req_set_fail_links(req);
3699 io_req_complete(req, ret);
3700 return 0;
3701}
3702
Jens Axboe14a11432020-09-28 14:27:37 -06003703static int io_unlinkat_prep(struct io_kiocb *req,
3704 const struct io_uring_sqe *sqe)
3705{
3706 struct io_unlink *un = &req->unlink;
3707 const char __user *fname;
3708
3709 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3710 return -EBADF;
3711
3712 un->dfd = READ_ONCE(sqe->fd);
3713
3714 un->flags = READ_ONCE(sqe->unlink_flags);
3715 if (un->flags & ~AT_REMOVEDIR)
3716 return -EINVAL;
3717
3718 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3719 un->filename = getname(fname);
3720 if (IS_ERR(un->filename))
3721 return PTR_ERR(un->filename);
3722
3723 req->flags |= REQ_F_NEED_CLEANUP;
3724 return 0;
3725}
3726
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003727static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003728{
3729 struct io_unlink *un = &req->unlink;
3730 int ret;
3731
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003732 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003733 return -EAGAIN;
3734
3735 if (un->flags & AT_REMOVEDIR)
3736 ret = do_rmdir(un->dfd, un->filename);
3737 else
3738 ret = do_unlinkat(un->dfd, un->filename);
3739
3740 req->flags &= ~REQ_F_NEED_CLEANUP;
3741 if (ret < 0)
3742 req_set_fail_links(req);
3743 io_req_complete(req, ret);
3744 return 0;
3745}
3746
Jens Axboe36f4fa62020-09-05 11:14:22 -06003747static int io_shutdown_prep(struct io_kiocb *req,
3748 const struct io_uring_sqe *sqe)
3749{
3750#if defined(CONFIG_NET)
3751 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3752 return -EINVAL;
3753 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3754 sqe->buf_index)
3755 return -EINVAL;
3756
3757 req->shutdown.how = READ_ONCE(sqe->len);
3758 return 0;
3759#else
3760 return -EOPNOTSUPP;
3761#endif
3762}
3763
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003764static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003765{
3766#if defined(CONFIG_NET)
3767 struct socket *sock;
3768 int ret;
3769
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003770 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003771 return -EAGAIN;
3772
Linus Torvalds48aba792020-12-16 12:44:05 -08003773 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003774 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003775 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003776
3777 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003778 if (ret < 0)
3779 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003780 io_req_complete(req, ret);
3781 return 0;
3782#else
3783 return -EOPNOTSUPP;
3784#endif
3785}
3786
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003787static int __io_splice_prep(struct io_kiocb *req,
3788 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003789{
3790 struct io_splice* sp = &req->splice;
3791 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003792
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003793 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3794 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003795
3796 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003797 sp->len = READ_ONCE(sqe->len);
3798 sp->flags = READ_ONCE(sqe->splice_flags);
3799
3800 if (unlikely(sp->flags & ~valid_flags))
3801 return -EINVAL;
3802
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003803 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3804 (sp->flags & SPLICE_F_FD_IN_FIXED));
3805 if (!sp->file_in)
3806 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003807 req->flags |= REQ_F_NEED_CLEANUP;
3808
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003809 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3810 /*
3811 * Splice operation will be punted aync, and here need to
3812 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3813 */
3814 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003815 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003816 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003817
3818 return 0;
3819}
3820
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003821static int io_tee_prep(struct io_kiocb *req,
3822 const struct io_uring_sqe *sqe)
3823{
3824 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3825 return -EINVAL;
3826 return __io_splice_prep(req, sqe);
3827}
3828
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003829static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003830{
3831 struct io_splice *sp = &req->splice;
3832 struct file *in = sp->file_in;
3833 struct file *out = sp->file_out;
3834 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3835 long ret = 0;
3836
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003837 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003838 return -EAGAIN;
3839 if (sp->len)
3840 ret = do_tee(in, out, sp->len, flags);
3841
3842 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3843 req->flags &= ~REQ_F_NEED_CLEANUP;
3844
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003845 if (ret != sp->len)
3846 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003847 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003848 return 0;
3849}
3850
3851static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3852{
3853 struct io_splice* sp = &req->splice;
3854
3855 sp->off_in = READ_ONCE(sqe->splice_off_in);
3856 sp->off_out = READ_ONCE(sqe->off);
3857 return __io_splice_prep(req, sqe);
3858}
3859
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003860static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003861{
3862 struct io_splice *sp = &req->splice;
3863 struct file *in = sp->file_in;
3864 struct file *out = sp->file_out;
3865 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3866 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003867 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003868
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003869 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003870 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003871
3872 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3873 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003874
Jens Axboe948a7742020-05-17 14:21:38 -06003875 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003876 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003877
3878 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3879 req->flags &= ~REQ_F_NEED_CLEANUP;
3880
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003881 if (ret != sp->len)
3882 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003883 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003884 return 0;
3885}
3886
Jens Axboe2b188cc2019-01-07 10:46:33 -07003887/*
3888 * IORING_OP_NOP just posts a completion event, nothing else.
3889 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003890static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003891{
3892 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003893
Jens Axboedef596e2019-01-09 08:59:42 -07003894 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3895 return -EINVAL;
3896
Pavel Begunkov889fca72021-02-10 00:03:09 +00003897 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003898 return 0;
3899}
3900
Jens Axboe3529d8c2019-12-19 18:24:38 -07003901static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003902{
Jens Axboe6b063142019-01-10 22:13:58 -07003903 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003904
Jens Axboe09bb8392019-03-13 12:39:28 -06003905 if (!req->file)
3906 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003907
Jens Axboe6b063142019-01-10 22:13:58 -07003908 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003909 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003910 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003911 return -EINVAL;
3912
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003913 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3914 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3915 return -EINVAL;
3916
3917 req->sync.off = READ_ONCE(sqe->off);
3918 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003919 return 0;
3920}
3921
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003922static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003923{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003924 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003925 int ret;
3926
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003927 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003928 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003929 return -EAGAIN;
3930
Jens Axboe9adbd452019-12-20 08:45:55 -07003931 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003932 end > 0 ? end : LLONG_MAX,
3933 req->sync.flags & IORING_FSYNC_DATASYNC);
3934 if (ret < 0)
3935 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003936 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003937 return 0;
3938}
3939
Jens Axboed63d1b52019-12-10 10:38:56 -07003940static int io_fallocate_prep(struct io_kiocb *req,
3941 const struct io_uring_sqe *sqe)
3942{
3943 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3944 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003945 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3946 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003947
3948 req->sync.off = READ_ONCE(sqe->off);
3949 req->sync.len = READ_ONCE(sqe->addr);
3950 req->sync.mode = READ_ONCE(sqe->len);
3951 return 0;
3952}
3953
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003954static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003955{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003956 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003957
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003958 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003959 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003960 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003961 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3962 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003963 if (ret < 0)
3964 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003965 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003966 return 0;
3967}
3968
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003969static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003970{
Jens Axboef8748882020-01-08 17:47:02 -07003971 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003972 int ret;
3973
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003974 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003975 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003976 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003977 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003978
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003979 /* open.how should be already initialised */
3980 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003981 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003982
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003983 req->open.dfd = READ_ONCE(sqe->fd);
3984 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003985 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003986 if (IS_ERR(req->open.filename)) {
3987 ret = PTR_ERR(req->open.filename);
3988 req->open.filename = NULL;
3989 return ret;
3990 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003991 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003992 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003993 return 0;
3994}
3995
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003996static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3997{
3998 u64 flags, mode;
3999
Jens Axboe14587a462020-09-05 11:36:08 -06004000 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004001 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004002 mode = READ_ONCE(sqe->len);
4003 flags = READ_ONCE(sqe->open_flags);
4004 req->open.how = build_open_how(flags, mode);
4005 return __io_openat_prep(req, sqe);
4006}
4007
Jens Axboecebdb982020-01-08 17:59:24 -07004008static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4009{
4010 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004011 size_t len;
4012 int ret;
4013
Jens Axboe14587a462020-09-05 11:36:08 -06004014 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004015 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004016 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4017 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004018 if (len < OPEN_HOW_SIZE_VER0)
4019 return -EINVAL;
4020
4021 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4022 len);
4023 if (ret)
4024 return ret;
4025
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004026 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004027}
4028
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004029static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004030{
4031 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004032 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004033 bool nonblock_set;
4034 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004035 int ret;
4036
Jens Axboecebdb982020-01-08 17:59:24 -07004037 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004038 if (ret)
4039 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004040 nonblock_set = op.open_flag & O_NONBLOCK;
4041 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004042 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004043 /*
4044 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4045 * it'll always -EAGAIN
4046 */
4047 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4048 return -EAGAIN;
4049 op.lookup_flags |= LOOKUP_CACHED;
4050 op.open_flag |= O_NONBLOCK;
4051 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004052
Jens Axboe4022e7a2020-03-19 19:23:18 -06004053 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004054 if (ret < 0)
4055 goto err;
4056
4057 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004058 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004059 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4060 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004061 /*
4062 * We could hang on to this 'fd', but seems like marginal
4063 * gain for something that is now known to be a slower path.
4064 * So just put it, and we'll get a new one when we retry.
4065 */
4066 put_unused_fd(ret);
4067 return -EAGAIN;
4068 }
4069
Jens Axboe15b71ab2019-12-11 11:20:36 -07004070 if (IS_ERR(file)) {
4071 put_unused_fd(ret);
4072 ret = PTR_ERR(file);
4073 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004074 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004075 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004076 fsnotify_open(file);
4077 fd_install(ret, file);
4078 }
4079err:
4080 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004081 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004082 if (ret < 0)
4083 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004084 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004085 return 0;
4086}
4087
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004088static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004089{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004090 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004091}
4092
Jens Axboe067524e2020-03-02 16:32:28 -07004093static int io_remove_buffers_prep(struct io_kiocb *req,
4094 const struct io_uring_sqe *sqe)
4095{
4096 struct io_provide_buf *p = &req->pbuf;
4097 u64 tmp;
4098
4099 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4100 return -EINVAL;
4101
4102 tmp = READ_ONCE(sqe->fd);
4103 if (!tmp || tmp > USHRT_MAX)
4104 return -EINVAL;
4105
4106 memset(p, 0, sizeof(*p));
4107 p->nbufs = tmp;
4108 p->bgid = READ_ONCE(sqe->buf_group);
4109 return 0;
4110}
4111
4112static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4113 int bgid, unsigned nbufs)
4114{
4115 unsigned i = 0;
4116
4117 /* shouldn't happen */
4118 if (!nbufs)
4119 return 0;
4120
4121 /* the head kbuf is the list itself */
4122 while (!list_empty(&buf->list)) {
4123 struct io_buffer *nxt;
4124
4125 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4126 list_del(&nxt->list);
4127 kfree(nxt);
4128 if (++i == nbufs)
4129 return i;
4130 }
4131 i++;
4132 kfree(buf);
4133 idr_remove(&ctx->io_buffer_idr, bgid);
4134
4135 return i;
4136}
4137
Pavel Begunkov889fca72021-02-10 00:03:09 +00004138static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004139{
4140 struct io_provide_buf *p = &req->pbuf;
4141 struct io_ring_ctx *ctx = req->ctx;
4142 struct io_buffer *head;
4143 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004144 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004145
4146 io_ring_submit_lock(ctx, !force_nonblock);
4147
4148 lockdep_assert_held(&ctx->uring_lock);
4149
4150 ret = -ENOENT;
4151 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4152 if (head)
4153 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004154 if (ret < 0)
4155 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004156
4157 /* need to hold the lock to complete IOPOLL requests */
4158 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004159 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004160 io_ring_submit_unlock(ctx, !force_nonblock);
4161 } else {
4162 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004163 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004164 }
Jens Axboe067524e2020-03-02 16:32:28 -07004165 return 0;
4166}
4167
Jens Axboeddf0322d2020-02-23 16:41:33 -07004168static int io_provide_buffers_prep(struct io_kiocb *req,
4169 const struct io_uring_sqe *sqe)
4170{
4171 struct io_provide_buf *p = &req->pbuf;
4172 u64 tmp;
4173
4174 if (sqe->ioprio || sqe->rw_flags)
4175 return -EINVAL;
4176
4177 tmp = READ_ONCE(sqe->fd);
4178 if (!tmp || tmp > USHRT_MAX)
4179 return -E2BIG;
4180 p->nbufs = tmp;
4181 p->addr = READ_ONCE(sqe->addr);
4182 p->len = READ_ONCE(sqe->len);
4183
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004184 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004185 return -EFAULT;
4186
4187 p->bgid = READ_ONCE(sqe->buf_group);
4188 tmp = READ_ONCE(sqe->off);
4189 if (tmp > USHRT_MAX)
4190 return -E2BIG;
4191 p->bid = tmp;
4192 return 0;
4193}
4194
4195static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4196{
4197 struct io_buffer *buf;
4198 u64 addr = pbuf->addr;
4199 int i, bid = pbuf->bid;
4200
4201 for (i = 0; i < pbuf->nbufs; i++) {
4202 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4203 if (!buf)
4204 break;
4205
4206 buf->addr = addr;
4207 buf->len = pbuf->len;
4208 buf->bid = bid;
4209 addr += pbuf->len;
4210 bid++;
4211 if (!*head) {
4212 INIT_LIST_HEAD(&buf->list);
4213 *head = buf;
4214 } else {
4215 list_add_tail(&buf->list, &(*head)->list);
4216 }
4217 }
4218
4219 return i ? i : -ENOMEM;
4220}
4221
Pavel Begunkov889fca72021-02-10 00:03:09 +00004222static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004223{
4224 struct io_provide_buf *p = &req->pbuf;
4225 struct io_ring_ctx *ctx = req->ctx;
4226 struct io_buffer *head, *list;
4227 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004228 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004229
4230 io_ring_submit_lock(ctx, !force_nonblock);
4231
4232 lockdep_assert_held(&ctx->uring_lock);
4233
4234 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4235
4236 ret = io_add_buffers(p, &head);
4237 if (ret < 0)
4238 goto out;
4239
4240 if (!list) {
4241 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4242 GFP_KERNEL);
4243 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004244 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004245 goto out;
4246 }
4247 }
4248out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004249 if (ret < 0)
4250 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004251
4252 /* need to hold the lock to complete IOPOLL requests */
4253 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004254 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004255 io_ring_submit_unlock(ctx, !force_nonblock);
4256 } else {
4257 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004258 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004259 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004260 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004261}
4262
Jens Axboe3e4827b2020-01-08 15:18:09 -07004263static int io_epoll_ctl_prep(struct io_kiocb *req,
4264 const struct io_uring_sqe *sqe)
4265{
4266#if defined(CONFIG_EPOLL)
4267 if (sqe->ioprio || sqe->buf_index)
4268 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004269 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004270 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004271
4272 req->epoll.epfd = READ_ONCE(sqe->fd);
4273 req->epoll.op = READ_ONCE(sqe->len);
4274 req->epoll.fd = READ_ONCE(sqe->off);
4275
4276 if (ep_op_has_event(req->epoll.op)) {
4277 struct epoll_event __user *ev;
4278
4279 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4280 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4281 return -EFAULT;
4282 }
4283
4284 return 0;
4285#else
4286 return -EOPNOTSUPP;
4287#endif
4288}
4289
Pavel Begunkov889fca72021-02-10 00:03:09 +00004290static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004291{
4292#if defined(CONFIG_EPOLL)
4293 struct io_epoll *ie = &req->epoll;
4294 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004295 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004296
4297 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4298 if (force_nonblock && ret == -EAGAIN)
4299 return -EAGAIN;
4300
4301 if (ret < 0)
4302 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004303 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004304 return 0;
4305#else
4306 return -EOPNOTSUPP;
4307#endif
4308}
4309
Jens Axboec1ca7572019-12-25 22:18:28 -07004310static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4311{
4312#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4313 if (sqe->ioprio || sqe->buf_index || sqe->off)
4314 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004315 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4316 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004317
4318 req->madvise.addr = READ_ONCE(sqe->addr);
4319 req->madvise.len = READ_ONCE(sqe->len);
4320 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4321 return 0;
4322#else
4323 return -EOPNOTSUPP;
4324#endif
4325}
4326
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004327static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004328{
4329#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4330 struct io_madvise *ma = &req->madvise;
4331 int ret;
4332
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004333 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004334 return -EAGAIN;
4335
Minchan Kim0726b012020-10-17 16:14:50 -07004336 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004337 if (ret < 0)
4338 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004339 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004340 return 0;
4341#else
4342 return -EOPNOTSUPP;
4343#endif
4344}
4345
Jens Axboe4840e412019-12-25 22:03:45 -07004346static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4347{
4348 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4349 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004350 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4351 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004352
4353 req->fadvise.offset = READ_ONCE(sqe->off);
4354 req->fadvise.len = READ_ONCE(sqe->len);
4355 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4356 return 0;
4357}
4358
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004359static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004360{
4361 struct io_fadvise *fa = &req->fadvise;
4362 int ret;
4363
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004364 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004365 switch (fa->advice) {
4366 case POSIX_FADV_NORMAL:
4367 case POSIX_FADV_RANDOM:
4368 case POSIX_FADV_SEQUENTIAL:
4369 break;
4370 default:
4371 return -EAGAIN;
4372 }
4373 }
Jens Axboe4840e412019-12-25 22:03:45 -07004374
4375 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4376 if (ret < 0)
4377 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004378 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004379 return 0;
4380}
4381
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004382static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4383{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004384 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004385 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004386 if (sqe->ioprio || sqe->buf_index)
4387 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004388 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004389 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004390
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004391 req->statx.dfd = READ_ONCE(sqe->fd);
4392 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004393 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004394 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4395 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004396
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004397 return 0;
4398}
4399
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004400static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004401{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004402 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004403 int ret;
4404
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004405 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004406 /* only need file table for an actual valid fd */
4407 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4408 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004409 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004410 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004411
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004412 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4413 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004414
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004415 if (ret < 0)
4416 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004417 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004418 return 0;
4419}
4420
Jens Axboeb5dba592019-12-11 14:02:38 -07004421static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4422{
Jens Axboe14587a462020-09-05 11:36:08 -06004423 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004424 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004425 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4426 sqe->rw_flags || sqe->buf_index)
4427 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004428 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004429 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004430
4431 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004432 return 0;
4433}
4434
Pavel Begunkov889fca72021-02-10 00:03:09 +00004435static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004436{
Jens Axboe9eac1902021-01-19 15:50:37 -07004437 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004438 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004439 struct fdtable *fdt;
4440 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004441 int ret;
4442
Jens Axboe9eac1902021-01-19 15:50:37 -07004443 file = NULL;
4444 ret = -EBADF;
4445 spin_lock(&files->file_lock);
4446 fdt = files_fdtable(files);
4447 if (close->fd >= fdt->max_fds) {
4448 spin_unlock(&files->file_lock);
4449 goto err;
4450 }
4451 file = fdt->fd[close->fd];
4452 if (!file) {
4453 spin_unlock(&files->file_lock);
4454 goto err;
4455 }
4456
4457 if (file->f_op == &io_uring_fops) {
4458 spin_unlock(&files->file_lock);
4459 file = NULL;
4460 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004461 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004462
4463 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004464 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004465 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004466 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004467 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004468
Jens Axboe9eac1902021-01-19 15:50:37 -07004469 ret = __close_fd_get_file(close->fd, &file);
4470 spin_unlock(&files->file_lock);
4471 if (ret < 0) {
4472 if (ret == -ENOENT)
4473 ret = -EBADF;
4474 goto err;
4475 }
4476
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004477 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004478 ret = filp_close(file, current->files);
4479err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004480 if (ret < 0)
4481 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004482 if (file)
4483 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004484 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004485 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004486}
4487
Jens Axboe3529d8c2019-12-19 18:24:38 -07004488static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004489{
4490 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004491
4492 if (!req->file)
4493 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004494
4495 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4496 return -EINVAL;
4497 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4498 return -EINVAL;
4499
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004500 req->sync.off = READ_ONCE(sqe->off);
4501 req->sync.len = READ_ONCE(sqe->len);
4502 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004503 return 0;
4504}
4505
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004506static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004507{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004508 int ret;
4509
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004510 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004511 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004512 return -EAGAIN;
4513
Jens Axboe9adbd452019-12-20 08:45:55 -07004514 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004515 req->sync.flags);
4516 if (ret < 0)
4517 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004518 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004519 return 0;
4520}
4521
YueHaibing469956e2020-03-04 15:53:52 +08004522#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004523static int io_setup_async_msg(struct io_kiocb *req,
4524 struct io_async_msghdr *kmsg)
4525{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004526 struct io_async_msghdr *async_msg = req->async_data;
4527
4528 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004529 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004530 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004531 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004532 return -ENOMEM;
4533 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004534 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004535 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004536 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004537 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004538 /* if were using fast_iov, set it to the new one */
4539 if (!async_msg->free_iov)
4540 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4541
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004542 return -EAGAIN;
4543}
4544
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004545static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4546 struct io_async_msghdr *iomsg)
4547{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004548 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004549 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004550 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004551 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004552}
4553
Jens Axboe3529d8c2019-12-19 18:24:38 -07004554static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004555{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004556 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004557 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004558 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004559
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004560 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4561 return -EINVAL;
4562
Jens Axboee47293f2019-12-20 08:58:21 -07004563 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004564 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004565 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004566
Jens Axboed8768362020-02-27 14:17:49 -07004567#ifdef CONFIG_COMPAT
4568 if (req->ctx->compat)
4569 sr->msg_flags |= MSG_CMSG_COMPAT;
4570#endif
4571
Jens Axboee8c2bc12020-08-15 18:44:09 -07004572 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004573 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004574 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004575 if (!ret)
4576 req->flags |= REQ_F_NEED_CLEANUP;
4577 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004578}
4579
Pavel Begunkov889fca72021-02-10 00:03:09 +00004580static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004581{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004582 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004583 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004584 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004585 int ret;
4586
Florent Revestdba4a922020-12-04 12:36:04 +01004587 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004588 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004589 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004590
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004591 kmsg = req->async_data;
4592 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004593 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004594 if (ret)
4595 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004596 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004597 }
4598
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004599 flags = req->sr_msg.msg_flags;
4600 if (flags & MSG_DONTWAIT)
4601 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004602 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004603 flags |= MSG_DONTWAIT;
4604
4605 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004606 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004607 return io_setup_async_msg(req, kmsg);
4608 if (ret == -ERESTARTSYS)
4609 ret = -EINTR;
4610
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004611 /* fast path, check for non-NULL to avoid function call */
4612 if (kmsg->free_iov)
4613 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004614 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004615 if (ret < 0)
4616 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004617 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004618 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004619}
4620
Pavel Begunkov889fca72021-02-10 00:03:09 +00004621static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004622{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004623 struct io_sr_msg *sr = &req->sr_msg;
4624 struct msghdr msg;
4625 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004626 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004627 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004628 int ret;
4629
Florent Revestdba4a922020-12-04 12:36:04 +01004630 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004632 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004633
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004634 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4635 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004636 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004637
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004638 msg.msg_name = NULL;
4639 msg.msg_control = NULL;
4640 msg.msg_controllen = 0;
4641 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004642
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004643 flags = req->sr_msg.msg_flags;
4644 if (flags & MSG_DONTWAIT)
4645 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004646 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004647 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004648
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004649 msg.msg_flags = flags;
4650 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004651 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004652 return -EAGAIN;
4653 if (ret == -ERESTARTSYS)
4654 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004655
Jens Axboe03b12302019-12-02 18:50:25 -07004656 if (ret < 0)
4657 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004658 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004659 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004660}
4661
Pavel Begunkov1400e692020-07-12 20:41:05 +03004662static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4663 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004664{
4665 struct io_sr_msg *sr = &req->sr_msg;
4666 struct iovec __user *uiov;
4667 size_t iov_len;
4668 int ret;
4669
Pavel Begunkov1400e692020-07-12 20:41:05 +03004670 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4671 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004672 if (ret)
4673 return ret;
4674
4675 if (req->flags & REQ_F_BUFFER_SELECT) {
4676 if (iov_len > 1)
4677 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004678 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004679 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004680 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004681 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004682 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004683 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004684 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004685 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004686 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004687 if (ret > 0)
4688 ret = 0;
4689 }
4690
4691 return ret;
4692}
4693
4694#ifdef CONFIG_COMPAT
4695static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004696 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004697{
4698 struct compat_msghdr __user *msg_compat;
4699 struct io_sr_msg *sr = &req->sr_msg;
4700 struct compat_iovec __user *uiov;
4701 compat_uptr_t ptr;
4702 compat_size_t len;
4703 int ret;
4704
Pavel Begunkov270a5942020-07-12 20:41:04 +03004705 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004706 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004707 &ptr, &len);
4708 if (ret)
4709 return ret;
4710
4711 uiov = compat_ptr(ptr);
4712 if (req->flags & REQ_F_BUFFER_SELECT) {
4713 compat_ssize_t clen;
4714
4715 if (len > 1)
4716 return -EINVAL;
4717 if (!access_ok(uiov, sizeof(*uiov)))
4718 return -EFAULT;
4719 if (__get_user(clen, &uiov->iov_len))
4720 return -EFAULT;
4721 if (clen < 0)
4722 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004723 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004724 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004725 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004726 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004727 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004728 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004729 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004730 if (ret < 0)
4731 return ret;
4732 }
4733
4734 return 0;
4735}
Jens Axboe03b12302019-12-02 18:50:25 -07004736#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004737
Pavel Begunkov1400e692020-07-12 20:41:05 +03004738static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4739 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004740{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004741 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004742
4743#ifdef CONFIG_COMPAT
4744 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004745 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004746#endif
4747
Pavel Begunkov1400e692020-07-12 20:41:05 +03004748 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004749}
4750
Jens Axboebcda7ba2020-02-23 16:42:51 -07004751static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004752 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004753{
4754 struct io_sr_msg *sr = &req->sr_msg;
4755 struct io_buffer *kbuf;
4756
Jens Axboebcda7ba2020-02-23 16:42:51 -07004757 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4758 if (IS_ERR(kbuf))
4759 return kbuf;
4760
4761 sr->kbuf = kbuf;
4762 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004763 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004764}
4765
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004766static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4767{
4768 return io_put_kbuf(req, req->sr_msg.kbuf);
4769}
4770
Jens Axboe3529d8c2019-12-19 18:24:38 -07004771static int io_recvmsg_prep(struct io_kiocb *req,
4772 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004773{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004774 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004775 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004776 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004777
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004778 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4779 return -EINVAL;
4780
Jens Axboe3529d8c2019-12-19 18:24:38 -07004781 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004782 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004783 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004784 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004785
Jens Axboed8768362020-02-27 14:17:49 -07004786#ifdef CONFIG_COMPAT
4787 if (req->ctx->compat)
4788 sr->msg_flags |= MSG_CMSG_COMPAT;
4789#endif
4790
Jens Axboee8c2bc12020-08-15 18:44:09 -07004791 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004792 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004793 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004794 if (!ret)
4795 req->flags |= REQ_F_NEED_CLEANUP;
4796 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004797}
4798
Pavel Begunkov889fca72021-02-10 00:03:09 +00004799static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004800{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004801 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004802 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004803 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004804 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004805 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004806 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004807
Florent Revestdba4a922020-12-04 12:36:04 +01004808 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004809 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004810 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004811
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004812 kmsg = req->async_data;
4813 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004814 ret = io_recvmsg_copy_hdr(req, &iomsg);
4815 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004816 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004817 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004818 }
4819
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004820 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004821 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004822 if (IS_ERR(kbuf))
4823 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004824 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004825 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4826 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004827 1, req->sr_msg.len);
4828 }
4829
4830 flags = req->sr_msg.msg_flags;
4831 if (flags & MSG_DONTWAIT)
4832 req->flags |= REQ_F_NOWAIT;
4833 else if (force_nonblock)
4834 flags |= MSG_DONTWAIT;
4835
4836 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4837 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004838 if (force_nonblock && ret == -EAGAIN)
4839 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004840 if (ret == -ERESTARTSYS)
4841 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004842
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004843 if (req->flags & REQ_F_BUFFER_SELECTED)
4844 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004845 /* fast path, check for non-NULL to avoid function call */
4846 if (kmsg->free_iov)
4847 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004848 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004849 if (ret < 0)
4850 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004851 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004852 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004853}
4854
Pavel Begunkov889fca72021-02-10 00:03:09 +00004855static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004856{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004857 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004858 struct io_sr_msg *sr = &req->sr_msg;
4859 struct msghdr msg;
4860 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004861 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004862 struct iovec iov;
4863 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004864 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004865 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004866
Florent Revestdba4a922020-12-04 12:36:04 +01004867 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004868 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004869 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004870
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004871 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004872 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004873 if (IS_ERR(kbuf))
4874 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004875 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004876 }
4877
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004878 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004879 if (unlikely(ret))
4880 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004881
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004882 msg.msg_name = NULL;
4883 msg.msg_control = NULL;
4884 msg.msg_controllen = 0;
4885 msg.msg_namelen = 0;
4886 msg.msg_iocb = NULL;
4887 msg.msg_flags = 0;
4888
4889 flags = req->sr_msg.msg_flags;
4890 if (flags & MSG_DONTWAIT)
4891 req->flags |= REQ_F_NOWAIT;
4892 else if (force_nonblock)
4893 flags |= MSG_DONTWAIT;
4894
4895 ret = sock_recvmsg(sock, &msg, flags);
4896 if (force_nonblock && ret == -EAGAIN)
4897 return -EAGAIN;
4898 if (ret == -ERESTARTSYS)
4899 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004900out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004901 if (req->flags & REQ_F_BUFFER_SELECTED)
4902 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004903 if (ret < 0)
4904 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004905 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004906 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004907}
4908
Jens Axboe3529d8c2019-12-19 18:24:38 -07004909static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004910{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004911 struct io_accept *accept = &req->accept;
4912
Jens Axboe14587a462020-09-05 11:36:08 -06004913 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004914 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004915 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004916 return -EINVAL;
4917
Jens Axboed55e5f52019-12-11 16:12:15 -07004918 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4919 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004920 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004921 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004922 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004923}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004924
Pavel Begunkov889fca72021-02-10 00:03:09 +00004925static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004926{
4927 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004928 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004929 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004930 int ret;
4931
Jiufei Xuee697dee2020-06-10 13:41:59 +08004932 if (req->file->f_flags & O_NONBLOCK)
4933 req->flags |= REQ_F_NOWAIT;
4934
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004935 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004936 accept->addr_len, accept->flags,
4937 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004938 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004939 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004940 if (ret < 0) {
4941 if (ret == -ERESTARTSYS)
4942 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004943 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004944 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004945 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004946 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004947}
4948
Jens Axboe3529d8c2019-12-19 18:24:38 -07004949static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004950{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004951 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004952 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004953
Jens Axboe14587a462020-09-05 11:36:08 -06004954 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004955 return -EINVAL;
4956 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4957 return -EINVAL;
4958
Jens Axboe3529d8c2019-12-19 18:24:38 -07004959 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4960 conn->addr_len = READ_ONCE(sqe->addr2);
4961
4962 if (!io)
4963 return 0;
4964
4965 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004966 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004967}
4968
Pavel Begunkov889fca72021-02-10 00:03:09 +00004969static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004970{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004971 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004972 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004973 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004974 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004975
Jens Axboee8c2bc12020-08-15 18:44:09 -07004976 if (req->async_data) {
4977 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004978 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004979 ret = move_addr_to_kernel(req->connect.addr,
4980 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004981 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004982 if (ret)
4983 goto out;
4984 io = &__io;
4985 }
4986
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004987 file_flags = force_nonblock ? O_NONBLOCK : 0;
4988
Jens Axboee8c2bc12020-08-15 18:44:09 -07004989 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004990 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004991 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004992 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004993 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004994 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004995 ret = -ENOMEM;
4996 goto out;
4997 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004998 io = req->async_data;
4999 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005000 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005001 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005002 if (ret == -ERESTARTSYS)
5003 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005004out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005005 if (ret < 0)
5006 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005007 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005008 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005009}
YueHaibing469956e2020-03-04 15:53:52 +08005010#else /* !CONFIG_NET */
5011static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5012{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005013 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005014}
5015
Pavel Begunkov889fca72021-02-10 00:03:09 +00005016static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005017{
YueHaibing469956e2020-03-04 15:53:52 +08005018 return -EOPNOTSUPP;
5019}
5020
Pavel Begunkov889fca72021-02-10 00:03:09 +00005021static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005022{
5023 return -EOPNOTSUPP;
5024}
5025
5026static int io_recvmsg_prep(struct io_kiocb *req,
5027 const struct io_uring_sqe *sqe)
5028{
5029 return -EOPNOTSUPP;
5030}
5031
Pavel Begunkov889fca72021-02-10 00:03:09 +00005032static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005033{
5034 return -EOPNOTSUPP;
5035}
5036
Pavel Begunkov889fca72021-02-10 00:03:09 +00005037static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005038{
5039 return -EOPNOTSUPP;
5040}
5041
5042static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5043{
5044 return -EOPNOTSUPP;
5045}
5046
Pavel Begunkov889fca72021-02-10 00:03:09 +00005047static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005048{
5049 return -EOPNOTSUPP;
5050}
5051
5052static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5053{
5054 return -EOPNOTSUPP;
5055}
5056
Pavel Begunkov889fca72021-02-10 00:03:09 +00005057static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005058{
5059 return -EOPNOTSUPP;
5060}
5061#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005062
Jens Axboed7718a92020-02-14 22:23:12 -07005063struct io_poll_table {
5064 struct poll_table_struct pt;
5065 struct io_kiocb *req;
5066 int error;
5067};
5068
Jens Axboed7718a92020-02-14 22:23:12 -07005069static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5070 __poll_t mask, task_work_func_t func)
5071{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005072 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005073
5074 /* for instances that support it check for an event match first: */
5075 if (mask && !(mask & poll->events))
5076 return 0;
5077
5078 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5079
5080 list_del_init(&poll->wait.entry);
5081
Jens Axboed7718a92020-02-14 22:23:12 -07005082 req->result = mask;
5083 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005084 percpu_ref_get(&req->ctx->refs);
5085
Jens Axboed7718a92020-02-14 22:23:12 -07005086 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005087 * If this fails, then the task is exiting. When a task exits, the
5088 * work gets canceled, so just cancel this request as well instead
5089 * of executing it. We can't safely execute it anyway, as we may not
5090 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005091 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005092 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005093 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005094 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005095 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005096 }
Jens Axboed7718a92020-02-14 22:23:12 -07005097 return 1;
5098}
5099
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005100static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5101 __acquires(&req->ctx->completion_lock)
5102{
5103 struct io_ring_ctx *ctx = req->ctx;
5104
5105 if (!req->result && !READ_ONCE(poll->canceled)) {
5106 struct poll_table_struct pt = { ._key = poll->events };
5107
5108 req->result = vfs_poll(req->file, &pt) & poll->events;
5109 }
5110
5111 spin_lock_irq(&ctx->completion_lock);
5112 if (!req->result && !READ_ONCE(poll->canceled)) {
5113 add_wait_queue(poll->head, &poll->wait);
5114 return true;
5115 }
5116
5117 return false;
5118}
5119
Jens Axboed4e7cd32020-08-15 11:44:50 -07005120static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005121{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005122 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005123 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005124 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005125 return req->apoll->double_poll;
5126}
5127
5128static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5129{
5130 if (req->opcode == IORING_OP_POLL_ADD)
5131 return &req->poll;
5132 return &req->apoll->poll;
5133}
5134
5135static void io_poll_remove_double(struct io_kiocb *req)
5136{
5137 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005138
5139 lockdep_assert_held(&req->ctx->completion_lock);
5140
5141 if (poll && poll->head) {
5142 struct wait_queue_head *head = poll->head;
5143
5144 spin_lock(&head->lock);
5145 list_del_init(&poll->wait.entry);
5146 if (poll->wait.private)
5147 refcount_dec(&req->refs);
5148 poll->head = NULL;
5149 spin_unlock(&head->lock);
5150 }
5151}
5152
5153static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5154{
5155 struct io_ring_ctx *ctx = req->ctx;
5156
Jens Axboed4e7cd32020-08-15 11:44:50 -07005157 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005158 req->poll.done = true;
5159 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5160 io_commit_cqring(ctx);
5161}
5162
Jens Axboe18bceab2020-05-15 11:56:54 -06005163static void io_poll_task_func(struct callback_head *cb)
5164{
5165 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005166 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005167 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005168
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005169 if (io_poll_rewait(req, &req->poll)) {
5170 spin_unlock_irq(&ctx->completion_lock);
5171 } else {
5172 hash_del(&req->hash_node);
5173 io_poll_complete(req, req->result, 0);
5174 spin_unlock_irq(&ctx->completion_lock);
5175
5176 nxt = io_put_req_find_next(req);
5177 io_cqring_ev_posted(ctx);
5178 if (nxt)
5179 __io_req_task_submit(nxt);
5180 }
5181
Jens Axboe6d816e02020-08-11 08:04:14 -06005182 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005183}
5184
5185static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5186 int sync, void *key)
5187{
5188 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005189 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005190 __poll_t mask = key_to_poll(key);
5191
5192 /* for instances that support it check for an event match first: */
5193 if (mask && !(mask & poll->events))
5194 return 0;
5195
Jens Axboe8706e042020-09-28 08:38:54 -06005196 list_del_init(&wait->entry);
5197
Jens Axboe807abcb2020-07-17 17:09:27 -06005198 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005199 bool done;
5200
Jens Axboe807abcb2020-07-17 17:09:27 -06005201 spin_lock(&poll->head->lock);
5202 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005203 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005204 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005205 /* make sure double remove sees this as being gone */
5206 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005207 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005208 if (!done) {
5209 /* use wait func handler, so it matches the rq type */
5210 poll->wait.func(&poll->wait, mode, sync, key);
5211 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005212 }
5213 refcount_dec(&req->refs);
5214 return 1;
5215}
5216
5217static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5218 wait_queue_func_t wake_func)
5219{
5220 poll->head = NULL;
5221 poll->done = false;
5222 poll->canceled = false;
5223 poll->events = events;
5224 INIT_LIST_HEAD(&poll->wait.entry);
5225 init_waitqueue_func_entry(&poll->wait, wake_func);
5226}
5227
5228static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005229 struct wait_queue_head *head,
5230 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005231{
5232 struct io_kiocb *req = pt->req;
5233
5234 /*
5235 * If poll->head is already set, it's because the file being polled
5236 * uses multiple waitqueues for poll handling (eg one for read, one
5237 * for write). Setup a separate io_poll_iocb if this happens.
5238 */
5239 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005240 struct io_poll_iocb *poll_one = poll;
5241
Jens Axboe18bceab2020-05-15 11:56:54 -06005242 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005243 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005244 pt->error = -EINVAL;
5245 return;
5246 }
5247 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5248 if (!poll) {
5249 pt->error = -ENOMEM;
5250 return;
5251 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005252 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005253 refcount_inc(&req->refs);
5254 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005255 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005256 }
5257
5258 pt->error = 0;
5259 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005260
5261 if (poll->events & EPOLLEXCLUSIVE)
5262 add_wait_queue_exclusive(head, &poll->wait);
5263 else
5264 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005265}
5266
5267static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5268 struct poll_table_struct *p)
5269{
5270 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005271 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005272
Jens Axboe807abcb2020-07-17 17:09:27 -06005273 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005274}
5275
Jens Axboed7718a92020-02-14 22:23:12 -07005276static void io_async_task_func(struct callback_head *cb)
5277{
5278 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5279 struct async_poll *apoll = req->apoll;
5280 struct io_ring_ctx *ctx = req->ctx;
5281
5282 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5283
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005284 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005285 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005286 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005287 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005288 }
5289
Jens Axboe31067252020-05-17 17:43:31 -06005290 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005291 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005292 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005293
Jens Axboed4e7cd32020-08-15 11:44:50 -07005294 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005295 spin_unlock_irq(&ctx->completion_lock);
5296
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005297 if (!READ_ONCE(apoll->poll.canceled))
5298 __io_req_task_submit(req);
5299 else
5300 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005301
Jens Axboe6d816e02020-08-11 08:04:14 -06005302 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005303 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005304 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005305}
5306
5307static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5308 void *key)
5309{
5310 struct io_kiocb *req = wait->private;
5311 struct io_poll_iocb *poll = &req->apoll->poll;
5312
5313 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5314 key_to_poll(key));
5315
5316 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5317}
5318
5319static void io_poll_req_insert(struct io_kiocb *req)
5320{
5321 struct io_ring_ctx *ctx = req->ctx;
5322 struct hlist_head *list;
5323
5324 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5325 hlist_add_head(&req->hash_node, list);
5326}
5327
5328static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5329 struct io_poll_iocb *poll,
5330 struct io_poll_table *ipt, __poll_t mask,
5331 wait_queue_func_t wake_func)
5332 __acquires(&ctx->completion_lock)
5333{
5334 struct io_ring_ctx *ctx = req->ctx;
5335 bool cancel = false;
5336
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005337 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005338 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005339 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005340 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005341
5342 ipt->pt._key = mask;
5343 ipt->req = req;
5344 ipt->error = -EINVAL;
5345
Jens Axboed7718a92020-02-14 22:23:12 -07005346 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5347
5348 spin_lock_irq(&ctx->completion_lock);
5349 if (likely(poll->head)) {
5350 spin_lock(&poll->head->lock);
5351 if (unlikely(list_empty(&poll->wait.entry))) {
5352 if (ipt->error)
5353 cancel = true;
5354 ipt->error = 0;
5355 mask = 0;
5356 }
5357 if (mask || ipt->error)
5358 list_del_init(&poll->wait.entry);
5359 else if (cancel)
5360 WRITE_ONCE(poll->canceled, true);
5361 else if (!poll->done) /* actually waiting for an event */
5362 io_poll_req_insert(req);
5363 spin_unlock(&poll->head->lock);
5364 }
5365
5366 return mask;
5367}
5368
5369static bool io_arm_poll_handler(struct io_kiocb *req)
5370{
5371 const struct io_op_def *def = &io_op_defs[req->opcode];
5372 struct io_ring_ctx *ctx = req->ctx;
5373 struct async_poll *apoll;
5374 struct io_poll_table ipt;
5375 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005376 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005377
5378 if (!req->file || !file_can_poll(req->file))
5379 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005380 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005381 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005382 if (def->pollin)
5383 rw = READ;
5384 else if (def->pollout)
5385 rw = WRITE;
5386 else
5387 return false;
5388 /* if we can't nonblock try, then no point in arming a poll handler */
5389 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005390 return false;
5391
5392 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5393 if (unlikely(!apoll))
5394 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005395 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005396
5397 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005398 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005399
Nathan Chancellor8755d972020-03-02 16:01:19 -07005400 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005401 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005402 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005403 if (def->pollout)
5404 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005405
5406 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5407 if ((req->opcode == IORING_OP_RECVMSG) &&
5408 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5409 mask &= ~POLLIN;
5410
Jens Axboed7718a92020-02-14 22:23:12 -07005411 mask |= POLLERR | POLLPRI;
5412
5413 ipt.pt._qproc = io_async_queue_proc;
5414
5415 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5416 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005417 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005418 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005419 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005420 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005421 kfree(apoll);
5422 return false;
5423 }
5424 spin_unlock_irq(&ctx->completion_lock);
5425 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5426 apoll->poll.events);
5427 return true;
5428}
5429
5430static bool __io_poll_remove_one(struct io_kiocb *req,
5431 struct io_poll_iocb *poll)
5432{
Jens Axboeb41e9852020-02-17 09:52:41 -07005433 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005434
5435 spin_lock(&poll->head->lock);
5436 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005437 if (!list_empty(&poll->wait.entry)) {
5438 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005439 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005440 }
5441 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005442 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005443 return do_complete;
5444}
5445
5446static bool io_poll_remove_one(struct io_kiocb *req)
5447{
5448 bool do_complete;
5449
Jens Axboed4e7cd32020-08-15 11:44:50 -07005450 io_poll_remove_double(req);
5451
Jens Axboed7718a92020-02-14 22:23:12 -07005452 if (req->opcode == IORING_OP_POLL_ADD) {
5453 do_complete = __io_poll_remove_one(req, &req->poll);
5454 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005455 struct async_poll *apoll = req->apoll;
5456
Jens Axboed7718a92020-02-14 22:23:12 -07005457 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005458 do_complete = __io_poll_remove_one(req, &apoll->poll);
5459 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005460 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005461 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005462 kfree(apoll);
5463 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005464 }
5465
Jens Axboeb41e9852020-02-17 09:52:41 -07005466 if (do_complete) {
5467 io_cqring_fill_event(req, -ECANCELED);
5468 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005469 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005470 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005471 }
5472
5473 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005474}
5475
Jens Axboe76e1b642020-09-26 15:05:03 -06005476/*
5477 * Returns true if we found and killed one or more poll requests
5478 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005479static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5480 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005481{
Jens Axboe78076bb2019-12-04 19:56:40 -07005482 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005483 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005484 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005485
5486 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005487 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5488 struct hlist_head *list;
5489
5490 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005491 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005492 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005493 posted += io_poll_remove_one(req);
5494 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005495 }
5496 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005497
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005498 if (posted)
5499 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005500
5501 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005502}
5503
Jens Axboe47f46762019-11-09 17:43:02 -07005504static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5505{
Jens Axboe78076bb2019-12-04 19:56:40 -07005506 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005507 struct io_kiocb *req;
5508
Jens Axboe78076bb2019-12-04 19:56:40 -07005509 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5510 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005511 if (sqe_addr != req->user_data)
5512 continue;
5513 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005514 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005515 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005516 }
5517
5518 return -ENOENT;
5519}
5520
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521static int io_poll_remove_prep(struct io_kiocb *req,
5522 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005523{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005524 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5525 return -EINVAL;
5526 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5527 sqe->poll_events)
5528 return -EINVAL;
5529
Pavel Begunkov018043b2020-10-27 23:17:18 +00005530 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005531 return 0;
5532}
5533
5534/*
5535 * Find a running poll command that matches one specified in sqe->addr,
5536 * and remove it if found.
5537 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005538static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005539{
5540 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005541 int ret;
5542
Jens Axboe221c5eb2019-01-17 09:41:58 -07005543 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005544 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005545 spin_unlock_irq(&ctx->completion_lock);
5546
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005547 if (ret < 0)
5548 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005549 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005550 return 0;
5551}
5552
Jens Axboe221c5eb2019-01-17 09:41:58 -07005553static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5554 void *key)
5555{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005556 struct io_kiocb *req = wait->private;
5557 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005558
Jens Axboed7718a92020-02-14 22:23:12 -07005559 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005560}
5561
Jens Axboe221c5eb2019-01-17 09:41:58 -07005562static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5563 struct poll_table_struct *p)
5564{
5565 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5566
Jens Axboee8c2bc12020-08-15 18:44:09 -07005567 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005568}
5569
Jens Axboe3529d8c2019-12-19 18:24:38 -07005570static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005571{
5572 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005573 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005574
5575 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5576 return -EINVAL;
5577 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5578 return -EINVAL;
5579
Jiufei Xue5769a352020-06-17 17:53:55 +08005580 events = READ_ONCE(sqe->poll32_events);
5581#ifdef __BIG_ENDIAN
5582 events = swahw32(events);
5583#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005584 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5585 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005586 return 0;
5587}
5588
Pavel Begunkov61e98202021-02-10 00:03:08 +00005589static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005590{
5591 struct io_poll_iocb *poll = &req->poll;
5592 struct io_ring_ctx *ctx = req->ctx;
5593 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005594 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005595
Jens Axboed7718a92020-02-14 22:23:12 -07005596 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005597
Jens Axboed7718a92020-02-14 22:23:12 -07005598 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5599 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005600
Jens Axboe8c838782019-03-12 15:48:16 -06005601 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005602 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005603 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005604 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005605 spin_unlock_irq(&ctx->completion_lock);
5606
Jens Axboe8c838782019-03-12 15:48:16 -06005607 if (mask) {
5608 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005609 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005610 }
Jens Axboe8c838782019-03-12 15:48:16 -06005611 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005612}
5613
Jens Axboe5262f562019-09-17 12:26:57 -06005614static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5615{
Jens Axboead8a48a2019-11-15 08:49:11 -07005616 struct io_timeout_data *data = container_of(timer,
5617 struct io_timeout_data, timer);
5618 struct io_kiocb *req = data->req;
5619 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005620 unsigned long flags;
5621
Jens Axboe5262f562019-09-17 12:26:57 -06005622 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005623 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005624 atomic_set(&req->ctx->cq_timeouts,
5625 atomic_read(&req->ctx->cq_timeouts) + 1);
5626
Jens Axboe78e19bb2019-11-06 15:21:34 -07005627 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005628 io_commit_cqring(ctx);
5629 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5630
5631 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005632 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005633 io_put_req(req);
5634 return HRTIMER_NORESTART;
5635}
5636
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005637static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5638 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005639{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005640 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005641 struct io_kiocb *req;
5642 int ret = -ENOENT;
5643
5644 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5645 if (user_data == req->user_data) {
5646 ret = 0;
5647 break;
5648 }
5649 }
5650
5651 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005652 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005653
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005654 io = req->async_data;
5655 ret = hrtimer_try_to_cancel(&io->timer);
5656 if (ret == -1)
5657 return ERR_PTR(-EALREADY);
5658 list_del_init(&req->timeout.list);
5659 return req;
5660}
5661
5662static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5663{
5664 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5665
5666 if (IS_ERR(req))
5667 return PTR_ERR(req);
5668
5669 req_set_fail_links(req);
5670 io_cqring_fill_event(req, -ECANCELED);
5671 io_put_req_deferred(req, 1);
5672 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005673}
5674
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005675static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5676 struct timespec64 *ts, enum hrtimer_mode mode)
5677{
5678 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5679 struct io_timeout_data *data;
5680
5681 if (IS_ERR(req))
5682 return PTR_ERR(req);
5683
5684 req->timeout.off = 0; /* noseq */
5685 data = req->async_data;
5686 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5687 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5688 data->timer.function = io_timeout_fn;
5689 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5690 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005691}
5692
Jens Axboe3529d8c2019-12-19 18:24:38 -07005693static int io_timeout_remove_prep(struct io_kiocb *req,
5694 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005695{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005696 struct io_timeout_rem *tr = &req->timeout_rem;
5697
Jens Axboeb29472e2019-12-17 18:50:29 -07005698 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5699 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005700 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5701 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005702 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005703 return -EINVAL;
5704
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005705 tr->addr = READ_ONCE(sqe->addr);
5706 tr->flags = READ_ONCE(sqe->timeout_flags);
5707 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5708 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5709 return -EINVAL;
5710 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5711 return -EFAULT;
5712 } else if (tr->flags) {
5713 /* timeout removal doesn't support flags */
5714 return -EINVAL;
5715 }
5716
Jens Axboeb29472e2019-12-17 18:50:29 -07005717 return 0;
5718}
5719
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005720static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5721{
5722 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5723 : HRTIMER_MODE_REL;
5724}
5725
Jens Axboe11365042019-10-16 09:08:32 -06005726/*
5727 * Remove or update an existing timeout command
5728 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005729static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005730{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005731 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005732 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005733 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005734
Jens Axboe11365042019-10-16 09:08:32 -06005735 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005736 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005737 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005738 else
5739 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5740 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005741
Jens Axboe47f46762019-11-09 17:43:02 -07005742 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005743 io_commit_cqring(ctx);
5744 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005745 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005746 if (ret < 0)
5747 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005748 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005749 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005750}
5751
Jens Axboe3529d8c2019-12-19 18:24:38 -07005752static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005753 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005754{
Jens Axboead8a48a2019-11-15 08:49:11 -07005755 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005756 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005757 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005758
Jens Axboead8a48a2019-11-15 08:49:11 -07005759 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005760 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005761 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005762 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005763 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005764 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005765 flags = READ_ONCE(sqe->timeout_flags);
5766 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005767 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005768
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005769 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005770
Jens Axboee8c2bc12020-08-15 18:44:09 -07005771 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005772 return -ENOMEM;
5773
Jens Axboee8c2bc12020-08-15 18:44:09 -07005774 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005775 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005776
5777 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005778 return -EFAULT;
5779
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005780 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005781 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5782 return 0;
5783}
5784
Pavel Begunkov61e98202021-02-10 00:03:08 +00005785static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005786{
Jens Axboead8a48a2019-11-15 08:49:11 -07005787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005788 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005789 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005790 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005791
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005792 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005793
Jens Axboe5262f562019-09-17 12:26:57 -06005794 /*
5795 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005796 * timeout event to be satisfied. If it isn't set, then this is
5797 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005798 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005799 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005800 entry = ctx->timeout_list.prev;
5801 goto add;
5802 }
Jens Axboe5262f562019-09-17 12:26:57 -06005803
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005804 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5805 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005806
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005807 /* Update the last seq here in case io_flush_timeouts() hasn't.
5808 * This is safe because ->completion_lock is held, and submissions
5809 * and completions are never mixed in the same ->completion_lock section.
5810 */
5811 ctx->cq_last_tm_flush = tail;
5812
Jens Axboe5262f562019-09-17 12:26:57 -06005813 /*
5814 * Insertion sort, ensuring the first entry in the list is always
5815 * the one we need first.
5816 */
Jens Axboe5262f562019-09-17 12:26:57 -06005817 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005818 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5819 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005820
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005821 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005822 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005823 /* nxt.seq is behind @tail, otherwise would've been completed */
5824 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005825 break;
5826 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005827add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005828 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005829 data->timer.function = io_timeout_fn;
5830 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005831 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005832 return 0;
5833}
5834
Jens Axboe62755e32019-10-28 21:49:21 -06005835static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005836{
Jens Axboe62755e32019-10-28 21:49:21 -06005837 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005838
Jens Axboe62755e32019-10-28 21:49:21 -06005839 return req->user_data == (unsigned long) data;
5840}
5841
Jens Axboee977d6d2019-11-05 12:39:45 -07005842static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005843{
Jens Axboe62755e32019-10-28 21:49:21 -06005844 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005845 int ret = 0;
5846
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005847 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005848 switch (cancel_ret) {
5849 case IO_WQ_CANCEL_OK:
5850 ret = 0;
5851 break;
5852 case IO_WQ_CANCEL_RUNNING:
5853 ret = -EALREADY;
5854 break;
5855 case IO_WQ_CANCEL_NOTFOUND:
5856 ret = -ENOENT;
5857 break;
5858 }
5859
Jens Axboee977d6d2019-11-05 12:39:45 -07005860 return ret;
5861}
5862
Jens Axboe47f46762019-11-09 17:43:02 -07005863static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5864 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005865 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005866{
5867 unsigned long flags;
5868 int ret;
5869
5870 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5871 if (ret != -ENOENT) {
5872 spin_lock_irqsave(&ctx->completion_lock, flags);
5873 goto done;
5874 }
5875
5876 spin_lock_irqsave(&ctx->completion_lock, flags);
5877 ret = io_timeout_cancel(ctx, sqe_addr);
5878 if (ret != -ENOENT)
5879 goto done;
5880 ret = io_poll_cancel(ctx, sqe_addr);
5881done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005882 if (!ret)
5883 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005884 io_cqring_fill_event(req, ret);
5885 io_commit_cqring(ctx);
5886 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5887 io_cqring_ev_posted(ctx);
5888
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005889 if (ret < 0)
5890 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005891 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005892}
5893
Jens Axboe3529d8c2019-12-19 18:24:38 -07005894static int io_async_cancel_prep(struct io_kiocb *req,
5895 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005896{
Jens Axboefbf23842019-12-17 18:45:56 -07005897 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005898 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005899 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5900 return -EINVAL;
5901 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005902 return -EINVAL;
5903
Jens Axboefbf23842019-12-17 18:45:56 -07005904 req->cancel.addr = READ_ONCE(sqe->addr);
5905 return 0;
5906}
5907
Pavel Begunkov61e98202021-02-10 00:03:08 +00005908static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005909{
5910 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005911
Pavel Begunkov014db002020-03-03 21:33:12 +03005912 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005913 return 0;
5914}
5915
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005916static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005917 const struct io_uring_sqe *sqe)
5918{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005919 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5920 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005921 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5922 return -EINVAL;
5923 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005924 return -EINVAL;
5925
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005926 req->rsrc_update.offset = READ_ONCE(sqe->off);
5927 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5928 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005929 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005930 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005931 return 0;
5932}
5933
Pavel Begunkov889fca72021-02-10 00:03:09 +00005934static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005935{
5936 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005937 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005938 int ret;
5939
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005940 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005941 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005942
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005943 up.offset = req->rsrc_update.offset;
5944 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005945
5946 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005947 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005948 mutex_unlock(&ctx->uring_lock);
5949
5950 if (ret < 0)
5951 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005952 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005953 return 0;
5954}
5955
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005956static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005957{
Jens Axboed625c6e2019-12-17 19:53:05 -07005958 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005959 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005960 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005961 case IORING_OP_READV:
5962 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005963 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005964 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005965 case IORING_OP_WRITEV:
5966 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005967 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005968 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005969 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005970 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005971 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005972 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005973 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005974 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005975 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005976 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005977 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005978 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005979 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005980 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005981 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005982 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005983 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005984 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005985 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005986 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005987 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005988 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005989 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005990 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005991 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005992 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005993 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005994 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005995 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005996 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005997 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005998 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005999 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006000 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006001 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006002 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006003 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006004 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006005 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006006 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006007 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006008 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006009 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006010 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006011 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006012 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006013 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006014 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006015 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006016 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006017 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006018 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006019 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006020 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006021 case IORING_OP_SHUTDOWN:
6022 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006023 case IORING_OP_RENAMEAT:
6024 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006025 case IORING_OP_UNLINKAT:
6026 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006027 }
6028
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006029 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6030 req->opcode);
6031 return-EINVAL;
6032}
6033
Jens Axboedef596e2019-01-09 08:59:42 -07006034static int io_req_defer_prep(struct io_kiocb *req,
6035 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006036{
Jens Axboedef596e2019-01-09 08:59:42 -07006037 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006038 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006039 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006040 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006041 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006042}
6043
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006044static u32 io_get_sequence(struct io_kiocb *req)
6045{
6046 struct io_kiocb *pos;
6047 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006048 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006049
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006050 io_for_each_link(pos, req)
6051 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006052
6053 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6054 return total_submitted - nr_reqs;
6055}
6056
Jens Axboe3529d8c2019-12-19 18:24:38 -07006057static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058{
6059 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006060 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006062 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063
6064 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006065 if (likely(list_empty_careful(&ctx->defer_list) &&
6066 !(req->flags & REQ_F_IO_DRAIN)))
6067 return 0;
6068
6069 seq = io_get_sequence(req);
6070 /* Still a chance to pass the sequence check */
6071 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006072 return 0;
6073
Jens Axboee8c2bc12020-08-15 18:44:09 -07006074 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006075 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006076 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006077 return ret;
6078 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006079 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006080 de = kmalloc(sizeof(*de), GFP_KERNEL);
6081 if (!de)
6082 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006083
6084 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006085 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006086 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006087 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006088 io_queue_async_work(req);
6089 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006090 }
6091
6092 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006093 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006094 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006095 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006096 spin_unlock_irq(&ctx->completion_lock);
6097 return -EIOCBQUEUED;
6098}
Jens Axboeedafcce2019-01-09 09:16:05 -07006099
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006100static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006101{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006102 if (req->flags & REQ_F_BUFFER_SELECTED) {
6103 switch (req->opcode) {
6104 case IORING_OP_READV:
6105 case IORING_OP_READ_FIXED:
6106 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006107 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006108 break;
6109 case IORING_OP_RECVMSG:
6110 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006111 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006112 break;
6113 }
6114 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006115 }
6116
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006117 if (req->flags & REQ_F_NEED_CLEANUP) {
6118 switch (req->opcode) {
6119 case IORING_OP_READV:
6120 case IORING_OP_READ_FIXED:
6121 case IORING_OP_READ:
6122 case IORING_OP_WRITEV:
6123 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006124 case IORING_OP_WRITE: {
6125 struct io_async_rw *io = req->async_data;
6126 if (io->free_iovec)
6127 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006128 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006129 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006130 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006131 case IORING_OP_SENDMSG: {
6132 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006133
6134 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006135 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006136 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006137 case IORING_OP_SPLICE:
6138 case IORING_OP_TEE:
6139 io_put_file(req, req->splice.file_in,
6140 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6141 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006142 case IORING_OP_OPENAT:
6143 case IORING_OP_OPENAT2:
6144 if (req->open.filename)
6145 putname(req->open.filename);
6146 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006147 case IORING_OP_RENAMEAT:
6148 putname(req->rename.oldpath);
6149 putname(req->rename.newpath);
6150 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006151 case IORING_OP_UNLINKAT:
6152 putname(req->unlink.filename);
6153 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006154 }
6155 req->flags &= ~REQ_F_NEED_CLEANUP;
6156 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006157}
6158
Pavel Begunkov889fca72021-02-10 00:03:09 +00006159static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006160{
Jens Axboeedafcce2019-01-09 09:16:05 -07006161 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006162 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006163
Jens Axboed625c6e2019-12-17 19:53:05 -07006164 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006165 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006166 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006167 break;
6168 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006169 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006170 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006171 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006172 break;
6173 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006174 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006175 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006176 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177 break;
6178 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006179 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180 break;
6181 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006182 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183 break;
6184 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006185 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006186 break;
6187 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006188 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006189 break;
6190 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006191 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006192 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006193 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006194 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006195 break;
6196 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006197 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006198 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006199 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006200 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201 break;
6202 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006203 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 break;
6205 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006206 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006207 break;
6208 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006209 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006210 break;
6211 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006212 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006213 break;
6214 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006215 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006216 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006217 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006218 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006219 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006220 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006221 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006222 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006223 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006224 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006225 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006226 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006227 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006228 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006229 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006230 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006231 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006232 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006233 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006234 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006235 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006236 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006237 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006238 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006239 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006240 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006241 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006242 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006243 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006244 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006245 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006246 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006247 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006248 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006249 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006250 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006251 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006253 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006254 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006255 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006256 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006257 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006258 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006259 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006260 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006261 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006262 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006263 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006264 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 default:
6266 ret = -EINVAL;
6267 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006268 }
6269
6270 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006271 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006272
Jens Axboeb5325762020-05-19 21:20:27 -06006273 /* If the op doesn't have a file, we're not polling for it */
6274 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006275 const bool in_async = io_wq_current_is_worker();
6276
Jens Axboe11ba8202020-01-15 21:51:17 -07006277 /* workqueue context doesn't hold uring_lock, grab it now */
6278 if (in_async)
6279 mutex_lock(&ctx->uring_lock);
6280
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006281 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006282
6283 if (in_async)
6284 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006285 }
6286
6287 return 0;
6288}
6289
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006290static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006291{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006292 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006293 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006294 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006296 timeout = io_prep_linked_timeout(req);
6297 if (timeout)
6298 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006299
Jens Axboe4014d942021-01-19 15:53:54 -07006300 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006301 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006302
Jens Axboe561fb042019-10-24 07:25:42 -06006303 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006304 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006305 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006306 /*
6307 * We can get EAGAIN for polled IO even though we're
6308 * forcing a sync submission from here, since we can't
6309 * wait for request slots on the block side.
6310 */
6311 if (ret != -EAGAIN)
6312 break;
6313 cond_resched();
6314 } while (1);
6315 }
Jens Axboe31b51512019-01-18 22:56:34 -07006316
Jens Axboe561fb042019-10-24 07:25:42 -06006317 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006318 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006319
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006320 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6321 lock_ctx = req->ctx;
6322
6323 /*
6324 * io_iopoll_complete() does not hold completion_lock to
6325 * complete polled io, so here for polled io, we can not call
6326 * io_req_complete() directly, otherwise there maybe concurrent
6327 * access to cqring, defer_list, etc, which is not safe. Given
6328 * that io_iopoll_complete() is always called under uring_lock,
6329 * so here for polled io, we also get uring_lock to complete
6330 * it.
6331 */
6332 if (lock_ctx)
6333 mutex_lock(&lock_ctx->uring_lock);
6334
6335 req_set_fail_links(req);
6336 io_req_complete(req, ret);
6337
6338 if (lock_ctx)
6339 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006340 }
Jens Axboe31b51512019-01-18 22:56:34 -07006341}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342
Jens Axboe65e19f52019-10-26 07:20:21 -06006343static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6344 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006345{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006346 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006347
Jens Axboe05f3fb32019-12-09 11:22:50 -07006348 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006349 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006350}
6351
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006352static struct file *io_file_get(struct io_submit_state *state,
6353 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006354{
6355 struct io_ring_ctx *ctx = req->ctx;
6356 struct file *file;
6357
6358 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006359 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006360 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006361 fd = array_index_nospec(fd, ctx->nr_user_files);
6362 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006363 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006364 } else {
6365 trace_io_uring_file_get(ctx, fd);
6366 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006367 }
6368
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006369 if (file && unlikely(file->f_op == &io_uring_fops))
6370 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006371 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006372}
6373
Jens Axboe2665abf2019-11-05 12:40:47 -07006374static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6375{
Jens Axboead8a48a2019-11-15 08:49:11 -07006376 struct io_timeout_data *data = container_of(timer,
6377 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006378 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006379 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006380 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006381
6382 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006383 prev = req->timeout.head;
6384 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006385
6386 /*
6387 * We don't expect the list to be empty, that will only happen if we
6388 * race with the completion of the linked work.
6389 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006390 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006391 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006392 else
6393 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006394 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6395
6396 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006397 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006398 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006399 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006400 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006401 io_req_complete_post(req, -ETIME, 0);
6402 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006403 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006404 return HRTIMER_NORESTART;
6405}
6406
Jens Axboe7271ef32020-08-10 09:55:22 -06006407static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006408{
Jens Axboe76a46e02019-11-10 23:34:16 -07006409 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006410 * If the back reference is NULL, then our linked request finished
6411 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006412 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006413 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006414 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006415
Jens Axboead8a48a2019-11-15 08:49:11 -07006416 data->timer.function = io_link_timeout_fn;
6417 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6418 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006419 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006420}
6421
6422static void io_queue_linked_timeout(struct io_kiocb *req)
6423{
6424 struct io_ring_ctx *ctx = req->ctx;
6425
6426 spin_lock_irq(&ctx->completion_lock);
6427 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006428 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006429
Jens Axboe2665abf2019-11-05 12:40:47 -07006430 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006431 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006432}
6433
Jens Axboead8a48a2019-11-15 08:49:11 -07006434static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006435{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006436 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006437
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006438 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6439 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006440 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006441
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006442 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006443 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006444 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006445 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006446}
6447
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006448static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006449{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006450 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006451 const struct cred *old_creds = NULL;
Pavel Begunkov889fca72021-02-10 00:03:09 +00006452 int ret, issue_flags = IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006453
Pavel Begunkov889fca72021-02-10 00:03:09 +00006454 if (cs)
6455 issue_flags |= IO_URING_F_COMPLETE_DEFER;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006456again:
6457 linked_timeout = io_prep_linked_timeout(req);
6458
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006459 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6460 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006461 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006462 if (old_creds)
6463 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006464 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006465 old_creds = NULL; /* restored original creds */
6466 else
Jens Axboe98447d62020-10-14 10:48:51 -06006467 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006468 }
6469
Pavel Begunkov889fca72021-02-10 00:03:09 +00006470 ret = io_issue_sqe(req, issue_flags);
Jens Axboe491381ce2019-10-17 09:20:46 -06006471
6472 /*
6473 * We async punt it if the file wasn't marked NOWAIT, or if the file
6474 * doesn't support non-blocking read/write attempts
6475 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006476 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006477 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006478 /*
6479 * Queued up for async execution, worker will release
6480 * submit reference when the iocb is actually submitted.
6481 */
6482 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006483 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006484
Pavel Begunkovf063c542020-07-25 14:41:59 +03006485 if (linked_timeout)
6486 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006487 } else if (likely(!ret)) {
6488 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006489 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006490 cs->reqs[cs->nr++] = req;
6491 if (cs->nr == IO_COMPL_BATCH)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006492 io_submit_flush_completions(cs, req->ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006493 req = NULL;
6494 } else {
6495 req = io_put_req_find_next(req);
6496 }
6497
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006498 if (linked_timeout)
6499 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006500
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006501 if (req) {
6502 if (!(req->flags & REQ_F_FORCE_ASYNC))
6503 goto again;
6504 io_queue_async_work(req);
6505 }
6506 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006507 /* un-prep timeout, so it'll be killed as any other linked */
6508 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006509 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006510 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006511 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006512 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006513
Jens Axboe193155c2020-02-22 23:22:19 -07006514 if (old_creds)
6515 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006516}
6517
Jens Axboef13fad72020-06-22 09:34:30 -06006518static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6519 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006520{
6521 int ret;
6522
Jens Axboe3529d8c2019-12-19 18:24:38 -07006523 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006524 if (ret) {
6525 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006526fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006527 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006528 io_put_req(req);
6529 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006530 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006531 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006532 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006533 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006534 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006535 goto fail_req;
6536 }
Jens Axboece35a472019-12-17 08:04:44 -07006537 io_queue_async_work(req);
6538 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006539 if (sqe) {
6540 ret = io_req_prep(req, sqe);
6541 if (unlikely(ret))
6542 goto fail_req;
6543 }
6544 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006545 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006546}
6547
Jens Axboef13fad72020-06-22 09:34:30 -06006548static inline void io_queue_link_head(struct io_kiocb *req,
6549 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006550{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006551 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006552 io_put_req(req);
6553 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006554 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006555 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006556}
6557
Pavel Begunkov863e0562020-10-27 23:25:35 +00006558struct io_submit_link {
6559 struct io_kiocb *head;
6560 struct io_kiocb *last;
6561};
6562
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006563static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006564 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006565{
Jackie Liua197f662019-11-08 08:09:12 -07006566 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006567 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006568
Jens Axboe9e645e112019-05-10 16:07:28 -06006569 /*
6570 * If we already have a head request, queue this one for async
6571 * submittal once the head completes. If we don't have a head but
6572 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6573 * submitted sync once the chain is complete. If none of those
6574 * conditions are true (normal request), then just queue it.
6575 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006576 if (link->head) {
6577 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006578
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006579 /*
6580 * Taking sequential execution of a link, draining both sides
6581 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6582 * requests in the link. So, it drains the head and the
6583 * next after the link request. The last one is done via
6584 * drain_next flag to persist the effect across calls.
6585 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006586 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006587 head->flags |= REQ_F_IO_DRAIN;
6588 ctx->drain_next = 1;
6589 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006590 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006591 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006592 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006593 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006594 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006595 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006596 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006597 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006598 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006599
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006600 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006601 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006602 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006603 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006604 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006605 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006606 if (unlikely(ctx->drain_next)) {
6607 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006608 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006609 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006610 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006611 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006612 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006613 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006614 link->head = req;
6615 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006616 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006617 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006618 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006619 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006620
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006621 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006622}
6623
Jens Axboe9a56a232019-01-09 09:06:50 -07006624/*
6625 * Batched submission is done, ensure local IO is flushed out.
6626 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006627static void io_submit_state_end(struct io_submit_state *state,
6628 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006629{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006630 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006631 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006632 if (state->plug_started)
6633 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006634 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07006635}
6636
6637/*
6638 * Start submission side cache.
6639 */
6640static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006641 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006642{
Jens Axboe27926b62020-10-28 09:33:23 -06006643 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006644 state->ios_left = max_ios;
6645}
6646
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647static void io_commit_sqring(struct io_ring_ctx *ctx)
6648{
Hristo Venev75b28af2019-08-26 17:23:46 +00006649 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006650
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006651 /*
6652 * Ensure any loads from the SQEs are done at this point,
6653 * since once we write the new head, the application could
6654 * write new data to them.
6655 */
6656 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006657}
6658
6659/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006660 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661 * that is mapped by userspace. This means that care needs to be taken to
6662 * ensure that reads are stable, as we cannot rely on userspace always
6663 * being a good citizen. If members of the sqe are validated and then later
6664 * used, it's important that those reads are done through READ_ONCE() to
6665 * prevent a re-load down the line.
6666 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006667static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006668{
Hristo Venev75b28af2019-08-26 17:23:46 +00006669 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006670 unsigned head;
6671
6672 /*
6673 * The cached sq head (or cq tail) serves two purposes:
6674 *
6675 * 1) allows us to batch the cost of updating the user visible
6676 * head updates.
6677 * 2) allows the kernel side to track the head on its own, even
6678 * though the application is the one updating it.
6679 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006680 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006681 if (likely(head < ctx->sq_entries))
6682 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006683
6684 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006685 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006686 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006687 return NULL;
6688}
6689
6690static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6691{
6692 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006693}
6694
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006695/*
6696 * Check SQE restrictions (opcode and flags).
6697 *
6698 * Returns 'true' if SQE is allowed, 'false' otherwise.
6699 */
6700static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6701 struct io_kiocb *req,
6702 unsigned int sqe_flags)
6703{
6704 if (!ctx->restricted)
6705 return true;
6706
6707 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6708 return false;
6709
6710 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6711 ctx->restrictions.sqe_flags_required)
6712 return false;
6713
6714 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6715 ctx->restrictions.sqe_flags_required))
6716 return false;
6717
6718 return true;
6719}
6720
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006721#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6722 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6723 IOSQE_BUFFER_SELECT)
6724
6725static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006726 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006727{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006728 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006729 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006730 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006731
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006732 req->opcode = READ_ONCE(sqe->opcode);
6733 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006734 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006735 req->file = NULL;
6736 req->ctx = ctx;
6737 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006738 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006739 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006740 /* one is dropped after submission, the other at completion */
6741 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006742 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006743 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006744
6745 if (unlikely(req->opcode >= IORING_OP_LAST))
6746 return -EINVAL;
6747
Jens Axboe28cea78a2020-09-14 10:51:17 -06006748 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006749 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006750
6751 sqe_flags = READ_ONCE(sqe->flags);
6752 /* enforce forwards compatibility on users */
6753 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6754 return -EINVAL;
6755
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006756 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6757 return -EACCES;
6758
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006759 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6760 !io_op_defs[req->opcode].buffer_select)
6761 return -EOPNOTSUPP;
6762
6763 id = READ_ONCE(sqe->personality);
6764 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006765 struct io_identity *iod;
6766
Jens Axboe1e6fa522020-10-15 08:46:24 -06006767 iod = idr_find(&ctx->personality_idr, id);
6768 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006769 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006770 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006771
6772 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006773 get_cred(iod->creds);
6774 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006775 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006776 }
6777
6778 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006779 req->flags |= sqe_flags;
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006780 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006781
Jens Axboe27926b62020-10-28 09:33:23 -06006782 /*
6783 * Plug now if we have more than 1 IO left after this, and the target
6784 * is potentially a read/write to block based storage.
6785 */
6786 if (!state->plug_started && state->ios_left > 1 &&
6787 io_op_defs[req->opcode].plug) {
6788 blk_start_plug(&state->plug);
6789 state->plug_started = true;
6790 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006791
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006792 ret = 0;
6793 if (io_op_defs[req->opcode].needs_file) {
6794 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006795
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006796 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006797 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006798 ret = -EBADF;
6799 }
6800
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006801 state->ios_left--;
6802 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006803}
6804
Jens Axboe0f212202020-09-13 13:09:39 -06006805static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006806{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006807 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006808 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006809
Jens Axboec4a2ed72019-11-21 21:01:26 -07006810 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006811 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006812 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006813 return -EBUSY;
6814 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006815
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006816 /* make sure SQ entry isn't read before tail */
6817 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006818
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006819 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6820 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006821
Jens Axboed8a6df12020-10-15 16:24:45 -06006822 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006823 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006824
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006825 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006826 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006827
Jens Axboe6c271ce2019-01-10 11:22:30 -07006828 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006829 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006830 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006831 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006832
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006833 sqe = io_get_sqe(ctx);
6834 if (unlikely(!sqe)) {
6835 io_consume_sqe(ctx);
6836 break;
6837 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006838 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006839 if (unlikely(!req)) {
6840 if (!submitted)
6841 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006842 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006843 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006844 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006845 /* will complete beyond this point, count as submitted */
6846 submitted++;
6847
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006848 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006849 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006850fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006851 io_put_req(req);
6852 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006853 break;
6854 }
6855
Jens Axboe354420f2020-01-08 18:55:15 -07006856 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006857 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006858 err = io_submit_sqe(req, sqe, &link, &ctx->submit_state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006859 if (err)
6860 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006861 }
6862
Pavel Begunkov9466f432020-01-25 22:34:01 +03006863 if (unlikely(submitted != nr)) {
6864 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006865 struct io_uring_task *tctx = current->io_uring;
6866 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006867
Jens Axboed8a6df12020-10-15 16:24:45 -06006868 percpu_ref_put_many(&ctx->refs, unused);
6869 percpu_counter_sub(&tctx->inflight, unused);
6870 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006871 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006872 if (link.head)
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006873 io_queue_link_head(link.head, &ctx->submit_state.comp);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006874 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006875
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006876 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6877 io_commit_sqring(ctx);
6878
Jens Axboe6c271ce2019-01-10 11:22:30 -07006879 return submitted;
6880}
6881
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006882static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6883{
6884 /* Tell userspace we may need a wakeup call */
6885 spin_lock_irq(&ctx->completion_lock);
6886 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6887 spin_unlock_irq(&ctx->completion_lock);
6888}
6889
6890static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6891{
6892 spin_lock_irq(&ctx->completion_lock);
6893 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6894 spin_unlock_irq(&ctx->completion_lock);
6895}
6896
Xiaoguang Wang08369242020-11-03 14:15:59 +08006897static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006898{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006899 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006900 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006901
Jens Axboec8d1ba52020-09-14 11:07:26 -06006902 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006903 /* if we're handling multiple rings, cap submit size for fairness */
6904 if (cap_entries && to_submit > 8)
6905 to_submit = 8;
6906
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006907 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6908 unsigned nr_events = 0;
6909
Xiaoguang Wang08369242020-11-03 14:15:59 +08006910 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006911 if (!list_empty(&ctx->iopoll_list))
6912 io_do_iopoll(ctx, &nr_events, 0);
6913
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006914 if (to_submit && !ctx->sqo_dead &&
6915 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006916 ret = io_submit_sqes(ctx, to_submit);
6917 mutex_unlock(&ctx->uring_lock);
6918 }
Jens Axboe90554202020-09-03 12:12:41 -06006919
6920 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6921 wake_up(&ctx->sqo_sq_wait);
6922
Xiaoguang Wang08369242020-11-03 14:15:59 +08006923 return ret;
6924}
6925
6926static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6927{
6928 struct io_ring_ctx *ctx;
6929 unsigned sq_thread_idle = 0;
6930
6931 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6932 if (sq_thread_idle < ctx->sq_thread_idle)
6933 sq_thread_idle = ctx->sq_thread_idle;
6934 }
6935
6936 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006937}
6938
Jens Axboe69fb2132020-09-14 11:16:23 -06006939static void io_sqd_init_new(struct io_sq_data *sqd)
6940{
6941 struct io_ring_ctx *ctx;
6942
6943 while (!list_empty(&sqd->ctx_new_list)) {
6944 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006945 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6946 complete(&ctx->sq_thread_comp);
6947 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006948
6949 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006950}
6951
Jens Axboe6c271ce2019-01-10 11:22:30 -07006952static int io_sq_thread(void *data)
6953{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006954 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006955 struct files_struct *old_files = current->files;
6956 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006957 const struct cred *old_cred = NULL;
6958 struct io_sq_data *sqd = data;
6959 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006960 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006961 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006962
Jens Axboe28cea78a2020-09-14 10:51:17 -06006963 task_lock(current);
6964 current->files = NULL;
6965 current->nsproxy = NULL;
6966 task_unlock(current);
6967
Jens Axboe69fb2132020-09-14 11:16:23 -06006968 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006969 int ret;
6970 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006971
6972 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006973 * Any changes to the sqd lists are synchronized through the
6974 * kthread parking. This synchronizes the thread vs users,
6975 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006976 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006977 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006978 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006979 /*
6980 * When sq thread is unparked, in case the previous park operation
6981 * comes from io_put_sq_data(), which means that sq thread is going
6982 * to be stopped, so here needs to have a check.
6983 */
6984 if (kthread_should_stop())
6985 break;
6986 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006987
Xiaoguang Wang08369242020-11-03 14:15:59 +08006988 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006989 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006990 timeout = jiffies + sqd->sq_thread_idle;
6991 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006992
Xiaoguang Wang08369242020-11-03 14:15:59 +08006993 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006994 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006995 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6996 if (current->cred != ctx->creds) {
6997 if (old_cred)
6998 revert_creds(old_cred);
6999 old_cred = override_creds(ctx->creds);
7000 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007001 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007002#ifdef CONFIG_AUDIT
7003 current->loginuid = ctx->loginuid;
7004 current->sessionid = ctx->sessionid;
7005#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007006
Xiaoguang Wang08369242020-11-03 14:15:59 +08007007 ret = __io_sq_thread(ctx, cap_entries);
7008 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7009 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007010
Jens Axboe28cea78a2020-09-14 10:51:17 -06007011 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007012 }
7013
Xiaoguang Wang08369242020-11-03 14:15:59 +08007014 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007015 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007016 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007017 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007018 if (sqt_spin)
7019 timeout = jiffies + sqd->sq_thread_idle;
7020 continue;
7021 }
7022
Xiaoguang Wang08369242020-11-03 14:15:59 +08007023 needs_sched = true;
7024 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7025 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7026 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7027 !list_empty_careful(&ctx->iopoll_list)) {
7028 needs_sched = false;
7029 break;
7030 }
7031 if (io_sqring_entries(ctx)) {
7032 needs_sched = false;
7033 break;
7034 }
7035 }
7036
Hao Xu8b28fdf2021-01-31 22:39:04 +08007037 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007038 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7039 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007040
Jens Axboe69fb2132020-09-14 11:16:23 -06007041 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007042 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7043 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007044 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007045
7046 finish_wait(&sqd->wait, &wait);
7047 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007048 }
7049
Jens Axboe4c6e2772020-07-01 11:29:10 -06007050 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007051 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007052
Dennis Zhou91d8f512020-09-16 13:41:05 -07007053 if (cur_css)
7054 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007055 if (old_cred)
7056 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007057
Jens Axboe28cea78a2020-09-14 10:51:17 -06007058 task_lock(current);
7059 current->files = old_files;
7060 current->nsproxy = old_nsproxy;
7061 task_unlock(current);
7062
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007063 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007064
Jens Axboe6c271ce2019-01-10 11:22:30 -07007065 return 0;
7066}
7067
Jens Axboebda52162019-09-24 13:47:15 -06007068struct io_wait_queue {
7069 struct wait_queue_entry wq;
7070 struct io_ring_ctx *ctx;
7071 unsigned to_wait;
7072 unsigned nr_timeouts;
7073};
7074
Pavel Begunkov6c503152021-01-04 20:36:36 +00007075static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007076{
7077 struct io_ring_ctx *ctx = iowq->ctx;
7078
7079 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007080 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007081 * started waiting. For timeouts, we always want to return to userspace,
7082 * regardless of event count.
7083 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007084 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007085 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7086}
7087
7088static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7089 int wake_flags, void *key)
7090{
7091 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7092 wq);
7093
Pavel Begunkov6c503152021-01-04 20:36:36 +00007094 /*
7095 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7096 * the task, and the next invocation will do it.
7097 */
7098 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7099 return autoremove_wake_function(curr, mode, wake_flags, key);
7100 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007101}
7102
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007103static int io_run_task_work_sig(void)
7104{
7105 if (io_run_task_work())
7106 return 1;
7107 if (!signal_pending(current))
7108 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007109 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7110 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007111 return -EINTR;
7112}
7113
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007114/* when returns >0, the caller should retry */
7115static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7116 struct io_wait_queue *iowq,
7117 signed long *timeout)
7118{
7119 int ret;
7120
7121 /* make sure we run task_work before checking for signals */
7122 ret = io_run_task_work_sig();
7123 if (ret || io_should_wake(iowq))
7124 return ret;
7125 /* let the caller flush overflows, retry */
7126 if (test_bit(0, &ctx->cq_check_overflow))
7127 return 1;
7128
7129 *timeout = schedule_timeout(*timeout);
7130 return !*timeout ? -ETIME : 1;
7131}
7132
Jens Axboe2b188cc2019-01-07 10:46:33 -07007133/*
7134 * Wait until events become available, if we don't already have some. The
7135 * application must reap them itself, as they reside on the shared cq ring.
7136 */
7137static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007138 const sigset_t __user *sig, size_t sigsz,
7139 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007140{
Jens Axboebda52162019-09-24 13:47:15 -06007141 struct io_wait_queue iowq = {
7142 .wq = {
7143 .private = current,
7144 .func = io_wake_function,
7145 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7146 },
7147 .ctx = ctx,
7148 .to_wait = min_events,
7149 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007150 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007151 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7152 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007153
Jens Axboeb41e9852020-02-17 09:52:41 -07007154 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007155 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7156 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007157 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007158 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007159 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007160 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007161
7162 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007163#ifdef CONFIG_COMPAT
7164 if (in_compat_syscall())
7165 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007166 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007167 else
7168#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007169 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007170
Jens Axboe2b188cc2019-01-07 10:46:33 -07007171 if (ret)
7172 return ret;
7173 }
7174
Hao Xuc73ebb62020-11-03 10:54:37 +08007175 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007176 struct timespec64 ts;
7177
Hao Xuc73ebb62020-11-03 10:54:37 +08007178 if (get_timespec64(&ts, uts))
7179 return -EFAULT;
7180 timeout = timespec64_to_jiffies(&ts);
7181 }
7182
Jens Axboebda52162019-09-24 13:47:15 -06007183 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007184 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007185 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007186 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007187 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7188 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007189 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7190 finish_wait(&ctx->wait, &iowq.wq);
7191 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007192
Jens Axboeb7db41c2020-07-04 08:55:50 -06007193 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007194
Hristo Venev75b28af2019-08-26 17:23:46 +00007195 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007196}
7197
Jens Axboe6b063142019-01-10 22:13:58 -07007198static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7199{
7200#if defined(CONFIG_UNIX)
7201 if (ctx->ring_sock) {
7202 struct sock *sock = ctx->ring_sock->sk;
7203 struct sk_buff *skb;
7204
7205 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7206 kfree_skb(skb);
7207 }
7208#else
7209 int i;
7210
Jens Axboe65e19f52019-10-26 07:20:21 -06007211 for (i = 0; i < ctx->nr_user_files; i++) {
7212 struct file *file;
7213
7214 file = io_file_from_index(ctx, i);
7215 if (file)
7216 fput(file);
7217 }
Jens Axboe6b063142019-01-10 22:13:58 -07007218#endif
7219}
7220
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007221static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007222{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007223 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007224
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007225 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007226 complete(&data->done);
7227}
7228
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007229static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7230{
7231 spin_lock_bh(&ctx->rsrc_ref_lock);
7232}
7233
7234static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7235{
7236 spin_unlock_bh(&ctx->rsrc_ref_lock);
7237}
7238
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007239static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7240 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007241 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007242{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007243 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007244 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007245 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007246 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007247 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007248}
7249
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007250static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7251 struct io_ring_ctx *ctx,
7252 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007253{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007254 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007255 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007256
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007257 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007258 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007259 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007260 if (ref_node)
7261 percpu_ref_kill(&ref_node->refs);
7262
7263 percpu_ref_kill(&data->refs);
7264
7265 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007266 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007267 do {
7268 ret = wait_for_completion_interruptible(&data->done);
7269 if (!ret)
7270 break;
7271 ret = io_run_task_work_sig();
7272 if (ret < 0) {
7273 percpu_ref_resurrect(&data->refs);
7274 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007275 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007276 return ret;
7277 }
7278 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007279
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007280 destroy_fixed_rsrc_ref_node(backup_node);
7281 return 0;
7282}
7283
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007284static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7285{
7286 struct fixed_rsrc_data *data;
7287
7288 data = kzalloc(sizeof(*data), GFP_KERNEL);
7289 if (!data)
7290 return NULL;
7291
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007292 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007293 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7294 kfree(data);
7295 return NULL;
7296 }
7297 data->ctx = ctx;
7298 init_completion(&data->done);
7299 return data;
7300}
7301
7302static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7303{
7304 percpu_ref_exit(&data->refs);
7305 kfree(data->table);
7306 kfree(data);
7307}
7308
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007309static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7310{
7311 struct fixed_rsrc_data *data = ctx->file_data;
7312 struct fixed_rsrc_ref_node *backup_node;
7313 unsigned nr_tables, i;
7314 int ret;
7315
7316 if (!data)
7317 return -ENXIO;
7318 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7319 if (!backup_node)
7320 return -ENOMEM;
7321 init_fixed_file_ref_node(ctx, backup_node);
7322
7323 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7324 if (ret)
7325 return ret;
7326
Jens Axboe6b063142019-01-10 22:13:58 -07007327 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007328 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7329 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007330 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007331 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007332 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007333 ctx->nr_user_files = 0;
7334 return 0;
7335}
7336
Jens Axboe534ca6d2020-09-02 13:52:19 -06007337static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007338{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007339 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007340 /*
7341 * The park is a bit of a work-around, without it we get
7342 * warning spews on shutdown with SQPOLL set and affinity
7343 * set to a single CPU.
7344 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007345 if (sqd->thread) {
7346 kthread_park(sqd->thread);
7347 kthread_stop(sqd->thread);
7348 }
7349
7350 kfree(sqd);
7351 }
7352}
7353
Jens Axboeaa061652020-09-02 14:50:27 -06007354static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7355{
7356 struct io_ring_ctx *ctx_attach;
7357 struct io_sq_data *sqd;
7358 struct fd f;
7359
7360 f = fdget(p->wq_fd);
7361 if (!f.file)
7362 return ERR_PTR(-ENXIO);
7363 if (f.file->f_op != &io_uring_fops) {
7364 fdput(f);
7365 return ERR_PTR(-EINVAL);
7366 }
7367
7368 ctx_attach = f.file->private_data;
7369 sqd = ctx_attach->sq_data;
7370 if (!sqd) {
7371 fdput(f);
7372 return ERR_PTR(-EINVAL);
7373 }
7374
7375 refcount_inc(&sqd->refs);
7376 fdput(f);
7377 return sqd;
7378}
7379
Jens Axboe534ca6d2020-09-02 13:52:19 -06007380static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7381{
7382 struct io_sq_data *sqd;
7383
Jens Axboeaa061652020-09-02 14:50:27 -06007384 if (p->flags & IORING_SETUP_ATTACH_WQ)
7385 return io_attach_sq_data(p);
7386
Jens Axboe534ca6d2020-09-02 13:52:19 -06007387 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7388 if (!sqd)
7389 return ERR_PTR(-ENOMEM);
7390
7391 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007392 INIT_LIST_HEAD(&sqd->ctx_list);
7393 INIT_LIST_HEAD(&sqd->ctx_new_list);
7394 mutex_init(&sqd->ctx_lock);
7395 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007396 init_waitqueue_head(&sqd->wait);
7397 return sqd;
7398}
7399
Jens Axboe69fb2132020-09-14 11:16:23 -06007400static void io_sq_thread_unpark(struct io_sq_data *sqd)
7401 __releases(&sqd->lock)
7402{
7403 if (!sqd->thread)
7404 return;
7405 kthread_unpark(sqd->thread);
7406 mutex_unlock(&sqd->lock);
7407}
7408
7409static void io_sq_thread_park(struct io_sq_data *sqd)
7410 __acquires(&sqd->lock)
7411{
7412 if (!sqd->thread)
7413 return;
7414 mutex_lock(&sqd->lock);
7415 kthread_park(sqd->thread);
7416}
7417
Jens Axboe534ca6d2020-09-02 13:52:19 -06007418static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7419{
7420 struct io_sq_data *sqd = ctx->sq_data;
7421
7422 if (sqd) {
7423 if (sqd->thread) {
7424 /*
7425 * We may arrive here from the error branch in
7426 * io_sq_offload_create() where the kthread is created
7427 * without being waked up, thus wake it up now to make
7428 * sure the wait will complete.
7429 */
7430 wake_up_process(sqd->thread);
7431 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007432
7433 io_sq_thread_park(sqd);
7434 }
7435
7436 mutex_lock(&sqd->ctx_lock);
7437 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007438 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007439 mutex_unlock(&sqd->ctx_lock);
7440
Xiaoguang Wang08369242020-11-03 14:15:59 +08007441 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007442 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007443
7444 io_put_sq_data(sqd);
7445 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007446 }
7447}
7448
Jens Axboe6b063142019-01-10 22:13:58 -07007449static void io_finish_async(struct io_ring_ctx *ctx)
7450{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007451 io_sq_thread_stop(ctx);
7452
Jens Axboe561fb042019-10-24 07:25:42 -06007453 if (ctx->io_wq) {
7454 io_wq_destroy(ctx->io_wq);
7455 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007456 }
7457}
7458
7459#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007460/*
7461 * Ensure the UNIX gc is aware of our file set, so we are certain that
7462 * the io_uring can be safely unregistered on process exit, even if we have
7463 * loops in the file referencing.
7464 */
7465static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7466{
7467 struct sock *sk = ctx->ring_sock->sk;
7468 struct scm_fp_list *fpl;
7469 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007470 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007471
Jens Axboe6b063142019-01-10 22:13:58 -07007472 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7473 if (!fpl)
7474 return -ENOMEM;
7475
7476 skb = alloc_skb(0, GFP_KERNEL);
7477 if (!skb) {
7478 kfree(fpl);
7479 return -ENOMEM;
7480 }
7481
7482 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007483
Jens Axboe08a45172019-10-03 08:11:03 -06007484 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007485 fpl->user = get_uid(ctx->user);
7486 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007487 struct file *file = io_file_from_index(ctx, i + offset);
7488
7489 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007490 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007491 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007492 unix_inflight(fpl->user, fpl->fp[nr_files]);
7493 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007494 }
7495
Jens Axboe08a45172019-10-03 08:11:03 -06007496 if (nr_files) {
7497 fpl->max = SCM_MAX_FD;
7498 fpl->count = nr_files;
7499 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007500 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007501 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7502 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007503
Jens Axboe08a45172019-10-03 08:11:03 -06007504 for (i = 0; i < nr_files; i++)
7505 fput(fpl->fp[i]);
7506 } else {
7507 kfree_skb(skb);
7508 kfree(fpl);
7509 }
Jens Axboe6b063142019-01-10 22:13:58 -07007510
7511 return 0;
7512}
7513
7514/*
7515 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7516 * causes regular reference counting to break down. We rely on the UNIX
7517 * garbage collection to take care of this problem for us.
7518 */
7519static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7520{
7521 unsigned left, total;
7522 int ret = 0;
7523
7524 total = 0;
7525 left = ctx->nr_user_files;
7526 while (left) {
7527 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007528
7529 ret = __io_sqe_files_scm(ctx, this_files, total);
7530 if (ret)
7531 break;
7532 left -= this_files;
7533 total += this_files;
7534 }
7535
7536 if (!ret)
7537 return 0;
7538
7539 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007540 struct file *file = io_file_from_index(ctx, total);
7541
7542 if (file)
7543 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007544 total++;
7545 }
7546
7547 return ret;
7548}
7549#else
7550static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7551{
7552 return 0;
7553}
7554#endif
7555
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007556static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007557 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007558{
7559 int i;
7560
7561 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007562 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007563 unsigned this_files;
7564
7565 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7566 table->files = kcalloc(this_files, sizeof(struct file *),
7567 GFP_KERNEL);
7568 if (!table->files)
7569 break;
7570 nr_files -= this_files;
7571 }
7572
7573 if (i == nr_tables)
7574 return 0;
7575
7576 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007577 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007578 kfree(table->files);
7579 }
7580 return 1;
7581}
7582
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007583static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007584{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007585 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007586#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007587 struct sock *sock = ctx->ring_sock->sk;
7588 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7589 struct sk_buff *skb;
7590 int i;
7591
7592 __skb_queue_head_init(&list);
7593
7594 /*
7595 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7596 * remove this entry and rearrange the file array.
7597 */
7598 skb = skb_dequeue(head);
7599 while (skb) {
7600 struct scm_fp_list *fp;
7601
7602 fp = UNIXCB(skb).fp;
7603 for (i = 0; i < fp->count; i++) {
7604 int left;
7605
7606 if (fp->fp[i] != file)
7607 continue;
7608
7609 unix_notinflight(fp->user, fp->fp[i]);
7610 left = fp->count - 1 - i;
7611 if (left) {
7612 memmove(&fp->fp[i], &fp->fp[i + 1],
7613 left * sizeof(struct file *));
7614 }
7615 fp->count--;
7616 if (!fp->count) {
7617 kfree_skb(skb);
7618 skb = NULL;
7619 } else {
7620 __skb_queue_tail(&list, skb);
7621 }
7622 fput(file);
7623 file = NULL;
7624 break;
7625 }
7626
7627 if (!file)
7628 break;
7629
7630 __skb_queue_tail(&list, skb);
7631
7632 skb = skb_dequeue(head);
7633 }
7634
7635 if (skb_peek(&list)) {
7636 spin_lock_irq(&head->lock);
7637 while ((skb = __skb_dequeue(&list)) != NULL)
7638 __skb_queue_tail(head, skb);
7639 spin_unlock_irq(&head->lock);
7640 }
7641#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007642 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007643#endif
7644}
7645
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007646static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007647{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007648 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7649 struct io_ring_ctx *ctx = rsrc_data->ctx;
7650 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007651
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007652 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7653 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007654 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007655 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007656 }
7657
Xiaoguang Wang05589552020-03-31 14:05:18 +08007658 percpu_ref_exit(&ref_node->refs);
7659 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007660 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007661}
7662
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007663static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007664{
7665 struct io_ring_ctx *ctx;
7666 struct llist_node *node;
7667
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007668 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7669 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007670
7671 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007672 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007673 struct llist_node *next = node->next;
7674
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007675 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7676 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007677 node = next;
7678 }
7679}
7680
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007681static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7682 unsigned i)
7683{
7684 struct fixed_rsrc_table *table;
7685
7686 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7687 return &table->files[i & IORING_FILE_TABLE_MASK];
7688}
7689
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007690static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007691{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007692 struct fixed_rsrc_ref_node *ref_node;
7693 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007694 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007695 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007696 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007697
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007698 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7699 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007700 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007701
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007702 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007703 ref_node->done = true;
7704
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007705 while (!list_empty(&ctx->rsrc_ref_list)) {
7706 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007707 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007708 /* recycle ref nodes in order */
7709 if (!ref_node->done)
7710 break;
7711 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007712 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007713 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007714 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007715
7716 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007717 delay = 0;
7718
Jens Axboe4a38aed22020-05-14 17:21:15 -06007719 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007720 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007721 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007722 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007723}
7724
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007725static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007726 struct io_ring_ctx *ctx)
7727{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007728 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007729
7730 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7731 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007732 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007733
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007734 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007735 0, GFP_KERNEL)) {
7736 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007737 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007738 }
7739 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007740 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007741 ref_node->done = false;
7742 return ref_node;
7743}
7744
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007745static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7746 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007747{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007748 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007749 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007750}
7751
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007752static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007753{
7754 percpu_ref_exit(&ref_node->refs);
7755 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007756}
7757
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007758
Jens Axboe05f3fb32019-12-09 11:22:50 -07007759static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7760 unsigned nr_args)
7761{
7762 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007763 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007764 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007765 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007766 struct fixed_rsrc_ref_node *ref_node;
7767 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007768
7769 if (ctx->file_data)
7770 return -EBUSY;
7771 if (!nr_args)
7772 return -EINVAL;
7773 if (nr_args > IORING_MAX_FIXED_FILES)
7774 return -EMFILE;
7775
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007776 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007777 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007778 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007779 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007780
7781 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007782 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007783 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007784 if (!file_data->table)
7785 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007786
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007787 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007788 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007789
7790 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007791 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7792 ret = -EFAULT;
7793 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007794 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007795 /* allow sparse sets */
7796 if (fd == -1)
7797 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798
Jens Axboe05f3fb32019-12-09 11:22:50 -07007799 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007800 ret = -EBADF;
7801 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007802 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007803
7804 /*
7805 * Don't allow io_uring instances to be registered. If UNIX
7806 * isn't enabled, then this causes a reference cycle and this
7807 * instance can never get freed. If UNIX is enabled we'll
7808 * handle it just fine, but there's still no point in allowing
7809 * a ring fd as it doesn't support regular read/write anyway.
7810 */
7811 if (file->f_op == &io_uring_fops) {
7812 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007813 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007814 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007815 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007816 }
7817
Jens Axboe05f3fb32019-12-09 11:22:50 -07007818 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007819 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007820 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007821 return ret;
7822 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007823
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007824 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007825 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007826 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007827 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007828 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007829 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007830
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007831 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007832 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007833out_fput:
7834 for (i = 0; i < ctx->nr_user_files; i++) {
7835 file = io_file_from_index(ctx, i);
7836 if (file)
7837 fput(file);
7838 }
7839 for (i = 0; i < nr_tables; i++)
7840 kfree(file_data->table[i].files);
7841 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007842out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007843 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007844 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007845 return ret;
7846}
7847
Jens Axboec3a31e62019-10-03 13:59:56 -06007848static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7849 int index)
7850{
7851#if defined(CONFIG_UNIX)
7852 struct sock *sock = ctx->ring_sock->sk;
7853 struct sk_buff_head *head = &sock->sk_receive_queue;
7854 struct sk_buff *skb;
7855
7856 /*
7857 * See if we can merge this file into an existing skb SCM_RIGHTS
7858 * file set. If there's no room, fall back to allocating a new skb
7859 * and filling it in.
7860 */
7861 spin_lock_irq(&head->lock);
7862 skb = skb_peek(head);
7863 if (skb) {
7864 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7865
7866 if (fpl->count < SCM_MAX_FD) {
7867 __skb_unlink(skb, head);
7868 spin_unlock_irq(&head->lock);
7869 fpl->fp[fpl->count] = get_file(file);
7870 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7871 fpl->count++;
7872 spin_lock_irq(&head->lock);
7873 __skb_queue_head(head, skb);
7874 } else {
7875 skb = NULL;
7876 }
7877 }
7878 spin_unlock_irq(&head->lock);
7879
7880 if (skb) {
7881 fput(file);
7882 return 0;
7883 }
7884
7885 return __io_sqe_files_scm(ctx, 1, index);
7886#else
7887 return 0;
7888#endif
7889}
7890
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007891static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007892{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007893 struct io_rsrc_put *prsrc;
7894 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007895
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007896 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7897 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007898 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007899
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007900 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007901 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007902
Hillf Dantona5318d32020-03-23 17:47:15 +08007903 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007904}
7905
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007906static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7907 struct file *file)
7908{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007909 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007910}
7911
Jens Axboe05f3fb32019-12-09 11:22:50 -07007912static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007913 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007914 unsigned nr_args)
7915{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007916 struct fixed_rsrc_data *data = ctx->file_data;
7917 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007918 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007919 __s32 __user *fds;
7920 int fd, i, err;
7921 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007922 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007923
Jens Axboe05f3fb32019-12-09 11:22:50 -07007924 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007925 return -EOVERFLOW;
7926 if (done > ctx->nr_user_files)
7927 return -EINVAL;
7928
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007929 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007930 if (!ref_node)
7931 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007932 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007933
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007934 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007935 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007936 err = 0;
7937 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7938 err = -EFAULT;
7939 break;
7940 }
noah4e0377a2021-01-26 15:23:28 -05007941 if (fd == IORING_REGISTER_FILES_SKIP)
7942 continue;
7943
Pavel Begunkov67973b92021-01-26 13:51:09 +00007944 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007945 file_slot = io_fixed_file_slot(ctx->file_data, i);
7946
7947 if (*file_slot) {
7948 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007949 if (err)
7950 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007951 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007952 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007953 }
7954 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007955 file = fget(fd);
7956 if (!file) {
7957 err = -EBADF;
7958 break;
7959 }
7960 /*
7961 * Don't allow io_uring instances to be registered. If
7962 * UNIX isn't enabled, then this causes a reference
7963 * cycle and this instance can never get freed. If UNIX
7964 * is enabled we'll handle it just fine, but there's
7965 * still no point in allowing a ring fd as it doesn't
7966 * support regular read/write anyway.
7967 */
7968 if (file->f_op == &io_uring_fops) {
7969 fput(file);
7970 err = -EBADF;
7971 break;
7972 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007973 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007974 if (err) {
7975 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007976 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007977 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007978 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007979 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007980 }
7981
Xiaoguang Wang05589552020-03-31 14:05:18 +08007982 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007983 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007984 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007985 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007986 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007987
7988 return done ? done : err;
7989}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007990
Jens Axboe05f3fb32019-12-09 11:22:50 -07007991static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7992 unsigned nr_args)
7993{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007994 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007995
7996 if (!ctx->file_data)
7997 return -ENXIO;
7998 if (!nr_args)
7999 return -EINVAL;
8000 if (copy_from_user(&up, arg, sizeof(up)))
8001 return -EFAULT;
8002 if (up.resv)
8003 return -EINVAL;
8004
8005 return __io_sqe_files_update(ctx, &up, nr_args);
8006}
Jens Axboec3a31e62019-10-03 13:59:56 -06008007
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008008static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008009{
8010 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8011
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008012 req = io_put_req_find_next(req);
8013 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008014}
8015
Pavel Begunkov24369c22020-01-28 03:15:48 +03008016static int io_init_wq_offload(struct io_ring_ctx *ctx,
8017 struct io_uring_params *p)
8018{
8019 struct io_wq_data data;
8020 struct fd f;
8021 struct io_ring_ctx *ctx_attach;
8022 unsigned int concurrency;
8023 int ret = 0;
8024
8025 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008026 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008027 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008028
8029 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8030 /* Do QD, or 4 * CPUS, whatever is smallest */
8031 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8032
8033 ctx->io_wq = io_wq_create(concurrency, &data);
8034 if (IS_ERR(ctx->io_wq)) {
8035 ret = PTR_ERR(ctx->io_wq);
8036 ctx->io_wq = NULL;
8037 }
8038 return ret;
8039 }
8040
8041 f = fdget(p->wq_fd);
8042 if (!f.file)
8043 return -EBADF;
8044
8045 if (f.file->f_op != &io_uring_fops) {
8046 ret = -EINVAL;
8047 goto out_fput;
8048 }
8049
8050 ctx_attach = f.file->private_data;
8051 /* @io_wq is protected by holding the fd */
8052 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8053 ret = -EINVAL;
8054 goto out_fput;
8055 }
8056
8057 ctx->io_wq = ctx_attach->io_wq;
8058out_fput:
8059 fdput(f);
8060 return ret;
8061}
8062
Jens Axboe0f212202020-09-13 13:09:39 -06008063static int io_uring_alloc_task_context(struct task_struct *task)
8064{
8065 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008066 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008067
8068 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8069 if (unlikely(!tctx))
8070 return -ENOMEM;
8071
Jens Axboed8a6df12020-10-15 16:24:45 -06008072 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8073 if (unlikely(ret)) {
8074 kfree(tctx);
8075 return ret;
8076 }
8077
Jens Axboe0f212202020-09-13 13:09:39 -06008078 xa_init(&tctx->xa);
8079 init_waitqueue_head(&tctx->wait);
8080 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008081 atomic_set(&tctx->in_idle, 0);
8082 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008083 io_init_identity(&tctx->__identity);
8084 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008085 task->io_uring = tctx;
8086 return 0;
8087}
8088
8089void __io_uring_free(struct task_struct *tsk)
8090{
8091 struct io_uring_task *tctx = tsk->io_uring;
8092
8093 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008094 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8095 if (tctx->identity != &tctx->__identity)
8096 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008097 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008098 kfree(tctx);
8099 tsk->io_uring = NULL;
8100}
8101
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008102static int io_sq_offload_create(struct io_ring_ctx *ctx,
8103 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008104{
8105 int ret;
8106
Jens Axboe6c271ce2019-01-10 11:22:30 -07008107 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008108 struct io_sq_data *sqd;
8109
Jens Axboe3ec482d2019-04-08 10:51:01 -06008110 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008111 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008112 goto err;
8113
Jens Axboe534ca6d2020-09-02 13:52:19 -06008114 sqd = io_get_sq_data(p);
8115 if (IS_ERR(sqd)) {
8116 ret = PTR_ERR(sqd);
8117 goto err;
8118 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008119
Jens Axboe534ca6d2020-09-02 13:52:19 -06008120 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008121 io_sq_thread_park(sqd);
8122 mutex_lock(&sqd->ctx_lock);
8123 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8124 mutex_unlock(&sqd->ctx_lock);
8125 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008126
Jens Axboe917257d2019-04-13 09:28:55 -06008127 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8128 if (!ctx->sq_thread_idle)
8129 ctx->sq_thread_idle = HZ;
8130
Jens Axboeaa061652020-09-02 14:50:27 -06008131 if (sqd->thread)
8132 goto done;
8133
Jens Axboe6c271ce2019-01-10 11:22:30 -07008134 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008135 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008136
Jens Axboe917257d2019-04-13 09:28:55 -06008137 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008138 if (cpu >= nr_cpu_ids)
8139 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008140 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008141 goto err;
8142
Jens Axboe69fb2132020-09-14 11:16:23 -06008143 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008144 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008145 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008146 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008147 "io_uring-sq");
8148 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008149 if (IS_ERR(sqd->thread)) {
8150 ret = PTR_ERR(sqd->thread);
8151 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008152 goto err;
8153 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008154 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008155 if (ret)
8156 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008157 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8158 /* Can't have SQ_AFF without SQPOLL */
8159 ret = -EINVAL;
8160 goto err;
8161 }
8162
Jens Axboeaa061652020-09-02 14:50:27 -06008163done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008164 ret = io_init_wq_offload(ctx, p);
8165 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008166 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008167
8168 return 0;
8169err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008170 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008171 return ret;
8172}
8173
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008174static void io_sq_offload_start(struct io_ring_ctx *ctx)
8175{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008176 struct io_sq_data *sqd = ctx->sq_data;
8177
8178 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8179 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008180}
8181
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008182static inline void __io_unaccount_mem(struct user_struct *user,
8183 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008184{
8185 atomic_long_sub(nr_pages, &user->locked_vm);
8186}
8187
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008188static inline int __io_account_mem(struct user_struct *user,
8189 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008190{
8191 unsigned long page_limit, cur_pages, new_pages;
8192
8193 /* Don't allow more pages than we can safely lock */
8194 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8195
8196 do {
8197 cur_pages = atomic_long_read(&user->locked_vm);
8198 new_pages = cur_pages + nr_pages;
8199 if (new_pages > page_limit)
8200 return -ENOMEM;
8201 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8202 new_pages) != cur_pages);
8203
8204 return 0;
8205}
8206
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008207static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8208 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008209{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008210 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008211 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008212
Jens Axboe2aede0e2020-09-14 10:45:53 -06008213 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008214 if (acct == ACCT_LOCKED) {
8215 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008216 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008217 mmap_write_unlock(ctx->mm_account);
8218 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008219 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008220 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008221 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008222}
8223
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008224static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8225 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008226{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008227 int ret;
8228
8229 if (ctx->limit_mem) {
8230 ret = __io_account_mem(ctx->user, nr_pages);
8231 if (ret)
8232 return ret;
8233 }
8234
Jens Axboe2aede0e2020-09-14 10:45:53 -06008235 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008236 if (acct == ACCT_LOCKED) {
8237 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008238 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008239 mmap_write_unlock(ctx->mm_account);
8240 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008241 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008242 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008243 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008244
8245 return 0;
8246}
8247
Jens Axboe2b188cc2019-01-07 10:46:33 -07008248static void io_mem_free(void *ptr)
8249{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008250 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008251
Mark Rutland52e04ef2019-04-30 17:30:21 +01008252 if (!ptr)
8253 return;
8254
8255 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256 if (put_page_testzero(page))
8257 free_compound_page(page);
8258}
8259
8260static void *io_mem_alloc(size_t size)
8261{
8262 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8263 __GFP_NORETRY;
8264
8265 return (void *) __get_free_pages(gfp_flags, get_order(size));
8266}
8267
Hristo Venev75b28af2019-08-26 17:23:46 +00008268static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8269 size_t *sq_offset)
8270{
8271 struct io_rings *rings;
8272 size_t off, sq_array_size;
8273
8274 off = struct_size(rings, cqes, cq_entries);
8275 if (off == SIZE_MAX)
8276 return SIZE_MAX;
8277
8278#ifdef CONFIG_SMP
8279 off = ALIGN(off, SMP_CACHE_BYTES);
8280 if (off == 0)
8281 return SIZE_MAX;
8282#endif
8283
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008284 if (sq_offset)
8285 *sq_offset = off;
8286
Hristo Venev75b28af2019-08-26 17:23:46 +00008287 sq_array_size = array_size(sizeof(u32), sq_entries);
8288 if (sq_array_size == SIZE_MAX)
8289 return SIZE_MAX;
8290
8291 if (check_add_overflow(off, sq_array_size, &off))
8292 return SIZE_MAX;
8293
Hristo Venev75b28af2019-08-26 17:23:46 +00008294 return off;
8295}
8296
Jens Axboe2b188cc2019-01-07 10:46:33 -07008297static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8298{
Hristo Venev75b28af2019-08-26 17:23:46 +00008299 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008300
Hristo Venev75b28af2019-08-26 17:23:46 +00008301 pages = (size_t)1 << get_order(
8302 rings_size(sq_entries, cq_entries, NULL));
8303 pages += (size_t)1 << get_order(
8304 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008305
Hristo Venev75b28af2019-08-26 17:23:46 +00008306 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008307}
8308
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008309static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008310{
8311 int i, j;
8312
8313 if (!ctx->user_bufs)
8314 return -ENXIO;
8315
8316 for (i = 0; i < ctx->nr_user_bufs; i++) {
8317 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8318
8319 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008320 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008321
Jens Axboede293932020-09-17 16:19:16 -06008322 if (imu->acct_pages)
8323 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008324 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008325 imu->nr_bvecs = 0;
8326 }
8327
8328 kfree(ctx->user_bufs);
8329 ctx->user_bufs = NULL;
8330 ctx->nr_user_bufs = 0;
8331 return 0;
8332}
8333
8334static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8335 void __user *arg, unsigned index)
8336{
8337 struct iovec __user *src;
8338
8339#ifdef CONFIG_COMPAT
8340 if (ctx->compat) {
8341 struct compat_iovec __user *ciovs;
8342 struct compat_iovec ciov;
8343
8344 ciovs = (struct compat_iovec __user *) arg;
8345 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8346 return -EFAULT;
8347
Jens Axboed55e5f52019-12-11 16:12:15 -07008348 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008349 dst->iov_len = ciov.iov_len;
8350 return 0;
8351 }
8352#endif
8353 src = (struct iovec __user *) arg;
8354 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8355 return -EFAULT;
8356 return 0;
8357}
8358
Jens Axboede293932020-09-17 16:19:16 -06008359/*
8360 * Not super efficient, but this is just a registration time. And we do cache
8361 * the last compound head, so generally we'll only do a full search if we don't
8362 * match that one.
8363 *
8364 * We check if the given compound head page has already been accounted, to
8365 * avoid double accounting it. This allows us to account the full size of the
8366 * page, not just the constituent pages of a huge page.
8367 */
8368static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8369 int nr_pages, struct page *hpage)
8370{
8371 int i, j;
8372
8373 /* check current page array */
8374 for (i = 0; i < nr_pages; i++) {
8375 if (!PageCompound(pages[i]))
8376 continue;
8377 if (compound_head(pages[i]) == hpage)
8378 return true;
8379 }
8380
8381 /* check previously registered pages */
8382 for (i = 0; i < ctx->nr_user_bufs; i++) {
8383 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8384
8385 for (j = 0; j < imu->nr_bvecs; j++) {
8386 if (!PageCompound(imu->bvec[j].bv_page))
8387 continue;
8388 if (compound_head(imu->bvec[j].bv_page) == hpage)
8389 return true;
8390 }
8391 }
8392
8393 return false;
8394}
8395
8396static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8397 int nr_pages, struct io_mapped_ubuf *imu,
8398 struct page **last_hpage)
8399{
8400 int i, ret;
8401
8402 for (i = 0; i < nr_pages; i++) {
8403 if (!PageCompound(pages[i])) {
8404 imu->acct_pages++;
8405 } else {
8406 struct page *hpage;
8407
8408 hpage = compound_head(pages[i]);
8409 if (hpage == *last_hpage)
8410 continue;
8411 *last_hpage = hpage;
8412 if (headpage_already_acct(ctx, pages, i, hpage))
8413 continue;
8414 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8415 }
8416 }
8417
8418 if (!imu->acct_pages)
8419 return 0;
8420
8421 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8422 if (ret)
8423 imu->acct_pages = 0;
8424 return ret;
8425}
8426
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008427static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8428 struct io_mapped_ubuf *imu,
8429 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008430{
8431 struct vm_area_struct **vmas = NULL;
8432 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008433 unsigned long off, start, end, ubuf;
8434 size_t size;
8435 int ret, pret, nr_pages, i;
8436
8437 ubuf = (unsigned long) iov->iov_base;
8438 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8439 start = ubuf >> PAGE_SHIFT;
8440 nr_pages = end - start;
8441
8442 ret = -ENOMEM;
8443
8444 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8445 if (!pages)
8446 goto done;
8447
8448 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8449 GFP_KERNEL);
8450 if (!vmas)
8451 goto done;
8452
8453 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8454 GFP_KERNEL);
8455 if (!imu->bvec)
8456 goto done;
8457
8458 ret = 0;
8459 mmap_read_lock(current->mm);
8460 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8461 pages, vmas);
8462 if (pret == nr_pages) {
8463 /* don't support file backed memory */
8464 for (i = 0; i < nr_pages; i++) {
8465 struct vm_area_struct *vma = vmas[i];
8466
8467 if (vma->vm_file &&
8468 !is_file_hugepages(vma->vm_file)) {
8469 ret = -EOPNOTSUPP;
8470 break;
8471 }
8472 }
8473 } else {
8474 ret = pret < 0 ? pret : -EFAULT;
8475 }
8476 mmap_read_unlock(current->mm);
8477 if (ret) {
8478 /*
8479 * if we did partial map, or found file backed vmas,
8480 * release any pages we did get
8481 */
8482 if (pret > 0)
8483 unpin_user_pages(pages, pret);
8484 kvfree(imu->bvec);
8485 goto done;
8486 }
8487
8488 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8489 if (ret) {
8490 unpin_user_pages(pages, pret);
8491 kvfree(imu->bvec);
8492 goto done;
8493 }
8494
8495 off = ubuf & ~PAGE_MASK;
8496 size = iov->iov_len;
8497 for (i = 0; i < nr_pages; i++) {
8498 size_t vec_len;
8499
8500 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8501 imu->bvec[i].bv_page = pages[i];
8502 imu->bvec[i].bv_len = vec_len;
8503 imu->bvec[i].bv_offset = off;
8504 off = 0;
8505 size -= vec_len;
8506 }
8507 /* store original address for later verification */
8508 imu->ubuf = ubuf;
8509 imu->len = iov->iov_len;
8510 imu->nr_bvecs = nr_pages;
8511 ret = 0;
8512done:
8513 kvfree(pages);
8514 kvfree(vmas);
8515 return ret;
8516}
8517
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008518static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008519{
Jens Axboeedafcce2019-01-09 09:16:05 -07008520 if (ctx->user_bufs)
8521 return -EBUSY;
8522 if (!nr_args || nr_args > UIO_MAXIOV)
8523 return -EINVAL;
8524
8525 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8526 GFP_KERNEL);
8527 if (!ctx->user_bufs)
8528 return -ENOMEM;
8529
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008530 return 0;
8531}
8532
8533static int io_buffer_validate(struct iovec *iov)
8534{
8535 /*
8536 * Don't impose further limits on the size and buffer
8537 * constraints here, we'll -EINVAL later when IO is
8538 * submitted if they are wrong.
8539 */
8540 if (!iov->iov_base || !iov->iov_len)
8541 return -EFAULT;
8542
8543 /* arbitrary limit, but we need something */
8544 if (iov->iov_len > SZ_1G)
8545 return -EFAULT;
8546
8547 return 0;
8548}
8549
8550static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8551 unsigned int nr_args)
8552{
8553 int i, ret;
8554 struct iovec iov;
8555 struct page *last_hpage = NULL;
8556
8557 ret = io_buffers_map_alloc(ctx, nr_args);
8558 if (ret)
8559 return ret;
8560
Jens Axboeedafcce2019-01-09 09:16:05 -07008561 for (i = 0; i < nr_args; i++) {
8562 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008563
8564 ret = io_copy_iov(ctx, &iov, arg, i);
8565 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008566 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008567
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008568 ret = io_buffer_validate(&iov);
8569 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008570 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008571
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008572 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8573 if (ret)
8574 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008575
8576 ctx->nr_user_bufs++;
8577 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008578
8579 if (ret)
8580 io_sqe_buffers_unregister(ctx);
8581
Jens Axboeedafcce2019-01-09 09:16:05 -07008582 return ret;
8583}
8584
Jens Axboe9b402842019-04-11 11:45:41 -06008585static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8586{
8587 __s32 __user *fds = arg;
8588 int fd;
8589
8590 if (ctx->cq_ev_fd)
8591 return -EBUSY;
8592
8593 if (copy_from_user(&fd, fds, sizeof(*fds)))
8594 return -EFAULT;
8595
8596 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8597 if (IS_ERR(ctx->cq_ev_fd)) {
8598 int ret = PTR_ERR(ctx->cq_ev_fd);
8599 ctx->cq_ev_fd = NULL;
8600 return ret;
8601 }
8602
8603 return 0;
8604}
8605
8606static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8607{
8608 if (ctx->cq_ev_fd) {
8609 eventfd_ctx_put(ctx->cq_ev_fd);
8610 ctx->cq_ev_fd = NULL;
8611 return 0;
8612 }
8613
8614 return -ENXIO;
8615}
8616
Jens Axboe5a2e7452020-02-23 16:23:11 -07008617static int __io_destroy_buffers(int id, void *p, void *data)
8618{
8619 struct io_ring_ctx *ctx = data;
8620 struct io_buffer *buf = p;
8621
Jens Axboe067524e2020-03-02 16:32:28 -07008622 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008623 return 0;
8624}
8625
8626static void io_destroy_buffers(struct io_ring_ctx *ctx)
8627{
8628 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8629 idr_destroy(&ctx->io_buffer_idr);
8630}
8631
Jens Axboe2b188cc2019-01-07 10:46:33 -07008632static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8633{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008634 struct io_submit_state *submit_state = &ctx->submit_state;
8635
Jens Axboe6b063142019-01-10 22:13:58 -07008636 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008637 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008638
8639 if (ctx->sqo_task) {
8640 put_task_struct(ctx->sqo_task);
8641 ctx->sqo_task = NULL;
8642 mmdrop(ctx->mm_account);
8643 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008644 }
Jens Axboedef596e2019-01-09 08:59:42 -07008645
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008646 if (submit_state->free_reqs)
8647 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8648 submit_state->reqs);
8649
Dennis Zhou91d8f512020-09-16 13:41:05 -07008650#ifdef CONFIG_BLK_CGROUP
8651 if (ctx->sqo_blkcg_css)
8652 css_put(ctx->sqo_blkcg_css);
8653#endif
8654
Jens Axboe6b063142019-01-10 22:13:58 -07008655 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008656 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008657 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008658 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008659
Jens Axboe2b188cc2019-01-07 10:46:33 -07008660#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008661 if (ctx->ring_sock) {
8662 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008663 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008664 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008665#endif
8666
Hristo Venev75b28af2019-08-26 17:23:46 +00008667 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008668 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008669
8670 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008671 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008672 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008673 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008674 kfree(ctx);
8675}
8676
8677static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8678{
8679 struct io_ring_ctx *ctx = file->private_data;
8680 __poll_t mask = 0;
8681
8682 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008683 /*
8684 * synchronizes with barrier from wq_has_sleeper call in
8685 * io_commit_cqring
8686 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008687 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008688 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008689 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008690 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8691 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008692 mask |= EPOLLIN | EPOLLRDNORM;
8693
8694 return mask;
8695}
8696
8697static int io_uring_fasync(int fd, struct file *file, int on)
8698{
8699 struct io_ring_ctx *ctx = file->private_data;
8700
8701 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8702}
8703
Yejune Deng0bead8c2020-12-24 11:02:20 +08008704static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008705{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008706 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008707
Jens Axboe1e6fa522020-10-15 08:46:24 -06008708 iod = idr_remove(&ctx->personality_idr, id);
8709 if (iod) {
8710 put_cred(iod->creds);
8711 if (refcount_dec_and_test(&iod->count))
8712 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008713 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008714 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008715
8716 return -EINVAL;
8717}
8718
8719static int io_remove_personalities(int id, void *p, void *data)
8720{
8721 struct io_ring_ctx *ctx = data;
8722
8723 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008724 return 0;
8725}
8726
Jens Axboe85faa7b2020-04-09 18:14:00 -06008727static void io_ring_exit_work(struct work_struct *work)
8728{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008729 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8730 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008731
Jens Axboe56952e92020-06-17 15:00:04 -06008732 /*
8733 * If we're doing polled IO and end up having requests being
8734 * submitted async (out-of-line), then completions can come in while
8735 * we're waiting for refs to drop. We need to reap these manually,
8736 * as nobody else will be looking for them.
8737 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008738 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008739 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008740 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008741 io_ring_ctx_free(ctx);
8742}
8743
Jens Axboe00c18642020-12-20 10:45:02 -07008744static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8745{
8746 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8747
8748 return req->ctx == data;
8749}
8750
Jens Axboe2b188cc2019-01-07 10:46:33 -07008751static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8752{
8753 mutex_lock(&ctx->uring_lock);
8754 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008755
8756 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8757 ctx->sqo_dead = 1;
8758
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008759 /* if force is set, the ring is going away. always drop after that */
8760 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008761 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008762 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008763 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008764 mutex_unlock(&ctx->uring_lock);
8765
Pavel Begunkov6b819282020-11-06 13:00:25 +00008766 io_kill_timeouts(ctx, NULL, NULL);
8767 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008768
8769 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008770 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008771
Jens Axboe15dff282019-11-13 09:09:23 -07008772 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008773 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008774
8775 /*
8776 * Do this upfront, so we won't have a grace period where the ring
8777 * is closed but resources aren't reaped yet. This can cause
8778 * spurious failure in setting up a new ring.
8779 */
Jens Axboe760618f2020-07-24 12:53:31 -06008780 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8781 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008782
Jens Axboe85faa7b2020-04-09 18:14:00 -06008783 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008784 /*
8785 * Use system_unbound_wq to avoid spawning tons of event kworkers
8786 * if we're exiting a ton of rings at the same time. It just adds
8787 * noise and overhead, there's no discernable change in runtime
8788 * over using system_wq.
8789 */
8790 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008791}
8792
8793static int io_uring_release(struct inode *inode, struct file *file)
8794{
8795 struct io_ring_ctx *ctx = file->private_data;
8796
8797 file->private_data = NULL;
8798 io_ring_ctx_wait_and_kill(ctx);
8799 return 0;
8800}
8801
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008802struct io_task_cancel {
8803 struct task_struct *task;
8804 struct files_struct *files;
8805};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008806
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008807static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008808{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008809 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008810 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008811 bool ret;
8812
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008813 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008814 unsigned long flags;
8815 struct io_ring_ctx *ctx = req->ctx;
8816
8817 /* protect against races with linked timeouts */
8818 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008819 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008820 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8821 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008822 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008823 }
8824 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008825}
8826
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008827static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008828 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008829 struct files_struct *files)
8830{
8831 struct io_defer_entry *de = NULL;
8832 LIST_HEAD(list);
8833
8834 spin_lock_irq(&ctx->completion_lock);
8835 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008836 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008837 list_cut_position(&list, &ctx->defer_list, &de->list);
8838 break;
8839 }
8840 }
8841 spin_unlock_irq(&ctx->completion_lock);
8842
8843 while (!list_empty(&list)) {
8844 de = list_first_entry(&list, struct io_defer_entry, list);
8845 list_del_init(&de->list);
8846 req_set_fail_links(de->req);
8847 io_put_req(de->req);
8848 io_req_complete(de->req, -ECANCELED);
8849 kfree(de);
8850 }
8851}
8852
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008853static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8854 struct task_struct *task,
8855 struct files_struct *files)
8856{
8857 struct io_task_cancel cancel = { .task = task, .files = files, };
8858
8859 while (1) {
8860 enum io_wq_cancel cret;
8861 bool ret = false;
8862
8863 if (ctx->io_wq) {
8864 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8865 &cancel, true);
8866 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8867 }
8868
8869 /* SQPOLL thread does its own polling */
8870 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8871 while (!list_empty_careful(&ctx->iopoll_list)) {
8872 io_iopoll_try_reap_events(ctx);
8873 ret = true;
8874 }
8875 }
8876
8877 ret |= io_poll_remove_all(ctx, task, files);
8878 ret |= io_kill_timeouts(ctx, task, files);
8879 ret |= io_run_task_work();
8880 io_cqring_overflow_flush(ctx, true, task, files);
8881 if (!ret)
8882 break;
8883 cond_resched();
8884 }
8885}
8886
Pavel Begunkovca70f002021-01-26 15:28:27 +00008887static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8888 struct task_struct *task,
8889 struct files_struct *files)
8890{
8891 struct io_kiocb *req;
8892 int cnt = 0;
8893
8894 spin_lock_irq(&ctx->inflight_lock);
8895 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8896 cnt += io_match_task(req, task, files);
8897 spin_unlock_irq(&ctx->inflight_lock);
8898 return cnt;
8899}
8900
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008901static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008902 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008903 struct files_struct *files)
8904{
Jens Axboefcb323c2019-10-24 12:39:47 -06008905 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008906 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008907 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008908
Pavel Begunkovca70f002021-01-26 15:28:27 +00008909 inflight = io_uring_count_inflight(ctx, task, files);
8910 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008911 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008912
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008913 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008914 prepare_to_wait(&task->io_uring->wait, &wait,
8915 TASK_UNINTERRUPTIBLE);
8916 if (inflight == io_uring_count_inflight(ctx, task, files))
8917 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008918 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008919 }
8920}
8921
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008922static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8923{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008924 mutex_lock(&ctx->uring_lock);
8925 ctx->sqo_dead = 1;
8926 mutex_unlock(&ctx->uring_lock);
8927
8928 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008929 if (ctx->rings)
8930 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008931}
8932
Jens Axboe0f212202020-09-13 13:09:39 -06008933/*
8934 * We need to iteratively cancel requests, in case a request has dependent
8935 * hard links. These persist even for failure of cancelations, hence keep
8936 * looping until none are found.
8937 */
8938static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8939 struct files_struct *files)
8940{
8941 struct task_struct *task = current;
8942
Jens Axboefdaf0832020-10-30 09:37:30 -06008943 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008944 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008945 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008946 atomic_inc(&task->io_uring->in_idle);
8947 io_sq_thread_park(ctx->sq_data);
8948 }
Jens Axboe0f212202020-09-13 13:09:39 -06008949
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008950 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008951
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008952 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008953 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008954 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008955
8956 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8957 atomic_dec(&task->io_uring->in_idle);
8958 /*
8959 * If the files that are going away are the ones in the thread
8960 * identity, clear them out.
8961 */
8962 if (task->io_uring->identity->files == files)
8963 task->io_uring->identity->files = NULL;
8964 io_sq_thread_unpark(ctx->sq_data);
8965 }
Jens Axboe0f212202020-09-13 13:09:39 -06008966}
8967
8968/*
8969 * Note that this task has used io_uring. We use it for cancelation purposes.
8970 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008971static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008972{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008973 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008974 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008975
8976 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008977 ret = io_uring_alloc_task_context(current);
8978 if (unlikely(ret))
8979 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008980 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008981 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008982 if (tctx->last != file) {
8983 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008984
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008985 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008986 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008987 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8988 file, GFP_KERNEL));
8989 if (ret) {
8990 fput(file);
8991 return ret;
8992 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008993
8994 /* one and only SQPOLL file note, held by sqo_task */
8995 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8996 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008997 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008998 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008999 }
9000
Jens Axboefdaf0832020-10-30 09:37:30 -06009001 /*
9002 * This is race safe in that the task itself is doing this, hence it
9003 * cannot be going through the exit/cancel paths at the same time.
9004 * This cannot be modified while exit/cancel is running.
9005 */
9006 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9007 tctx->sqpoll = true;
9008
Jens Axboe0f212202020-09-13 13:09:39 -06009009 return 0;
9010}
9011
9012/*
9013 * Remove this io_uring_file -> task mapping.
9014 */
9015static void io_uring_del_task_file(struct file *file)
9016{
9017 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009018
9019 if (tctx->last == file)
9020 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009021 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009022 if (file)
9023 fput(file);
9024}
9025
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009026static void io_uring_remove_task_files(struct io_uring_task *tctx)
9027{
9028 struct file *file;
9029 unsigned long index;
9030
9031 xa_for_each(&tctx->xa, index, file)
9032 io_uring_del_task_file(file);
9033}
9034
Jens Axboe0f212202020-09-13 13:09:39 -06009035void __io_uring_files_cancel(struct files_struct *files)
9036{
9037 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009038 struct file *file;
9039 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009040
9041 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009042 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009043 xa_for_each(&tctx->xa, index, file)
9044 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009045 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009046
9047 if (files)
9048 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009049}
9050
9051static s64 tctx_inflight(struct io_uring_task *tctx)
9052{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009053 return percpu_counter_sum(&tctx->inflight);
9054}
9055
9056static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9057{
9058 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009059 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009060 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009061
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009062 if (!ctx->sq_data)
9063 return;
9064 tctx = ctx->sq_data->thread->io_uring;
9065 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009066
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009067 atomic_inc(&tctx->in_idle);
9068 do {
9069 /* read completions before cancelations */
9070 inflight = tctx_inflight(tctx);
9071 if (!inflight)
9072 break;
9073 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009074
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009075 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9076 /*
9077 * If we've seen completions, retry without waiting. This
9078 * avoids a race where a completion comes in before we did
9079 * prepare_to_wait().
9080 */
9081 if (inflight == tctx_inflight(tctx))
9082 schedule();
9083 finish_wait(&tctx->wait, &wait);
9084 } while (1);
9085 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009086}
9087
Jens Axboe0f212202020-09-13 13:09:39 -06009088/*
9089 * Find any io_uring fd that this task has registered or done IO on, and cancel
9090 * requests.
9091 */
9092void __io_uring_task_cancel(void)
9093{
9094 struct io_uring_task *tctx = current->io_uring;
9095 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009096 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009097
9098 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009099 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009100
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009101 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009102 if (tctx->sqpoll) {
9103 struct file *file;
9104 unsigned long index;
9105
9106 xa_for_each(&tctx->xa, index, file)
9107 io_uring_cancel_sqpoll(file->private_data);
9108 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009109
Jens Axboed8a6df12020-10-15 16:24:45 -06009110 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009111 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009112 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009113 if (!inflight)
9114 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009115 __io_uring_files_cancel(NULL);
9116
9117 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9118
9119 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009120 * If we've seen completions, retry without waiting. This
9121 * avoids a race where a completion comes in before we did
9122 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009123 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009124 if (inflight == tctx_inflight(tctx))
9125 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009126 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009127 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009128
Jens Axboefdaf0832020-10-30 09:37:30 -06009129 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009130
9131 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009132}
9133
Jens Axboefcb323c2019-10-24 12:39:47 -06009134static int io_uring_flush(struct file *file, void *data)
9135{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009136 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009137 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009138
Jens Axboe84965ff2021-01-23 15:51:11 -07009139 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9140 io_uring_cancel_task_requests(ctx, NULL);
9141
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009142 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009143 return 0;
9144
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009145 /* we should have cancelled and erased it before PF_EXITING */
9146 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9147 xa_load(&tctx->xa, (unsigned long)file));
9148
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009149 /*
9150 * fput() is pending, will be 2 if the only other ref is our potential
9151 * task file note. If the task is exiting, drop regardless of count.
9152 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009153 if (atomic_long_read(&file->f_count) != 2)
9154 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009155
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009156 if (ctx->flags & IORING_SETUP_SQPOLL) {
9157 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009158 WARN_ON_ONCE(ctx->sqo_task != current &&
9159 xa_load(&tctx->xa, (unsigned long)file));
9160 /* sqo_dead check is for when this happens after cancellation */
9161 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009162 !xa_load(&tctx->xa, (unsigned long)file));
9163
9164 io_disable_sqo_submit(ctx);
9165 }
9166
9167 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9168 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009169 return 0;
9170}
9171
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009172static void *io_uring_validate_mmap_request(struct file *file,
9173 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009174{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009175 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009176 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009177 struct page *page;
9178 void *ptr;
9179
9180 switch (offset) {
9181 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009182 case IORING_OFF_CQ_RING:
9183 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009184 break;
9185 case IORING_OFF_SQES:
9186 ptr = ctx->sq_sqes;
9187 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009188 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009189 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009190 }
9191
9192 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009193 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009194 return ERR_PTR(-EINVAL);
9195
9196 return ptr;
9197}
9198
9199#ifdef CONFIG_MMU
9200
9201static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9202{
9203 size_t sz = vma->vm_end - vma->vm_start;
9204 unsigned long pfn;
9205 void *ptr;
9206
9207 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9208 if (IS_ERR(ptr))
9209 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009210
9211 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9212 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9213}
9214
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009215#else /* !CONFIG_MMU */
9216
9217static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9218{
9219 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9220}
9221
9222static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9223{
9224 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9225}
9226
9227static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9228 unsigned long addr, unsigned long len,
9229 unsigned long pgoff, unsigned long flags)
9230{
9231 void *ptr;
9232
9233 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9234 if (IS_ERR(ptr))
9235 return PTR_ERR(ptr);
9236
9237 return (unsigned long) ptr;
9238}
9239
9240#endif /* !CONFIG_MMU */
9241
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009242static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009243{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009244 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009245 DEFINE_WAIT(wait);
9246
9247 do {
9248 if (!io_sqring_full(ctx))
9249 break;
9250
9251 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9252
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009253 if (unlikely(ctx->sqo_dead)) {
9254 ret = -EOWNERDEAD;
9255 goto out;
9256 }
9257
Jens Axboe90554202020-09-03 12:12:41 -06009258 if (!io_sqring_full(ctx))
9259 break;
9260
9261 schedule();
9262 } while (!signal_pending(current));
9263
9264 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009265out:
9266 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009267}
9268
Hao Xuc73ebb62020-11-03 10:54:37 +08009269static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9270 struct __kernel_timespec __user **ts,
9271 const sigset_t __user **sig)
9272{
9273 struct io_uring_getevents_arg arg;
9274
9275 /*
9276 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9277 * is just a pointer to the sigset_t.
9278 */
9279 if (!(flags & IORING_ENTER_EXT_ARG)) {
9280 *sig = (const sigset_t __user *) argp;
9281 *ts = NULL;
9282 return 0;
9283 }
9284
9285 /*
9286 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9287 * timespec and sigset_t pointers if good.
9288 */
9289 if (*argsz != sizeof(arg))
9290 return -EINVAL;
9291 if (copy_from_user(&arg, argp, sizeof(arg)))
9292 return -EFAULT;
9293 *sig = u64_to_user_ptr(arg.sigmask);
9294 *argsz = arg.sigmask_sz;
9295 *ts = u64_to_user_ptr(arg.ts);
9296 return 0;
9297}
9298
Jens Axboe2b188cc2019-01-07 10:46:33 -07009299SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009300 u32, min_complete, u32, flags, const void __user *, argp,
9301 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009302{
9303 struct io_ring_ctx *ctx;
9304 long ret = -EBADF;
9305 int submitted = 0;
9306 struct fd f;
9307
Jens Axboe4c6e2772020-07-01 11:29:10 -06009308 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009309
Jens Axboe90554202020-09-03 12:12:41 -06009310 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009311 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009312 return -EINVAL;
9313
9314 f = fdget(fd);
9315 if (!f.file)
9316 return -EBADF;
9317
9318 ret = -EOPNOTSUPP;
9319 if (f.file->f_op != &io_uring_fops)
9320 goto out_fput;
9321
9322 ret = -ENXIO;
9323 ctx = f.file->private_data;
9324 if (!percpu_ref_tryget(&ctx->refs))
9325 goto out_fput;
9326
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009327 ret = -EBADFD;
9328 if (ctx->flags & IORING_SETUP_R_DISABLED)
9329 goto out;
9330
Jens Axboe6c271ce2019-01-10 11:22:30 -07009331 /*
9332 * For SQ polling, the thread will do all submissions and completions.
9333 * Just return the requested submit count, and wake the thread if
9334 * we were asked to.
9335 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009336 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009337 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009338 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009339
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009340 ret = -EOWNERDEAD;
9341 if (unlikely(ctx->sqo_dead))
9342 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009343 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009344 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009345 if (flags & IORING_ENTER_SQ_WAIT) {
9346 ret = io_sqpoll_wait_sq(ctx);
9347 if (ret)
9348 goto out;
9349 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009350 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009351 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009352 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009353 if (unlikely(ret))
9354 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009355 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009356 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009357 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009358
9359 if (submitted != to_submit)
9360 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009361 }
9362 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009363 const sigset_t __user *sig;
9364 struct __kernel_timespec __user *ts;
9365
9366 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9367 if (unlikely(ret))
9368 goto out;
9369
Jens Axboe2b188cc2019-01-07 10:46:33 -07009370 min_complete = min(min_complete, ctx->cq_entries);
9371
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009372 /*
9373 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9374 * space applications don't need to do io completion events
9375 * polling again, they can rely on io_sq_thread to do polling
9376 * work, which can reduce cpu usage and uring_lock contention.
9377 */
9378 if (ctx->flags & IORING_SETUP_IOPOLL &&
9379 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009380 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009381 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009382 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009383 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009384 }
9385
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009386out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009387 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009388out_fput:
9389 fdput(f);
9390 return submitted ? submitted : ret;
9391}
9392
Tobias Klauserbebdb652020-02-26 18:38:32 +01009393#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009394static int io_uring_show_cred(int id, void *p, void *data)
9395{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009396 struct io_identity *iod = p;
9397 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009398 struct seq_file *m = data;
9399 struct user_namespace *uns = seq_user_ns(m);
9400 struct group_info *gi;
9401 kernel_cap_t cap;
9402 unsigned __capi;
9403 int g;
9404
9405 seq_printf(m, "%5d\n", id);
9406 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9407 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9408 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9409 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9410 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9411 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9412 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9413 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9414 seq_puts(m, "\n\tGroups:\t");
9415 gi = cred->group_info;
9416 for (g = 0; g < gi->ngroups; g++) {
9417 seq_put_decimal_ull(m, g ? " " : "",
9418 from_kgid_munged(uns, gi->gid[g]));
9419 }
9420 seq_puts(m, "\n\tCapEff:\t");
9421 cap = cred->cap_effective;
9422 CAP_FOR_EACH_U32(__capi)
9423 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9424 seq_putc(m, '\n');
9425 return 0;
9426}
9427
9428static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9429{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009430 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009431 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009432 int i;
9433
Jens Axboefad8e0d2020-09-28 08:57:48 -06009434 /*
9435 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9436 * since fdinfo case grabs it in the opposite direction of normal use
9437 * cases. If we fail to get the lock, we just don't iterate any
9438 * structures that could be going away outside the io_uring mutex.
9439 */
9440 has_lock = mutex_trylock(&ctx->uring_lock);
9441
Joseph Qidbbe9c62020-09-29 09:01:22 -06009442 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9443 sq = ctx->sq_data;
9444
9445 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9446 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009447 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009448 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009449 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009450
Jens Axboe87ce9552020-01-30 08:25:34 -07009451 if (f)
9452 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9453 else
9454 seq_printf(m, "%5u: <none>\n", i);
9455 }
9456 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009457 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009458 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9459
9460 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9461 (unsigned int) buf->len);
9462 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009463 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009464 seq_printf(m, "Personalities:\n");
9465 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9466 }
Jens Axboed7718a92020-02-14 22:23:12 -07009467 seq_printf(m, "PollList:\n");
9468 spin_lock_irq(&ctx->completion_lock);
9469 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9470 struct hlist_head *list = &ctx->cancel_hash[i];
9471 struct io_kiocb *req;
9472
9473 hlist_for_each_entry(req, list, hash_node)
9474 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9475 req->task->task_works != NULL);
9476 }
9477 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009478 if (has_lock)
9479 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009480}
9481
9482static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9483{
9484 struct io_ring_ctx *ctx = f->private_data;
9485
9486 if (percpu_ref_tryget(&ctx->refs)) {
9487 __io_uring_show_fdinfo(ctx, m);
9488 percpu_ref_put(&ctx->refs);
9489 }
9490}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009491#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009492
Jens Axboe2b188cc2019-01-07 10:46:33 -07009493static const struct file_operations io_uring_fops = {
9494 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009495 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009496 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009497#ifndef CONFIG_MMU
9498 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9499 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9500#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009501 .poll = io_uring_poll,
9502 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009503#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009504 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009505#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009506};
9507
9508static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9509 struct io_uring_params *p)
9510{
Hristo Venev75b28af2019-08-26 17:23:46 +00009511 struct io_rings *rings;
9512 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009513
Jens Axboebd740482020-08-05 12:58:23 -06009514 /* make sure these are sane, as we already accounted them */
9515 ctx->sq_entries = p->sq_entries;
9516 ctx->cq_entries = p->cq_entries;
9517
Hristo Venev75b28af2019-08-26 17:23:46 +00009518 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9519 if (size == SIZE_MAX)
9520 return -EOVERFLOW;
9521
9522 rings = io_mem_alloc(size);
9523 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009524 return -ENOMEM;
9525
Hristo Venev75b28af2019-08-26 17:23:46 +00009526 ctx->rings = rings;
9527 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9528 rings->sq_ring_mask = p->sq_entries - 1;
9529 rings->cq_ring_mask = p->cq_entries - 1;
9530 rings->sq_ring_entries = p->sq_entries;
9531 rings->cq_ring_entries = p->cq_entries;
9532 ctx->sq_mask = rings->sq_ring_mask;
9533 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009534
9535 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009536 if (size == SIZE_MAX) {
9537 io_mem_free(ctx->rings);
9538 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009539 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009540 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541
9542 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009543 if (!ctx->sq_sqes) {
9544 io_mem_free(ctx->rings);
9545 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009546 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009547 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009548
Jens Axboe2b188cc2019-01-07 10:46:33 -07009549 return 0;
9550}
9551
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009552static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9553{
9554 int ret, fd;
9555
9556 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9557 if (fd < 0)
9558 return fd;
9559
9560 ret = io_uring_add_task_file(ctx, file);
9561 if (ret) {
9562 put_unused_fd(fd);
9563 return ret;
9564 }
9565 fd_install(fd, file);
9566 return fd;
9567}
9568
Jens Axboe2b188cc2019-01-07 10:46:33 -07009569/*
9570 * Allocate an anonymous fd, this is what constitutes the application
9571 * visible backing of an io_uring instance. The application mmaps this
9572 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9573 * we have to tie this fd to a socket for file garbage collection purposes.
9574 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009575static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009576{
9577 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009578#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009579 int ret;
9580
Jens Axboe2b188cc2019-01-07 10:46:33 -07009581 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9582 &ctx->ring_sock);
9583 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009584 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009585#endif
9586
Jens Axboe2b188cc2019-01-07 10:46:33 -07009587 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9588 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009589#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009590 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009591 sock_release(ctx->ring_sock);
9592 ctx->ring_sock = NULL;
9593 } else {
9594 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009595 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009596#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009597 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009598}
9599
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009600static int io_uring_create(unsigned entries, struct io_uring_params *p,
9601 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009602{
9603 struct user_struct *user = NULL;
9604 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009605 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009606 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009607 int ret;
9608
Jens Axboe8110c1a2019-12-28 15:39:54 -07009609 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009610 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009611 if (entries > IORING_MAX_ENTRIES) {
9612 if (!(p->flags & IORING_SETUP_CLAMP))
9613 return -EINVAL;
9614 entries = IORING_MAX_ENTRIES;
9615 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009616
9617 /*
9618 * Use twice as many entries for the CQ ring. It's possible for the
9619 * application to drive a higher depth than the size of the SQ ring,
9620 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009621 * some flexibility in overcommitting a bit. If the application has
9622 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9623 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009624 */
9625 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009626 if (p->flags & IORING_SETUP_CQSIZE) {
9627 /*
9628 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9629 * to a power-of-two, if it isn't already. We do NOT impose
9630 * any cq vs sq ring sizing.
9631 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009632 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009633 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009634 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9635 if (!(p->flags & IORING_SETUP_CLAMP))
9636 return -EINVAL;
9637 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9638 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009639 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9640 if (p->cq_entries < p->sq_entries)
9641 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009642 } else {
9643 p->cq_entries = 2 * p->sq_entries;
9644 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009645
9646 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009647 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009648
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009649 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009650 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009651 ring_pages(p->sq_entries, p->cq_entries));
9652 if (ret) {
9653 free_uid(user);
9654 return ret;
9655 }
9656 }
9657
9658 ctx = io_ring_ctx_alloc(p);
9659 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009660 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009661 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009662 p->cq_entries));
9663 free_uid(user);
9664 return -ENOMEM;
9665 }
9666 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009667 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009668 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009669#ifdef CONFIG_AUDIT
9670 ctx->loginuid = current->loginuid;
9671 ctx->sessionid = current->sessionid;
9672#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009673 ctx->sqo_task = get_task_struct(current);
9674
9675 /*
9676 * This is just grabbed for accounting purposes. When a process exits,
9677 * the mm is exited and dropped before the files, hence we need to hang
9678 * on to this mm purely for the purposes of being able to unaccount
9679 * memory (locked/pinned vm). It's not used for anything else.
9680 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009681 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009682 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009683
Dennis Zhou91d8f512020-09-16 13:41:05 -07009684#ifdef CONFIG_BLK_CGROUP
9685 /*
9686 * The sq thread will belong to the original cgroup it was inited in.
9687 * If the cgroup goes offline (e.g. disabling the io controller), then
9688 * issued bios will be associated with the closest cgroup later in the
9689 * block layer.
9690 */
9691 rcu_read_lock();
9692 ctx->sqo_blkcg_css = blkcg_css();
9693 ret = css_tryget_online(ctx->sqo_blkcg_css);
9694 rcu_read_unlock();
9695 if (!ret) {
9696 /* don't init against a dying cgroup, have the user try again */
9697 ctx->sqo_blkcg_css = NULL;
9698 ret = -ENODEV;
9699 goto err;
9700 }
9701#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009702
Jens Axboe2b188cc2019-01-07 10:46:33 -07009703 /*
9704 * Account memory _before_ installing the file descriptor. Once
9705 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009706 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009707 * will un-account as well.
9708 */
9709 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9710 ACCT_LOCKED);
9711 ctx->limit_mem = limit_mem;
9712
9713 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009714 if (ret)
9715 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009716
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009717 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009718 if (ret)
9719 goto err;
9720
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009721 if (!(p->flags & IORING_SETUP_R_DISABLED))
9722 io_sq_offload_start(ctx);
9723
Jens Axboe2b188cc2019-01-07 10:46:33 -07009724 memset(&p->sq_off, 0, sizeof(p->sq_off));
9725 p->sq_off.head = offsetof(struct io_rings, sq.head);
9726 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9727 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9728 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9729 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9730 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9731 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9732
9733 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009734 p->cq_off.head = offsetof(struct io_rings, cq.head);
9735 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9736 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9737 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9738 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9739 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009740 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009741
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009742 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9743 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009744 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009745 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9746 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009747
9748 if (copy_to_user(params, p, sizeof(*p))) {
9749 ret = -EFAULT;
9750 goto err;
9751 }
Jens Axboed1719f72020-07-30 13:43:53 -06009752
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009753 file = io_uring_get_file(ctx);
9754 if (IS_ERR(file)) {
9755 ret = PTR_ERR(file);
9756 goto err;
9757 }
9758
Jens Axboed1719f72020-07-30 13:43:53 -06009759 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009760 * Install ring fd as the very last thing, so we don't risk someone
9761 * having closed it before we finish setup
9762 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009763 ret = io_uring_install_fd(ctx, file);
9764 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009765 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009766 /* fput will clean it up */
9767 fput(file);
9768 return ret;
9769 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009770
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009771 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009772 return ret;
9773err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009774 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009775 io_ring_ctx_wait_and_kill(ctx);
9776 return ret;
9777}
9778
9779/*
9780 * Sets up an aio uring context, and returns the fd. Applications asks for a
9781 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9782 * params structure passed in.
9783 */
9784static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9785{
9786 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009787 int i;
9788
9789 if (copy_from_user(&p, params, sizeof(p)))
9790 return -EFAULT;
9791 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9792 if (p.resv[i])
9793 return -EINVAL;
9794 }
9795
Jens Axboe6c271ce2019-01-10 11:22:30 -07009796 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009797 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009798 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9799 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009800 return -EINVAL;
9801
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009802 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009803}
9804
9805SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9806 struct io_uring_params __user *, params)
9807{
9808 return io_uring_setup(entries, params);
9809}
9810
Jens Axboe66f4af92020-01-16 15:36:52 -07009811static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9812{
9813 struct io_uring_probe *p;
9814 size_t size;
9815 int i, ret;
9816
9817 size = struct_size(p, ops, nr_args);
9818 if (size == SIZE_MAX)
9819 return -EOVERFLOW;
9820 p = kzalloc(size, GFP_KERNEL);
9821 if (!p)
9822 return -ENOMEM;
9823
9824 ret = -EFAULT;
9825 if (copy_from_user(p, arg, size))
9826 goto out;
9827 ret = -EINVAL;
9828 if (memchr_inv(p, 0, size))
9829 goto out;
9830
9831 p->last_op = IORING_OP_LAST - 1;
9832 if (nr_args > IORING_OP_LAST)
9833 nr_args = IORING_OP_LAST;
9834
9835 for (i = 0; i < nr_args; i++) {
9836 p->ops[i].op = i;
9837 if (!io_op_defs[i].not_supported)
9838 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9839 }
9840 p->ops_len = i;
9841
9842 ret = 0;
9843 if (copy_to_user(arg, p, size))
9844 ret = -EFAULT;
9845out:
9846 kfree(p);
9847 return ret;
9848}
9849
Jens Axboe071698e2020-01-28 10:04:42 -07009850static int io_register_personality(struct io_ring_ctx *ctx)
9851{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009852 struct io_identity *id;
9853 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009854
Jens Axboe1e6fa522020-10-15 08:46:24 -06009855 id = kmalloc(sizeof(*id), GFP_KERNEL);
9856 if (unlikely(!id))
9857 return -ENOMEM;
9858
9859 io_init_identity(id);
9860 id->creds = get_current_cred();
9861
9862 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9863 if (ret < 0) {
9864 put_cred(id->creds);
9865 kfree(id);
9866 }
9867 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009868}
9869
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009870static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9871 unsigned int nr_args)
9872{
9873 struct io_uring_restriction *res;
9874 size_t size;
9875 int i, ret;
9876
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009877 /* Restrictions allowed only if rings started disabled */
9878 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9879 return -EBADFD;
9880
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009881 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009882 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009883 return -EBUSY;
9884
9885 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9886 return -EINVAL;
9887
9888 size = array_size(nr_args, sizeof(*res));
9889 if (size == SIZE_MAX)
9890 return -EOVERFLOW;
9891
9892 res = memdup_user(arg, size);
9893 if (IS_ERR(res))
9894 return PTR_ERR(res);
9895
9896 ret = 0;
9897
9898 for (i = 0; i < nr_args; i++) {
9899 switch (res[i].opcode) {
9900 case IORING_RESTRICTION_REGISTER_OP:
9901 if (res[i].register_op >= IORING_REGISTER_LAST) {
9902 ret = -EINVAL;
9903 goto out;
9904 }
9905
9906 __set_bit(res[i].register_op,
9907 ctx->restrictions.register_op);
9908 break;
9909 case IORING_RESTRICTION_SQE_OP:
9910 if (res[i].sqe_op >= IORING_OP_LAST) {
9911 ret = -EINVAL;
9912 goto out;
9913 }
9914
9915 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9916 break;
9917 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9918 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9919 break;
9920 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9921 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9922 break;
9923 default:
9924 ret = -EINVAL;
9925 goto out;
9926 }
9927 }
9928
9929out:
9930 /* Reset all restrictions if an error happened */
9931 if (ret != 0)
9932 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9933 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009934 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009935
9936 kfree(res);
9937 return ret;
9938}
9939
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009940static int io_register_enable_rings(struct io_ring_ctx *ctx)
9941{
9942 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9943 return -EBADFD;
9944
9945 if (ctx->restrictions.registered)
9946 ctx->restricted = 1;
9947
9948 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9949
9950 io_sq_offload_start(ctx);
9951
9952 return 0;
9953}
9954
Jens Axboe071698e2020-01-28 10:04:42 -07009955static bool io_register_op_must_quiesce(int op)
9956{
9957 switch (op) {
9958 case IORING_UNREGISTER_FILES:
9959 case IORING_REGISTER_FILES_UPDATE:
9960 case IORING_REGISTER_PROBE:
9961 case IORING_REGISTER_PERSONALITY:
9962 case IORING_UNREGISTER_PERSONALITY:
9963 return false;
9964 default:
9965 return true;
9966 }
9967}
9968
Jens Axboeedafcce2019-01-09 09:16:05 -07009969static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9970 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009971 __releases(ctx->uring_lock)
9972 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009973{
9974 int ret;
9975
Jens Axboe35fa71a2019-04-22 10:23:23 -06009976 /*
9977 * We're inside the ring mutex, if the ref is already dying, then
9978 * someone else killed the ctx or is already going through
9979 * io_uring_register().
9980 */
9981 if (percpu_ref_is_dying(&ctx->refs))
9982 return -ENXIO;
9983
Jens Axboe071698e2020-01-28 10:04:42 -07009984 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009985 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009986
Jens Axboe05f3fb32019-12-09 11:22:50 -07009987 /*
9988 * Drop uring mutex before waiting for references to exit. If
9989 * another thread is currently inside io_uring_enter() it might
9990 * need to grab the uring_lock to make progress. If we hold it
9991 * here across the drain wait, then we can deadlock. It's safe
9992 * to drop the mutex here, since no new references will come in
9993 * after we've killed the percpu ref.
9994 */
9995 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009996 do {
9997 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9998 if (!ret)
9999 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010000 ret = io_run_task_work_sig();
10001 if (ret < 0)
10002 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010003 } while (1);
10004
Jens Axboe05f3fb32019-12-09 11:22:50 -070010005 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010006
Jens Axboec1503682020-01-08 08:26:07 -070010007 if (ret) {
10008 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010009 goto out_quiesce;
10010 }
10011 }
10012
10013 if (ctx->restricted) {
10014 if (opcode >= IORING_REGISTER_LAST) {
10015 ret = -EINVAL;
10016 goto out;
10017 }
10018
10019 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10020 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010021 goto out;
10022 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010023 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010024
10025 switch (opcode) {
10026 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010027 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010028 break;
10029 case IORING_UNREGISTER_BUFFERS:
10030 ret = -EINVAL;
10031 if (arg || nr_args)
10032 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010033 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010034 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010035 case IORING_REGISTER_FILES:
10036 ret = io_sqe_files_register(ctx, arg, nr_args);
10037 break;
10038 case IORING_UNREGISTER_FILES:
10039 ret = -EINVAL;
10040 if (arg || nr_args)
10041 break;
10042 ret = io_sqe_files_unregister(ctx);
10043 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010044 case IORING_REGISTER_FILES_UPDATE:
10045 ret = io_sqe_files_update(ctx, arg, nr_args);
10046 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010047 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010048 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010049 ret = -EINVAL;
10050 if (nr_args != 1)
10051 break;
10052 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010053 if (ret)
10054 break;
10055 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10056 ctx->eventfd_async = 1;
10057 else
10058 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010059 break;
10060 case IORING_UNREGISTER_EVENTFD:
10061 ret = -EINVAL;
10062 if (arg || nr_args)
10063 break;
10064 ret = io_eventfd_unregister(ctx);
10065 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010066 case IORING_REGISTER_PROBE:
10067 ret = -EINVAL;
10068 if (!arg || nr_args > 256)
10069 break;
10070 ret = io_probe(ctx, arg, nr_args);
10071 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010072 case IORING_REGISTER_PERSONALITY:
10073 ret = -EINVAL;
10074 if (arg || nr_args)
10075 break;
10076 ret = io_register_personality(ctx);
10077 break;
10078 case IORING_UNREGISTER_PERSONALITY:
10079 ret = -EINVAL;
10080 if (arg)
10081 break;
10082 ret = io_unregister_personality(ctx, nr_args);
10083 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010084 case IORING_REGISTER_ENABLE_RINGS:
10085 ret = -EINVAL;
10086 if (arg || nr_args)
10087 break;
10088 ret = io_register_enable_rings(ctx);
10089 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010090 case IORING_REGISTER_RESTRICTIONS:
10091 ret = io_register_restrictions(ctx, arg, nr_args);
10092 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010093 default:
10094 ret = -EINVAL;
10095 break;
10096 }
10097
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010098out:
Jens Axboe071698e2020-01-28 10:04:42 -070010099 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010100 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010101 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010102out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010103 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010104 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010105 return ret;
10106}
10107
10108SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10109 void __user *, arg, unsigned int, nr_args)
10110{
10111 struct io_ring_ctx *ctx;
10112 long ret = -EBADF;
10113 struct fd f;
10114
10115 f = fdget(fd);
10116 if (!f.file)
10117 return -EBADF;
10118
10119 ret = -EOPNOTSUPP;
10120 if (f.file->f_op != &io_uring_fops)
10121 goto out_fput;
10122
10123 ctx = f.file->private_data;
10124
10125 mutex_lock(&ctx->uring_lock);
10126 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10127 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010128 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10129 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010130out_fput:
10131 fdput(f);
10132 return ret;
10133}
10134
Jens Axboe2b188cc2019-01-07 10:46:33 -070010135static int __init io_uring_init(void)
10136{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010137#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10138 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10139 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10140} while (0)
10141
10142#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10143 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10144 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10145 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10146 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10147 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10148 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10149 BUILD_BUG_SQE_ELEM(8, __u64, off);
10150 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10151 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010152 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010153 BUILD_BUG_SQE_ELEM(24, __u32, len);
10154 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10155 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10156 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10157 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010158 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10159 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010160 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10161 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10162 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10163 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10164 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10165 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10166 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10167 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010168 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010169 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10170 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10171 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010172 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010173
Jens Axboed3656342019-12-18 09:50:26 -070010174 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010175 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010176 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10177 return 0;
10178};
10179__initcall(io_uring_init);