blob: d0b7332ca70334398dcdf0903c3b274f728d0ac8 [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
Jens Axboeedafcce2019-01-09 09:16:05 -0700190struct io_mapped_ubuf {
191 u64 ubuf;
192 size_t len;
193 struct bio_vec *bvec;
194 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600195 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700196};
197
Jens Axboe65e19f52019-10-26 07:20:21 -0600198struct fixed_file_table {
199 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700200};
201
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202struct fixed_file_ref_node {
203 struct percpu_ref refs;
204 struct list_head node;
205 struct list_head file_list;
206 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600207 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000208 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800209};
210
Jens Axboe05f3fb32019-12-09 11:22:50 -0700211struct fixed_file_data {
212 struct fixed_file_table *table;
213 struct io_ring_ctx *ctx;
214
Pavel Begunkovb2e96852020-10-10 18:34:16 +0100215 struct fixed_file_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700217 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct list_head ref_list;
219 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700220};
221
Jens Axboe5a2e7452020-02-23 16:23:11 -0700222struct io_buffer {
223 struct list_head list;
224 __u64 addr;
225 __s32 len;
226 __u16 bid;
227};
228
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200229struct io_restriction {
230 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
231 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
232 u8 sqe_flags_allowed;
233 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200234 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200235};
236
Jens Axboe534ca6d2020-09-02 13:52:19 -0600237struct io_sq_data {
238 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600239 struct mutex lock;
240
241 /* ctx's that are using this sqd */
242 struct list_head ctx_list;
243 struct list_head ctx_new_list;
244 struct mutex ctx_lock;
245
Jens Axboe534ca6d2020-09-02 13:52:19 -0600246 struct task_struct *thread;
247 struct wait_queue_head wait;
248};
249
Jens Axboe2b188cc2019-01-07 10:46:33 -0700250struct io_ring_ctx {
251 struct {
252 struct percpu_ref refs;
253 } ____cacheline_aligned_in_smp;
254
255 struct {
256 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700258 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800259 unsigned int cq_overflow_flushed: 1;
260 unsigned int drain_next: 1;
261 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200262 unsigned int restricted: 1;
Pavel Begunkova63d9152021-01-26 11:17:03 +0000263 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700264
Hristo Venev75b28af2019-08-26 17:23:46 +0000265 /*
266 * Ring buffer of indices into array of io_uring_sqe, which is
267 * mmapped by the application using the IORING_OFF_SQES offset.
268 *
269 * This indirection could e.g. be used to assign fixed
270 * io_uring_sqe entries to operations and only submit them to
271 * the queue when needed.
272 *
273 * The kernel modifies neither the indices array nor the entries
274 * array.
275 */
276 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700277 unsigned cached_sq_head;
278 unsigned sq_entries;
279 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700280 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600281 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100282 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700283 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600284
285 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600286 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700287 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288
Jens Axboead3eb2c2019-12-18 17:12:20 -0700289 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 } ____cacheline_aligned_in_smp;
291
Hristo Venev75b28af2019-08-26 17:23:46 +0000292 struct io_rings *rings;
293
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600295 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600296
297 /*
298 * For SQPOLL usage - we hold a reference to the parent task, so we
299 * have access to the ->files
300 */
301 struct task_struct *sqo_task;
302
303 /* Only used for accounting purposes */
304 struct mm_struct *mm_account;
305
Dennis Zhou91d8f512020-09-16 13:41:05 -0700306#ifdef CONFIG_BLK_CGROUP
307 struct cgroup_subsys_state *sqo_blkcg_css;
308#endif
309
Jens Axboe534ca6d2020-09-02 13:52:19 -0600310 struct io_sq_data *sq_data; /* if using sq thread polling */
311
Jens Axboe90554202020-09-03 12:12:41 -0600312 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600313 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600314 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315
Jens Axboe6b063142019-01-10 22:13:58 -0700316 /*
317 * If used, fixed file set. Writers must ensure that ->refs is dead,
318 * readers must ensure that ->refs is alive as long as the file* is
319 * used. Only updated through io_uring_register(2).
320 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700321 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700322 unsigned nr_user_files;
323
Jens Axboeedafcce2019-01-09 09:16:05 -0700324 /* if used, fixed mapped user buffers */
325 unsigned nr_user_bufs;
326 struct io_mapped_ubuf *user_bufs;
327
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 struct user_struct *user;
329
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700330 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700331
Jens Axboe4ea33a92020-10-15 13:46:44 -0600332#ifdef CONFIG_AUDIT
333 kuid_t loginuid;
334 unsigned int sessionid;
335#endif
336
Jens Axboe0f158b42020-05-14 17:18:39 -0600337 struct completion ref_comp;
338 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700339
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700340 /* if all else fails... */
341 struct io_kiocb *fallback_req;
342
Jens Axboe206aefd2019-11-07 18:27:42 -0700343#if defined(CONFIG_UNIX)
344 struct socket *ring_sock;
345#endif
346
Jens Axboe5a2e7452020-02-23 16:23:11 -0700347 struct idr io_buffer_idr;
348
Jens Axboe071698e2020-01-28 10:04:42 -0700349 struct idr personality_idr;
350
Jens Axboe206aefd2019-11-07 18:27:42 -0700351 struct {
352 unsigned cached_cq_tail;
353 unsigned cq_entries;
354 unsigned cq_mask;
355 atomic_t cq_timeouts;
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -0500356 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700357 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700358 struct wait_queue_head cq_wait;
359 struct fasync_struct *cq_fasync;
360 struct eventfd_ctx *cq_ev_fd;
361 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362
363 struct {
364 struct mutex uring_lock;
365 wait_queue_head_t wait;
366 } ____cacheline_aligned_in_smp;
367
368 struct {
369 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700370
Jens Axboedef596e2019-01-09 08:59:42 -0700371 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300372 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700373 * io_uring instances that don't use IORING_SETUP_SQPOLL.
374 * For SQPOLL, only the single threaded io_sq_thread() will
375 * manipulate the list, hence no extra locking is needed there.
376 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300377 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700378 struct hlist_head *cancel_hash;
379 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700380 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600381
382 spinlock_t inflight_lock;
383 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600385
Jens Axboe4a38aed22020-05-14 17:21:15 -0600386 struct delayed_work file_put_work;
387 struct llist_head file_put_llist;
388
Jens Axboe85faa7b2020-04-09 18:14:00 -0600389 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200390 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700391};
392
Jens Axboe09bb8392019-03-13 12:39:28 -0600393/*
394 * First field must be the file pointer in all the
395 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
396 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700397struct io_poll_iocb {
398 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700399 union {
400 struct wait_queue_head *head;
401 u64 addr;
402 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700403 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600404 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700405 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700406 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700407};
408
Jens Axboeb5dba592019-12-11 14:02:38 -0700409struct io_close {
410 struct file *file;
411 struct file *put_file;
412 int fd;
413};
414
Jens Axboead8a48a2019-11-15 08:49:11 -0700415struct io_timeout_data {
416 struct io_kiocb *req;
417 struct hrtimer timer;
418 struct timespec64 ts;
419 enum hrtimer_mode mode;
420};
421
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700422struct io_accept {
423 struct file *file;
424 struct sockaddr __user *addr;
425 int __user *addr_len;
426 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600427 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700428};
429
430struct io_sync {
431 struct file *file;
432 loff_t len;
433 loff_t off;
434 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700435 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700436};
437
Jens Axboefbf23842019-12-17 18:45:56 -0700438struct io_cancel {
439 struct file *file;
440 u64 addr;
441};
442
Jens Axboeb29472e2019-12-17 18:50:29 -0700443struct io_timeout {
444 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300445 u32 off;
446 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300447 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700448};
449
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100450struct io_timeout_rem {
451 struct file *file;
452 u64 addr;
453};
454
Jens Axboe9adbd452019-12-20 08:45:55 -0700455struct io_rw {
456 /* NOTE: kiocb has the file as the first member, so don't do it here */
457 struct kiocb kiocb;
458 u64 addr;
459 u64 len;
460};
461
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700462struct io_connect {
463 struct file *file;
464 struct sockaddr __user *addr;
465 int addr_len;
466};
467
Jens Axboee47293f2019-12-20 08:58:21 -0700468struct io_sr_msg {
469 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700470 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300471 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700472 void __user *buf;
473 };
Jens Axboee47293f2019-12-20 08:58:21 -0700474 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700475 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700476 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700477 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700478};
479
Jens Axboe15b71ab2019-12-11 11:20:36 -0700480struct io_open {
481 struct file *file;
482 int dfd;
Jens Axboe944d1442020-11-13 16:48:44 -0700483 bool ignore_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700484 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700485 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600486 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700487};
488
Jens Axboe05f3fb32019-12-09 11:22:50 -0700489struct io_files_update {
490 struct file *file;
491 u64 arg;
492 u32 nr_args;
493 u32 offset;
494};
495
Jens Axboe4840e412019-12-25 22:03:45 -0700496struct io_fadvise {
497 struct file *file;
498 u64 offset;
499 u32 len;
500 u32 advice;
501};
502
Jens Axboec1ca7572019-12-25 22:18:28 -0700503struct io_madvise {
504 struct file *file;
505 u64 addr;
506 u32 len;
507 u32 advice;
508};
509
Jens Axboe3e4827b2020-01-08 15:18:09 -0700510struct io_epoll {
511 struct file *file;
512 int epfd;
513 int op;
514 int fd;
515 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700516};
517
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300518struct io_splice {
519 struct file *file_out;
520 struct file *file_in;
521 loff_t off_out;
522 loff_t off_in;
523 u64 len;
524 unsigned int flags;
525};
526
Jens Axboeddf0322d2020-02-23 16:41:33 -0700527struct io_provide_buf {
528 struct file *file;
529 __u64 addr;
530 __s32 len;
531 __u32 bgid;
532 __u16 nbufs;
533 __u16 bid;
534};
535
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700536struct io_statx {
537 struct file *file;
538 int dfd;
539 unsigned int mask;
540 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700541 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700542 struct statx __user *buffer;
543};
544
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300545struct io_completion {
546 struct file *file;
547 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300548 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300549};
550
Jens Axboef499a022019-12-02 16:28:46 -0700551struct io_async_connect {
552 struct sockaddr_storage address;
553};
554
Jens Axboe03b12302019-12-02 18:50:25 -0700555struct io_async_msghdr {
556 struct iovec fast_iov[UIO_FASTIOV];
557 struct iovec *iov;
558 struct sockaddr __user *uaddr;
559 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700560 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700561};
562
Jens Axboef67676d2019-12-02 11:03:47 -0700563struct io_async_rw {
564 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600565 const struct iovec *free_iovec;
566 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600567 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600568 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700569};
570
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300571enum {
572 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
573 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
574 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
575 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
576 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700577 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300579 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300580 REQ_F_FAIL_LINK_BIT,
581 REQ_F_INFLIGHT_BIT,
582 REQ_F_CUR_POS_BIT,
583 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300586 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700587 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700588 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600589 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800590 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100591 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700592
593 /* not a real bit, just to check we're not overflowing the space */
594 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300595};
596
597enum {
598 /* ctx owns file */
599 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
600 /* drain existing IO first */
601 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
602 /* linked sqes */
603 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
604 /* doesn't sever on completion < 0 */
605 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
606 /* IOSQE_ASYNC */
607 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700608 /* IOSQE_BUFFER_SELECT */
609 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300610
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300611 /* head of a link */
612 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 /* fail rest of links */
614 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
615 /* on inflight list */
616 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
617 /* read/write uses file position */
618 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
619 /* must not punt to workers */
620 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100621 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300622 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300623 /* regular file */
624 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300625 /* needs cleanup */
626 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700627 /* already went through poll handler */
628 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700629 /* buffer already selected */
630 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600631 /* doesn't need file table for this request */
632 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800633 /* io_wq_work is initialized */
634 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100635 /* linked timeout is active, i.e. prepared by link's head */
636 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700637};
638
639struct async_poll {
640 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600641 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300642};
643
Jens Axboe09bb8392019-03-13 12:39:28 -0600644/*
645 * NOTE! Each of the iocb union members has the file pointer
646 * as the first entry in their struct definition. So you can
647 * access the file pointer through any of the sub-structs,
648 * or directly as just 'ki_filp' in this struct.
649 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700650struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700651 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600652 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700653 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700654 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700655 struct io_accept accept;
656 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700657 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700658 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100659 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700660 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700661 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700662 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700663 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700664 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700665 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700666 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700667 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300668 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700669 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700670 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300671 /* use only after cleaning per-op data, see io_clean_op() */
672 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700673 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700674
Jens Axboee8c2bc12020-08-15 18:44:09 -0700675 /* opcode allocated if it needs to store data for async defer */
676 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700677 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800678 /* polled IO has completed */
679 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700681 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300682 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700683
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300684 struct io_ring_ctx *ctx;
685 unsigned int flags;
686 refcount_t refs;
687 struct task_struct *task;
688 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700689
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300690 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700691
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300692 /*
693 * 1. used with ctx->iopoll_list with reads/writes
694 * 2. to track reqs with ->files (see io_op_def::file_table)
695 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300696 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600697
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300698 struct percpu_ref *fixed_file_refs;
699 struct callback_head task_work;
700 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
701 struct hlist_node hash_node;
702 struct async_poll *apoll;
703 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700704};
705
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300706struct io_defer_entry {
707 struct list_head list;
708 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300709 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300710};
711
Jens Axboedef596e2019-01-09 08:59:42 -0700712#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713
Jens Axboe013538b2020-06-22 09:29:15 -0600714struct io_comp_state {
715 unsigned int nr;
716 struct list_head list;
717 struct io_ring_ctx *ctx;
718};
719
Jens Axboe9a56a232019-01-09 09:06:50 -0700720struct io_submit_state {
721 struct blk_plug plug;
722
723 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700724 * io_kiocb alloc cache
725 */
726 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300727 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700728
729 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600730 * Batch completion logic
731 */
732 struct io_comp_state comp;
733
734 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700735 * File reference cache
736 */
737 struct file *file;
738 unsigned int fd;
739 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700740 unsigned int ios_left;
741};
742
Jens Axboed3656342019-12-18 09:50:26 -0700743struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700744 /* needs req->file assigned */
745 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600746 /* don't fail if file grab fails */
747 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700748 /* hash wq insertion if file is a regular file */
749 unsigned hash_reg_file : 1;
750 /* unbound wq insertion if file is a non-regular file */
751 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700752 /* opcode is not supported by this kernel */
753 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700754 /* set if opcode supports polled "wait" */
755 unsigned pollin : 1;
756 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700757 /* op supports buffer selection */
758 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700759 /* must always have async data allocated */
760 unsigned needs_async_data : 1;
761 /* size of async data needed, if any */
762 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600763 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700764};
765
Jens Axboe09186822020-10-13 15:01:40 -0600766static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_NOP] = {},
768 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .needs_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700772 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700773 .needs_async_data = 1,
774 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600775 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .needs_file = 1,
779 .hash_reg_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700781 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700782 .needs_async_data = 1,
783 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600784 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
785 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700786 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300787 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700788 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600789 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .needs_file = 1,
793 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700794 .pollin = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700795 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600796 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .needs_file = 1,
800 .hash_reg_file = 1,
801 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700802 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700803 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600804 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
805 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .needs_file = 1,
809 .unbound_nonreg_file = 1,
810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_POLL_REMOVE] = {},
812 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700813 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600814 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700817 .needs_file = 1,
818 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700820 .needs_async_data = 1,
821 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600822 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
823 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700826 .needs_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700828 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700829 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700830 .needs_async_data = 1,
831 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600832 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
833 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700836 .needs_async_data = 1,
837 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600838 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_TIMEOUT_REMOVE] = {},
841 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700842 .needs_file = 1,
843 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700844 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600845 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_ASYNC_CANCEL] = {},
848 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700849 .needs_async_data = 1,
850 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600851 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700852 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300853 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700854 .needs_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700857 .needs_async_data = 1,
858 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600859 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700862 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600863 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600866 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
867 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700868 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300869 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600870 .needs_file = 1,
871 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600872 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700873 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300874 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600875 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700876 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300877 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600878 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
879 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700882 .needs_file = 1,
883 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700884 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700885 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700886 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600887 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700892 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700893 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600894 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
895 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700898 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600899 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600902 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700903 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300904 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700905 .needs_file = 1,
906 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700907 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600908 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700911 .needs_file = 1,
912 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700913 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700914 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600915 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600918 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
919 IO_WQ_WORK_BLKCG,
Jens Axboecebdb982020-01-08 17:59:24 -0700920 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700921 [IORING_OP_EPOLL_CTL] = {
922 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600923 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700924 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300925 [IORING_OP_SPLICE] = {
926 .needs_file = 1,
927 .hash_reg_file = 1,
928 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600929 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700930 },
931 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700932 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300933 [IORING_OP_TEE] = {
934 .needs_file = 1,
935 .hash_reg_file = 1,
936 .unbound_nonreg_file = 1,
937 },
Jens Axboed3656342019-12-18 09:50:26 -0700938};
939
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700940enum io_mem_account {
941 ACCT_LOCKED,
942 ACCT_PINNED,
943};
944
Pavel Begunkovce00a7d2020-12-30 21:34:15 +0000945static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node);
946static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
947 struct io_ring_ctx *ctx);
948
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300949static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
950 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700951static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800952static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +0100953static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -0600954static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700955static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600956static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700957static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700958static int __io_sqe_files_update(struct io_ring_ctx *ctx,
959 struct io_uring_files_update *ip,
960 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300961static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100962static struct file *io_file_get(struct io_submit_state *state,
963 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300964static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600965static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600966
Jens Axboeb63534c2020-06-04 11:28:00 -0600967static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
968 struct iovec **iovec, struct iov_iter *iter,
969 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600970static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
971 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600972 struct iov_iter *iter, bool force);
Pavel Begunkovd92d0082021-01-26 11:17:10 +0000973static void io_req_drop_files(struct io_kiocb *req);
Pavel Begunkovbc79ff02021-01-26 23:35:10 +0000974static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700975
976static struct kmem_cache *req_cachep;
977
Jens Axboe09186822020-10-13 15:01:40 -0600978static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700979
980struct sock *io_uring_get_socket(struct file *file)
981{
982#if defined(CONFIG_UNIX)
983 if (file->f_op == &io_uring_fops) {
984 struct io_ring_ctx *ctx = file->private_data;
985
986 return ctx->ring_sock->sk;
987 }
988#endif
989 return NULL;
990}
991EXPORT_SYMBOL(io_uring_get_socket);
992
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300993static inline void io_clean_op(struct io_kiocb *req)
994{
Pavel Begunkovd92d0082021-01-26 11:17:10 +0000995 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300996 __io_clean_op(req);
997}
998
Pavel Begunkovf6d93f82021-02-09 04:47:36 +0000999static inline bool __io_match_files(struct io_kiocb *req,
1000 struct files_struct *files)
1001{
Jens Axboed16692a2021-02-09 04:47:41 +00001002 if (req->file && req->file->f_op == &io_uring_fops)
1003 return true;
1004
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001005 return ((req->flags & REQ_F_WORK_INITIALIZED) &&
1006 (req->work.flags & IO_WQ_WORK_FILES)) &&
1007 req->work.identity->files == files;
1008}
1009
1010static bool io_match_task(struct io_kiocb *head,
1011 struct task_struct *task,
1012 struct files_struct *files)
1013{
1014 struct io_kiocb *link;
1015
Jens Axboef0ff1a92021-02-09 04:47:42 +00001016 if (task && head->task != task) {
1017 /* in terms of cancelation, always match if req task is dead */
1018 if (head->task->flags & PF_EXITING)
1019 return true;
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001020 return false;
Jens Axboef0ff1a92021-02-09 04:47:42 +00001021 }
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001022 if (!files)
1023 return true;
1024 if (__io_match_files(head, files))
1025 return true;
1026 if (head->flags & REQ_F_LINK_HEAD) {
1027 list_for_each_entry(link, &head->link_list, link_list) {
1028 if (__io_match_files(link, files))
1029 return true;
1030 }
1031 }
1032 return false;
1033}
1034
1035
Jens Axboe4349f302020-07-09 15:07:01 -06001036static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001037{
1038 struct mm_struct *mm = current->mm;
1039
1040 if (mm) {
1041 kthread_unuse_mm(mm);
1042 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001043 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001044 }
1045}
1046
1047static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1048{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001049 struct mm_struct *mm;
1050
Pavel Begunkova3647cd2021-01-11 04:00:31 +00001051 if (current->flags & PF_EXITING)
1052 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001053 if (current->mm)
1054 return 0;
1055
1056 /* Should never happen */
1057 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1058 return -EFAULT;
1059
1060 task_lock(ctx->sqo_task);
1061 mm = ctx->sqo_task->mm;
1062 if (unlikely(!mm || !mmget_not_zero(mm)))
1063 mm = NULL;
1064 task_unlock(ctx->sqo_task);
1065
1066 if (mm) {
1067 kthread_use_mm(mm);
1068 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001069 }
1070
Jens Axboe4b70cf92020-11-02 10:39:05 -07001071 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001072}
1073
1074static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1075 struct io_kiocb *req)
1076{
Jens Axboe0f203762020-10-14 09:23:55 -06001077 if (!(io_op_defs[req->opcode].work_flags & IO_WQ_WORK_MM))
Jens Axboec40f6372020-06-25 15:39:59 -06001078 return 0;
1079 return __io_sq_thread_acquire_mm(ctx);
1080}
1081
Dennis Zhou91d8f512020-09-16 13:41:05 -07001082static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1083 struct cgroup_subsys_state **cur_css)
1084
1085{
1086#ifdef CONFIG_BLK_CGROUP
1087 /* puts the old one when swapping */
1088 if (*cur_css != ctx->sqo_blkcg_css) {
1089 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1090 *cur_css = ctx->sqo_blkcg_css;
1091 }
1092#endif
1093}
1094
1095static void io_sq_thread_unassociate_blkcg(void)
1096{
1097#ifdef CONFIG_BLK_CGROUP
1098 kthread_associate_blkcg(NULL);
1099#endif
1100}
1101
Jens Axboec40f6372020-06-25 15:39:59 -06001102static inline void req_set_fail_links(struct io_kiocb *req)
1103{
1104 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1105 req->flags |= REQ_F_FAIL_LINK;
1106}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001107
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001108/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001109 * None of these are dereferenced, they are simply used to check if any of
1110 * them have changed. If we're under current and check they are still the
1111 * same, we're fine to grab references to them for actual out-of-line use.
1112 */
1113static void io_init_identity(struct io_identity *id)
1114{
1115 id->files = current->files;
1116 id->mm = current->mm;
1117#ifdef CONFIG_BLK_CGROUP
1118 rcu_read_lock();
1119 id->blkcg_css = blkcg_css();
1120 rcu_read_unlock();
1121#endif
1122 id->creds = current_cred();
1123 id->nsproxy = current->nsproxy;
1124 id->fs = current->fs;
1125 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001126#ifdef CONFIG_AUDIT
1127 id->loginuid = current->loginuid;
1128 id->sessionid = current->sessionid;
1129#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001130 refcount_set(&id->count, 1);
1131}
1132
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001133static inline void __io_req_init_async(struct io_kiocb *req)
1134{
1135 memset(&req->work, 0, sizeof(req->work));
1136 req->flags |= REQ_F_WORK_INITIALIZED;
1137}
1138
Jens Axboe1e6fa522020-10-15 08:46:24 -06001139/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001140 * Note: must call io_req_init_async() for the first time you
1141 * touch any members of io_wq_work.
1142 */
1143static inline void io_req_init_async(struct io_kiocb *req)
1144{
Jens Axboe500a3732020-10-15 17:38:03 -06001145 struct io_uring_task *tctx = current->io_uring;
1146
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001147 if (req->flags & REQ_F_WORK_INITIALIZED)
1148 return;
1149
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001150 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001151
1152 /* Grab a ref if this isn't our static identity */
1153 req->work.identity = tctx->identity;
1154 if (tctx->identity != &tctx->__identity)
1155 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001156}
1157
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001158static inline bool io_async_submit(struct io_ring_ctx *ctx)
1159{
1160 return ctx->flags & IORING_SETUP_SQPOLL;
1161}
1162
Jens Axboe2b188cc2019-01-07 10:46:33 -07001163static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1164{
1165 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1166
Jens Axboe0f158b42020-05-14 17:18:39 -06001167 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168}
1169
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001170static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1171{
1172 return !req->timeout.off;
1173}
1174
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1176{
1177 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001178 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001179
1180 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1181 if (!ctx)
1182 return NULL;
1183
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001184 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1185 if (!ctx->fallback_req)
1186 goto err;
1187
Jens Axboe78076bb2019-12-04 19:56:40 -07001188 /*
1189 * Use 5 bits less than the max cq entries, that should give us around
1190 * 32 entries per hash list if totally full and uniformly spread.
1191 */
1192 hash_bits = ilog2(p->cq_entries);
1193 hash_bits -= 5;
1194 if (hash_bits <= 0)
1195 hash_bits = 1;
1196 ctx->cancel_hash_bits = hash_bits;
1197 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1198 GFP_KERNEL);
1199 if (!ctx->cancel_hash)
1200 goto err;
1201 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1202
Roman Gushchin21482892019-05-07 10:01:48 -07001203 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001204 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1205 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206
1207 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001208 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001209 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001210 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001211 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001212 init_completion(&ctx->ref_comp);
1213 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001214 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001215 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216 mutex_init(&ctx->uring_lock);
1217 init_waitqueue_head(&ctx->wait);
1218 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001219 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001220 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001221 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001222 spin_lock_init(&ctx->inflight_lock);
1223 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001224 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1225 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001227err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001228 if (ctx->fallback_req)
1229 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001230 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001231 kfree(ctx);
1232 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233}
1234
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001235static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001236{
Jens Axboe2bc99302020-07-09 09:43:27 -06001237 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1238 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001239
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001240 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001241 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001242 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001243
Bob Liu9d858b22019-11-13 18:06:25 +08001244 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001245}
1246
Jens Axboede0617e2019-04-06 21:51:27 -06001247static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248{
Hristo Venev75b28af2019-08-26 17:23:46 +00001249 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250
Pavel Begunkov07910152020-01-17 03:52:46 +03001251 /* order cqe stores with ring update */
1252 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253}
1254
Jens Axboe5c3462c2020-10-15 09:02:33 -06001255static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001256{
Jens Axboe500a3732020-10-15 17:38:03 -06001257 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001258 return;
1259 if (refcount_dec_and_test(&req->work.identity->count))
1260 kfree(req->work.identity);
1261}
1262
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001263static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001264{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001265 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001266 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001267
1268 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001269
Jens Axboedfead8a2020-10-14 10:12:37 -06001270 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001271 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001272 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001273 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001274#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001275 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001276 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001277 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001278 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001279#endif
1280 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001281 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001282 req->work.flags &= ~IO_WQ_WORK_CREDS;
1283 }
1284 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001285 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001286
Jens Axboe98447d62020-10-14 10:48:51 -06001287 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001288 if (--fs->users)
1289 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001290 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001291 if (fs)
1292 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001293 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001294 }
Pavel Begunkovd92d0082021-01-26 11:17:10 +00001295 if (req->flags & REQ_F_INFLIGHT)
1296 io_req_drop_files(req);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001297
Jens Axboe5c3462c2020-10-15 09:02:33 -06001298 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001299}
1300
1301/*
1302 * Create a private copy of io_identity, since some fields don't match
1303 * the current context.
1304 */
1305static bool io_identity_cow(struct io_kiocb *req)
1306{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001307 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001308 const struct cred *creds = NULL;
1309 struct io_identity *id;
1310
1311 if (req->work.flags & IO_WQ_WORK_CREDS)
1312 creds = req->work.identity->creds;
1313
1314 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1315 if (unlikely(!id)) {
1316 req->work.flags |= IO_WQ_WORK_CANCEL;
1317 return false;
1318 }
1319
1320 /*
1321 * We can safely just re-init the creds we copied Either the field
1322 * matches the current one, or we haven't grabbed it yet. The only
1323 * exception is ->creds, through registered personalities, so handle
1324 * that one separately.
1325 */
1326 io_init_identity(id);
1327 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001328 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001329
1330 /* add one for this request */
1331 refcount_inc(&id->count);
1332
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001333 /* drop tctx and req identity references, if needed */
1334 if (tctx->identity != &tctx->__identity &&
1335 refcount_dec_and_test(&tctx->identity->count))
1336 kfree(tctx->identity);
1337 if (req->work.identity != &tctx->__identity &&
1338 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001339 kfree(req->work.identity);
1340
1341 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001342 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001343 return true;
1344}
1345
1346static bool io_grab_identity(struct io_kiocb *req)
1347{
1348 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001349 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001350 struct io_ring_ctx *ctx = req->ctx;
1351
Jens Axboe69228332020-10-20 14:28:41 -06001352 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1353 if (id->fsize != rlimit(RLIMIT_FSIZE))
1354 return false;
1355 req->work.flags |= IO_WQ_WORK_FSIZE;
1356 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001357#ifdef CONFIG_BLK_CGROUP
1358 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1359 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1360 rcu_read_lock();
1361 if (id->blkcg_css != blkcg_css()) {
1362 rcu_read_unlock();
1363 return false;
1364 }
1365 /*
1366 * This should be rare, either the cgroup is dying or the task
1367 * is moving cgroups. Just punt to root for the handful of ios.
1368 */
1369 if (css_tryget_online(id->blkcg_css))
1370 req->work.flags |= IO_WQ_WORK_BLKCG;
1371 rcu_read_unlock();
1372 }
1373#endif
1374 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1375 if (id->creds != current_cred())
1376 return false;
1377 get_cred(id->creds);
1378 req->work.flags |= IO_WQ_WORK_CREDS;
1379 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001380#ifdef CONFIG_AUDIT
1381 if (!uid_eq(current->loginuid, id->loginuid) ||
1382 current->sessionid != id->sessionid)
1383 return false;
1384#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001385 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1386 (def->work_flags & IO_WQ_WORK_FS)) {
1387 if (current->fs != id->fs)
1388 return false;
1389 spin_lock(&id->fs->lock);
1390 if (!id->fs->in_exec) {
1391 id->fs->users++;
1392 req->work.flags |= IO_WQ_WORK_FS;
1393 } else {
1394 req->work.flags |= IO_WQ_WORK_CANCEL;
1395 }
1396 spin_unlock(&current->fs->lock);
1397 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001398 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1399 (def->work_flags & IO_WQ_WORK_FILES) &&
1400 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1401 if (id->files != current->files ||
1402 id->nsproxy != current->nsproxy)
1403 return false;
1404 atomic_inc(&id->files->count);
1405 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001406
Jens Axboed16692a2021-02-09 04:47:41 +00001407 if (!(req->flags & REQ_F_INFLIGHT)) {
1408 req->flags |= REQ_F_INFLIGHT;
1409
1410 spin_lock_irq(&ctx->inflight_lock);
1411 list_add(&req->inflight_entry, &ctx->inflight_list);
1412 spin_unlock_irq(&ctx->inflight_lock);
1413 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001414 req->work.flags |= IO_WQ_WORK_FILES;
1415 }
Jens Axboe7247bc62020-12-29 10:50:46 -07001416 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1417 (def->work_flags & IO_WQ_WORK_MM)) {
1418 if (id->mm != current->mm)
1419 return false;
1420 mmgrab(id->mm);
1421 req->work.flags |= IO_WQ_WORK_MM;
1422 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001423
1424 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001425}
1426
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001427static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001428{
Jens Axboed3656342019-12-18 09:50:26 -07001429 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001430 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5c3462c2020-10-15 09:02:33 -06001431 struct io_identity *id;
Jens Axboe54a91f32019-09-10 09:15:04 -06001432
Pavel Begunkov16d59802020-07-12 16:16:47 +03001433 io_req_init_async(req);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001434 id = req->work.identity;
Pavel Begunkov16d59802020-07-12 16:16:47 +03001435
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001436 if (req->flags & REQ_F_FORCE_ASYNC)
1437 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1438
Jens Axboed3656342019-12-18 09:50:26 -07001439 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001440 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001441 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001442 } else {
1443 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001444 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001445 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001446
Jens Axboe1e6fa522020-10-15 08:46:24 -06001447 /* if we fail grabbing identity, we must COW, regrab, and retry */
1448 if (io_grab_identity(req))
1449 return;
1450
1451 if (!io_identity_cow(req))
1452 return;
1453
1454 /* can't fail at this point */
1455 if (!io_grab_identity(req))
1456 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001457}
1458
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001459static void io_prep_async_link(struct io_kiocb *req)
1460{
1461 struct io_kiocb *cur;
1462
1463 io_prep_async_work(req);
1464 if (req->flags & REQ_F_LINK_HEAD)
1465 list_for_each_entry(cur, &req->link_list, link_list)
1466 io_prep_async_work(cur);
1467}
1468
Jens Axboe7271ef32020-08-10 09:55:22 -06001469static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001470{
Jackie Liua197f662019-11-08 08:09:12 -07001471 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001472 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001473
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001474 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1475 &req->work, req->flags);
1476 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001477 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001478}
1479
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001480static void io_queue_async_work(struct io_kiocb *req)
1481{
Jens Axboe7271ef32020-08-10 09:55:22 -06001482 struct io_kiocb *link;
1483
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001484 /* init ->work of the whole link before punting */
1485 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001486 link = __io_queue_async_work(req);
1487
1488 if (link)
1489 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001490}
1491
Jens Axboe5262f562019-09-17 12:26:57 -06001492static void io_kill_timeout(struct io_kiocb *req)
1493{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001494 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001495 int ret;
1496
Jens Axboee8c2bc12020-08-15 18:44:09 -07001497 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001498 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001499 atomic_set(&req->ctx->cq_timeouts,
1500 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001501 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001502 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001503 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001504 }
1505}
1506
Jens Axboe76e1b642020-09-26 15:05:03 -06001507/*
1508 * Returns true if we found and killed one or more timeouts
1509 */
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00001510static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1511 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001512{
1513 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001514 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001515
1516 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001517 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00001518 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001519 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001520 canceled++;
1521 }
Jens Axboef3606e32020-09-22 08:18:24 -06001522 }
Jens Axboe5262f562019-09-17 12:26:57 -06001523 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001524 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001525}
1526
Pavel Begunkov04518942020-05-26 20:34:05 +03001527static void __io_queue_deferred(struct io_ring_ctx *ctx)
1528{
1529 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001530 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1531 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001532
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001533 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001534 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001535 list_del_init(&de->list);
Pavel Begunkovbc79ff02021-01-26 23:35:10 +00001536 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001537 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001538 } while (!list_empty(&ctx->defer_list));
1539}
1540
Pavel Begunkov360428f2020-05-30 14:54:17 +03001541static void io_flush_timeouts(struct io_ring_ctx *ctx)
1542{
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05001543 u32 seq;
1544
1545 if (list_empty(&ctx->timeout_list))
1546 return;
1547
1548 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1549
1550 do {
1551 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001552 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001553 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001554
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001555 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001556 break;
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05001557
1558 /*
1559 * Since seq can easily wrap around over time, subtract
1560 * the last seq at which timeouts were flushed before comparing.
1561 * Assuming not more than 2^31-1 events have happened since,
1562 * these subtractions won't have wrapped, so we can check if
1563 * target is in [last_seq, current_seq] by comparing the two.
1564 */
1565 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1566 events_got = seq - ctx->cq_last_tm_flush;
1567 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001568 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001569
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001570 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001571 io_kill_timeout(req);
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05001572 } while (!list_empty(&ctx->timeout_list));
1573
1574 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001575}
1576
Jens Axboede0617e2019-04-06 21:51:27 -06001577static void io_commit_cqring(struct io_ring_ctx *ctx)
1578{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001579 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001580 __io_commit_cqring(ctx);
1581
Pavel Begunkov04518942020-05-26 20:34:05 +03001582 if (unlikely(!list_empty(&ctx->defer_list)))
1583 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001584}
1585
Jens Axboe90554202020-09-03 12:12:41 -06001586static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1587{
1588 struct io_rings *r = ctx->rings;
1589
1590 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1591}
1592
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1594{
Hristo Venev75b28af2019-08-26 17:23:46 +00001595 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001596 unsigned tail;
1597
1598 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001599 /*
1600 * writes to the cq entry need to come after reading head; the
1601 * control dependency is enough as we're using WRITE_ONCE to
1602 * fill the cq entry
1603 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001604 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605 return NULL;
1606
1607 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001608 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001609}
1610
Jens Axboef2842ab2020-01-08 11:04:00 -07001611static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1612{
Jens Axboef0b493e2020-02-01 21:30:11 -07001613 if (!ctx->cq_ev_fd)
1614 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001615 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1616 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001617 if (!ctx->eventfd_async)
1618 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001619 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001620}
1621
Jens Axboeb41e9852020-02-17 09:52:41 -07001622static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001623{
Pavel Begunkov7bccd1c2021-01-26 11:17:09 +00001624 if (wq_has_sleeper(&ctx->cq_wait)) {
1625 wake_up_interruptible(&ctx->cq_wait);
1626 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1627 }
Jens Axboe8c838782019-03-12 15:48:16 -06001628 if (waitqueue_active(&ctx->wait))
1629 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001630 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1631 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001632 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001633 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001634}
1635
Pavel Begunkov46930142020-07-30 18:43:49 +03001636static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1637{
1638 if (list_empty(&ctx->cq_overflow_list)) {
1639 clear_bit(0, &ctx->sq_check_overflow);
1640 clear_bit(0, &ctx->cq_check_overflow);
1641 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1642 }
1643}
1644
Jens Axboec4a2ed72019-11-21 21:01:26 -07001645/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00001646static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1647 struct task_struct *tsk,
1648 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001650 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001651 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001652 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001653 unsigned long flags;
1654 LIST_HEAD(list);
1655
1656 if (!force) {
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001657 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1658 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001659 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001660 }
1661
1662 spin_lock_irqsave(&ctx->completion_lock, flags);
1663
Jens Axboec4a2ed72019-11-21 21:01:26 -07001664 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001665 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00001666 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001667 continue;
1668
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001669 cqe = io_get_cqring(ctx);
1670 if (!cqe && !force)
1671 break;
1672
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001673 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001674 if (cqe) {
1675 WRITE_ONCE(cqe->user_data, req->user_data);
1676 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001677 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001678 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001679 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001680 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001681 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001682 }
1683 }
1684
1685 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001686 io_cqring_mark_overflow(ctx);
1687
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001688 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1689 io_cqring_ev_posted(ctx);
1690
1691 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001692 req = list_first_entry(&list, struct io_kiocb, compl.list);
1693 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001694 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001695 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001696
1697 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001698}
1699
Pavel Begunkov85e25e22021-01-12 21:17:26 +00001700static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1701 struct task_struct *tsk,
1702 struct files_struct *files)
1703{
1704 if (test_bit(0, &ctx->cq_check_overflow)) {
1705 /* iopoll syncs against uring_lock, not completion_lock */
1706 if (ctx->flags & IORING_SETUP_IOPOLL)
1707 mutex_lock(&ctx->uring_lock);
1708 __io_cqring_overflow_flush(ctx, force, tsk, files);
1709 if (ctx->flags & IORING_SETUP_IOPOLL)
1710 mutex_unlock(&ctx->uring_lock);
1711 }
1712}
1713
Jens Axboebcda7ba2020-02-23 16:42:51 -07001714static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001716 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717 struct io_uring_cqe *cqe;
1718
Jens Axboe78e19bb2019-11-06 15:21:34 -07001719 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001720
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721 /*
1722 * If we can't get a cq entry, userspace overflowed the
1723 * submission (by quite a lot). Increment the overflow count in
1724 * the ring.
1725 */
1726 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001727 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001728 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001730 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001731 } else if (ctx->cq_overflow_flushed ||
1732 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001733 /*
1734 * If we're in ring overflow flush mode, or in task cancel mode,
1735 * then we cannot store the request for later flushing, we need
1736 * to drop it on the floor.
1737 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001738 ctx->cached_cq_overflow++;
1739 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001740 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001741 if (list_empty(&ctx->cq_overflow_list)) {
1742 set_bit(0, &ctx->sq_check_overflow);
1743 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001744 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001745 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001746 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001747 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001748 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001749 refcount_inc(&req->refs);
1750 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751 }
1752}
1753
Jens Axboebcda7ba2020-02-23 16:42:51 -07001754static void io_cqring_fill_event(struct io_kiocb *req, long res)
1755{
1756 __io_cqring_fill_event(req, res, 0);
1757}
1758
Jens Axboee1e16092020-06-22 09:17:17 -06001759static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001760{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001761 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762 unsigned long flags;
1763
1764 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001765 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766 io_commit_cqring(ctx);
1767 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1768
Jens Axboe8c838782019-03-12 15:48:16 -06001769 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001770}
1771
Jens Axboe229a7b62020-06-22 10:13:11 -06001772static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001773{
Jens Axboe229a7b62020-06-22 10:13:11 -06001774 struct io_ring_ctx *ctx = cs->ctx;
1775
1776 spin_lock_irq(&ctx->completion_lock);
1777 while (!list_empty(&cs->list)) {
1778 struct io_kiocb *req;
1779
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001780 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1781 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001782 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001783
1784 /*
1785 * io_free_req() doesn't care about completion_lock unless one
1786 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1787 * because of a potential deadlock with req->work.fs->lock
1788 */
1789 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1790 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001791 spin_unlock_irq(&ctx->completion_lock);
1792 io_put_req(req);
1793 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001794 } else {
1795 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001796 }
1797 }
1798 io_commit_cqring(ctx);
1799 spin_unlock_irq(&ctx->completion_lock);
1800
1801 io_cqring_ev_posted(ctx);
1802 cs->nr = 0;
1803}
1804
1805static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1806 struct io_comp_state *cs)
1807{
1808 if (!cs) {
1809 io_cqring_add_event(req, res, cflags);
1810 io_put_req(req);
1811 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001812 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001813 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001814 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001815 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001816 if (++cs->nr >= 32)
1817 io_submit_flush_completions(cs);
1818 }
Jens Axboee1e16092020-06-22 09:17:17 -06001819}
1820
1821static void io_req_complete(struct io_kiocb *req, long res)
1822{
Jens Axboe229a7b62020-06-22 10:13:11 -06001823 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001824}
1825
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001826static inline bool io_is_fallback_req(struct io_kiocb *req)
1827{
1828 return req == (struct io_kiocb *)
1829 ((unsigned long) req->ctx->fallback_req & ~1UL);
1830}
1831
1832static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1833{
1834 struct io_kiocb *req;
1835
1836 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001837 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001838 return req;
1839
1840 return NULL;
1841}
1842
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001843static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1844 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001846 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001847 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001848 size_t sz;
1849 int ret;
1850
1851 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001852 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1853
1854 /*
1855 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1856 * retry single alloc to be on the safe side.
1857 */
1858 if (unlikely(ret <= 0)) {
1859 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1860 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001861 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001862 ret = 1;
1863 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001864 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865 }
1866
Pavel Begunkov291b2822020-09-30 22:57:01 +03001867 state->free_reqs--;
1868 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001869fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001870 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001871}
1872
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001873static inline void io_put_file(struct io_kiocb *req, struct file *file,
1874 bool fixed)
1875{
1876 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001877 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001878 else
1879 fput(file);
1880}
1881
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001882static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001883{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001884 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001885
Jens Axboee8c2bc12020-08-15 18:44:09 -07001886 if (req->async_data)
1887 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001888 if (req->file)
1889 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001890
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001891 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001892}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001893
Pavel Begunkov216578e2020-10-13 09:44:00 +01001894static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001895{
Jens Axboe0f212202020-09-13 13:09:39 -06001896 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001897 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001898
Pavel Begunkov216578e2020-10-13 09:44:00 +01001899 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001900
Jens Axboed8a6df12020-10-15 16:24:45 -06001901 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06001902 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06001903 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001904 put_task_struct(req->task);
1905
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001906 if (likely(!io_is_fallback_req(req)))
1907 kmem_cache_free(req_cachep, req);
1908 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001909 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1910 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001911}
1912
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001913static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001914{
Jackie Liua197f662019-11-08 08:09:12 -07001915 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001916 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001917 bool cancelled = false;
1918 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001919
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001920 spin_lock_irqsave(&ctx->completion_lock, flags);
1921 link = list_first_entry_or_null(&req->link_list, struct io_kiocb,
1922 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01001923 /*
1924 * Can happen if a linked timeout fired and link had been like
1925 * req -> link t-out -> link t-out [-> ...]
1926 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001927 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1928 struct io_timeout_data *io = link->async_data;
1929 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001930
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001931 list_del_init(&link->link_list);
1932 ret = hrtimer_try_to_cancel(&io->timer);
1933 if (ret != -1) {
1934 io_cqring_fill_event(link, -ECANCELED);
1935 io_commit_cqring(ctx);
1936 cancelled = true;
1937 }
1938 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001939 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001940 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001941
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001942 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001943 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001944 io_put_req(link);
1945 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001946}
1947
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001948static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001949{
1950 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001951
Jens Axboe9e645e112019-05-10 16:07:28 -06001952 /*
1953 * The list should never be empty when we are called here. But could
1954 * potentially happen if the chain is messed up, check to be on the
1955 * safe side.
1956 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001957 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001958 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001959
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001960 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1961 list_del_init(&req->link_list);
1962 if (!list_empty(&nxt->link_list))
1963 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001964 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001965}
1966
1967/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001968 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001969 */
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001970static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001971{
Jens Axboe2665abf2019-11-05 12:40:47 -07001972 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001973 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001974
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001975 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001976 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001977 struct io_kiocb *link = list_first_entry(&req->link_list,
1978 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001979
Pavel Begunkov44932332019-12-05 16:16:35 +03001980 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001981 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001982
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001983 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001984
1985 /*
1986 * It's ok to free under spinlock as they're not linked anymore,
1987 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1988 * work.fs->lock.
1989 */
1990 if (link->flags & REQ_F_WORK_INITIALIZED)
1991 io_put_req_deferred(link, 2);
1992 else
1993 io_double_put_req(link);
Jens Axboe9e645e112019-05-10 16:07:28 -06001994 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001995
1996 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001997 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001998
Jens Axboe2665abf2019-11-05 12:40:47 -07001999 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002000}
2001
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002002static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002003{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03002004 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002005 if (req->flags & REQ_F_LINK_TIMEOUT)
2006 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002007
Jens Axboe9e645e112019-05-10 16:07:28 -06002008 /*
2009 * If LINK is set, we have dependent requests in this chain. If we
2010 * didn't fail this request, queue the first one up, moving any other
2011 * dependencies to the next request. In case of failure, fail the rest
2012 * of the chain.
2013 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002014 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
2015 return io_req_link_next(req);
2016 io_fail_links(req);
2017 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002018}
Jens Axboe2665abf2019-11-05 12:40:47 -07002019
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002020static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
2021{
2022 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
2023 return NULL;
2024 return __io_req_find_next(req);
2025}
2026
Jens Axboe87c43112020-09-30 21:00:14 -06002027static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002028{
2029 struct task_struct *tsk = req->task;
2030 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002031 enum task_work_notify_mode notify;
2032 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002033
Jens Axboe6200b0a2020-09-13 14:38:30 -06002034 if (tsk->flags & PF_EXITING)
2035 return -ESRCH;
2036
Jens Axboec2c4c832020-07-01 15:37:11 -06002037 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002038 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2039 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2040 * processing task_work. There's no reliable way to tell if TWA_RESUME
2041 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002042 */
Jens Axboe91989c72020-10-16 09:02:26 -06002043 notify = TWA_NONE;
Jens Axboefd7d6de2020-08-23 11:00:37 -06002044 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002045 notify = TWA_SIGNAL;
2046
Jens Axboe87c43112020-09-30 21:00:14 -06002047 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002048 if (!ret)
2049 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002050
Jens Axboec2c4c832020-07-01 15:37:11 -06002051 return ret;
2052}
2053
Jens Axboec40f6372020-06-25 15:39:59 -06002054static void __io_req_task_cancel(struct io_kiocb *req, int error)
2055{
2056 struct io_ring_ctx *ctx = req->ctx;
2057
2058 spin_lock_irq(&ctx->completion_lock);
2059 io_cqring_fill_event(req, error);
2060 io_commit_cqring(ctx);
2061 spin_unlock_irq(&ctx->completion_lock);
2062
2063 io_cqring_ev_posted(ctx);
2064 req_set_fail_links(req);
2065 io_double_put_req(req);
2066}
2067
2068static void io_req_task_cancel(struct callback_head *cb)
2069{
2070 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002071 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002072
2073 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002074 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002075}
2076
2077static void __io_req_task_submit(struct io_kiocb *req)
2078{
2079 struct io_ring_ctx *ctx = req->ctx;
2080
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002081 mutex_lock(&ctx->uring_lock);
Pavel Begunkova63d9152021-01-26 11:17:03 +00002082 if (!ctx->sqo_dead && !__io_sq_thread_acquire_mm(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002083 __io_queue_sqe(req, NULL);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002084 else
Jens Axboec40f6372020-06-25 15:39:59 -06002085 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002086 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov5592eae2021-02-09 04:47:50 +00002087
2088 if (ctx->flags & IORING_SETUP_SQPOLL)
2089 io_sq_thread_drop_mm();
Jens Axboe9e645e112019-05-10 16:07:28 -06002090}
2091
Jens Axboec40f6372020-06-25 15:39:59 -06002092static void io_req_task_submit(struct callback_head *cb)
2093{
2094 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002095 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002096
2097 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002098 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002099}
2100
2101static void io_req_task_queue(struct io_kiocb *req)
2102{
Jens Axboec40f6372020-06-25 15:39:59 -06002103 int ret;
2104
2105 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002106 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002107
Jens Axboe87c43112020-09-30 21:00:14 -06002108 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06002109 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002110 struct task_struct *tsk;
2111
Jens Axboec40f6372020-06-25 15:39:59 -06002112 init_task_work(&req->task_work, io_req_task_cancel);
2113 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002114 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002115 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002116 }
Jens Axboec40f6372020-06-25 15:39:59 -06002117}
2118
Pavel Begunkovc3524382020-06-28 12:52:32 +03002119static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002120{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002121 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002122
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002123 if (nxt)
2124 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002125}
2126
Jens Axboe9e645e112019-05-10 16:07:28 -06002127static void io_free_req(struct io_kiocb *req)
2128{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002129 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002130 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002131}
2132
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002133struct req_batch {
2134 void *reqs[IO_IOPOLL_BATCH];
2135 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002136
2137 struct task_struct *task;
2138 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002139};
2140
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002141static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002142{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002143 rb->to_free = 0;
2144 rb->task_refs = 0;
2145 rb->task = NULL;
2146}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002147
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002148static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2149 struct req_batch *rb)
2150{
2151 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2152 percpu_ref_put_many(&ctx->refs, rb->to_free);
2153 rb->to_free = 0;
2154}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002155
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002156static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2157 struct req_batch *rb)
2158{
2159 if (rb->to_free)
2160 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002161 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002162 struct io_uring_task *tctx = rb->task->io_uring;
2163
2164 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboeca758722021-01-16 11:52:11 -07002165 if (atomic_read(&tctx->in_idle))
2166 wake_up(&tctx->wait);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002167 put_task_struct_many(rb->task, rb->task_refs);
2168 rb->task = NULL;
2169 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002170}
2171
2172static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2173{
2174 if (unlikely(io_is_fallback_req(req))) {
2175 io_free_req(req);
2176 return;
2177 }
2178 if (req->flags & REQ_F_LINK_HEAD)
2179 io_queue_next(req);
2180
Jens Axboee3bc8e92020-09-24 08:45:57 -06002181 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002182 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002183 struct io_uring_task *tctx = rb->task->io_uring;
2184
2185 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboeca758722021-01-16 11:52:11 -07002186 if (atomic_read(&tctx->in_idle))
2187 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002188 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002189 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002190 rb->task = req->task;
2191 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002192 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002193 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002194
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002195 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002196 rb->reqs[rb->to_free++] = req;
2197 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2198 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002199}
2200
Jens Axboeba816ad2019-09-28 11:36:45 -06002201/*
2202 * Drop reference to request, return next in chain (if there is one) if this
2203 * was the last reference to this request.
2204 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002205static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002206{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002207 struct io_kiocb *nxt = NULL;
2208
Jens Axboe2a44f462020-02-25 13:25:41 -07002209 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002210 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002211 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002212 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002213 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002214}
2215
Jens Axboe2b188cc2019-01-07 10:46:33 -07002216static void io_put_req(struct io_kiocb *req)
2217{
Jens Axboedef596e2019-01-09 08:59:42 -07002218 if (refcount_dec_and_test(&req->refs))
2219 io_free_req(req);
2220}
2221
Pavel Begunkov216578e2020-10-13 09:44:00 +01002222static void io_put_req_deferred_cb(struct callback_head *cb)
2223{
2224 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2225
2226 io_free_req(req);
2227}
2228
2229static void io_free_req_deferred(struct io_kiocb *req)
2230{
2231 int ret;
2232
2233 init_task_work(&req->task_work, io_put_req_deferred_cb);
2234 ret = io_req_task_work_add(req, true);
2235 if (unlikely(ret)) {
2236 struct task_struct *tsk;
2237
2238 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002239 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002240 wake_up_process(tsk);
2241 }
2242}
2243
2244static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2245{
2246 if (refcount_sub_and_test(refs, &req->refs))
2247 io_free_req_deferred(req);
2248}
2249
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002250static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002251{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002252 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002253
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002254 /*
2255 * A ref is owned by io-wq in which context we're. So, if that's the
2256 * last one, it's safe to steal next work. False negatives are Ok,
2257 * it just will be re-punted async in io_put_work()
2258 */
2259 if (refcount_read(&req->refs) != 1)
2260 return NULL;
2261
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002262 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002263 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002264}
2265
Jens Axboe978db572019-11-14 22:39:04 -07002266static void io_double_put_req(struct io_kiocb *req)
2267{
2268 /* drop both submit and complete references */
2269 if (refcount_sub_and_test(2, &req->refs))
2270 io_free_req(req);
2271}
2272
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002273static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002274{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002275 struct io_rings *rings = ctx->rings;
2276
Jens Axboea3a0e432019-08-20 11:03:11 -06002277 /* See comment at the top of this file */
2278 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002279 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002280}
2281
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002282static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2283{
2284 struct io_rings *rings = ctx->rings;
2285
2286 /* make sure SQ entry isn't read before tail */
2287 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2288}
2289
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002290static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002291{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002292 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002293
Jens Axboebcda7ba2020-02-23 16:42:51 -07002294 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2295 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002296 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002297 kfree(kbuf);
2298 return cflags;
2299}
2300
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002301static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2302{
2303 struct io_buffer *kbuf;
2304
2305 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2306 return io_put_kbuf(req, kbuf);
2307}
2308
Jens Axboe4c6e2772020-07-01 11:29:10 -06002309static inline bool io_run_task_work(void)
2310{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002311 /*
2312 * Not safe to run on exiting task, and the task_work handling will
2313 * not add work to such a task.
2314 */
2315 if (unlikely(current->flags & PF_EXITING))
2316 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002317 if (current->task_works) {
2318 __set_current_state(TASK_RUNNING);
2319 task_work_run();
2320 return true;
2321 }
2322
2323 return false;
2324}
2325
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002326static void io_iopoll_queue(struct list_head *again)
2327{
2328 struct io_kiocb *req;
2329
2330 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002331 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2332 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002333 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002334 } while (!list_empty(again));
2335}
2336
Jens Axboedef596e2019-01-09 08:59:42 -07002337/*
2338 * Find and free completed poll iocbs
2339 */
2340static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2341 struct list_head *done)
2342{
Jens Axboe8237e042019-12-28 10:48:22 -07002343 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002344 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002345 LIST_HEAD(again);
2346
2347 /* order with ->result store in io_complete_rw_iopoll() */
2348 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002349
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002350 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002351 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002352 int cflags = 0;
2353
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002354 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002355 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002356 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002357 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002358 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002359 continue;
2360 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002361 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002362
Jens Axboebcda7ba2020-02-23 16:42:51 -07002363 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002364 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002365
2366 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002367 (*nr_events)++;
2368
Pavel Begunkovc3524382020-06-28 12:52:32 +03002369 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002370 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002371 }
Jens Axboedef596e2019-01-09 08:59:42 -07002372
Jens Axboe09bb8392019-03-13 12:39:28 -06002373 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002374 if (ctx->flags & IORING_SETUP_SQPOLL)
2375 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002376 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002377
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002378 if (!list_empty(&again))
2379 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002380}
2381
Jens Axboedef596e2019-01-09 08:59:42 -07002382static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2383 long min)
2384{
2385 struct io_kiocb *req, *tmp;
2386 LIST_HEAD(done);
2387 bool spin;
2388 int ret;
2389
2390 /*
2391 * Only spin for completions if we don't have multiple devices hanging
2392 * off our complete list, and we're under the requested amount.
2393 */
2394 spin = !ctx->poll_multi_file && *nr_events < min;
2395
2396 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002397 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002398 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002399
2400 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002401 * Move completed and retryable entries to our local lists.
2402 * If we find a request that requires polling, break out
2403 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002404 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002405 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002406 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002407 continue;
2408 }
2409 if (!list_empty(&done))
2410 break;
2411
2412 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2413 if (ret < 0)
2414 break;
2415
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002416 /* iopoll may have completed current req */
2417 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002418 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002419
Jens Axboedef596e2019-01-09 08:59:42 -07002420 if (ret && spin)
2421 spin = false;
2422 ret = 0;
2423 }
2424
2425 if (!list_empty(&done))
2426 io_iopoll_complete(ctx, nr_events, &done);
2427
2428 return ret;
2429}
2430
2431/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002432 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002433 * non-spinning poll check - we'll still enter the driver poll loop, but only
2434 * as a non-spinning completion check.
2435 */
2436static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2437 long min)
2438{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002439 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002440 int ret;
2441
2442 ret = io_do_iopoll(ctx, nr_events, min);
2443 if (ret < 0)
2444 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002445 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002446 return 0;
2447 }
2448
2449 return 1;
2450}
2451
2452/*
2453 * We can't just wait for polled events to come to us, we have to actively
2454 * find and complete them.
2455 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002456static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002457{
2458 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2459 return;
2460
2461 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002462 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002463 unsigned int nr_events = 0;
2464
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002465 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002466
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002467 /* let it sleep and repeat later if can't complete a request */
2468 if (nr_events == 0)
2469 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002470 /*
2471 * Ensure we allow local-to-the-cpu processing to take place,
2472 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002473 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002474 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002475 if (need_resched()) {
2476 mutex_unlock(&ctx->uring_lock);
2477 cond_resched();
2478 mutex_lock(&ctx->uring_lock);
2479 }
Jens Axboedef596e2019-01-09 08:59:42 -07002480 }
2481 mutex_unlock(&ctx->uring_lock);
2482}
2483
Pavel Begunkov7668b922020-07-07 16:36:21 +03002484static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002485{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002486 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002487 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002488
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002489 /*
2490 * We disallow the app entering submit/complete with polling, but we
2491 * still need to lock the ring to prevent racing with polled issue
2492 * that got punted to a workqueue.
2493 */
2494 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002495 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002496 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002497 * Don't enter poll loop if we already have events pending.
2498 * If we do, we can potentially be spinning for commands that
2499 * already triggered a CQE (eg in error).
2500 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002501 if (test_bit(0, &ctx->cq_check_overflow))
2502 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2503 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002504 break;
2505
2506 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002507 * If a submit got punted to a workqueue, we can have the
2508 * application entering polling for a command before it gets
2509 * issued. That app will hold the uring_lock for the duration
2510 * of the poll right here, so we need to take a breather every
2511 * now and then to ensure that the issue has a chance to add
2512 * the poll to the issued list. Otherwise we can spin here
2513 * forever, while the workqueue is stuck trying to acquire the
2514 * very same mutex.
2515 */
2516 if (!(++iters & 7)) {
2517 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002518 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002519 mutex_lock(&ctx->uring_lock);
2520 }
2521
Pavel Begunkov7668b922020-07-07 16:36:21 +03002522 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002523 if (ret <= 0)
2524 break;
2525 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002526 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002527
Jens Axboe500f9fb2019-08-19 12:15:59 -06002528 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002529 return ret;
2530}
2531
Jens Axboe491381ce2019-10-17 09:20:46 -06002532static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002533{
Jens Axboe491381ce2019-10-17 09:20:46 -06002534 /*
2535 * Tell lockdep we inherited freeze protection from submission
2536 * thread.
2537 */
2538 if (req->flags & REQ_F_ISREG) {
2539 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540
Jens Axboe491381ce2019-10-17 09:20:46 -06002541 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002542 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002543 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544}
2545
Jens Axboea1d7c392020-06-22 11:09:46 -06002546static void io_complete_rw_common(struct kiocb *kiocb, long res,
2547 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002548{
Jens Axboe9adbd452019-12-20 08:45:55 -07002549 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002550 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002551
Jens Axboe491381ce2019-10-17 09:20:46 -06002552 if (kiocb->ki_flags & IOCB_WRITE)
2553 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002555 if (res != req->result)
2556 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002557 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002558 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002559 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002560}
2561
Jens Axboeb63534c2020-06-04 11:28:00 -06002562#ifdef CONFIG_BLOCK
2563static bool io_resubmit_prep(struct io_kiocb *req, int error)
2564{
2565 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2566 ssize_t ret = -ECANCELED;
2567 struct iov_iter iter;
2568 int rw;
2569
2570 if (error) {
2571 ret = error;
2572 goto end_req;
2573 }
2574
2575 switch (req->opcode) {
2576 case IORING_OP_READV:
2577 case IORING_OP_READ_FIXED:
2578 case IORING_OP_READ:
2579 rw = READ;
2580 break;
2581 case IORING_OP_WRITEV:
2582 case IORING_OP_WRITE_FIXED:
2583 case IORING_OP_WRITE:
2584 rw = WRITE;
2585 break;
2586 default:
2587 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2588 req->opcode);
2589 goto end_req;
2590 }
2591
Jens Axboee8c2bc12020-08-15 18:44:09 -07002592 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002593 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2594 if (ret < 0)
2595 goto end_req;
2596 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2597 if (!ret)
2598 return true;
2599 kfree(iovec);
2600 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002601 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002602 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002603end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002604 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002605 return false;
2606}
Jens Axboeb63534c2020-06-04 11:28:00 -06002607#endif
2608
2609static bool io_rw_reissue(struct io_kiocb *req, long res)
2610{
2611#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002612 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002613 int ret;
2614
Jens Axboe355afae2020-09-02 09:30:31 -06002615 if (!S_ISBLK(mode) && !S_ISREG(mode))
2616 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002617 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2618 return false;
2619
Jens Axboefdee9462020-08-27 16:46:24 -06002620 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002621
Jens Axboefdee9462020-08-27 16:46:24 -06002622 if (io_resubmit_prep(req, ret)) {
2623 refcount_inc(&req->refs);
2624 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002625 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002626 }
2627
Jens Axboeb63534c2020-06-04 11:28:00 -06002628#endif
2629 return false;
2630}
2631
Jens Axboea1d7c392020-06-22 11:09:46 -06002632static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2633 struct io_comp_state *cs)
2634{
2635 if (!io_rw_reissue(req, res))
2636 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002637}
2638
2639static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2640{
Jens Axboe9adbd452019-12-20 08:45:55 -07002641 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002642
Jens Axboea1d7c392020-06-22 11:09:46 -06002643 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644}
2645
Jens Axboedef596e2019-01-09 08:59:42 -07002646static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2647{
Jens Axboe9adbd452019-12-20 08:45:55 -07002648 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002649
Jens Axboe491381ce2019-10-17 09:20:46 -06002650 if (kiocb->ki_flags & IOCB_WRITE)
2651 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002652
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002653 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002654 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002655
2656 WRITE_ONCE(req->result, res);
2657 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002658 smp_wmb();
2659 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002660}
2661
2662/*
2663 * After the iocb has been issued, it's safe to be found on the poll list.
2664 * Adding the kiocb to the list AFTER submission ensures that we don't
2665 * find it from a io_iopoll_getevents() thread before the issuer is done
2666 * accessing the kiocb cookie.
2667 */
2668static void io_iopoll_req_issued(struct io_kiocb *req)
2669{
2670 struct io_ring_ctx *ctx = req->ctx;
2671
2672 /*
2673 * Track whether we have multiple files in our lists. This will impact
2674 * how we do polling eventually, not spinning if we're on potentially
2675 * different devices.
2676 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002677 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002678 ctx->poll_multi_file = false;
2679 } else if (!ctx->poll_multi_file) {
2680 struct io_kiocb *list_req;
2681
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002682 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002683 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002684 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002685 ctx->poll_multi_file = true;
2686 }
2687
2688 /*
2689 * For fast devices, IO may have already completed. If it has, add
2690 * it to the front so we find it first.
2691 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002692 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002693 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002694 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002695 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002696
2697 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002698 wq_has_sleeper(&ctx->sq_data->wait))
2699 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002700}
2701
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002702static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002703{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002704 if (state->has_refs)
2705 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002706 state->file = NULL;
2707}
2708
2709static inline void io_state_file_put(struct io_submit_state *state)
2710{
2711 if (state->file)
2712 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002713}
2714
2715/*
2716 * Get as many references to a file as we have IOs left in this submission,
2717 * assuming most submissions are for one file, or at least that each file
2718 * has more than one submission.
2719 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002720static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002721{
2722 if (!state)
2723 return fget(fd);
2724
2725 if (state->file) {
2726 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002727 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002728 return state->file;
2729 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002730 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002731 }
2732 state->file = fget_many(fd, state->ios_left);
2733 if (!state->file)
2734 return NULL;
2735
2736 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002737 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002738 return state->file;
2739}
2740
Jens Axboe4503b762020-06-01 10:00:27 -06002741static bool io_bdev_nowait(struct block_device *bdev)
2742{
2743#ifdef CONFIG_BLOCK
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002744 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002745#else
2746 return true;
2747#endif
2748}
2749
Jens Axboe2b188cc2019-01-07 10:46:33 -07002750/*
2751 * If we tracked the file through the SCM inflight mechanism, we could support
2752 * any file. For now, just ensure that anything potentially problematic is done
2753 * inline.
2754 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002755static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002756{
2757 umode_t mode = file_inode(file)->i_mode;
2758
Jens Axboe4503b762020-06-01 10:00:27 -06002759 if (S_ISBLK(mode)) {
2760 if (io_bdev_nowait(file->f_inode->i_bdev))
2761 return true;
2762 return false;
2763 }
2764 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002765 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002766 if (S_ISREG(mode)) {
2767 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2768 file->f_op != &io_uring_fops)
2769 return true;
2770 return false;
2771 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002772
Jens Axboec5b85622020-06-09 19:23:05 -06002773 /* any ->read/write should understand O_NONBLOCK */
2774 if (file->f_flags & O_NONBLOCK)
2775 return true;
2776
Jens Axboeaf197f52020-04-28 13:15:06 -06002777 if (!(file->f_mode & FMODE_NOWAIT))
2778 return false;
2779
2780 if (rw == READ)
2781 return file->f_op->read_iter != NULL;
2782
2783 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784}
2785
Pavel Begunkova88fc402020-09-30 22:57:53 +03002786static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002787{
Jens Axboedef596e2019-01-09 08:59:42 -07002788 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002789 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002790 unsigned ioprio;
2791 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792
Jens Axboe491381ce2019-10-17 09:20:46 -06002793 if (S_ISREG(file_inode(req->file)->i_mode))
2794 req->flags |= REQ_F_ISREG;
2795
Jens Axboe2b188cc2019-01-07 10:46:33 -07002796 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002797 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2798 req->flags |= REQ_F_CUR_POS;
2799 kiocb->ki_pos = req->file->f_pos;
2800 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002802 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2803 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2804 if (unlikely(ret))
2805 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806
2807 ioprio = READ_ONCE(sqe->ioprio);
2808 if (ioprio) {
2809 ret = ioprio_check_cap(ioprio);
2810 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002811 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002812
2813 kiocb->ki_ioprio = ioprio;
2814 } else
2815 kiocb->ki_ioprio = get_current_ioprio();
2816
Stefan Bühler8449eed2019-04-27 20:34:19 +02002817 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002818 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002819 req->flags |= REQ_F_NOWAIT;
2820
Jens Axboedef596e2019-01-09 08:59:42 -07002821 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002822 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2823 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002824 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002825
Jens Axboedef596e2019-01-09 08:59:42 -07002826 kiocb->ki_flags |= IOCB_HIPRI;
2827 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002828 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002829 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002830 if (kiocb->ki_flags & IOCB_HIPRI)
2831 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002832 kiocb->ki_complete = io_complete_rw;
2833 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002834
Jens Axboe3529d8c2019-12-19 18:24:38 -07002835 req->rw.addr = READ_ONCE(sqe->addr);
2836 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002837 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002838 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002839}
2840
2841static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2842{
2843 switch (ret) {
2844 case -EIOCBQUEUED:
2845 break;
2846 case -ERESTARTSYS:
2847 case -ERESTARTNOINTR:
2848 case -ERESTARTNOHAND:
2849 case -ERESTART_RESTARTBLOCK:
2850 /*
2851 * We can't just restart the syscall, since previously
2852 * submitted sqes may already be in progress. Just fail this
2853 * IO with EINTR.
2854 */
2855 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002856 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857 default:
2858 kiocb->ki_complete(kiocb, ret, 0);
2859 }
2860}
2861
Jens Axboea1d7c392020-06-22 11:09:46 -06002862static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2863 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002864{
Jens Axboeba042912019-12-25 16:33:42 -07002865 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002866 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002867
Jens Axboe227c0c92020-08-13 11:51:40 -06002868 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002869 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002870 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002871 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002872 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002873 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002874 }
2875
Jens Axboeba042912019-12-25 16:33:42 -07002876 if (req->flags & REQ_F_CUR_POS)
2877 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002878 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002879 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002880 else
2881 io_rw_done(kiocb, ret);
2882}
2883
Jens Axboe9adbd452019-12-20 08:45:55 -07002884static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002885 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002886{
Jens Axboe9adbd452019-12-20 08:45:55 -07002887 struct io_ring_ctx *ctx = req->ctx;
2888 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002889 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002890 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002891 size_t offset;
2892 u64 buf_addr;
2893
Jens Axboeedafcce2019-01-09 09:16:05 -07002894 if (unlikely(buf_index >= ctx->nr_user_bufs))
2895 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002896 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2897 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002898 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002899
2900 /* overflow */
2901 if (buf_addr + len < buf_addr)
2902 return -EFAULT;
2903 /* not inside the mapped region */
2904 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2905 return -EFAULT;
2906
2907 /*
2908 * May not be a start of buffer, set size appropriately
2909 * and advance us to the beginning.
2910 */
2911 offset = buf_addr - imu->ubuf;
2912 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002913
2914 if (offset) {
2915 /*
2916 * Don't use iov_iter_advance() here, as it's really slow for
2917 * using the latter parts of a big fixed buffer - it iterates
2918 * over each segment manually. We can cheat a bit here, because
2919 * we know that:
2920 *
2921 * 1) it's a BVEC iter, we set it up
2922 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2923 * first and last bvec
2924 *
2925 * So just find our index, and adjust the iterator afterwards.
2926 * If the offset is within the first bvec (or the whole first
2927 * bvec, just use iov_iter_advance(). This makes it easier
2928 * since we can just skip the first segment, which may not
2929 * be PAGE_SIZE aligned.
2930 */
2931 const struct bio_vec *bvec = imu->bvec;
2932
2933 if (offset <= bvec->bv_len) {
2934 iov_iter_advance(iter, offset);
2935 } else {
2936 unsigned long seg_skip;
2937
2938 /* skip first vec */
2939 offset -= bvec->bv_len;
2940 seg_skip = 1 + (offset >> PAGE_SHIFT);
2941
2942 iter->bvec = bvec + seg_skip;
2943 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002944 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002945 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002946 }
2947 }
2948
Jens Axboe5e559562019-11-13 16:12:46 -07002949 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002950}
2951
Jens Axboebcda7ba2020-02-23 16:42:51 -07002952static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2953{
2954 if (needs_lock)
2955 mutex_unlock(&ctx->uring_lock);
2956}
2957
2958static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2959{
2960 /*
2961 * "Normal" inline submissions always hold the uring_lock, since we
2962 * grab it from the system call. Same is true for the SQPOLL offload.
2963 * The only exception is when we've detached the request and issue it
2964 * from an async worker thread, grab the lock for that case.
2965 */
2966 if (needs_lock)
2967 mutex_lock(&ctx->uring_lock);
2968}
2969
2970static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2971 int bgid, struct io_buffer *kbuf,
2972 bool needs_lock)
2973{
2974 struct io_buffer *head;
2975
2976 if (req->flags & REQ_F_BUFFER_SELECTED)
2977 return kbuf;
2978
2979 io_ring_submit_lock(req->ctx, needs_lock);
2980
2981 lockdep_assert_held(&req->ctx->uring_lock);
2982
2983 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2984 if (head) {
2985 if (!list_empty(&head->list)) {
2986 kbuf = list_last_entry(&head->list, struct io_buffer,
2987 list);
2988 list_del(&kbuf->list);
2989 } else {
2990 kbuf = head;
2991 idr_remove(&req->ctx->io_buffer_idr, bgid);
2992 }
2993 if (*len > kbuf->len)
2994 *len = kbuf->len;
2995 } else {
2996 kbuf = ERR_PTR(-ENOBUFS);
2997 }
2998
2999 io_ring_submit_unlock(req->ctx, needs_lock);
3000
3001 return kbuf;
3002}
3003
Jens Axboe4d954c22020-02-27 07:31:19 -07003004static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3005 bool needs_lock)
3006{
3007 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003008 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003009
3010 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003011 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003012 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3013 if (IS_ERR(kbuf))
3014 return kbuf;
3015 req->rw.addr = (u64) (unsigned long) kbuf;
3016 req->flags |= REQ_F_BUFFER_SELECTED;
3017 return u64_to_user_ptr(kbuf->addr);
3018}
3019
3020#ifdef CONFIG_COMPAT
3021static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3022 bool needs_lock)
3023{
3024 struct compat_iovec __user *uiov;
3025 compat_ssize_t clen;
3026 void __user *buf;
3027 ssize_t len;
3028
3029 uiov = u64_to_user_ptr(req->rw.addr);
3030 if (!access_ok(uiov, sizeof(*uiov)))
3031 return -EFAULT;
3032 if (__get_user(clen, &uiov->iov_len))
3033 return -EFAULT;
3034 if (clen < 0)
3035 return -EINVAL;
3036
3037 len = clen;
3038 buf = io_rw_buffer_select(req, &len, needs_lock);
3039 if (IS_ERR(buf))
3040 return PTR_ERR(buf);
3041 iov[0].iov_base = buf;
3042 iov[0].iov_len = (compat_size_t) len;
3043 return 0;
3044}
3045#endif
3046
3047static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3048 bool needs_lock)
3049{
3050 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3051 void __user *buf;
3052 ssize_t len;
3053
3054 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3055 return -EFAULT;
3056
3057 len = iov[0].iov_len;
3058 if (len < 0)
3059 return -EINVAL;
3060 buf = io_rw_buffer_select(req, &len, needs_lock);
3061 if (IS_ERR(buf))
3062 return PTR_ERR(buf);
3063 iov[0].iov_base = buf;
3064 iov[0].iov_len = len;
3065 return 0;
3066}
3067
3068static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3069 bool needs_lock)
3070{
Jens Axboedddb3e22020-06-04 11:27:01 -06003071 if (req->flags & REQ_F_BUFFER_SELECTED) {
3072 struct io_buffer *kbuf;
3073
3074 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3075 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3076 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003077 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003078 }
Pavel Begunkov72a016d2020-12-19 03:15:43 +00003079 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003080 return -EINVAL;
3081
3082#ifdef CONFIG_COMPAT
3083 if (req->ctx->compat)
3084 return io_compat_import(req, iov, needs_lock);
3085#endif
3086
3087 return __io_iov_buffer_select(req, iov, needs_lock);
3088}
3089
Jens Axboe8452fd02020-08-18 13:58:33 -07003090static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
3091 struct iovec **iovec, struct iov_iter *iter,
3092 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003093{
Jens Axboe9adbd452019-12-20 08:45:55 -07003094 void __user *buf = u64_to_user_ptr(req->rw.addr);
3095 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003096 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003097 u8 opcode;
3098
Jens Axboed625c6e2019-12-17 19:53:05 -07003099 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003100 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003101 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003102 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003103 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003104
Jens Axboebcda7ba2020-02-23 16:42:51 -07003105 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003106 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003107 return -EINVAL;
3108
Jens Axboe3a6820f2019-12-22 15:19:35 -07003109 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003110 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003111 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003112 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003113 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003114 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003115 }
3116
Jens Axboe3a6820f2019-12-22 15:19:35 -07003117 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3118 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07003119 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003120 }
3121
Jens Axboe4d954c22020-02-27 07:31:19 -07003122 if (req->flags & REQ_F_BUFFER_SELECT) {
3123 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003124 if (!ret) {
3125 ret = (*iovec)->iov_len;
3126 iov_iter_init(iter, rw, *iovec, 1, ret);
3127 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003128 *iovec = NULL;
3129 return ret;
3130 }
3131
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003132 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3133 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134}
3135
Jens Axboe8452fd02020-08-18 13:58:33 -07003136static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3137 struct iovec **iovec, struct iov_iter *iter,
3138 bool needs_lock)
3139{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003140 struct io_async_rw *iorw = req->async_data;
3141
3142 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003143 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3144 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003145 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07003146}
3147
Jens Axboe0fef9482020-08-26 10:36:20 -06003148static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3149{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003150 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003151}
3152
Jens Axboe32960612019-09-23 11:05:34 -06003153/*
3154 * For files that don't have ->read_iter() and ->write_iter(), handle them
3155 * by looping over ->read() or ->write() manually.
3156 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003157static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003158{
Jens Axboe4017eb92020-10-22 14:14:12 -06003159 struct kiocb *kiocb = &req->rw.kiocb;
3160 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003161 ssize_t ret = 0;
3162
3163 /*
3164 * Don't support polled IO through this interface, and we can't
3165 * support non-blocking either. For the latter, this just causes
3166 * the kiocb to be handled from an async context.
3167 */
3168 if (kiocb->ki_flags & IOCB_HIPRI)
3169 return -EOPNOTSUPP;
3170 if (kiocb->ki_flags & IOCB_NOWAIT)
3171 return -EAGAIN;
3172
3173 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003174 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003175 ssize_t nr;
3176
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003177 if (!iov_iter_is_bvec(iter)) {
3178 iovec = iov_iter_iovec(iter);
3179 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003180 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3181 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003182 }
3183
Jens Axboe32960612019-09-23 11:05:34 -06003184 if (rw == READ) {
3185 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003186 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003187 } else {
3188 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003189 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003190 }
3191
3192 if (nr < 0) {
3193 if (!ret)
3194 ret = nr;
3195 break;
3196 }
3197 ret += nr;
3198 if (nr != iovec.iov_len)
3199 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003200 req->rw.len -= nr;
3201 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003202 iov_iter_advance(iter, nr);
3203 }
3204
3205 return ret;
3206}
3207
Jens Axboeff6165b2020-08-13 09:47:43 -06003208static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3209 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003210{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003211 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003212
Jens Axboeff6165b2020-08-13 09:47:43 -06003213 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003214 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003215 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003216 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003217 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003218 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003219 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003220 unsigned iov_off = 0;
3221
3222 rw->iter.iov = rw->fast_iov;
3223 if (iter->iov != fast_iov) {
3224 iov_off = iter->iov - fast_iov;
3225 rw->iter.iov += iov_off;
3226 }
3227 if (rw->fast_iov != fast_iov)
3228 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003229 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003230 } else {
3231 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003232 }
3233}
3234
Jens Axboee8c2bc12020-08-15 18:44:09 -07003235static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003236{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003237 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3238 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3239 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003240}
3241
Jens Axboee8c2bc12020-08-15 18:44:09 -07003242static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003243{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003244 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003245 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003246
Jens Axboee8c2bc12020-08-15 18:44:09 -07003247 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003248}
3249
Jens Axboeff6165b2020-08-13 09:47:43 -06003250static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3251 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003252 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003253{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003254 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003255 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003256 if (!req->async_data) {
3257 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003258 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003259
Jens Axboeff6165b2020-08-13 09:47:43 -06003260 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003261 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003262 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003263}
3264
Pavel Begunkov73debe62020-09-30 22:57:54 +03003265static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003266{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003267 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003268 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003269 ssize_t ret;
3270
Pavel Begunkov73debe62020-09-30 22:57:54 +03003271 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003272 if (unlikely(ret < 0))
3273 return ret;
3274
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003275 iorw->bytes_done = 0;
3276 iorw->free_iovec = iov;
3277 if (iov)
3278 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003279 return 0;
3280}
3281
Pavel Begunkov73debe62020-09-30 22:57:54 +03003282static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003283{
3284 ssize_t ret;
3285
Pavel Begunkova88fc402020-09-30 22:57:53 +03003286 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003287 if (ret)
3288 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003289
Jens Axboe3529d8c2019-12-19 18:24:38 -07003290 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3291 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003292
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003293 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003294 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003295 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003296 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003297}
3298
Jens Axboec1dd91d2020-08-03 16:43:59 -06003299/*
3300 * This is our waitqueue callback handler, registered through lock_page_async()
3301 * when we initially tried to do the IO with the iocb armed our waitqueue.
3302 * This gets called when the page is unlocked, and we generally expect that to
3303 * happen when the page IO is completed and the page is now uptodate. This will
3304 * queue a task_work based retry of the operation, attempting to copy the data
3305 * again. If the latter fails because the page was NOT uptodate, then we will
3306 * do a thread based blocking retry of the operation. That's the unexpected
3307 * slow path.
3308 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003309static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3310 int sync, void *arg)
3311{
3312 struct wait_page_queue *wpq;
3313 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003314 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003315 int ret;
3316
3317 wpq = container_of(wait, struct wait_page_queue, wait);
3318
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003319 if (!wake_page_match(wpq, key))
3320 return 0;
3321
Hao Xuc8d317a2020-09-29 20:00:45 +08003322 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003323 list_del_init(&wait->entry);
3324
Pavel Begunkove7375122020-07-12 20:42:04 +03003325 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003326 percpu_ref_get(&req->ctx->refs);
3327
Jens Axboebcf5a062020-05-22 09:24:42 -06003328 /* submit ref gets dropped, acquire a new one */
3329 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003330 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003331 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003332 struct task_struct *tsk;
3333
Jens Axboebcf5a062020-05-22 09:24:42 -06003334 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003335 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003336 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003337 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003338 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003339 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003340 return 1;
3341}
3342
Jens Axboec1dd91d2020-08-03 16:43:59 -06003343/*
3344 * This controls whether a given IO request should be armed for async page
3345 * based retry. If we return false here, the request is handed to the async
3346 * worker threads for retry. If we're doing buffered reads on a regular file,
3347 * we prepare a private wait_page_queue entry and retry the operation. This
3348 * will either succeed because the page is now uptodate and unlocked, or it
3349 * will register a callback when the page is unlocked at IO completion. Through
3350 * that callback, io_uring uses task_work to setup a retry of the operation.
3351 * That retry will attempt the buffered read again. The retry will generally
3352 * succeed, or in rare cases where it fails, we then fall back to using the
3353 * async worker threads for a blocking retry.
3354 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003355static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003356{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003357 struct io_async_rw *rw = req->async_data;
3358 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003359 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003360
3361 /* never retry for NOWAIT, we just complete with -EAGAIN */
3362 if (req->flags & REQ_F_NOWAIT)
3363 return false;
3364
Jens Axboe227c0c92020-08-13 11:51:40 -06003365 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003366 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003367 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003368
Jens Axboebcf5a062020-05-22 09:24:42 -06003369 /*
3370 * just use poll if we can, and don't attempt if the fs doesn't
3371 * support callback based unlocks
3372 */
3373 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3374 return false;
3375
Jens Axboe3b2a4432020-08-16 10:58:43 -07003376 wait->wait.func = io_async_buf_func;
3377 wait->wait.private = req;
3378 wait->wait.flags = 0;
3379 INIT_LIST_HEAD(&wait->wait.entry);
3380 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003381 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003382 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003383 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003384}
3385
3386static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3387{
3388 if (req->file->f_op->read_iter)
3389 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003390 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003391 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003392 else
3393 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003394}
3395
Jens Axboea1d7c392020-06-22 11:09:46 -06003396static int io_read(struct io_kiocb *req, bool force_nonblock,
3397 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398{
3399 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003400 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003401 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003402 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003403 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003404 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003405 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003406
Jens Axboee8c2bc12020-08-15 18:44:09 -07003407 if (rw)
3408 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003409
3410 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003411 if (ret < 0)
3412 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003413 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003414 io_size = ret;
3415 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003416 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003417
Jens Axboefd6c2e42019-12-18 12:19:41 -07003418 /* Ensure we clear previously set non-block flag */
3419 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003420 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003421 else
3422 kiocb->ki_flags |= IOCB_NOWAIT;
3423
Jens Axboefd6c2e42019-12-18 12:19:41 -07003424
Pavel Begunkov24c74672020-06-21 13:09:51 +03003425 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003426 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3427 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003428 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003429
Jens Axboe0fef9482020-08-26 10:36:20 -06003430 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003431 if (unlikely(ret))
3432 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003433
Jens Axboe227c0c92020-08-13 11:51:40 -06003434 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003435
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 if (!ret) {
3437 goto done;
3438 } else if (ret == -EIOCBQUEUED) {
3439 ret = 0;
3440 goto out_free;
3441 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003442 /* IOPOLL retry should happen for io-wq threads */
3443 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003444 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003445 /* no retry on NONBLOCK marked file */
3446 if (req->file->f_flags & O_NONBLOCK)
3447 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003448 /* some cases will consume bytes even on error returns */
3449 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003450 ret = 0;
3451 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003452 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003453 /* make sure -ERESTARTSYS -> -EINTR is done */
3454 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003455 }
3456
3457 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003458 if (!iov_iter_count(iter) || !force_nonblock ||
Pavel Begunkov2df15ef2021-01-21 12:01:08 +00003459 (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG))
Jens Axboe227c0c92020-08-13 11:51:40 -06003460 goto done;
3461
3462 io_size -= ret;
3463copy_iov:
3464 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3465 if (ret2) {
3466 ret = ret2;
3467 goto out_free;
3468 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003469 if (no_async)
3470 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003471 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003472 /* it's copied and will be cleaned with ->io */
3473 iovec = NULL;
3474 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003475 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003476retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003477 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003478 /* if we can retry, do so with the callbacks armed */
3479 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003480 kiocb->ki_flags &= ~IOCB_WAITQ;
3481 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003482 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003483
3484 /*
3485 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3486 * get -EIOCBQUEUED, then we'll get a notification when the desired
3487 * page gets unlocked. We can also get a partial read here, and if we
3488 * do, then just retry at the new offset.
3489 */
3490 ret = io_iter_do_read(req, iter);
3491 if (ret == -EIOCBQUEUED) {
3492 ret = 0;
3493 goto out_free;
3494 } else if (ret > 0 && ret < io_size) {
3495 /* we got some bytes, but not all. retry. */
3496 goto retry;
3497 }
3498done:
3499 kiocb_done(kiocb, ret, cs);
3500 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003501out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003502 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003503 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003504 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003505 return ret;
3506}
3507
Pavel Begunkov73debe62020-09-30 22:57:54 +03003508static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003509{
3510 ssize_t ret;
3511
Pavel Begunkova88fc402020-09-30 22:57:53 +03003512 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003513 if (ret)
3514 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003515
Jens Axboe3529d8c2019-12-19 18:24:38 -07003516 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3517 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003518
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003519 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003520 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003521 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003522 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003523}
3524
Jens Axboea1d7c392020-06-22 11:09:46 -06003525static int io_write(struct io_kiocb *req, bool force_nonblock,
3526 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003527{
3528 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003529 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003530 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003531 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003532 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003533 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003534
Jens Axboee8c2bc12020-08-15 18:44:09 -07003535 if (rw)
3536 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003537
3538 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003539 if (ret < 0)
3540 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003541 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003542 io_size = ret;
3543 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003544
Jens Axboefd6c2e42019-12-18 12:19:41 -07003545 /* Ensure we clear previously set non-block flag */
3546 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003547 kiocb->ki_flags &= ~IOCB_NOWAIT;
3548 else
3549 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003550
Pavel Begunkov24c74672020-06-21 13:09:51 +03003551 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003552 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003553 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003554
Jens Axboe10d59342019-12-09 20:16:22 -07003555 /* file path doesn't support NOWAIT for non-direct_IO */
3556 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3557 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003558 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003559
Jens Axboe0fef9482020-08-26 10:36:20 -06003560 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003561 if (unlikely(ret))
3562 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003563
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003564 /*
3565 * Open-code file_start_write here to grab freeze protection,
3566 * which will be released by another thread in
3567 * io_complete_rw(). Fool lockdep by telling it the lock got
3568 * released so that it doesn't complain about the held lock when
3569 * we return to userspace.
3570 */
3571 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003572 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003573 __sb_writers_release(file_inode(req->file)->i_sb,
3574 SB_FREEZE_WRITE);
3575 }
3576 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003577
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003578 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003579 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003580 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003581 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003582 else
3583 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003584
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003585 /*
3586 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3587 * retry them without IOCB_NOWAIT.
3588 */
3589 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3590 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003591 /* no retry on NONBLOCK marked file */
3592 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3593 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003594 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003595 /* IOPOLL retry should happen for io-wq threads */
3596 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3597 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003598done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003599 kiocb_done(kiocb, ret2, cs);
3600 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003601copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003602 /* some cases will consume bytes even on error returns */
3603 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003604 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003605 if (!ret)
3606 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003607 }
Jens Axboe31b51512019-01-18 22:56:34 -07003608out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003609 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003610 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003611 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003612 return ret;
3613}
3614
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003615static int __io_splice_prep(struct io_kiocb *req,
3616 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003617{
3618 struct io_splice* sp = &req->splice;
3619 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003620
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003621 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3622 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003623
3624 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003625 sp->len = READ_ONCE(sqe->len);
3626 sp->flags = READ_ONCE(sqe->splice_flags);
3627
3628 if (unlikely(sp->flags & ~valid_flags))
3629 return -EINVAL;
3630
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003631 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3632 (sp->flags & SPLICE_F_FD_IN_FIXED));
3633 if (!sp->file_in)
3634 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003635 req->flags |= REQ_F_NEED_CLEANUP;
3636
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003637 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3638 /*
3639 * Splice operation will be punted aync, and here need to
3640 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3641 */
3642 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003643 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003644 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003645
3646 return 0;
3647}
3648
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003649static int io_tee_prep(struct io_kiocb *req,
3650 const struct io_uring_sqe *sqe)
3651{
3652 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3653 return -EINVAL;
3654 return __io_splice_prep(req, sqe);
3655}
3656
3657static int io_tee(struct io_kiocb *req, bool force_nonblock)
3658{
3659 struct io_splice *sp = &req->splice;
3660 struct file *in = sp->file_in;
3661 struct file *out = sp->file_out;
3662 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3663 long ret = 0;
3664
3665 if (force_nonblock)
3666 return -EAGAIN;
3667 if (sp->len)
3668 ret = do_tee(in, out, sp->len, flags);
3669
3670 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3671 req->flags &= ~REQ_F_NEED_CLEANUP;
3672
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003673 if (ret != sp->len)
3674 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003675 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003676 return 0;
3677}
3678
3679static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3680{
3681 struct io_splice* sp = &req->splice;
3682
3683 sp->off_in = READ_ONCE(sqe->splice_off_in);
3684 sp->off_out = READ_ONCE(sqe->off);
3685 return __io_splice_prep(req, sqe);
3686}
3687
Pavel Begunkov014db002020-03-03 21:33:12 +03003688static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003689{
3690 struct io_splice *sp = &req->splice;
3691 struct file *in = sp->file_in;
3692 struct file *out = sp->file_out;
3693 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3694 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003695 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003696
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003697 if (force_nonblock)
3698 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003699
3700 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3701 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003702
Jens Axboe948a7742020-05-17 14:21:38 -06003703 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003704 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003705
3706 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3707 req->flags &= ~REQ_F_NEED_CLEANUP;
3708
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003709 if (ret != sp->len)
3710 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003711 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003712 return 0;
3713}
3714
Jens Axboe2b188cc2019-01-07 10:46:33 -07003715/*
3716 * IORING_OP_NOP just posts a completion event, nothing else.
3717 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003718static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003719{
3720 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003721
Jens Axboedef596e2019-01-09 08:59:42 -07003722 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3723 return -EINVAL;
3724
Jens Axboe229a7b62020-06-22 10:13:11 -06003725 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003726 return 0;
3727}
3728
Jens Axboe3529d8c2019-12-19 18:24:38 -07003729static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003730{
Jens Axboe6b063142019-01-10 22:13:58 -07003731 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003732
Jens Axboe09bb8392019-03-13 12:39:28 -06003733 if (!req->file)
3734 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003735
Jens Axboe6b063142019-01-10 22:13:58 -07003736 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003737 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003738 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003739 return -EINVAL;
3740
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003741 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3742 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3743 return -EINVAL;
3744
3745 req->sync.off = READ_ONCE(sqe->off);
3746 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003747 return 0;
3748}
3749
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003750static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003751{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003752 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003753 int ret;
3754
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003755 /* fsync always requires a blocking context */
3756 if (force_nonblock)
3757 return -EAGAIN;
3758
Jens Axboe9adbd452019-12-20 08:45:55 -07003759 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003760 end > 0 ? end : LLONG_MAX,
3761 req->sync.flags & IORING_FSYNC_DATASYNC);
3762 if (ret < 0)
3763 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003764 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003765 return 0;
3766}
3767
Jens Axboed63d1b52019-12-10 10:38:56 -07003768static int io_fallocate_prep(struct io_kiocb *req,
3769 const struct io_uring_sqe *sqe)
3770{
3771 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3772 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003773 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3774 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003775
3776 req->sync.off = READ_ONCE(sqe->off);
3777 req->sync.len = READ_ONCE(sqe->addr);
3778 req->sync.mode = READ_ONCE(sqe->len);
3779 return 0;
3780}
3781
Pavel Begunkov014db002020-03-03 21:33:12 +03003782static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003783{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003784 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003785
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003786 /* fallocate always requiring blocking context */
3787 if (force_nonblock)
3788 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003789 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3790 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003791 if (ret < 0)
3792 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003793 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003794 return 0;
3795}
3796
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003797static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003798{
Jens Axboef8748882020-01-08 17:47:02 -07003799 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003800 int ret;
3801
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003802 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003803 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003804 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003805 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003806
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003807 /* open.how should be already initialised */
3808 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003809 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003810
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003811 req->open.dfd = READ_ONCE(sqe->fd);
3812 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003813 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003814 if (IS_ERR(req->open.filename)) {
3815 ret = PTR_ERR(req->open.filename);
3816 req->open.filename = NULL;
3817 return ret;
3818 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003819 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07003820 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003821 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003822 return 0;
3823}
3824
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003825static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3826{
3827 u64 flags, mode;
3828
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003829 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3830 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003831 mode = READ_ONCE(sqe->len);
3832 flags = READ_ONCE(sqe->open_flags);
3833 req->open.how = build_open_how(flags, mode);
3834 return __io_openat_prep(req, sqe);
3835}
3836
Jens Axboecebdb982020-01-08 17:59:24 -07003837static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3838{
3839 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003840 size_t len;
3841 int ret;
3842
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003843 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3844 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003845 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3846 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003847 if (len < OPEN_HOW_SIZE_VER0)
3848 return -EINVAL;
3849
3850 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3851 len);
3852 if (ret)
3853 return ret;
3854
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003855 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003856}
3857
Pavel Begunkov014db002020-03-03 21:33:12 +03003858static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003859{
3860 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003861 struct file *file;
3862 int ret;
3863
Jens Axboe944d1442020-11-13 16:48:44 -07003864 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003865 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003866
Jens Axboecebdb982020-01-08 17:59:24 -07003867 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003868 if (ret)
3869 goto err;
3870
Jens Axboe4022e7a2020-03-19 19:23:18 -06003871 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003872 if (ret < 0)
3873 goto err;
3874
3875 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3876 if (IS_ERR(file)) {
3877 put_unused_fd(ret);
3878 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07003879 /*
3880 * A work-around to ensure that /proc/self works that way
3881 * that it should - if we get -EOPNOTSUPP back, then assume
3882 * that proc_self_get_link() failed us because we're in async
3883 * context. We should be safe to retry this from the task
3884 * itself with force_nonblock == false set, as it should not
3885 * block on lookup. Would be nice to know this upfront and
3886 * avoid the async dance, but doesn't seem feasible.
3887 */
3888 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
3889 req->open.ignore_nonblock = true;
3890 refcount_inc(&req->refs);
3891 io_req_task_queue(req);
3892 return 0;
3893 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003894 } else {
3895 fsnotify_open(file);
3896 fd_install(ret, file);
3897 }
3898err:
3899 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003900 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003901 if (ret < 0)
3902 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003903 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003904 return 0;
3905}
3906
Pavel Begunkov014db002020-03-03 21:33:12 +03003907static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003908{
Pavel Begunkov014db002020-03-03 21:33:12 +03003909 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003910}
3911
Jens Axboe067524e2020-03-02 16:32:28 -07003912static int io_remove_buffers_prep(struct io_kiocb *req,
3913 const struct io_uring_sqe *sqe)
3914{
3915 struct io_provide_buf *p = &req->pbuf;
3916 u64 tmp;
3917
3918 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3919 return -EINVAL;
3920
3921 tmp = READ_ONCE(sqe->fd);
3922 if (!tmp || tmp > USHRT_MAX)
3923 return -EINVAL;
3924
3925 memset(p, 0, sizeof(*p));
3926 p->nbufs = tmp;
3927 p->bgid = READ_ONCE(sqe->buf_group);
3928 return 0;
3929}
3930
3931static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3932 int bgid, unsigned nbufs)
3933{
3934 unsigned i = 0;
3935
3936 /* shouldn't happen */
3937 if (!nbufs)
3938 return 0;
3939
3940 /* the head kbuf is the list itself */
3941 while (!list_empty(&buf->list)) {
3942 struct io_buffer *nxt;
3943
3944 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3945 list_del(&nxt->list);
3946 kfree(nxt);
3947 if (++i == nbufs)
3948 return i;
3949 }
3950 i++;
3951 kfree(buf);
3952 idr_remove(&ctx->io_buffer_idr, bgid);
3953
3954 return i;
3955}
3956
Jens Axboe229a7b62020-06-22 10:13:11 -06003957static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3958 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003959{
3960 struct io_provide_buf *p = &req->pbuf;
3961 struct io_ring_ctx *ctx = req->ctx;
3962 struct io_buffer *head;
3963 int ret = 0;
3964
3965 io_ring_submit_lock(ctx, !force_nonblock);
3966
3967 lockdep_assert_held(&ctx->uring_lock);
3968
3969 ret = -ENOENT;
3970 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3971 if (head)
3972 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003973 if (ret < 0)
3974 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00003975
3976 /* need to hold the lock to complete IOPOLL requests */
3977 if (ctx->flags & IORING_SETUP_IOPOLL) {
3978 __io_req_complete(req, ret, 0, cs);
3979 io_ring_submit_unlock(ctx, !force_nonblock);
3980 } else {
3981 io_ring_submit_unlock(ctx, !force_nonblock);
3982 __io_req_complete(req, ret, 0, cs);
3983 }
Jens Axboe067524e2020-03-02 16:32:28 -07003984 return 0;
3985}
3986
Jens Axboeddf0322d2020-02-23 16:41:33 -07003987static int io_provide_buffers_prep(struct io_kiocb *req,
3988 const struct io_uring_sqe *sqe)
3989{
3990 struct io_provide_buf *p = &req->pbuf;
3991 u64 tmp;
3992
3993 if (sqe->ioprio || sqe->rw_flags)
3994 return -EINVAL;
3995
3996 tmp = READ_ONCE(sqe->fd);
3997 if (!tmp || tmp > USHRT_MAX)
3998 return -E2BIG;
3999 p->nbufs = tmp;
4000 p->addr = READ_ONCE(sqe->addr);
4001 p->len = READ_ONCE(sqe->len);
4002
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004003 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004004 return -EFAULT;
4005
4006 p->bgid = READ_ONCE(sqe->buf_group);
4007 tmp = READ_ONCE(sqe->off);
4008 if (tmp > USHRT_MAX)
4009 return -E2BIG;
4010 p->bid = tmp;
4011 return 0;
4012}
4013
4014static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4015{
4016 struct io_buffer *buf;
4017 u64 addr = pbuf->addr;
4018 int i, bid = pbuf->bid;
4019
4020 for (i = 0; i < pbuf->nbufs; i++) {
4021 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4022 if (!buf)
4023 break;
4024
4025 buf->addr = addr;
4026 buf->len = pbuf->len;
4027 buf->bid = bid;
4028 addr += pbuf->len;
4029 bid++;
4030 if (!*head) {
4031 INIT_LIST_HEAD(&buf->list);
4032 *head = buf;
4033 } else {
4034 list_add_tail(&buf->list, &(*head)->list);
4035 }
4036 }
4037
4038 return i ? i : -ENOMEM;
4039}
4040
Jens Axboe229a7b62020-06-22 10:13:11 -06004041static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4042 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004043{
4044 struct io_provide_buf *p = &req->pbuf;
4045 struct io_ring_ctx *ctx = req->ctx;
4046 struct io_buffer *head, *list;
4047 int ret = 0;
4048
4049 io_ring_submit_lock(ctx, !force_nonblock);
4050
4051 lockdep_assert_held(&ctx->uring_lock);
4052
4053 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4054
4055 ret = io_add_buffers(p, &head);
4056 if (ret < 0)
4057 goto out;
4058
4059 if (!list) {
4060 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4061 GFP_KERNEL);
4062 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004063 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004064 goto out;
4065 }
4066 }
4067out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004068 if (ret < 0)
4069 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00004070
4071 /* need to hold the lock to complete IOPOLL requests */
4072 if (ctx->flags & IORING_SETUP_IOPOLL) {
4073 __io_req_complete(req, ret, 0, cs);
4074 io_ring_submit_unlock(ctx, !force_nonblock);
4075 } else {
4076 io_ring_submit_unlock(ctx, !force_nonblock);
4077 __io_req_complete(req, ret, 0, cs);
4078 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004079 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004080}
4081
Jens Axboe3e4827b2020-01-08 15:18:09 -07004082static int io_epoll_ctl_prep(struct io_kiocb *req,
4083 const struct io_uring_sqe *sqe)
4084{
4085#if defined(CONFIG_EPOLL)
4086 if (sqe->ioprio || sqe->buf_index)
4087 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004088 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004089 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004090
4091 req->epoll.epfd = READ_ONCE(sqe->fd);
4092 req->epoll.op = READ_ONCE(sqe->len);
4093 req->epoll.fd = READ_ONCE(sqe->off);
4094
4095 if (ep_op_has_event(req->epoll.op)) {
4096 struct epoll_event __user *ev;
4097
4098 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4099 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4100 return -EFAULT;
4101 }
4102
4103 return 0;
4104#else
4105 return -EOPNOTSUPP;
4106#endif
4107}
4108
Jens Axboe229a7b62020-06-22 10:13:11 -06004109static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4110 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004111{
4112#if defined(CONFIG_EPOLL)
4113 struct io_epoll *ie = &req->epoll;
4114 int ret;
4115
4116 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4117 if (force_nonblock && ret == -EAGAIN)
4118 return -EAGAIN;
4119
4120 if (ret < 0)
4121 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004122 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004123 return 0;
4124#else
4125 return -EOPNOTSUPP;
4126#endif
4127}
4128
Jens Axboec1ca7572019-12-25 22:18:28 -07004129static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4130{
4131#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4132 if (sqe->ioprio || sqe->buf_index || sqe->off)
4133 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004134 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4135 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004136
4137 req->madvise.addr = READ_ONCE(sqe->addr);
4138 req->madvise.len = READ_ONCE(sqe->len);
4139 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4140 return 0;
4141#else
4142 return -EOPNOTSUPP;
4143#endif
4144}
4145
Pavel Begunkov014db002020-03-03 21:33:12 +03004146static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004147{
4148#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4149 struct io_madvise *ma = &req->madvise;
4150 int ret;
4151
4152 if (force_nonblock)
4153 return -EAGAIN;
4154
Minchan Kim0726b012020-10-17 16:14:50 -07004155 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004156 if (ret < 0)
4157 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004158 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004159 return 0;
4160#else
4161 return -EOPNOTSUPP;
4162#endif
4163}
4164
Jens Axboe4840e412019-12-25 22:03:45 -07004165static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4166{
4167 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4168 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004169 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4170 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004171
4172 req->fadvise.offset = READ_ONCE(sqe->off);
4173 req->fadvise.len = READ_ONCE(sqe->len);
4174 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4175 return 0;
4176}
4177
Pavel Begunkov014db002020-03-03 21:33:12 +03004178static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004179{
4180 struct io_fadvise *fa = &req->fadvise;
4181 int ret;
4182
Jens Axboe3e694262020-02-01 09:22:49 -07004183 if (force_nonblock) {
4184 switch (fa->advice) {
4185 case POSIX_FADV_NORMAL:
4186 case POSIX_FADV_RANDOM:
4187 case POSIX_FADV_SEQUENTIAL:
4188 break;
4189 default:
4190 return -EAGAIN;
4191 }
4192 }
Jens Axboe4840e412019-12-25 22:03:45 -07004193
4194 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4195 if (ret < 0)
4196 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004197 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004198 return 0;
4199}
4200
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004201static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4202{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004203 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004204 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004205 if (sqe->ioprio || sqe->buf_index)
4206 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004207 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004208 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004209
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004210 req->statx.dfd = READ_ONCE(sqe->fd);
4211 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004212 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004213 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4214 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004215
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004216 return 0;
4217}
4218
Pavel Begunkov014db002020-03-03 21:33:12 +03004219static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004220{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004221 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004222 int ret;
4223
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004224 if (force_nonblock) {
4225 /* only need file table for an actual valid fd */
4226 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4227 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004228 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004229 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004230
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004231 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4232 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004233
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004234 if (ret < 0)
4235 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004236 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004237 return 0;
4238}
4239
Jens Axboeb5dba592019-12-11 14:02:38 -07004240static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4241{
4242 /*
4243 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004244 * leave the 'file' in an undeterminate state, and here need to modify
4245 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004246 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004247 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004248
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004249 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4250 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004251 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4252 sqe->rw_flags || sqe->buf_index)
4253 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004254 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004255 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004256
4257 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004258 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004259 return -EBADF;
4260
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004261 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004262 return 0;
4263}
4264
Jens Axboe229a7b62020-06-22 10:13:11 -06004265static int io_close(struct io_kiocb *req, bool force_nonblock,
4266 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004267{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004268 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004269 int ret;
4270
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004271 /* might be already done during nonblock submission */
4272 if (!close->put_file) {
4273 ret = __close_fd_get_file(close->fd, &close->put_file);
4274 if (ret < 0)
4275 return (ret == -ENOENT) ? -EBADF : ret;
4276 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004277
4278 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004279 if (close->put_file->f_op->flush && force_nonblock) {
Jens Axboef3ac7a52021-01-19 10:10:54 -07004280 /* not safe to cancel at this point */
4281 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004282 /* was never set, but play safe */
4283 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004284 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004285 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004286 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004287 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004288
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004289 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004290 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004291 if (ret < 0)
4292 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004293 fput(close->put_file);
4294 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004295 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004296 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004297}
4298
Jens Axboe3529d8c2019-12-19 18:24:38 -07004299static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004300{
4301 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004302
4303 if (!req->file)
4304 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004305
4306 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4307 return -EINVAL;
4308 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4309 return -EINVAL;
4310
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004311 req->sync.off = READ_ONCE(sqe->off);
4312 req->sync.len = READ_ONCE(sqe->len);
4313 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004314 return 0;
4315}
4316
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004317static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004318{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004319 int ret;
4320
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004321 /* sync_file_range always requires a blocking context */
4322 if (force_nonblock)
4323 return -EAGAIN;
4324
Jens Axboe9adbd452019-12-20 08:45:55 -07004325 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004326 req->sync.flags);
4327 if (ret < 0)
4328 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004329 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004330 return 0;
4331}
4332
YueHaibing469956e2020-03-04 15:53:52 +08004333#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004334static int io_setup_async_msg(struct io_kiocb *req,
4335 struct io_async_msghdr *kmsg)
4336{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004337 struct io_async_msghdr *async_msg = req->async_data;
4338
4339 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004340 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004341 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004342 if (kmsg->iov != kmsg->fast_iov)
4343 kfree(kmsg->iov);
4344 return -ENOMEM;
4345 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004346 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004347 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004348 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004349 return -EAGAIN;
4350}
4351
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004352static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4353 struct io_async_msghdr *iomsg)
4354{
4355 iomsg->iov = iomsg->fast_iov;
4356 iomsg->msg.msg_name = &iomsg->addr;
4357 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4358 req->sr_msg.msg_flags, &iomsg->iov);
4359}
4360
Jens Axboe3529d8c2019-12-19 18:24:38 -07004361static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004362{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004363 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004364 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004365 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004366
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004367 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4368 return -EINVAL;
4369
Jens Axboee47293f2019-12-20 08:58:21 -07004370 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004371 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004372 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004373
Jens Axboed8768362020-02-27 14:17:49 -07004374#ifdef CONFIG_COMPAT
4375 if (req->ctx->compat)
4376 sr->msg_flags |= MSG_CMSG_COMPAT;
4377#endif
4378
Jens Axboee8c2bc12020-08-15 18:44:09 -07004379 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004380 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004381 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004382 if (!ret)
4383 req->flags |= REQ_F_NEED_CLEANUP;
4384 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004385}
4386
Jens Axboe229a7b62020-06-22 10:13:11 -06004387static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4388 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004389{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004390 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004391 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004392 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004393 int ret;
4394
Jens Axboe03b12302019-12-02 18:50:25 -07004395 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004396 if (unlikely(!sock))
4397 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004398
Jens Axboee8c2bc12020-08-15 18:44:09 -07004399 if (req->async_data) {
4400 kmsg = req->async_data;
4401 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004402 /* if iov is set, it's allocated already */
4403 if (!kmsg->iov)
4404 kmsg->iov = kmsg->fast_iov;
4405 kmsg->msg.msg_iter.iov = kmsg->iov;
4406 } else {
4407 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004408 if (ret)
4409 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004410 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004411 }
4412
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004413 flags = req->sr_msg.msg_flags;
4414 if (flags & MSG_DONTWAIT)
4415 req->flags |= REQ_F_NOWAIT;
4416 else if (force_nonblock)
4417 flags |= MSG_DONTWAIT;
4418
4419 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4420 if (force_nonblock && ret == -EAGAIN)
4421 return io_setup_async_msg(req, kmsg);
4422 if (ret == -ERESTARTSYS)
4423 ret = -EINTR;
4424
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004425 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004426 kfree(kmsg->iov);
4427 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004428 if (ret < 0)
4429 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004430 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004431 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004432}
4433
Jens Axboe229a7b62020-06-22 10:13:11 -06004434static int io_send(struct io_kiocb *req, bool force_nonblock,
4435 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004436{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004437 struct io_sr_msg *sr = &req->sr_msg;
4438 struct msghdr msg;
4439 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004440 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004441 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004442 int ret;
4443
4444 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004445 if (unlikely(!sock))
4446 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004447
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004448 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4449 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004450 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004451
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004452 msg.msg_name = NULL;
4453 msg.msg_control = NULL;
4454 msg.msg_controllen = 0;
4455 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004456
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004457 flags = req->sr_msg.msg_flags;
4458 if (flags & MSG_DONTWAIT)
4459 req->flags |= REQ_F_NOWAIT;
4460 else if (force_nonblock)
4461 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004462
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004463 msg.msg_flags = flags;
4464 ret = sock_sendmsg(sock, &msg);
4465 if (force_nonblock && ret == -EAGAIN)
4466 return -EAGAIN;
4467 if (ret == -ERESTARTSYS)
4468 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004469
Jens Axboe03b12302019-12-02 18:50:25 -07004470 if (ret < 0)
4471 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004472 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004473 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004474}
4475
Pavel Begunkov1400e692020-07-12 20:41:05 +03004476static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4477 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004478{
4479 struct io_sr_msg *sr = &req->sr_msg;
4480 struct iovec __user *uiov;
4481 size_t iov_len;
4482 int ret;
4483
Pavel Begunkov1400e692020-07-12 20:41:05 +03004484 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4485 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004486 if (ret)
4487 return ret;
4488
4489 if (req->flags & REQ_F_BUFFER_SELECT) {
4490 if (iov_len > 1)
4491 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004492 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004493 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004494 sr->len = iomsg->iov[0].iov_len;
4495 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004496 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004497 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004498 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004499 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4500 &iomsg->iov, &iomsg->msg.msg_iter,
4501 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004502 if (ret > 0)
4503 ret = 0;
4504 }
4505
4506 return ret;
4507}
4508
4509#ifdef CONFIG_COMPAT
4510static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004511 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004512{
4513 struct compat_msghdr __user *msg_compat;
4514 struct io_sr_msg *sr = &req->sr_msg;
4515 struct compat_iovec __user *uiov;
4516 compat_uptr_t ptr;
4517 compat_size_t len;
4518 int ret;
4519
Pavel Begunkov270a5942020-07-12 20:41:04 +03004520 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004521 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004522 &ptr, &len);
4523 if (ret)
4524 return ret;
4525
4526 uiov = compat_ptr(ptr);
4527 if (req->flags & REQ_F_BUFFER_SELECT) {
4528 compat_ssize_t clen;
4529
4530 if (len > 1)
4531 return -EINVAL;
4532 if (!access_ok(uiov, sizeof(*uiov)))
4533 return -EFAULT;
4534 if (__get_user(clen, &uiov->iov_len))
4535 return -EFAULT;
4536 if (clen < 0)
4537 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004538 sr->len = clen;
4539 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004540 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004541 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004542 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4543 UIO_FASTIOV, &iomsg->iov,
4544 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004545 if (ret < 0)
4546 return ret;
4547 }
4548
4549 return 0;
4550}
Jens Axboe03b12302019-12-02 18:50:25 -07004551#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004552
Pavel Begunkov1400e692020-07-12 20:41:05 +03004553static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4554 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004555{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004556 iomsg->msg.msg_name = &iomsg->addr;
4557 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004558
4559#ifdef CONFIG_COMPAT
4560 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004561 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004562#endif
4563
Pavel Begunkov1400e692020-07-12 20:41:05 +03004564 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004565}
4566
Jens Axboebcda7ba2020-02-23 16:42:51 -07004567static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004568 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004569{
4570 struct io_sr_msg *sr = &req->sr_msg;
4571 struct io_buffer *kbuf;
4572
Jens Axboebcda7ba2020-02-23 16:42:51 -07004573 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4574 if (IS_ERR(kbuf))
4575 return kbuf;
4576
4577 sr->kbuf = kbuf;
4578 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004579 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004580}
4581
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004582static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4583{
4584 return io_put_kbuf(req, req->sr_msg.kbuf);
4585}
4586
Jens Axboe3529d8c2019-12-19 18:24:38 -07004587static int io_recvmsg_prep(struct io_kiocb *req,
4588 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004589{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004590 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004591 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004592 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004593
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004594 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4595 return -EINVAL;
4596
Jens Axboe3529d8c2019-12-19 18:24:38 -07004597 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004598 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004599 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004600 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004601
Jens Axboed8768362020-02-27 14:17:49 -07004602#ifdef CONFIG_COMPAT
4603 if (req->ctx->compat)
4604 sr->msg_flags |= MSG_CMSG_COMPAT;
4605#endif
4606
Jens Axboee8c2bc12020-08-15 18:44:09 -07004607 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004608 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004609 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004610 if (!ret)
4611 req->flags |= REQ_F_NEED_CLEANUP;
4612 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004613}
4614
Jens Axboe229a7b62020-06-22 10:13:11 -06004615static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4616 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004617{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004618 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004619 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004620 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004621 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004622 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004623
Jens Axboe0fa03c62019-04-19 13:34:07 -06004624 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004625 if (unlikely(!sock))
4626 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004627
Jens Axboee8c2bc12020-08-15 18:44:09 -07004628 if (req->async_data) {
4629 kmsg = req->async_data;
4630 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 /* if iov is set, it's allocated already */
4632 if (!kmsg->iov)
4633 kmsg->iov = kmsg->fast_iov;
4634 kmsg->msg.msg_iter.iov = kmsg->iov;
4635 } else {
4636 ret = io_recvmsg_copy_hdr(req, &iomsg);
4637 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004638 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004639 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004640 }
4641
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004642 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004643 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004644 if (IS_ERR(kbuf))
4645 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004646 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4647 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4648 1, req->sr_msg.len);
4649 }
4650
4651 flags = req->sr_msg.msg_flags;
4652 if (flags & MSG_DONTWAIT)
4653 req->flags |= REQ_F_NOWAIT;
4654 else if (force_nonblock)
4655 flags |= MSG_DONTWAIT;
4656
4657 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4658 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004659 if (force_nonblock && ret == -EAGAIN)
4660 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004661 if (ret == -ERESTARTSYS)
4662 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004663
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004664 if (req->flags & REQ_F_BUFFER_SELECTED)
4665 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004666 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004667 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004668 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004669 if (ret < 0)
4670 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004671 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004672 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004673}
4674
Jens Axboe229a7b62020-06-22 10:13:11 -06004675static int io_recv(struct io_kiocb *req, bool force_nonblock,
4676 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004677{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004678 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004679 struct io_sr_msg *sr = &req->sr_msg;
4680 struct msghdr msg;
4681 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004682 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004683 struct iovec iov;
4684 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004685 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004686
Jens Axboefddafac2020-01-04 20:19:44 -07004687 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004688 if (unlikely(!sock))
4689 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004690
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004691 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004692 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004693 if (IS_ERR(kbuf))
4694 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004695 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004696 }
4697
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004698 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004699 if (unlikely(ret))
4700 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004701
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004702 msg.msg_name = NULL;
4703 msg.msg_control = NULL;
4704 msg.msg_controllen = 0;
4705 msg.msg_namelen = 0;
4706 msg.msg_iocb = NULL;
4707 msg.msg_flags = 0;
4708
4709 flags = req->sr_msg.msg_flags;
4710 if (flags & MSG_DONTWAIT)
4711 req->flags |= REQ_F_NOWAIT;
4712 else if (force_nonblock)
4713 flags |= MSG_DONTWAIT;
4714
4715 ret = sock_recvmsg(sock, &msg, flags);
4716 if (force_nonblock && ret == -EAGAIN)
4717 return -EAGAIN;
4718 if (ret == -ERESTARTSYS)
4719 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004720out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004721 if (req->flags & REQ_F_BUFFER_SELECTED)
4722 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004723 if (ret < 0)
4724 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004725 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004726 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004727}
4728
Jens Axboe3529d8c2019-12-19 18:24:38 -07004729static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004730{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004731 struct io_accept *accept = &req->accept;
4732
Jens Axboe17f2fe32019-10-17 14:42:58 -06004733 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4734 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004735 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004736 return -EINVAL;
4737
Jens Axboed55e5f52019-12-11 16:12:15 -07004738 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4739 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004740 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004741 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004742 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004743}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004744
Jens Axboe229a7b62020-06-22 10:13:11 -06004745static int io_accept(struct io_kiocb *req, bool force_nonblock,
4746 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004747{
4748 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004749 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004750 int ret;
4751
Jiufei Xuee697dee2020-06-10 13:41:59 +08004752 if (req->file->f_flags & O_NONBLOCK)
4753 req->flags |= REQ_F_NOWAIT;
4754
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004755 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004756 accept->addr_len, accept->flags,
4757 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004758 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004759 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004760 if (ret < 0) {
4761 if (ret == -ERESTARTSYS)
4762 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004763 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004764 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004765 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004766 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004767}
4768
Jens Axboe3529d8c2019-12-19 18:24:38 -07004769static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004770{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004771 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004772 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004773
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004774 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4775 return -EINVAL;
4776 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4777 return -EINVAL;
4778
Jens Axboe3529d8c2019-12-19 18:24:38 -07004779 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4780 conn->addr_len = READ_ONCE(sqe->addr2);
4781
4782 if (!io)
4783 return 0;
4784
4785 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004786 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004787}
4788
Jens Axboe229a7b62020-06-22 10:13:11 -06004789static int io_connect(struct io_kiocb *req, bool force_nonblock,
4790 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004791{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004792 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004793 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004794 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004795
Jens Axboee8c2bc12020-08-15 18:44:09 -07004796 if (req->async_data) {
4797 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004798 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004799 ret = move_addr_to_kernel(req->connect.addr,
4800 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004801 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004802 if (ret)
4803 goto out;
4804 io = &__io;
4805 }
4806
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004807 file_flags = force_nonblock ? O_NONBLOCK : 0;
4808
Jens Axboee8c2bc12020-08-15 18:44:09 -07004809 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004810 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004811 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004812 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004813 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004814 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004815 ret = -ENOMEM;
4816 goto out;
4817 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004818 io = req->async_data;
4819 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004820 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004821 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004822 if (ret == -ERESTARTSYS)
4823 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004824out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004825 if (ret < 0)
4826 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004827 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004828 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004829}
YueHaibing469956e2020-03-04 15:53:52 +08004830#else /* !CONFIG_NET */
4831static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4832{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004833 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004834}
4835
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004836static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4837 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004838{
YueHaibing469956e2020-03-04 15:53:52 +08004839 return -EOPNOTSUPP;
4840}
4841
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004842static int io_send(struct io_kiocb *req, bool force_nonblock,
4843 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004844{
4845 return -EOPNOTSUPP;
4846}
4847
4848static int io_recvmsg_prep(struct io_kiocb *req,
4849 const struct io_uring_sqe *sqe)
4850{
4851 return -EOPNOTSUPP;
4852}
4853
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004854static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4855 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004856{
4857 return -EOPNOTSUPP;
4858}
4859
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004860static int io_recv(struct io_kiocb *req, bool force_nonblock,
4861 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004862{
4863 return -EOPNOTSUPP;
4864}
4865
4866static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4867{
4868 return -EOPNOTSUPP;
4869}
4870
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004871static int io_accept(struct io_kiocb *req, bool force_nonblock,
4872 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004873{
4874 return -EOPNOTSUPP;
4875}
4876
4877static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4878{
4879 return -EOPNOTSUPP;
4880}
4881
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004882static int io_connect(struct io_kiocb *req, bool force_nonblock,
4883 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004884{
4885 return -EOPNOTSUPP;
4886}
4887#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004888
Jens Axboed7718a92020-02-14 22:23:12 -07004889struct io_poll_table {
4890 struct poll_table_struct pt;
4891 struct io_kiocb *req;
4892 int error;
4893};
4894
Jens Axboed7718a92020-02-14 22:23:12 -07004895static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4896 __poll_t mask, task_work_func_t func)
4897{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004898 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004899 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004900
4901 /* for instances that support it check for an event match first: */
4902 if (mask && !(mask & poll->events))
4903 return 0;
4904
4905 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4906
4907 list_del_init(&poll->wait.entry);
4908
Jens Axboed7718a92020-02-14 22:23:12 -07004909 req->result = mask;
4910 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004911 percpu_ref_get(&req->ctx->refs);
4912
Jens Axboed7718a92020-02-14 22:23:12 -07004913 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004914 * If we using the signalfd wait_queue_head for this wakeup, then
4915 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4916 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4917 * either, as the normal wakeup will suffice.
4918 */
4919 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4920
4921 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004922 * If this fails, then the task is exiting. When a task exits, the
4923 * work gets canceled, so just cancel this request as well instead
4924 * of executing it. We can't safely execute it anyway, as we may not
4925 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004926 */
Jens Axboe87c43112020-09-30 21:00:14 -06004927 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004928 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004929 struct task_struct *tsk;
4930
Jens Axboee3aabf92020-05-18 11:04:17 -06004931 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004932 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06004933 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06004934 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004935 }
Jens Axboed7718a92020-02-14 22:23:12 -07004936 return 1;
4937}
4938
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004939static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4940 __acquires(&req->ctx->completion_lock)
4941{
4942 struct io_ring_ctx *ctx = req->ctx;
4943
4944 if (!req->result && !READ_ONCE(poll->canceled)) {
4945 struct poll_table_struct pt = { ._key = poll->events };
4946
4947 req->result = vfs_poll(req->file, &pt) & poll->events;
4948 }
4949
4950 spin_lock_irq(&ctx->completion_lock);
4951 if (!req->result && !READ_ONCE(poll->canceled)) {
4952 add_wait_queue(poll->head, &poll->wait);
4953 return true;
4954 }
4955
4956 return false;
4957}
4958
Jens Axboed4e7cd32020-08-15 11:44:50 -07004959static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004960{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004961 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004962 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004963 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004964 return req->apoll->double_poll;
4965}
4966
4967static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4968{
4969 if (req->opcode == IORING_OP_POLL_ADD)
4970 return &req->poll;
4971 return &req->apoll->poll;
4972}
4973
4974static void io_poll_remove_double(struct io_kiocb *req)
4975{
4976 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004977
4978 lockdep_assert_held(&req->ctx->completion_lock);
4979
4980 if (poll && poll->head) {
4981 struct wait_queue_head *head = poll->head;
4982
4983 spin_lock(&head->lock);
4984 list_del_init(&poll->wait.entry);
4985 if (poll->wait.private)
4986 refcount_dec(&req->refs);
4987 poll->head = NULL;
4988 spin_unlock(&head->lock);
4989 }
4990}
4991
4992static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4993{
4994 struct io_ring_ctx *ctx = req->ctx;
4995
Jens Axboed4e7cd32020-08-15 11:44:50 -07004996 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004997 req->poll.done = true;
4998 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4999 io_commit_cqring(ctx);
5000}
5001
Jens Axboe18bceab2020-05-15 11:56:54 -06005002static void io_poll_task_func(struct callback_head *cb)
5003{
5004 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005005 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005006 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005007
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005008 if (io_poll_rewait(req, &req->poll)) {
5009 spin_unlock_irq(&ctx->completion_lock);
5010 } else {
5011 hash_del(&req->hash_node);
5012 io_poll_complete(req, req->result, 0);
5013 spin_unlock_irq(&ctx->completion_lock);
5014
5015 nxt = io_put_req_find_next(req);
5016 io_cqring_ev_posted(ctx);
5017 if (nxt)
5018 __io_req_task_submit(nxt);
5019 }
5020
Jens Axboe6d816e02020-08-11 08:04:14 -06005021 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005022}
5023
5024static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5025 int sync, void *key)
5026{
5027 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005028 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005029 __poll_t mask = key_to_poll(key);
5030
5031 /* for instances that support it check for an event match first: */
5032 if (mask && !(mask & poll->events))
5033 return 0;
5034
Jens Axboe8706e042020-09-28 08:38:54 -06005035 list_del_init(&wait->entry);
5036
Jens Axboe807abcb2020-07-17 17:09:27 -06005037 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005038 bool done;
5039
Jens Axboe807abcb2020-07-17 17:09:27 -06005040 spin_lock(&poll->head->lock);
5041 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005042 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005043 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005044 /* make sure double remove sees this as being gone */
5045 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005046 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005047 if (!done) {
5048 /* use wait func handler, so it matches the rq type */
5049 poll->wait.func(&poll->wait, mode, sync, key);
5050 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005051 }
5052 refcount_dec(&req->refs);
5053 return 1;
5054}
5055
5056static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5057 wait_queue_func_t wake_func)
5058{
5059 poll->head = NULL;
5060 poll->done = false;
5061 poll->canceled = false;
5062 poll->events = events;
5063 INIT_LIST_HEAD(&poll->wait.entry);
5064 init_waitqueue_func_entry(&poll->wait, wake_func);
5065}
5066
5067static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005068 struct wait_queue_head *head,
5069 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005070{
5071 struct io_kiocb *req = pt->req;
5072
5073 /*
5074 * If poll->head is already set, it's because the file being polled
5075 * uses multiple waitqueues for poll handling (eg one for read, one
5076 * for write). Setup a separate io_poll_iocb if this happens.
5077 */
5078 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005079 struct io_poll_iocb *poll_one = poll;
5080
Jens Axboe18bceab2020-05-15 11:56:54 -06005081 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005082 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005083 pt->error = -EINVAL;
5084 return;
5085 }
5086 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5087 if (!poll) {
5088 pt->error = -ENOMEM;
5089 return;
5090 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005091 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005092 refcount_inc(&req->refs);
5093 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005094 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005095 }
5096
5097 pt->error = 0;
5098 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005099
5100 if (poll->events & EPOLLEXCLUSIVE)
5101 add_wait_queue_exclusive(head, &poll->wait);
5102 else
5103 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005104}
5105
5106static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5107 struct poll_table_struct *p)
5108{
5109 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005110 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005111
Jens Axboe807abcb2020-07-17 17:09:27 -06005112 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005113}
5114
Jens Axboed7718a92020-02-14 22:23:12 -07005115static void io_async_task_func(struct callback_head *cb)
5116{
5117 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5118 struct async_poll *apoll = req->apoll;
5119 struct io_ring_ctx *ctx = req->ctx;
5120
5121 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5122
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005123 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005124 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005125 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005126 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005127 }
5128
Jens Axboe31067252020-05-17 17:43:31 -06005129 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005130 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005131 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005132
Jens Axboed4e7cd32020-08-15 11:44:50 -07005133 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005134 spin_unlock_irq(&ctx->completion_lock);
5135
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005136 if (!READ_ONCE(apoll->poll.canceled))
5137 __io_req_task_submit(req);
5138 else
5139 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005140
Jens Axboe6d816e02020-08-11 08:04:14 -06005141 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005142 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005143 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005144}
5145
5146static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5147 void *key)
5148{
5149 struct io_kiocb *req = wait->private;
5150 struct io_poll_iocb *poll = &req->apoll->poll;
5151
5152 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5153 key_to_poll(key));
5154
5155 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5156}
5157
5158static void io_poll_req_insert(struct io_kiocb *req)
5159{
5160 struct io_ring_ctx *ctx = req->ctx;
5161 struct hlist_head *list;
5162
5163 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5164 hlist_add_head(&req->hash_node, list);
5165}
5166
5167static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5168 struct io_poll_iocb *poll,
5169 struct io_poll_table *ipt, __poll_t mask,
5170 wait_queue_func_t wake_func)
5171 __acquires(&ctx->completion_lock)
5172{
5173 struct io_ring_ctx *ctx = req->ctx;
5174 bool cancel = false;
5175
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005176 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005177 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005178 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005179 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005180
5181 ipt->pt._key = mask;
5182 ipt->req = req;
5183 ipt->error = -EINVAL;
5184
Jens Axboed7718a92020-02-14 22:23:12 -07005185 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5186
5187 spin_lock_irq(&ctx->completion_lock);
5188 if (likely(poll->head)) {
5189 spin_lock(&poll->head->lock);
5190 if (unlikely(list_empty(&poll->wait.entry))) {
5191 if (ipt->error)
5192 cancel = true;
5193 ipt->error = 0;
5194 mask = 0;
5195 }
5196 if (mask || ipt->error)
5197 list_del_init(&poll->wait.entry);
5198 else if (cancel)
5199 WRITE_ONCE(poll->canceled, true);
5200 else if (!poll->done) /* actually waiting for an event */
5201 io_poll_req_insert(req);
5202 spin_unlock(&poll->head->lock);
5203 }
5204
5205 return mask;
5206}
5207
5208static bool io_arm_poll_handler(struct io_kiocb *req)
5209{
5210 const struct io_op_def *def = &io_op_defs[req->opcode];
5211 struct io_ring_ctx *ctx = req->ctx;
5212 struct async_poll *apoll;
5213 struct io_poll_table ipt;
5214 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005215 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005216
5217 if (!req->file || !file_can_poll(req->file))
5218 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005219 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005220 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005221 if (def->pollin)
5222 rw = READ;
5223 else if (def->pollout)
5224 rw = WRITE;
5225 else
5226 return false;
5227 /* if we can't nonblock try, then no point in arming a poll handler */
5228 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005229 return false;
5230
5231 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5232 if (unlikely(!apoll))
5233 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005234 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005235
5236 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005237 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005238
Nathan Chancellor8755d972020-03-02 16:01:19 -07005239 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005240 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005241 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005242 if (def->pollout)
5243 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005244
5245 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5246 if ((req->opcode == IORING_OP_RECVMSG) &&
5247 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5248 mask &= ~POLLIN;
5249
Jens Axboed7718a92020-02-14 22:23:12 -07005250 mask |= POLLERR | POLLPRI;
5251
5252 ipt.pt._qproc = io_async_queue_proc;
5253
5254 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5255 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005256 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005257 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005258 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005259 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005260 kfree(apoll);
5261 return false;
5262 }
5263 spin_unlock_irq(&ctx->completion_lock);
5264 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5265 apoll->poll.events);
5266 return true;
5267}
5268
5269static bool __io_poll_remove_one(struct io_kiocb *req,
5270 struct io_poll_iocb *poll)
5271{
Jens Axboeb41e9852020-02-17 09:52:41 -07005272 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005273
5274 spin_lock(&poll->head->lock);
5275 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005276 if (!list_empty(&poll->wait.entry)) {
5277 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005278 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005279 }
5280 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005281 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005282 return do_complete;
5283}
5284
5285static bool io_poll_remove_one(struct io_kiocb *req)
5286{
5287 bool do_complete;
5288
Jens Axboed4e7cd32020-08-15 11:44:50 -07005289 io_poll_remove_double(req);
5290
Jens Axboed7718a92020-02-14 22:23:12 -07005291 if (req->opcode == IORING_OP_POLL_ADD) {
5292 do_complete = __io_poll_remove_one(req, &req->poll);
5293 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005294 struct async_poll *apoll = req->apoll;
5295
Jens Axboed7718a92020-02-14 22:23:12 -07005296 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005297 do_complete = __io_poll_remove_one(req, &apoll->poll);
5298 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005299 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005300 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005301 kfree(apoll);
5302 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005303 }
5304
Jens Axboeb41e9852020-02-17 09:52:41 -07005305 if (do_complete) {
5306 io_cqring_fill_event(req, -ECANCELED);
5307 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005308 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005309 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005310 }
5311
5312 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005313}
5314
Jens Axboe76e1b642020-09-26 15:05:03 -06005315/*
5316 * Returns true if we found and killed one or more poll requests
5317 */
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00005318static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5319 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005320{
Jens Axboe78076bb2019-12-04 19:56:40 -07005321 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005322 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005323 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005324
5325 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005326 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5327 struct hlist_head *list;
5328
5329 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005330 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00005331 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005332 posted += io_poll_remove_one(req);
5333 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005334 }
5335 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005336
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005337 if (posted)
5338 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005339
5340 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005341}
5342
Jens Axboe47f46762019-11-09 17:43:02 -07005343static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5344{
Jens Axboe78076bb2019-12-04 19:56:40 -07005345 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005346 struct io_kiocb *req;
5347
Jens Axboe78076bb2019-12-04 19:56:40 -07005348 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5349 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005350 if (sqe_addr != req->user_data)
5351 continue;
5352 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005353 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005354 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005355 }
5356
5357 return -ENOENT;
5358}
5359
Jens Axboe3529d8c2019-12-19 18:24:38 -07005360static int io_poll_remove_prep(struct io_kiocb *req,
5361 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005362{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005363 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5364 return -EINVAL;
5365 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5366 sqe->poll_events)
5367 return -EINVAL;
5368
Jens Axboe0969e782019-12-17 18:40:57 -07005369 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005370 return 0;
5371}
5372
5373/*
5374 * Find a running poll command that matches one specified in sqe->addr,
5375 * and remove it if found.
5376 */
5377static int io_poll_remove(struct io_kiocb *req)
5378{
5379 struct io_ring_ctx *ctx = req->ctx;
5380 u64 addr;
5381 int ret;
5382
Jens Axboe0969e782019-12-17 18:40:57 -07005383 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005384 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005385 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005386 spin_unlock_irq(&ctx->completion_lock);
5387
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005388 if (ret < 0)
5389 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005390 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005391 return 0;
5392}
5393
Jens Axboe221c5eb2019-01-17 09:41:58 -07005394static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5395 void *key)
5396{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005397 struct io_kiocb *req = wait->private;
5398 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005399
Jens Axboed7718a92020-02-14 22:23:12 -07005400 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005401}
5402
Jens Axboe221c5eb2019-01-17 09:41:58 -07005403static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5404 struct poll_table_struct *p)
5405{
5406 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5407
Jens Axboee8c2bc12020-08-15 18:44:09 -07005408 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005409}
5410
Jens Axboe3529d8c2019-12-19 18:24:38 -07005411static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005412{
5413 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005414 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005415
5416 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5417 return -EINVAL;
5418 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5419 return -EINVAL;
5420
Jiufei Xue5769a352020-06-17 17:53:55 +08005421 events = READ_ONCE(sqe->poll32_events);
5422#ifdef __BIG_ENDIAN
5423 events = swahw32(events);
5424#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005425 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5426 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005427 return 0;
5428}
5429
Pavel Begunkov014db002020-03-03 21:33:12 +03005430static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005431{
5432 struct io_poll_iocb *poll = &req->poll;
5433 struct io_ring_ctx *ctx = req->ctx;
5434 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005435 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005436
Jens Axboed7718a92020-02-14 22:23:12 -07005437 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005438
Jens Axboed7718a92020-02-14 22:23:12 -07005439 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5440 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005441
Jens Axboe8c838782019-03-12 15:48:16 -06005442 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005443 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005444 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005445 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005446 spin_unlock_irq(&ctx->completion_lock);
5447
Jens Axboe8c838782019-03-12 15:48:16 -06005448 if (mask) {
5449 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005450 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005451 }
Jens Axboe8c838782019-03-12 15:48:16 -06005452 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005453}
5454
Jens Axboe5262f562019-09-17 12:26:57 -06005455static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5456{
Jens Axboead8a48a2019-11-15 08:49:11 -07005457 struct io_timeout_data *data = container_of(timer,
5458 struct io_timeout_data, timer);
5459 struct io_kiocb *req = data->req;
5460 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005461 unsigned long flags;
5462
Jens Axboe5262f562019-09-17 12:26:57 -06005463 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005464 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005465 atomic_set(&req->ctx->cq_timeouts,
5466 atomic_read(&req->ctx->cq_timeouts) + 1);
5467
Jens Axboe78e19bb2019-11-06 15:21:34 -07005468 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005469 io_commit_cqring(ctx);
5470 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5471
5472 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005473 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005474 io_put_req(req);
5475 return HRTIMER_NORESTART;
5476}
5477
Jens Axboef254ac02020-08-12 17:33:30 -06005478static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005479{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005480 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005481 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005482
Jens Axboee8c2bc12020-08-15 18:44:09 -07005483 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005484 if (ret == -1)
5485 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005486 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005487
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005488 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005489 io_cqring_fill_event(req, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005490 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07005491 return 0;
5492}
5493
Jens Axboef254ac02020-08-12 17:33:30 -06005494static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5495{
5496 struct io_kiocb *req;
5497 int ret = -ENOENT;
5498
5499 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5500 if (user_data == req->user_data) {
5501 ret = 0;
5502 break;
5503 }
5504 }
5505
5506 if (ret == -ENOENT)
5507 return ret;
5508
5509 return __io_timeout_cancel(req);
5510}
5511
Jens Axboe3529d8c2019-12-19 18:24:38 -07005512static int io_timeout_remove_prep(struct io_kiocb *req,
5513 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005514{
Jens Axboeb29472e2019-12-17 18:50:29 -07005515 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5516 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005517 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5518 return -EINVAL;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005519 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
Jens Axboeb29472e2019-12-17 18:50:29 -07005520 return -EINVAL;
5521
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005522 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005523 return 0;
5524}
5525
Jens Axboe11365042019-10-16 09:08:32 -06005526/*
5527 * Remove or update an existing timeout command
5528 */
Jens Axboefc4df992019-12-10 14:38:45 -07005529static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005530{
5531 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005532 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005533
Jens Axboe11365042019-10-16 09:08:32 -06005534 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005535 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005536
Jens Axboe47f46762019-11-09 17:43:02 -07005537 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005538 io_commit_cqring(ctx);
5539 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005540 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005541 if (ret < 0)
5542 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005543 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005544 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005545}
5546
Jens Axboe3529d8c2019-12-19 18:24:38 -07005547static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005548 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005549{
Jens Axboead8a48a2019-11-15 08:49:11 -07005550 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005551 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005552 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005553
Jens Axboead8a48a2019-11-15 08:49:11 -07005554 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005555 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005556 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005557 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005558 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005559 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005560 flags = READ_ONCE(sqe->timeout_flags);
5561 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005562 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005563
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005564 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005565
Jens Axboee8c2bc12020-08-15 18:44:09 -07005566 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005567 return -ENOMEM;
5568
Jens Axboee8c2bc12020-08-15 18:44:09 -07005569 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005570 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005571
5572 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005573 return -EFAULT;
5574
Jens Axboe11365042019-10-16 09:08:32 -06005575 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005576 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005577 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005578 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005579
Jens Axboead8a48a2019-11-15 08:49:11 -07005580 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5581 return 0;
5582}
5583
Jens Axboefc4df992019-12-10 14:38:45 -07005584static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005585{
Jens Axboead8a48a2019-11-15 08:49:11 -07005586 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005587 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005588 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005589 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005590
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005591 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005592
Jens Axboe5262f562019-09-17 12:26:57 -06005593 /*
5594 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005595 * timeout event to be satisfied. If it isn't set, then this is
5596 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005597 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005598 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005599 entry = ctx->timeout_list.prev;
5600 goto add;
5601 }
Jens Axboe5262f562019-09-17 12:26:57 -06005602
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005603 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5604 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005605
Marcelo Diop-Gonzalez2ca824c2021-01-15 11:54:40 -05005606 /* Update the last seq here in case io_flush_timeouts() hasn't.
5607 * This is safe because ->completion_lock is held, and submissions
5608 * and completions are never mixed in the same ->completion_lock section.
5609 */
5610 ctx->cq_last_tm_flush = tail;
5611
Jens Axboe5262f562019-09-17 12:26:57 -06005612 /*
5613 * Insertion sort, ensuring the first entry in the list is always
5614 * the one we need first.
5615 */
Jens Axboe5262f562019-09-17 12:26:57 -06005616 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005617 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5618 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005619
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005620 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005621 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005622 /* nxt.seq is behind @tail, otherwise would've been completed */
5623 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005624 break;
5625 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005626add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005627 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005628 data->timer.function = io_timeout_fn;
5629 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005630 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005631 return 0;
5632}
5633
Jens Axboe62755e32019-10-28 21:49:21 -06005634static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005635{
Jens Axboe62755e32019-10-28 21:49:21 -06005636 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005637
Jens Axboe62755e32019-10-28 21:49:21 -06005638 return req->user_data == (unsigned long) data;
5639}
5640
Jens Axboee977d6d2019-11-05 12:39:45 -07005641static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005642{
Jens Axboe62755e32019-10-28 21:49:21 -06005643 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005644 int ret = 0;
5645
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005646 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005647 switch (cancel_ret) {
5648 case IO_WQ_CANCEL_OK:
5649 ret = 0;
5650 break;
5651 case IO_WQ_CANCEL_RUNNING:
5652 ret = -EALREADY;
5653 break;
5654 case IO_WQ_CANCEL_NOTFOUND:
5655 ret = -ENOENT;
5656 break;
5657 }
5658
Jens Axboee977d6d2019-11-05 12:39:45 -07005659 return ret;
5660}
5661
Jens Axboe47f46762019-11-09 17:43:02 -07005662static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5663 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005664 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005665{
5666 unsigned long flags;
5667 int ret;
5668
5669 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5670 if (ret != -ENOENT) {
5671 spin_lock_irqsave(&ctx->completion_lock, flags);
5672 goto done;
5673 }
5674
5675 spin_lock_irqsave(&ctx->completion_lock, flags);
5676 ret = io_timeout_cancel(ctx, sqe_addr);
5677 if (ret != -ENOENT)
5678 goto done;
5679 ret = io_poll_cancel(ctx, sqe_addr);
5680done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005681 if (!ret)
5682 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005683 io_cqring_fill_event(req, ret);
5684 io_commit_cqring(ctx);
5685 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5686 io_cqring_ev_posted(ctx);
5687
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005688 if (ret < 0)
5689 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005690 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005691}
5692
Jens Axboe3529d8c2019-12-19 18:24:38 -07005693static int io_async_cancel_prep(struct io_kiocb *req,
5694 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005695{
Jens Axboefbf23842019-12-17 18:45:56 -07005696 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005697 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005698 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5699 return -EINVAL;
5700 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005701 return -EINVAL;
5702
Jens Axboefbf23842019-12-17 18:45:56 -07005703 req->cancel.addr = READ_ONCE(sqe->addr);
5704 return 0;
5705}
5706
Pavel Begunkov014db002020-03-03 21:33:12 +03005707static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005708{
5709 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005710
Pavel Begunkov014db002020-03-03 21:33:12 +03005711 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005712 return 0;
5713}
5714
Jens Axboe05f3fb32019-12-09 11:22:50 -07005715static int io_files_update_prep(struct io_kiocb *req,
5716 const struct io_uring_sqe *sqe)
5717{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005718 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5719 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005720 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5721 return -EINVAL;
5722 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005723 return -EINVAL;
5724
5725 req->files_update.offset = READ_ONCE(sqe->off);
5726 req->files_update.nr_args = READ_ONCE(sqe->len);
5727 if (!req->files_update.nr_args)
5728 return -EINVAL;
5729 req->files_update.arg = READ_ONCE(sqe->addr);
5730 return 0;
5731}
5732
Jens Axboe229a7b62020-06-22 10:13:11 -06005733static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5734 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005735{
5736 struct io_ring_ctx *ctx = req->ctx;
5737 struct io_uring_files_update up;
5738 int ret;
5739
Jens Axboef86cd202020-01-29 13:46:44 -07005740 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005741 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005742
5743 up.offset = req->files_update.offset;
5744 up.fds = req->files_update.arg;
5745
5746 mutex_lock(&ctx->uring_lock);
5747 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5748 mutex_unlock(&ctx->uring_lock);
5749
5750 if (ret < 0)
5751 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005752 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005753 return 0;
5754}
5755
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005756static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005757{
Jens Axboed625c6e2019-12-17 19:53:05 -07005758 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005759 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005760 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005761 case IORING_OP_READV:
5762 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005763 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005764 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005765 case IORING_OP_WRITEV:
5766 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005767 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005768 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005769 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005770 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005771 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005772 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005773 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005774 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005775 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005776 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005777 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005778 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005779 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005780 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005781 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005782 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005783 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005784 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005785 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005786 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005787 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005788 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005789 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005790 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005791 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005792 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005793 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005794 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005795 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005796 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005797 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005798 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005799 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005800 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005801 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005802 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005803 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005804 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005805 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005806 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005807 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005808 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005809 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005810 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005811 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005812 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005813 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005814 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005815 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005816 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005817 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005818 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005819 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005820 return io_tee_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005821 }
5822
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005823 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5824 req->opcode);
5825 return-EINVAL;
5826}
5827
Jens Axboedef596e2019-01-09 08:59:42 -07005828static int io_req_defer_prep(struct io_kiocb *req,
5829 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005830{
Jens Axboedef596e2019-01-09 08:59:42 -07005831 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005832 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005833 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005834 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005835 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005836}
5837
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005838static u32 io_get_sequence(struct io_kiocb *req)
5839{
5840 struct io_kiocb *pos;
5841 struct io_ring_ctx *ctx = req->ctx;
5842 u32 total_submitted, nr_reqs = 1;
5843
5844 if (req->flags & REQ_F_LINK_HEAD)
5845 list_for_each_entry(pos, &req->link_list, link_list)
5846 nr_reqs++;
5847
5848 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5849 return total_submitted - nr_reqs;
5850}
5851
Jens Axboe3529d8c2019-12-19 18:24:38 -07005852static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005853{
5854 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005855 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005856 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005857 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005858
5859 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005860 if (likely(list_empty_careful(&ctx->defer_list) &&
5861 !(req->flags & REQ_F_IO_DRAIN)))
5862 return 0;
5863
5864 seq = io_get_sequence(req);
5865 /* Still a chance to pass the sequence check */
5866 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005867 return 0;
5868
Jens Axboee8c2bc12020-08-15 18:44:09 -07005869 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005870 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005871 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005872 return ret;
5873 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005874 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005875 de = kmalloc(sizeof(*de), GFP_KERNEL);
5876 if (!de)
5877 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005878
5879 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005880 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005881 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005882 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005883 io_queue_async_work(req);
5884 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005885 }
5886
5887 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005888 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005889 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005890 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005891 spin_unlock_irq(&ctx->completion_lock);
5892 return -EIOCBQUEUED;
5893}
Jens Axboeedafcce2019-01-09 09:16:05 -07005894
Jens Axboef573d382020-09-22 10:19:24 -06005895static void io_req_drop_files(struct io_kiocb *req)
5896{
5897 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov52382df82021-02-09 04:47:44 +00005898 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06005899 unsigned long flags;
5900
Jens Axboed16692a2021-02-09 04:47:41 +00005901 if (req->work.flags & IO_WQ_WORK_FILES) {
5902 put_files_struct(req->work.identity->files);
5903 put_nsproxy(req->work.identity->nsproxy);
5904 }
Pavel Begunkov52504a62020-12-18 13:12:21 +00005905 spin_lock_irqsave(&ctx->inflight_lock, flags);
5906 list_del(&req->inflight_entry);
5907 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5908 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06005909 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkov52382df82021-02-09 04:47:44 +00005910 if (atomic_read(&tctx->in_idle))
5911 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06005912}
5913
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005914static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005915{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005916 if (req->flags & REQ_F_BUFFER_SELECTED) {
5917 switch (req->opcode) {
5918 case IORING_OP_READV:
5919 case IORING_OP_READ_FIXED:
5920 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005921 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005922 break;
5923 case IORING_OP_RECVMSG:
5924 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005925 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005926 break;
5927 }
5928 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005929 }
5930
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005931 if (req->flags & REQ_F_NEED_CLEANUP) {
5932 switch (req->opcode) {
5933 case IORING_OP_READV:
5934 case IORING_OP_READ_FIXED:
5935 case IORING_OP_READ:
5936 case IORING_OP_WRITEV:
5937 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005938 case IORING_OP_WRITE: {
5939 struct io_async_rw *io = req->async_data;
5940 if (io->free_iovec)
5941 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005942 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005943 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005944 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005945 case IORING_OP_SENDMSG: {
5946 struct io_async_msghdr *io = req->async_data;
5947 if (io->iov != io->fast_iov)
5948 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005949 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005950 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005951 case IORING_OP_SPLICE:
5952 case IORING_OP_TEE:
5953 io_put_file(req, req->splice.file_in,
5954 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5955 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005956 case IORING_OP_OPENAT:
5957 case IORING_OP_OPENAT2:
5958 if (req->open.filename)
5959 putname(req->open.filename);
5960 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005961 }
5962 req->flags &= ~REQ_F_NEED_CLEANUP;
5963 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005964}
5965
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005966static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
5967 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07005968{
Jens Axboeedafcce2019-01-09 09:16:05 -07005969 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005970 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005971
Jens Axboed625c6e2019-12-17 19:53:05 -07005972 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005973 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005974 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005975 break;
5976 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005977 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005978 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06005979 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005980 break;
5981 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005982 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005983 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06005984 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005985 break;
5986 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03005987 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005988 break;
5989 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03005990 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005991 break;
5992 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07005993 ret = io_poll_remove(req);
5994 break;
5995 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005996 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07005997 break;
5998 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005999 ret = io_sendmsg(req, force_nonblock, cs);
6000 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006001 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006002 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006003 break;
6004 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006005 ret = io_recvmsg(req, force_nonblock, cs);
6006 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006007 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006008 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006009 break;
6010 case IORING_OP_TIMEOUT:
6011 ret = io_timeout(req);
6012 break;
6013 case IORING_OP_TIMEOUT_REMOVE:
6014 ret = io_timeout_remove(req);
6015 break;
6016 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006017 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006018 break;
6019 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006020 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006021 break;
6022 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006023 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006024 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006025 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006026 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006027 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006028 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006029 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006030 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006031 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006032 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006033 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006034 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006035 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006036 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006037 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006038 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006039 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006040 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006041 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006042 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006043 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006044 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006045 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006046 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006047 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006048 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006049 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006050 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006051 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006052 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006053 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006054 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006055 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006056 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006057 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006058 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006059 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006061 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006062 ret = io_tee(req, force_nonblock);
6063 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006064 default:
6065 ret = -EINVAL;
6066 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006067 }
6068
6069 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006070 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006071
Jens Axboeb5325762020-05-19 21:20:27 -06006072 /* If the op doesn't have a file, we're not polling for it */
6073 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006074 const bool in_async = io_wq_current_is_worker();
6075
Jens Axboe11ba8202020-01-15 21:51:17 -07006076 /* workqueue context doesn't hold uring_lock, grab it now */
6077 if (in_async)
6078 mutex_lock(&ctx->uring_lock);
6079
Jens Axboe2b188cc2019-01-07 10:46:33 -07006080 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006081
6082 if (in_async)
6083 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006084 }
6085
6086 return 0;
6087}
6088
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006089static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006090{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006092 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006093 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006095 timeout = io_prep_linked_timeout(req);
6096 if (timeout)
6097 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006098
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006099 /* if NO_CANCEL is set, we must still run the work */
6100 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6101 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006102 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006103 }
Jens Axboe31b51512019-01-18 22:56:34 -07006104
Jens Axboe561fb042019-10-24 07:25:42 -06006105 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006106 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006107 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006108 /*
6109 * We can get EAGAIN for polled IO even though we're
6110 * forcing a sync submission from here, since we can't
6111 * wait for request slots on the block side.
6112 */
6113 if (ret != -EAGAIN)
6114 break;
6115 cond_resched();
6116 } while (1);
6117 }
Jens Axboe31b51512019-01-18 22:56:34 -07006118
Jens Axboe561fb042019-10-24 07:25:42 -06006119 if (ret) {
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006120 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangcd13f1d2020-12-06 22:22:42 +00006121
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006122 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6123 lock_ctx = req->ctx;
6124
6125 /*
6126 * io_iopoll_complete() does not hold completion_lock to
6127 * complete polled io, so here for polled io, we can not call
6128 * io_req_complete() directly, otherwise there maybe concurrent
6129 * access to cqring, defer_list, etc, which is not safe. Given
6130 * that io_iopoll_complete() is always called under uring_lock,
6131 * so here for polled io, we also get uring_lock to complete
6132 * it.
6133 */
6134 if (lock_ctx)
6135 mutex_lock(&lock_ctx->uring_lock);
6136
6137 req_set_fail_links(req);
6138 io_req_complete(req, ret);
6139
6140 if (lock_ctx)
6141 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006142 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006143
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006144 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006145}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006146
Jens Axboe65e19f52019-10-26 07:20:21 -06006147static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6148 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006149{
Jens Axboe65e19f52019-10-26 07:20:21 -06006150 struct fixed_file_table *table;
6151
Jens Axboe05f3fb32019-12-09 11:22:50 -07006152 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006153 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006154}
6155
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006156static struct file *io_file_get(struct io_submit_state *state,
6157 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006158{
6159 struct io_ring_ctx *ctx = req->ctx;
6160 struct file *file;
6161
6162 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006163 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006164 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006165 fd = array_index_nospec(fd, ctx->nr_user_files);
6166 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006167 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01006168 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06006169 percpu_ref_get(req->fixed_file_refs);
6170 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006171 } else {
6172 trace_io_uring_file_get(ctx, fd);
6173 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006174 }
6175
Pavel Begunkov8c7febf2021-02-09 04:47:47 +00006176 if (file && file->f_op == &io_uring_fops &&
6177 !(req->flags & REQ_F_INFLIGHT)) {
Jens Axboed16692a2021-02-09 04:47:41 +00006178 io_req_init_async(req);
6179 req->flags |= REQ_F_INFLIGHT;
6180
6181 spin_lock_irq(&ctx->inflight_lock);
6182 list_add(&req->inflight_entry, &ctx->inflight_list);
6183 spin_unlock_irq(&ctx->inflight_lock);
6184 }
6185
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006186 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006187}
6188
Jens Axboe3529d8c2019-12-19 18:24:38 -07006189static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006190 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006191{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006192 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006193
Jens Axboe63ff8222020-05-07 14:56:15 -06006194 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006195 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006196 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006197
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006198 req->file = io_file_get(state, req, fd, fixed);
6199 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07006200 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006201 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006202}
6203
Jens Axboe2665abf2019-11-05 12:40:47 -07006204static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6205{
Jens Axboead8a48a2019-11-15 08:49:11 -07006206 struct io_timeout_data *data = container_of(timer,
6207 struct io_timeout_data, timer);
6208 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006209 struct io_ring_ctx *ctx = req->ctx;
6210 struct io_kiocb *prev = NULL;
6211 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006212
6213 spin_lock_irqsave(&ctx->completion_lock, flags);
6214
6215 /*
6216 * We don't expect the list to be empty, that will only happen if we
6217 * race with the completion of the linked work.
6218 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006219 if (!list_empty(&req->link_list)) {
6220 prev = list_entry(req->link_list.prev, struct io_kiocb,
6221 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006222 if (refcount_inc_not_zero(&prev->refs))
Pavel Begunkov44932332019-12-05 16:16:35 +03006223 list_del_init(&req->link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006224 else
Jens Axboe76a46e02019-11-10 23:34:16 -07006225 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006226 }
6227
6228 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6229
6230 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006231 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006232 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006233 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006234 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006235 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006236 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006237 return HRTIMER_NORESTART;
6238}
6239
Jens Axboe7271ef32020-08-10 09:55:22 -06006240static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006241{
Jens Axboe76a46e02019-11-10 23:34:16 -07006242 /*
6243 * If the list is now empty, then our linked request finished before
6244 * we got a chance to setup the timer
6245 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006246 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006247 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006248
Jens Axboead8a48a2019-11-15 08:49:11 -07006249 data->timer.function = io_link_timeout_fn;
6250 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6251 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006252 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006253}
6254
6255static void io_queue_linked_timeout(struct io_kiocb *req)
6256{
6257 struct io_ring_ctx *ctx = req->ctx;
6258
6259 spin_lock_irq(&ctx->completion_lock);
6260 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006261 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006262
Jens Axboe2665abf2019-11-05 12:40:47 -07006263 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006264 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006265}
6266
Jens Axboead8a48a2019-11-15 08:49:11 -07006267static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006268{
6269 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006270
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006271 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006272 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006273 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006274 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006275
Pavel Begunkov44932332019-12-05 16:16:35 +03006276 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6277 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006278 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006279 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006280
Pavel Begunkov900fad42020-10-19 16:39:16 +01006281 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006282 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006283 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006284}
6285
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006286static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006287{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006288 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006289 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290 int ret;
6291
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006292again:
6293 linked_timeout = io_prep_linked_timeout(req);
6294
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006295 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6296 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006297 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006298 if (old_creds)
6299 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006300 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006301 old_creds = NULL; /* restored original creds */
6302 else
Jens Axboe98447d62020-10-14 10:48:51 -06006303 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006304 }
6305
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006306 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006307
6308 /*
6309 * We async punt it if the file wasn't marked NOWAIT, or if the file
6310 * doesn't support non-blocking read/write attempts
6311 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006312 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006313 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006314 /*
6315 * Queued up for async execution, worker will release
6316 * submit reference when the iocb is actually submitted.
6317 */
6318 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006320
Pavel Begunkovf063c542020-07-25 14:41:59 +03006321 if (linked_timeout)
6322 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006323 } else if (likely(!ret)) {
6324 /* drop submission reference */
6325 req = io_put_req_find_next(req);
6326 if (linked_timeout)
6327 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006328
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006329 if (req) {
6330 if (!(req->flags & REQ_F_FORCE_ASYNC))
6331 goto again;
6332 io_queue_async_work(req);
6333 }
6334 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006335 /* un-prep timeout, so it'll be killed as any other linked */
6336 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006337 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006338 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006339 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006340 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006341
Jens Axboe193155c2020-02-22 23:22:19 -07006342 if (old_creds)
6343 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344}
6345
Jens Axboef13fad72020-06-22 09:34:30 -06006346static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6347 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006348{
6349 int ret;
6350
Jens Axboe3529d8c2019-12-19 18:24:38 -07006351 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006352 if (ret) {
6353 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006354fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006355 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006356 io_put_req(req);
6357 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006358 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006359 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006360 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006361 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006362 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006363 goto fail_req;
6364 }
Jens Axboece35a472019-12-17 08:04:44 -07006365 io_queue_async_work(req);
6366 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006367 if (sqe) {
6368 ret = io_req_prep(req, sqe);
6369 if (unlikely(ret))
6370 goto fail_req;
6371 }
6372 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006373 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006374}
6375
Jens Axboef13fad72020-06-22 09:34:30 -06006376static inline void io_queue_link_head(struct io_kiocb *req,
6377 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006378{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006379 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006380 io_put_req(req);
6381 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006382 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006383 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006384}
6385
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006386static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006387 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006388{
Jackie Liua197f662019-11-08 08:09:12 -07006389 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006390 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006391
Jens Axboe9e645e112019-05-10 16:07:28 -06006392 /*
6393 * If we already have a head request, queue this one for async
6394 * submittal once the head completes. If we don't have a head but
6395 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6396 * submitted sync once the chain is complete. If none of those
6397 * conditions are true (normal request), then just queue it.
6398 */
6399 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006400 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006401
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006402 /*
6403 * Taking sequential execution of a link, draining both sides
6404 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6405 * requests in the link. So, it drains the head and the
6406 * next after the link request. The last one is done via
6407 * drain_next flag to persist the effect across calls.
6408 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006409 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006410 head->flags |= REQ_F_IO_DRAIN;
6411 ctx->drain_next = 1;
6412 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006413 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006414 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006415 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006416 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006417 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006418 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006419 trace_io_uring_link(ctx, req, head);
6420 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006421
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006422 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006423 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006424 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006425 *link = NULL;
6426 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006427 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006428 if (unlikely(ctx->drain_next)) {
6429 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006430 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006431 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006432 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006433 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006434 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006435
Pavel Begunkov711be032020-01-17 03:57:59 +03006436 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006437 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006438 req->flags |= REQ_F_FAIL_LINK;
6439 *link = req;
6440 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006441 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006442 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006443 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006444
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006445 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006446}
6447
Jens Axboe9a56a232019-01-09 09:06:50 -07006448/*
6449 * Batched submission is done, ensure local IO is flushed out.
6450 */
6451static void io_submit_state_end(struct io_submit_state *state)
6452{
Jens Axboef13fad72020-06-22 09:34:30 -06006453 if (!list_empty(&state->comp.list))
6454 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006455 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006456 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006457 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006458 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006459}
6460
6461/*
6462 * Start submission side cache.
6463 */
6464static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006465 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006466{
6467 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006468 state->comp.nr = 0;
6469 INIT_LIST_HEAD(&state->comp.list);
6470 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006471 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006472 state->file = NULL;
6473 state->ios_left = max_ios;
6474}
6475
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476static void io_commit_sqring(struct io_ring_ctx *ctx)
6477{
Hristo Venev75b28af2019-08-26 17:23:46 +00006478 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006479
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006480 /*
6481 * Ensure any loads from the SQEs are done at this point,
6482 * since once we write the new head, the application could
6483 * write new data to them.
6484 */
6485 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006486}
6487
6488/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006489 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006490 * that is mapped by userspace. This means that care needs to be taken to
6491 * ensure that reads are stable, as we cannot rely on userspace always
6492 * being a good citizen. If members of the sqe are validated and then later
6493 * used, it's important that those reads are done through READ_ONCE() to
6494 * prevent a re-load down the line.
6495 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006496static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006497{
Hristo Venev75b28af2019-08-26 17:23:46 +00006498 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006499 unsigned head;
6500
6501 /*
6502 * The cached sq head (or cq tail) serves two purposes:
6503 *
6504 * 1) allows us to batch the cost of updating the user visible
6505 * head updates.
6506 * 2) allows the kernel side to track the head on its own, even
6507 * though the application is the one updating it.
6508 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006509 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006510 if (likely(head < ctx->sq_entries))
6511 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006512
6513 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006514 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006515 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006516 return NULL;
6517}
6518
6519static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6520{
6521 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006522}
6523
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006524/*
6525 * Check SQE restrictions (opcode and flags).
6526 *
6527 * Returns 'true' if SQE is allowed, 'false' otherwise.
6528 */
6529static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6530 struct io_kiocb *req,
6531 unsigned int sqe_flags)
6532{
6533 if (!ctx->restricted)
6534 return true;
6535
6536 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6537 return false;
6538
6539 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6540 ctx->restrictions.sqe_flags_required)
6541 return false;
6542
6543 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6544 ctx->restrictions.sqe_flags_required))
6545 return false;
6546
6547 return true;
6548}
6549
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006550#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6551 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6552 IOSQE_BUFFER_SELECT)
6553
6554static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6555 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006556 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006557{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006558 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006559 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006560
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006561 req->opcode = READ_ONCE(sqe->opcode);
6562 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006563 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006564 req->file = NULL;
6565 req->ctx = ctx;
6566 req->flags = 0;
6567 /* one is dropped after submission, the other at completion */
6568 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006569 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006570 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006571
6572 if (unlikely(req->opcode >= IORING_OP_LAST))
6573 return -EINVAL;
6574
Jens Axboe9d8426a2020-06-16 18:42:49 -06006575 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6576 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006577
6578 sqe_flags = READ_ONCE(sqe->flags);
6579 /* enforce forwards compatibility on users */
6580 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6581 return -EINVAL;
6582
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006583 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6584 return -EACCES;
6585
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006586 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6587 !io_op_defs[req->opcode].buffer_select)
6588 return -EOPNOTSUPP;
6589
6590 id = READ_ONCE(sqe->personality);
6591 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006592 struct io_identity *iod;
6593
Jens Axboe1e6fa522020-10-15 08:46:24 -06006594 iod = idr_find(&ctx->personality_idr, id);
6595 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006596 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006597 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006598
6599 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006600 get_cred(iod->creds);
6601 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006602 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006603 }
6604
6605 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006606 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006607
Jens Axboe63ff8222020-05-07 14:56:15 -06006608 if (!io_op_defs[req->opcode].needs_file)
6609 return 0;
6610
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006611 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6612 state->ios_left--;
6613 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006614}
6615
Jens Axboe0f212202020-09-13 13:09:39 -06006616static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006617{
Jens Axboeac8691c2020-06-01 08:30:41 -06006618 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006619 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006620 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006621
Jens Axboec4a2ed72019-11-21 21:01:26 -07006622 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006623 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006624 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006625 return -EBUSY;
6626 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006627
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006628 /* make sure SQ entry isn't read before tail */
6629 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006630
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006631 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6632 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006633
Jens Axboed8a6df12020-10-15 16:24:45 -06006634 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006635 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006636
Jens Axboe6c271ce2019-01-10 11:22:30 -07006637 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006638
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006640 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006641 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006642 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006643
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006644 sqe = io_get_sqe(ctx);
6645 if (unlikely(!sqe)) {
6646 io_consume_sqe(ctx);
6647 break;
6648 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006649 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006650 if (unlikely(!req)) {
6651 if (!submitted)
6652 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006653 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006654 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006655 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006656 /* will complete beyond this point, count as submitted */
6657 submitted++;
6658
Pavel Begunkov692d8362020-10-10 18:34:13 +01006659 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006660 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006661fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006662 io_put_req(req);
6663 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006664 break;
6665 }
6666
Jens Axboe354420f2020-01-08 18:55:15 -07006667 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006668 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006669 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006670 if (err)
6671 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006672 }
6673
Pavel Begunkov9466f432020-01-25 22:34:01 +03006674 if (unlikely(submitted != nr)) {
6675 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006676 struct io_uring_task *tctx = current->io_uring;
6677 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006678
Jens Axboed8a6df12020-10-15 16:24:45 -06006679 percpu_ref_put_many(&ctx->refs, unused);
6680 percpu_counter_sub(&tctx->inflight, unused);
6681 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006682 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006683 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006684 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006685 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006686
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006687 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6688 io_commit_sqring(ctx);
6689
Jens Axboe6c271ce2019-01-10 11:22:30 -07006690 return submitted;
6691}
6692
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006693static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6694{
6695 /* Tell userspace we may need a wakeup call */
6696 spin_lock_irq(&ctx->completion_lock);
6697 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6698 spin_unlock_irq(&ctx->completion_lock);
6699}
6700
6701static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6702{
6703 spin_lock_irq(&ctx->completion_lock);
6704 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6705 spin_unlock_irq(&ctx->completion_lock);
6706}
6707
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006708static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6709 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006710{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006711 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6712 int ret;
6713
6714 ret = autoremove_wake_function(wqe, mode, sync, key);
6715 if (ret) {
6716 unsigned long flags;
6717
6718 spin_lock_irqsave(&ctx->completion_lock, flags);
6719 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6720 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6721 }
6722 return ret;
6723}
6724
Jens Axboec8d1ba52020-09-14 11:07:26 -06006725enum sq_ret {
6726 SQT_IDLE = 1,
6727 SQT_SPIN = 2,
6728 SQT_DID_WORK = 4,
6729};
6730
6731static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006732 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006733{
6734 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006735 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006736 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006737 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006738
Jens Axboec8d1ba52020-09-14 11:07:26 -06006739again:
6740 if (!list_empty(&ctx->iopoll_list)) {
6741 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006742
Jens Axboec8d1ba52020-09-14 11:07:26 -06006743 mutex_lock(&ctx->uring_lock);
6744 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6745 io_do_iopoll(ctx, &nr_events, 0);
6746 mutex_unlock(&ctx->uring_lock);
6747 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006748
Jens Axboec8d1ba52020-09-14 11:07:26 -06006749 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006750
Jens Axboec8d1ba52020-09-14 11:07:26 -06006751 /*
6752 * If submit got -EBUSY, flag us as needing the application
6753 * to enter the kernel to reap and flush events.
6754 */
6755 if (!to_submit || ret == -EBUSY || need_resched()) {
6756 /*
6757 * Drop cur_mm before scheduling, we can't hold it for
6758 * long periods (or over schedule()). Do this before
6759 * adding ourselves to the waitqueue, as the unuse/drop
6760 * may sleep.
6761 */
6762 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006763
Jens Axboec8d1ba52020-09-14 11:07:26 -06006764 /*
6765 * We're polling. If we're within the defined idle
6766 * period, then let us spin without work before going
6767 * to sleep. The exception is if we got EBUSY doing
6768 * more IO, we should wait for the application to
6769 * reap events and wake us up.
6770 */
6771 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6772 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6773 !percpu_ref_is_dying(&ctx->refs)))
6774 return SQT_SPIN;
6775
Jens Axboe534ca6d2020-09-02 13:52:19 -06006776 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006777 TASK_INTERRUPTIBLE);
6778
6779 /*
6780 * While doing polled IO, before going to sleep, we need
6781 * to check if there are new reqs added to iopoll_list,
6782 * it is because reqs may have been punted to io worker
6783 * and will be added to iopoll_list later, hence check
6784 * the iopoll_list again.
6785 */
6786 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6787 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006788 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006789 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006790 }
6791
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006792 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006793 if (!to_submit || ret == -EBUSY)
6794 return SQT_IDLE;
6795 }
6796
Jens Axboe534ca6d2020-09-02 13:52:19 -06006797 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006798 io_ring_clear_wakeup_flag(ctx);
6799
Jens Axboee95eee22020-09-08 09:11:32 -06006800 /* if we're handling multiple rings, cap submit size for fairness */
6801 if (cap_entries && to_submit > 8)
6802 to_submit = 8;
6803
Jens Axboec8d1ba52020-09-14 11:07:26 -06006804 mutex_lock(&ctx->uring_lock);
Pavel Begunkova63d9152021-01-26 11:17:03 +00006805 if (likely(!percpu_ref_is_dying(&ctx->refs) && !ctx->sqo_dead))
Jens Axboec8d1ba52020-09-14 11:07:26 -06006806 ret = io_submit_sqes(ctx, to_submit);
6807 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006808
6809 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6810 wake_up(&ctx->sqo_sq_wait);
6811
Jens Axboec8d1ba52020-09-14 11:07:26 -06006812 return SQT_DID_WORK;
6813}
6814
Jens Axboe69fb2132020-09-14 11:16:23 -06006815static void io_sqd_init_new(struct io_sq_data *sqd)
6816{
6817 struct io_ring_ctx *ctx;
6818
6819 while (!list_empty(&sqd->ctx_new_list)) {
6820 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6821 init_wait(&ctx->sqo_wait_entry);
6822 ctx->sqo_wait_entry.func = io_sq_wake_function;
6823 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6824 complete(&ctx->sq_thread_comp);
6825 }
6826}
6827
Jens Axboe6c271ce2019-01-10 11:22:30 -07006828static int io_sq_thread(void *data)
6829{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006830 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006831 const struct cred *old_cred = NULL;
6832 struct io_sq_data *sqd = data;
6833 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006834 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006835
Jens Axboec8d1ba52020-09-14 11:07:26 -06006836 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006837 while (!kthread_should_stop()) {
6838 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006839 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006840
6841 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006842 * Any changes to the sqd lists are synchronized through the
6843 * kthread parking. This synchronizes the thread vs users,
6844 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006845 */
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006846 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006847 kthread_parkme();
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006848 /*
6849 * When sq thread is unparked, in case the previous park operation
6850 * comes from io_put_sq_data(), which means that sq thread is going
6851 * to be stopped, so here needs to have a check.
6852 */
6853 if (kthread_should_stop())
6854 break;
6855 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006856
6857 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6858 io_sqd_init_new(sqd);
6859
Jens Axboee95eee22020-09-08 09:11:32 -06006860 cap_entries = !list_is_singular(&sqd->ctx_list);
6861
Jens Axboe69fb2132020-09-14 11:16:23 -06006862 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6863 if (current->cred != ctx->creds) {
6864 if (old_cred)
6865 revert_creds(old_cred);
6866 old_cred = override_creds(ctx->creds);
6867 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006868 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006869#ifdef CONFIG_AUDIT
6870 current->loginuid = ctx->loginuid;
6871 current->sessionid = ctx->sessionid;
6872#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06006873
Jens Axboee95eee22020-09-08 09:11:32 -06006874 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006875
Jens Axboe4349f302020-07-09 15:07:01 -06006876 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006877 }
6878
Jens Axboe69fb2132020-09-14 11:16:23 -06006879 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006880 io_run_task_work();
Pavel Begunkovf7f32822021-01-11 04:00:30 +00006881 io_sq_thread_drop_mm();
Jens Axboec8d1ba52020-09-14 11:07:26 -06006882 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006883 } else if (ret == SQT_IDLE) {
6884 if (kthread_should_park())
6885 continue;
6886 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6887 io_ring_set_wakeup_flag(ctx);
6888 schedule();
6889 start_jiffies = jiffies;
6890 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6891 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006892 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006893 }
6894
Jens Axboe4c6e2772020-07-01 11:29:10 -06006895 io_run_task_work();
Pavel Begunkovf7f32822021-01-11 04:00:30 +00006896 io_sq_thread_drop_mm();
Jens Axboeb41e9852020-02-17 09:52:41 -07006897
Dennis Zhou91d8f512020-09-16 13:41:05 -07006898 if (cur_css)
6899 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006900 if (old_cred)
6901 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006902
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006903 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006904
Jens Axboe6c271ce2019-01-10 11:22:30 -07006905 return 0;
6906}
6907
Jens Axboebda52162019-09-24 13:47:15 -06006908struct io_wait_queue {
6909 struct wait_queue_entry wq;
6910 struct io_ring_ctx *ctx;
6911 unsigned to_wait;
6912 unsigned nr_timeouts;
6913};
6914
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006915static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006916{
6917 struct io_ring_ctx *ctx = iowq->ctx;
6918
6919 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006920 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006921 * started waiting. For timeouts, we always want to return to userspace,
6922 * regardless of event count.
6923 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006924 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006925 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6926}
6927
6928static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6929 int wake_flags, void *key)
6930{
6931 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6932 wq);
6933
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006934 /*
6935 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6936 * the task, and the next invocation will do it.
6937 */
6938 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6939 return autoremove_wake_function(curr, mode, wake_flags, key);
6940 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006941}
6942
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006943static int io_run_task_work_sig(void)
6944{
6945 if (io_run_task_work())
6946 return 1;
6947 if (!signal_pending(current))
6948 return 0;
6949 if (current->jobctl & JOBCTL_TASK_WORK) {
6950 spin_lock_irq(&current->sighand->siglock);
6951 current->jobctl &= ~JOBCTL_TASK_WORK;
6952 recalc_sigpending();
6953 spin_unlock_irq(&current->sighand->siglock);
6954 return 1;
6955 }
6956 return -EINTR;
6957}
6958
Jens Axboe2b188cc2019-01-07 10:46:33 -07006959/*
6960 * Wait until events become available, if we don't already have some. The
6961 * application must reap them itself, as they reside on the shared cq ring.
6962 */
6963static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6964 const sigset_t __user *sig, size_t sigsz)
6965{
Jens Axboebda52162019-09-24 13:47:15 -06006966 struct io_wait_queue iowq = {
6967 .wq = {
6968 .private = current,
6969 .func = io_wake_function,
6970 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6971 },
6972 .ctx = ctx,
6973 .to_wait = min_events,
6974 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006975 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006976 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006977
Jens Axboeb41e9852020-02-17 09:52:41 -07006978 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006979 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6980 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006981 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006982 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006983 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006984 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006985
6986 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006987#ifdef CONFIG_COMPAT
6988 if (in_compat_syscall())
6989 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006990 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006991 else
6992#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006993 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006994
Jens Axboe2b188cc2019-01-07 10:46:33 -07006995 if (ret)
6996 return ret;
6997 }
6998
Jens Axboebda52162019-09-24 13:47:15 -06006999 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007000 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007001 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007002 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007003 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7004 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007005 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007006 ret = io_run_task_work_sig();
Hao Xu7250f332021-02-09 04:47:46 +00007007 if (ret > 0) {
7008 finish_wait(&ctx->wait, &iowq.wq);
Jens Axboe4c6e2772020-07-01 11:29:10 -06007009 continue;
Hao Xu7250f332021-02-09 04:47:46 +00007010 }
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007011 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007012 break;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007013 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007014 break;
Hao Xu7250f332021-02-09 04:47:46 +00007015 if (test_bit(0, &ctx->cq_check_overflow)) {
7016 finish_wait(&ctx->wait, &iowq.wq);
Pavel Begunkov85e25e22021-01-12 21:17:26 +00007017 continue;
Hao Xu7250f332021-02-09 04:47:46 +00007018 }
Jens Axboebda52162019-09-24 13:47:15 -06007019 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06007020 } while (1);
7021 finish_wait(&ctx->wait, &iowq.wq);
7022
Jens Axboeb7db41c2020-07-04 08:55:50 -06007023 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007024
Hristo Venev75b28af2019-08-26 17:23:46 +00007025 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007026}
7027
Jens Axboe6b063142019-01-10 22:13:58 -07007028static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7029{
7030#if defined(CONFIG_UNIX)
7031 if (ctx->ring_sock) {
7032 struct sock *sock = ctx->ring_sock->sk;
7033 struct sk_buff *skb;
7034
7035 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7036 kfree_skb(skb);
7037 }
7038#else
7039 int i;
7040
Jens Axboe65e19f52019-10-26 07:20:21 -06007041 for (i = 0; i < ctx->nr_user_files; i++) {
7042 struct file *file;
7043
7044 file = io_file_from_index(ctx, i);
7045 if (file)
7046 fput(file);
7047 }
Jens Axboe6b063142019-01-10 22:13:58 -07007048#endif
7049}
7050
Jens Axboe05f3fb32019-12-09 11:22:50 -07007051static void io_file_ref_kill(struct percpu_ref *ref)
7052{
7053 struct fixed_file_data *data;
7054
7055 data = container_of(ref, struct fixed_file_data, refs);
7056 complete(&data->done);
7057}
7058
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007059static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7060 struct fixed_file_ref_node *ref_node)
7061{
7062 spin_lock_bh(&file_data->lock);
7063 file_data->node = ref_node;
7064 list_add_tail(&ref_node->node, &file_data->ref_list);
7065 spin_unlock_bh(&file_data->lock);
7066 percpu_ref_get(&file_data->refs);
7067}
7068
Jens Axboe6b063142019-01-10 22:13:58 -07007069static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7070{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007071 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007072 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007073 unsigned nr_tables, i;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007074 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007075
Jens Axboe05f3fb32019-12-09 11:22:50 -07007076 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007077 return -ENXIO;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007078 backup_node = alloc_fixed_file_ref_node(ctx);
7079 if (!backup_node)
7080 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007081
Jens Axboe25a2de62020-11-23 09:37:51 -07007082 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007083 ref_node = data->node;
Jens Axboe25a2de62020-11-23 09:37:51 -07007084 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007085 if (ref_node)
7086 percpu_ref_kill(&ref_node->refs);
7087
7088 percpu_ref_kill(&data->refs);
7089
7090 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007091 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007092 do {
7093 ret = wait_for_completion_interruptible(&data->done);
7094 if (!ret)
7095 break;
7096 ret = io_run_task_work_sig();
7097 if (ret < 0) {
7098 percpu_ref_resurrect(&data->refs);
7099 reinit_completion(&data->done);
7100 io_sqe_files_set_node(data, backup_node);
7101 return ret;
7102 }
7103 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007104
Jens Axboe6b063142019-01-10 22:13:58 -07007105 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007106 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7107 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007108 kfree(data->table[i].files);
7109 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007110 percpu_ref_exit(&data->refs);
7111 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007112 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007113 ctx->nr_user_files = 0;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007114 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007115 return 0;
7116}
7117
Jens Axboe534ca6d2020-09-02 13:52:19 -06007118static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007119{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007120 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007121 /*
7122 * The park is a bit of a work-around, without it we get
7123 * warning spews on shutdown with SQPOLL set and affinity
7124 * set to a single CPU.
7125 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007126 if (sqd->thread) {
7127 kthread_park(sqd->thread);
7128 kthread_stop(sqd->thread);
7129 }
7130
7131 kfree(sqd);
7132 }
7133}
7134
Jens Axboeaa061652020-09-02 14:50:27 -06007135static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7136{
7137 struct io_ring_ctx *ctx_attach;
7138 struct io_sq_data *sqd;
7139 struct fd f;
7140
7141 f = fdget(p->wq_fd);
7142 if (!f.file)
7143 return ERR_PTR(-ENXIO);
7144 if (f.file->f_op != &io_uring_fops) {
7145 fdput(f);
7146 return ERR_PTR(-EINVAL);
7147 }
7148
7149 ctx_attach = f.file->private_data;
7150 sqd = ctx_attach->sq_data;
7151 if (!sqd) {
7152 fdput(f);
7153 return ERR_PTR(-EINVAL);
7154 }
7155
7156 refcount_inc(&sqd->refs);
7157 fdput(f);
7158 return sqd;
7159}
7160
Jens Axboe534ca6d2020-09-02 13:52:19 -06007161static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7162{
7163 struct io_sq_data *sqd;
7164
Jens Axboeaa061652020-09-02 14:50:27 -06007165 if (p->flags & IORING_SETUP_ATTACH_WQ)
7166 return io_attach_sq_data(p);
7167
Jens Axboe534ca6d2020-09-02 13:52:19 -06007168 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7169 if (!sqd)
7170 return ERR_PTR(-ENOMEM);
7171
7172 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007173 INIT_LIST_HEAD(&sqd->ctx_list);
7174 INIT_LIST_HEAD(&sqd->ctx_new_list);
7175 mutex_init(&sqd->ctx_lock);
7176 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007177 init_waitqueue_head(&sqd->wait);
7178 return sqd;
7179}
7180
Jens Axboe69fb2132020-09-14 11:16:23 -06007181static void io_sq_thread_unpark(struct io_sq_data *sqd)
7182 __releases(&sqd->lock)
7183{
7184 if (!sqd->thread)
7185 return;
7186 kthread_unpark(sqd->thread);
7187 mutex_unlock(&sqd->lock);
7188}
7189
7190static void io_sq_thread_park(struct io_sq_data *sqd)
7191 __acquires(&sqd->lock)
7192{
7193 if (!sqd->thread)
7194 return;
7195 mutex_lock(&sqd->lock);
7196 kthread_park(sqd->thread);
7197}
7198
Jens Axboe534ca6d2020-09-02 13:52:19 -06007199static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7200{
7201 struct io_sq_data *sqd = ctx->sq_data;
7202
7203 if (sqd) {
7204 if (sqd->thread) {
7205 /*
7206 * We may arrive here from the error branch in
7207 * io_sq_offload_create() where the kthread is created
7208 * without being waked up, thus wake it up now to make
7209 * sure the wait will complete.
7210 */
7211 wake_up_process(sqd->thread);
7212 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007213
7214 io_sq_thread_park(sqd);
7215 }
7216
7217 mutex_lock(&sqd->ctx_lock);
7218 list_del(&ctx->sqd_list);
7219 mutex_unlock(&sqd->ctx_lock);
7220
7221 if (sqd->thread) {
7222 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7223 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007224 }
7225
7226 io_put_sq_data(sqd);
7227 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007228 }
7229}
7230
Jens Axboe6b063142019-01-10 22:13:58 -07007231static void io_finish_async(struct io_ring_ctx *ctx)
7232{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007233 io_sq_thread_stop(ctx);
7234
Jens Axboe561fb042019-10-24 07:25:42 -06007235 if (ctx->io_wq) {
7236 io_wq_destroy(ctx->io_wq);
7237 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007238 }
7239}
7240
7241#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007242/*
7243 * Ensure the UNIX gc is aware of our file set, so we are certain that
7244 * the io_uring can be safely unregistered on process exit, even if we have
7245 * loops in the file referencing.
7246 */
7247static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7248{
7249 struct sock *sk = ctx->ring_sock->sk;
7250 struct scm_fp_list *fpl;
7251 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007252 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007253
Jens Axboe6b063142019-01-10 22:13:58 -07007254 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7255 if (!fpl)
7256 return -ENOMEM;
7257
7258 skb = alloc_skb(0, GFP_KERNEL);
7259 if (!skb) {
7260 kfree(fpl);
7261 return -ENOMEM;
7262 }
7263
7264 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007265
Jens Axboe08a45172019-10-03 08:11:03 -06007266 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007267 fpl->user = get_uid(ctx->user);
7268 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007269 struct file *file = io_file_from_index(ctx, i + offset);
7270
7271 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007272 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007273 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007274 unix_inflight(fpl->user, fpl->fp[nr_files]);
7275 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007276 }
7277
Jens Axboe08a45172019-10-03 08:11:03 -06007278 if (nr_files) {
7279 fpl->max = SCM_MAX_FD;
7280 fpl->count = nr_files;
7281 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007282 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007283 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7284 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007285
Jens Axboe08a45172019-10-03 08:11:03 -06007286 for (i = 0; i < nr_files; i++)
7287 fput(fpl->fp[i]);
7288 } else {
7289 kfree_skb(skb);
7290 kfree(fpl);
7291 }
Jens Axboe6b063142019-01-10 22:13:58 -07007292
7293 return 0;
7294}
7295
7296/*
7297 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7298 * causes regular reference counting to break down. We rely on the UNIX
7299 * garbage collection to take care of this problem for us.
7300 */
7301static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7302{
7303 unsigned left, total;
7304 int ret = 0;
7305
7306 total = 0;
7307 left = ctx->nr_user_files;
7308 while (left) {
7309 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007310
7311 ret = __io_sqe_files_scm(ctx, this_files, total);
7312 if (ret)
7313 break;
7314 left -= this_files;
7315 total += this_files;
7316 }
7317
7318 if (!ret)
7319 return 0;
7320
7321 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007322 struct file *file = io_file_from_index(ctx, total);
7323
7324 if (file)
7325 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007326 total++;
7327 }
7328
7329 return ret;
7330}
7331#else
7332static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7333{
7334 return 0;
7335}
7336#endif
7337
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007338static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7339 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007340{
7341 int i;
7342
7343 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007344 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007345 unsigned this_files;
7346
7347 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7348 table->files = kcalloc(this_files, sizeof(struct file *),
7349 GFP_KERNEL);
7350 if (!table->files)
7351 break;
7352 nr_files -= this_files;
7353 }
7354
7355 if (i == nr_tables)
7356 return 0;
7357
7358 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007359 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007360 kfree(table->files);
7361 }
7362 return 1;
7363}
7364
Jens Axboe05f3fb32019-12-09 11:22:50 -07007365static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007366{
7367#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007368 struct sock *sock = ctx->ring_sock->sk;
7369 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7370 struct sk_buff *skb;
7371 int i;
7372
7373 __skb_queue_head_init(&list);
7374
7375 /*
7376 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7377 * remove this entry and rearrange the file array.
7378 */
7379 skb = skb_dequeue(head);
7380 while (skb) {
7381 struct scm_fp_list *fp;
7382
7383 fp = UNIXCB(skb).fp;
7384 for (i = 0; i < fp->count; i++) {
7385 int left;
7386
7387 if (fp->fp[i] != file)
7388 continue;
7389
7390 unix_notinflight(fp->user, fp->fp[i]);
7391 left = fp->count - 1 - i;
7392 if (left) {
7393 memmove(&fp->fp[i], &fp->fp[i + 1],
7394 left * sizeof(struct file *));
7395 }
7396 fp->count--;
7397 if (!fp->count) {
7398 kfree_skb(skb);
7399 skb = NULL;
7400 } else {
7401 __skb_queue_tail(&list, skb);
7402 }
7403 fput(file);
7404 file = NULL;
7405 break;
7406 }
7407
7408 if (!file)
7409 break;
7410
7411 __skb_queue_tail(&list, skb);
7412
7413 skb = skb_dequeue(head);
7414 }
7415
7416 if (skb_peek(&list)) {
7417 spin_lock_irq(&head->lock);
7418 while ((skb = __skb_dequeue(&list)) != NULL)
7419 __skb_queue_tail(head, skb);
7420 spin_unlock_irq(&head->lock);
7421 }
7422#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007423 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007424#endif
7425}
7426
Jens Axboe05f3fb32019-12-09 11:22:50 -07007427struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007428 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007429 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007430};
7431
Jens Axboe4a38aed22020-05-14 17:21:15 -06007432static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007433{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007434 struct fixed_file_data *file_data = ref_node->file_data;
7435 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007436 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007437
7438 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007439 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007440 io_ring_file_put(ctx, pfile->file);
7441 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007442 }
7443
Xiaoguang Wang05589552020-03-31 14:05:18 +08007444 percpu_ref_exit(&ref_node->refs);
7445 kfree(ref_node);
7446 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007447}
7448
Jens Axboe4a38aed22020-05-14 17:21:15 -06007449static void io_file_put_work(struct work_struct *work)
7450{
7451 struct io_ring_ctx *ctx;
7452 struct llist_node *node;
7453
7454 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7455 node = llist_del_all(&ctx->file_put_llist);
7456
7457 while (node) {
7458 struct fixed_file_ref_node *ref_node;
7459 struct llist_node *next = node->next;
7460
7461 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7462 __io_file_put_work(ref_node);
7463 node = next;
7464 }
7465}
7466
Jens Axboe05f3fb32019-12-09 11:22:50 -07007467static void io_file_data_ref_zero(struct percpu_ref *ref)
7468{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007469 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007470 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007471 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007472 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007473 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007474
Xiaoguang Wang05589552020-03-31 14:05:18 +08007475 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007476 data = ref_node->file_data;
7477 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007478
Jens Axboe25a2de62020-11-23 09:37:51 -07007479 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007480 ref_node->done = true;
7481
7482 while (!list_empty(&data->ref_list)) {
7483 ref_node = list_first_entry(&data->ref_list,
7484 struct fixed_file_ref_node, node);
7485 /* recycle ref nodes in order */
7486 if (!ref_node->done)
7487 break;
7488 list_del(&ref_node->node);
7489 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7490 }
Jens Axboe25a2de62020-11-23 09:37:51 -07007491 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007492
7493 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007494 delay = 0;
7495
Jens Axboe4a38aed22020-05-14 17:21:15 -06007496 if (!delay)
7497 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7498 else if (first_add)
7499 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007500}
7501
7502static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7503 struct io_ring_ctx *ctx)
7504{
7505 struct fixed_file_ref_node *ref_node;
7506
7507 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7508 if (!ref_node)
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007509 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007510
7511 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7512 0, GFP_KERNEL)) {
7513 kfree(ref_node);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007514 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007515 }
7516 INIT_LIST_HEAD(&ref_node->node);
7517 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007518 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007519 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007520 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007521}
7522
7523static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7524{
7525 percpu_ref_exit(&ref_node->refs);
7526 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007527}
7528
7529static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7530 unsigned nr_args)
7531{
7532 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007533 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007534 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007535 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007536 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007537 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007538
7539 if (ctx->file_data)
7540 return -EBUSY;
7541 if (!nr_args)
7542 return -EINVAL;
7543 if (nr_args > IORING_MAX_FIXED_FILES)
7544 return -EMFILE;
7545
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007546 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7547 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007548 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007549 file_data->ctx = ctx;
7550 init_completion(&file_data->done);
7551 INIT_LIST_HEAD(&file_data->ref_list);
7552 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007553
7554 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007555 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007556 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007557 if (!file_data->table)
7558 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007559
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007560 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007561 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7562 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007563
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007564 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7565 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007566 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007567
7568 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7569 struct fixed_file_table *table;
7570 unsigned index;
7571
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007572 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7573 ret = -EFAULT;
7574 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007575 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007576 /* allow sparse sets */
7577 if (fd == -1)
7578 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007579
Jens Axboe05f3fb32019-12-09 11:22:50 -07007580 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007581 ret = -EBADF;
7582 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007583 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007584
7585 /*
7586 * Don't allow io_uring instances to be registered. If UNIX
7587 * isn't enabled, then this causes a reference cycle and this
7588 * instance can never get freed. If UNIX is enabled we'll
7589 * handle it just fine, but there's still no point in allowing
7590 * a ring fd as it doesn't support regular read/write anyway.
7591 */
7592 if (file->f_op == &io_uring_fops) {
7593 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007594 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007595 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007596 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7597 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007598 table->files[index] = file;
7599 }
7600
Jens Axboe05f3fb32019-12-09 11:22:50 -07007601 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007602 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007603 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007604 return ret;
7605 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007606
Xiaoguang Wang05589552020-03-31 14:05:18 +08007607 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007608 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007609 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007610 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007611 }
7612
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007613 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007614 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007615out_fput:
7616 for (i = 0; i < ctx->nr_user_files; i++) {
7617 file = io_file_from_index(ctx, i);
7618 if (file)
7619 fput(file);
7620 }
7621 for (i = 0; i < nr_tables; i++)
7622 kfree(file_data->table[i].files);
7623 ctx->nr_user_files = 0;
7624out_ref:
7625 percpu_ref_exit(&file_data->refs);
7626out_free:
7627 kfree(file_data->table);
7628 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007629 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007630 return ret;
7631}
7632
Jens Axboec3a31e62019-10-03 13:59:56 -06007633static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7634 int index)
7635{
7636#if defined(CONFIG_UNIX)
7637 struct sock *sock = ctx->ring_sock->sk;
7638 struct sk_buff_head *head = &sock->sk_receive_queue;
7639 struct sk_buff *skb;
7640
7641 /*
7642 * See if we can merge this file into an existing skb SCM_RIGHTS
7643 * file set. If there's no room, fall back to allocating a new skb
7644 * and filling it in.
7645 */
7646 spin_lock_irq(&head->lock);
7647 skb = skb_peek(head);
7648 if (skb) {
7649 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7650
7651 if (fpl->count < SCM_MAX_FD) {
7652 __skb_unlink(skb, head);
7653 spin_unlock_irq(&head->lock);
7654 fpl->fp[fpl->count] = get_file(file);
7655 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7656 fpl->count++;
7657 spin_lock_irq(&head->lock);
7658 __skb_queue_head(head, skb);
7659 } else {
7660 skb = NULL;
7661 }
7662 }
7663 spin_unlock_irq(&head->lock);
7664
7665 if (skb) {
7666 fput(file);
7667 return 0;
7668 }
7669
7670 return __io_sqe_files_scm(ctx, 1, index);
7671#else
7672 return 0;
7673#endif
7674}
7675
Hillf Dantona5318d32020-03-23 17:47:15 +08007676static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007677 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007678{
Hillf Dantona5318d32020-03-23 17:47:15 +08007679 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007680 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007681
Jens Axboe05f3fb32019-12-09 11:22:50 -07007682 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007683 if (!pfile)
7684 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007685
7686 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007687 list_add(&pfile->list, &ref_node->file_list);
7688
Hillf Dantona5318d32020-03-23 17:47:15 +08007689 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007690}
7691
7692static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7693 struct io_uring_files_update *up,
7694 unsigned nr_args)
7695{
7696 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007697 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007698 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007699 __s32 __user *fds;
7700 int fd, i, err;
7701 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007702 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007703
Jens Axboe05f3fb32019-12-09 11:22:50 -07007704 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007705 return -EOVERFLOW;
7706 if (done > ctx->nr_user_files)
7707 return -EINVAL;
7708
Xiaoguang Wang05589552020-03-31 14:05:18 +08007709 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007710 if (!ref_node)
7711 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007712
Jens Axboec3a31e62019-10-03 13:59:56 -06007713 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007714 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007715 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007716 struct fixed_file_table *table;
7717 unsigned index;
7718
Jens Axboec3a31e62019-10-03 13:59:56 -06007719 err = 0;
7720 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7721 err = -EFAULT;
7722 break;
7723 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007724 i = array_index_nospec(up->offset, ctx->nr_user_files);
7725 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007726 index = i & IORING_FILE_TABLE_MASK;
7727 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007728 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007729 err = io_queue_file_removal(data, file);
7730 if (err)
7731 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007732 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007733 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007734 }
7735 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007736 file = fget(fd);
7737 if (!file) {
7738 err = -EBADF;
7739 break;
7740 }
7741 /*
7742 * Don't allow io_uring instances to be registered. If
7743 * UNIX isn't enabled, then this causes a reference
7744 * cycle and this instance can never get freed. If UNIX
7745 * is enabled we'll handle it just fine, but there's
7746 * still no point in allowing a ring fd as it doesn't
7747 * support regular read/write anyway.
7748 */
7749 if (file->f_op == &io_uring_fops) {
7750 fput(file);
7751 err = -EBADF;
7752 break;
7753 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007754 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007755 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007756 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007757 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007758 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007759 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007760 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007761 }
7762 nr_args--;
7763 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007764 up->offset++;
7765 }
7766
Xiaoguang Wang05589552020-03-31 14:05:18 +08007767 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007768 percpu_ref_kill(&data->node->refs);
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007769 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007770 } else
7771 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007772
7773 return done ? done : err;
7774}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007775
Jens Axboe05f3fb32019-12-09 11:22:50 -07007776static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7777 unsigned nr_args)
7778{
7779 struct io_uring_files_update up;
7780
7781 if (!ctx->file_data)
7782 return -ENXIO;
7783 if (!nr_args)
7784 return -EINVAL;
7785 if (copy_from_user(&up, arg, sizeof(up)))
7786 return -EFAULT;
7787 if (up.resv)
7788 return -EINVAL;
7789
7790 return __io_sqe_files_update(ctx, &up, nr_args);
7791}
Jens Axboec3a31e62019-10-03 13:59:56 -06007792
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007793static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007794{
7795 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7796
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007797 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007798 io_put_req(req);
7799}
7800
Pavel Begunkov24369c22020-01-28 03:15:48 +03007801static int io_init_wq_offload(struct io_ring_ctx *ctx,
7802 struct io_uring_params *p)
7803{
7804 struct io_wq_data data;
7805 struct fd f;
7806 struct io_ring_ctx *ctx_attach;
7807 unsigned int concurrency;
7808 int ret = 0;
7809
7810 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007811 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007812 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007813
7814 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7815 /* Do QD, or 4 * CPUS, whatever is smallest */
7816 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7817
7818 ctx->io_wq = io_wq_create(concurrency, &data);
7819 if (IS_ERR(ctx->io_wq)) {
7820 ret = PTR_ERR(ctx->io_wq);
7821 ctx->io_wq = NULL;
7822 }
7823 return ret;
7824 }
7825
7826 f = fdget(p->wq_fd);
7827 if (!f.file)
7828 return -EBADF;
7829
7830 if (f.file->f_op != &io_uring_fops) {
7831 ret = -EINVAL;
7832 goto out_fput;
7833 }
7834
7835 ctx_attach = f.file->private_data;
7836 /* @io_wq is protected by holding the fd */
7837 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7838 ret = -EINVAL;
7839 goto out_fput;
7840 }
7841
7842 ctx->io_wq = ctx_attach->io_wq;
7843out_fput:
7844 fdput(f);
7845 return ret;
7846}
7847
Jens Axboe0f212202020-09-13 13:09:39 -06007848static int io_uring_alloc_task_context(struct task_struct *task)
7849{
7850 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007851 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007852
7853 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7854 if (unlikely(!tctx))
7855 return -ENOMEM;
7856
Jens Axboed8a6df12020-10-15 16:24:45 -06007857 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7858 if (unlikely(ret)) {
7859 kfree(tctx);
7860 return ret;
7861 }
7862
Jens Axboe0f212202020-09-13 13:09:39 -06007863 xa_init(&tctx->xa);
7864 init_waitqueue_head(&tctx->wait);
7865 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007866 atomic_set(&tctx->in_idle, 0);
7867 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06007868 io_init_identity(&tctx->__identity);
7869 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06007870 task->io_uring = tctx;
7871 return 0;
7872}
7873
7874void __io_uring_free(struct task_struct *tsk)
7875{
7876 struct io_uring_task *tctx = tsk->io_uring;
7877
7878 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06007879 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
7880 if (tctx->identity != &tctx->__identity)
7881 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06007882 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007883 kfree(tctx);
7884 tsk->io_uring = NULL;
7885}
7886
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007887static int io_sq_offload_create(struct io_ring_ctx *ctx,
7888 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007889{
7890 int ret;
7891
Jens Axboe6c271ce2019-01-10 11:22:30 -07007892 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007893 struct io_sq_data *sqd;
7894
Jens Axboe3ec482d2019-04-08 10:51:01 -06007895 ret = -EPERM;
7896 if (!capable(CAP_SYS_ADMIN))
7897 goto err;
7898
Jens Axboe534ca6d2020-09-02 13:52:19 -06007899 sqd = io_get_sq_data(p);
7900 if (IS_ERR(sqd)) {
7901 ret = PTR_ERR(sqd);
7902 goto err;
7903 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007904
Jens Axboe534ca6d2020-09-02 13:52:19 -06007905 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007906 io_sq_thread_park(sqd);
7907 mutex_lock(&sqd->ctx_lock);
7908 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7909 mutex_unlock(&sqd->ctx_lock);
7910 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007911
Jens Axboe917257d2019-04-13 09:28:55 -06007912 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7913 if (!ctx->sq_thread_idle)
7914 ctx->sq_thread_idle = HZ;
7915
Jens Axboeaa061652020-09-02 14:50:27 -06007916 if (sqd->thread)
7917 goto done;
7918
Jens Axboe6c271ce2019-01-10 11:22:30 -07007919 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007920 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007921
Jens Axboe917257d2019-04-13 09:28:55 -06007922 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007923 if (cpu >= nr_cpu_ids)
7924 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007925 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007926 goto err;
7927
Jens Axboe69fb2132020-09-14 11:16:23 -06007928 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007929 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007930 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007931 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007932 "io_uring-sq");
7933 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007934 if (IS_ERR(sqd->thread)) {
7935 ret = PTR_ERR(sqd->thread);
7936 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007937 goto err;
7938 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007939 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007940 if (ret)
7941 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007942 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7943 /* Can't have SQ_AFF without SQPOLL */
7944 ret = -EINVAL;
7945 goto err;
7946 }
7947
Jens Axboeaa061652020-09-02 14:50:27 -06007948done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007949 ret = io_init_wq_offload(ctx, p);
7950 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007951 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007952
7953 return 0;
7954err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007955 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007956 return ret;
7957}
7958
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007959static void io_sq_offload_start(struct io_ring_ctx *ctx)
7960{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007961 struct io_sq_data *sqd = ctx->sq_data;
7962
7963 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7964 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007965}
7966
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007967static inline void __io_unaccount_mem(struct user_struct *user,
7968 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007969{
7970 atomic_long_sub(nr_pages, &user->locked_vm);
7971}
7972
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007973static inline int __io_account_mem(struct user_struct *user,
7974 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007975{
7976 unsigned long page_limit, cur_pages, new_pages;
7977
7978 /* Don't allow more pages than we can safely lock */
7979 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7980
7981 do {
7982 cur_pages = atomic_long_read(&user->locked_vm);
7983 new_pages = cur_pages + nr_pages;
7984 if (new_pages > page_limit)
7985 return -ENOMEM;
7986 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7987 new_pages) != cur_pages);
7988
7989 return 0;
7990}
7991
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007992static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7993 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007994{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007995 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007996 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007997
Jens Axboe2aede0e2020-09-14 10:45:53 -06007998 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007999 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008000 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008001 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008002 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008003 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008004}
8005
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008006static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8007 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008008{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008009 int ret;
8010
8011 if (ctx->limit_mem) {
8012 ret = __io_account_mem(ctx->user, nr_pages);
8013 if (ret)
8014 return ret;
8015 }
8016
Jens Axboe2aede0e2020-09-14 10:45:53 -06008017 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008018 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008019 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008020 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008021 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008022 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008023
8024 return 0;
8025}
8026
Jens Axboe2b188cc2019-01-07 10:46:33 -07008027static void io_mem_free(void *ptr)
8028{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008029 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008030
Mark Rutland52e04ef2019-04-30 17:30:21 +01008031 if (!ptr)
8032 return;
8033
8034 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008035 if (put_page_testzero(page))
8036 free_compound_page(page);
8037}
8038
8039static void *io_mem_alloc(size_t size)
8040{
8041 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8042 __GFP_NORETRY;
8043
8044 return (void *) __get_free_pages(gfp_flags, get_order(size));
8045}
8046
Hristo Venev75b28af2019-08-26 17:23:46 +00008047static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8048 size_t *sq_offset)
8049{
8050 struct io_rings *rings;
8051 size_t off, sq_array_size;
8052
8053 off = struct_size(rings, cqes, cq_entries);
8054 if (off == SIZE_MAX)
8055 return SIZE_MAX;
8056
8057#ifdef CONFIG_SMP
8058 off = ALIGN(off, SMP_CACHE_BYTES);
8059 if (off == 0)
8060 return SIZE_MAX;
8061#endif
8062
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008063 if (sq_offset)
8064 *sq_offset = off;
8065
Hristo Venev75b28af2019-08-26 17:23:46 +00008066 sq_array_size = array_size(sizeof(u32), sq_entries);
8067 if (sq_array_size == SIZE_MAX)
8068 return SIZE_MAX;
8069
8070 if (check_add_overflow(off, sq_array_size, &off))
8071 return SIZE_MAX;
8072
Hristo Venev75b28af2019-08-26 17:23:46 +00008073 return off;
8074}
8075
Jens Axboe2b188cc2019-01-07 10:46:33 -07008076static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8077{
Hristo Venev75b28af2019-08-26 17:23:46 +00008078 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008079
Hristo Venev75b28af2019-08-26 17:23:46 +00008080 pages = (size_t)1 << get_order(
8081 rings_size(sq_entries, cq_entries, NULL));
8082 pages += (size_t)1 << get_order(
8083 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008084
Hristo Venev75b28af2019-08-26 17:23:46 +00008085 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008086}
8087
Jens Axboeedafcce2019-01-09 09:16:05 -07008088static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8089{
8090 int i, j;
8091
8092 if (!ctx->user_bufs)
8093 return -ENXIO;
8094
8095 for (i = 0; i < ctx->nr_user_bufs; i++) {
8096 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8097
8098 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008099 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008100
Jens Axboede293932020-09-17 16:19:16 -06008101 if (imu->acct_pages)
8102 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008103 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008104 imu->nr_bvecs = 0;
8105 }
8106
8107 kfree(ctx->user_bufs);
8108 ctx->user_bufs = NULL;
8109 ctx->nr_user_bufs = 0;
8110 return 0;
8111}
8112
8113static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8114 void __user *arg, unsigned index)
8115{
8116 struct iovec __user *src;
8117
8118#ifdef CONFIG_COMPAT
8119 if (ctx->compat) {
8120 struct compat_iovec __user *ciovs;
8121 struct compat_iovec ciov;
8122
8123 ciovs = (struct compat_iovec __user *) arg;
8124 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8125 return -EFAULT;
8126
Jens Axboed55e5f52019-12-11 16:12:15 -07008127 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008128 dst->iov_len = ciov.iov_len;
8129 return 0;
8130 }
8131#endif
8132 src = (struct iovec __user *) arg;
8133 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8134 return -EFAULT;
8135 return 0;
8136}
8137
Jens Axboede293932020-09-17 16:19:16 -06008138/*
8139 * Not super efficient, but this is just a registration time. And we do cache
8140 * the last compound head, so generally we'll only do a full search if we don't
8141 * match that one.
8142 *
8143 * We check if the given compound head page has already been accounted, to
8144 * avoid double accounting it. This allows us to account the full size of the
8145 * page, not just the constituent pages of a huge page.
8146 */
8147static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8148 int nr_pages, struct page *hpage)
8149{
8150 int i, j;
8151
8152 /* check current page array */
8153 for (i = 0; i < nr_pages; i++) {
8154 if (!PageCompound(pages[i]))
8155 continue;
8156 if (compound_head(pages[i]) == hpage)
8157 return true;
8158 }
8159
8160 /* check previously registered pages */
8161 for (i = 0; i < ctx->nr_user_bufs; i++) {
8162 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8163
8164 for (j = 0; j < imu->nr_bvecs; j++) {
8165 if (!PageCompound(imu->bvec[j].bv_page))
8166 continue;
8167 if (compound_head(imu->bvec[j].bv_page) == hpage)
8168 return true;
8169 }
8170 }
8171
8172 return false;
8173}
8174
8175static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8176 int nr_pages, struct io_mapped_ubuf *imu,
8177 struct page **last_hpage)
8178{
8179 int i, ret;
8180
8181 for (i = 0; i < nr_pages; i++) {
8182 if (!PageCompound(pages[i])) {
8183 imu->acct_pages++;
8184 } else {
8185 struct page *hpage;
8186
8187 hpage = compound_head(pages[i]);
8188 if (hpage == *last_hpage)
8189 continue;
8190 *last_hpage = hpage;
8191 if (headpage_already_acct(ctx, pages, i, hpage))
8192 continue;
8193 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8194 }
8195 }
8196
8197 if (!imu->acct_pages)
8198 return 0;
8199
8200 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8201 if (ret)
8202 imu->acct_pages = 0;
8203 return ret;
8204}
8205
Jens Axboeedafcce2019-01-09 09:16:05 -07008206static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8207 unsigned nr_args)
8208{
8209 struct vm_area_struct **vmas = NULL;
8210 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008211 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008212 int i, j, got_pages = 0;
8213 int ret = -EINVAL;
8214
8215 if (ctx->user_bufs)
8216 return -EBUSY;
8217 if (!nr_args || nr_args > UIO_MAXIOV)
8218 return -EINVAL;
8219
8220 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8221 GFP_KERNEL);
8222 if (!ctx->user_bufs)
8223 return -ENOMEM;
8224
8225 for (i = 0; i < nr_args; i++) {
8226 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8227 unsigned long off, start, end, ubuf;
8228 int pret, nr_pages;
8229 struct iovec iov;
8230 size_t size;
8231
8232 ret = io_copy_iov(ctx, &iov, arg, i);
8233 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008234 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008235
8236 /*
8237 * Don't impose further limits on the size and buffer
8238 * constraints here, we'll -EINVAL later when IO is
8239 * submitted if they are wrong.
8240 */
8241 ret = -EFAULT;
8242 if (!iov.iov_base || !iov.iov_len)
8243 goto err;
8244
8245 /* arbitrary limit, but we need something */
8246 if (iov.iov_len > SZ_1G)
8247 goto err;
8248
8249 ubuf = (unsigned long) iov.iov_base;
8250 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8251 start = ubuf >> PAGE_SHIFT;
8252 nr_pages = end - start;
8253
Jens Axboeedafcce2019-01-09 09:16:05 -07008254 ret = 0;
8255 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008256 kvfree(vmas);
8257 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008258 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008259 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008260 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008261 sizeof(struct vm_area_struct *),
8262 GFP_KERNEL);
8263 if (!pages || !vmas) {
8264 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008265 goto err;
8266 }
8267 got_pages = nr_pages;
8268 }
8269
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008270 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008271 GFP_KERNEL);
8272 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008273 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008274 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008275
8276 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008277 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008278 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008279 FOLL_WRITE | FOLL_LONGTERM,
8280 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008281 if (pret == nr_pages) {
8282 /* don't support file backed memory */
8283 for (j = 0; j < nr_pages; j++) {
8284 struct vm_area_struct *vma = vmas[j];
8285
8286 if (vma->vm_file &&
8287 !is_file_hugepages(vma->vm_file)) {
8288 ret = -EOPNOTSUPP;
8289 break;
8290 }
8291 }
8292 } else {
8293 ret = pret < 0 ? pret : -EFAULT;
8294 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008295 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008296 if (ret) {
8297 /*
8298 * if we did partial map, or found file backed vmas,
8299 * release any pages we did get
8300 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008301 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008302 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008303 kvfree(imu->bvec);
8304 goto err;
8305 }
8306
8307 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8308 if (ret) {
8309 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008310 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008311 goto err;
8312 }
8313
8314 off = ubuf & ~PAGE_MASK;
8315 size = iov.iov_len;
8316 for (j = 0; j < nr_pages; j++) {
8317 size_t vec_len;
8318
8319 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8320 imu->bvec[j].bv_page = pages[j];
8321 imu->bvec[j].bv_len = vec_len;
8322 imu->bvec[j].bv_offset = off;
8323 off = 0;
8324 size -= vec_len;
8325 }
8326 /* store original address for later verification */
8327 imu->ubuf = ubuf;
8328 imu->len = iov.iov_len;
8329 imu->nr_bvecs = nr_pages;
8330
8331 ctx->nr_user_bufs++;
8332 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008333 kvfree(pages);
8334 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008335 return 0;
8336err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008337 kvfree(pages);
8338 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008339 io_sqe_buffer_unregister(ctx);
8340 return ret;
8341}
8342
Jens Axboe9b402842019-04-11 11:45:41 -06008343static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8344{
8345 __s32 __user *fds = arg;
8346 int fd;
8347
8348 if (ctx->cq_ev_fd)
8349 return -EBUSY;
8350
8351 if (copy_from_user(&fd, fds, sizeof(*fds)))
8352 return -EFAULT;
8353
8354 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8355 if (IS_ERR(ctx->cq_ev_fd)) {
8356 int ret = PTR_ERR(ctx->cq_ev_fd);
8357 ctx->cq_ev_fd = NULL;
8358 return ret;
8359 }
8360
8361 return 0;
8362}
8363
8364static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8365{
8366 if (ctx->cq_ev_fd) {
8367 eventfd_ctx_put(ctx->cq_ev_fd);
8368 ctx->cq_ev_fd = NULL;
8369 return 0;
8370 }
8371
8372 return -ENXIO;
8373}
8374
Jens Axboe5a2e7452020-02-23 16:23:11 -07008375static int __io_destroy_buffers(int id, void *p, void *data)
8376{
8377 struct io_ring_ctx *ctx = data;
8378 struct io_buffer *buf = p;
8379
Jens Axboe067524e2020-03-02 16:32:28 -07008380 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008381 return 0;
8382}
8383
8384static void io_destroy_buffers(struct io_ring_ctx *ctx)
8385{
8386 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8387 idr_destroy(&ctx->io_buffer_idr);
8388}
8389
Jens Axboe2b188cc2019-01-07 10:46:33 -07008390static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8391{
Jens Axboe6b063142019-01-10 22:13:58 -07008392 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008393 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008394
8395 if (ctx->sqo_task) {
8396 put_task_struct(ctx->sqo_task);
8397 ctx->sqo_task = NULL;
8398 mmdrop(ctx->mm_account);
8399 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008400 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008401
Dennis Zhou91d8f512020-09-16 13:41:05 -07008402#ifdef CONFIG_BLK_CGROUP
8403 if (ctx->sqo_blkcg_css)
8404 css_put(ctx->sqo_blkcg_css);
8405#endif
8406
Jens Axboe6b063142019-01-10 22:13:58 -07008407 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008408 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008409 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008410 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008411
Jens Axboe2b188cc2019-01-07 10:46:33 -07008412#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008413 if (ctx->ring_sock) {
8414 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008415 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008416 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008417#endif
8418
Hristo Venev75b28af2019-08-26 17:23:46 +00008419 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008420 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008421
8422 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008423 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008424 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008425 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008426 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008427 kfree(ctx);
8428}
8429
8430static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8431{
8432 struct io_ring_ctx *ctx = file->private_data;
8433 __poll_t mask = 0;
8434
8435 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008436 /*
8437 * synchronizes with barrier from wq_has_sleeper call in
8438 * io_commit_cqring
8439 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008440 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008441 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008442 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00008443 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8444 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008445 mask |= EPOLLIN | EPOLLRDNORM;
8446
8447 return mask;
8448}
8449
8450static int io_uring_fasync(int fd, struct file *file, int on)
8451{
8452 struct io_ring_ctx *ctx = file->private_data;
8453
8454 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8455}
8456
Jens Axboe071698e2020-01-28 10:04:42 -07008457static int io_remove_personalities(int id, void *p, void *data)
8458{
8459 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008460 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008461
Jens Axboe1e6fa522020-10-15 08:46:24 -06008462 iod = idr_remove(&ctx->personality_idr, id);
8463 if (iod) {
8464 put_cred(iod->creds);
8465 if (refcount_dec_and_test(&iod->count))
8466 kfree(iod);
8467 }
Jens Axboe071698e2020-01-28 10:04:42 -07008468 return 0;
8469}
8470
Jens Axboe85faa7b2020-04-09 18:14:00 -06008471static void io_ring_exit_work(struct work_struct *work)
8472{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008473 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8474 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008475
Jens Axboe56952e92020-06-17 15:00:04 -06008476 /*
8477 * If we're doing polled IO and end up having requests being
8478 * submitted async (out-of-line), then completions can come in while
8479 * we're waiting for refs to drop. We need to reap these manually,
8480 * as nobody else will be looking for them.
8481 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008482 do {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008483 io_iopoll_try_reap_events(ctx);
8484 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008485 io_ring_ctx_free(ctx);
8486}
8487
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008488static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8489{
8490 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8491
8492 return req->ctx == data;
8493}
8494
Jens Axboe2b188cc2019-01-07 10:46:33 -07008495static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8496{
8497 mutex_lock(&ctx->uring_lock);
8498 percpu_ref_kill(&ctx->refs);
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008499 /* if force is set, the ring is going away. always drop after that */
Pavel Begunkova63d9152021-01-26 11:17:03 +00008500
8501 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8502 ctx->sqo_dead = 1;
8503
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008504 ctx->cq_overflow_flushed = 1;
Pavel Begunkovc0fd45a2020-12-06 22:22:44 +00008505 if (ctx->rings)
Pavel Begunkov85e25e22021-01-12 21:17:26 +00008506 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008507 mutex_unlock(&ctx->uring_lock);
8508
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00008509 io_kill_timeouts(ctx, NULL, NULL);
8510 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008511
8512 if (ctx->io_wq)
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008513 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008514
Jens Axboe15dff282019-11-13 09:09:23 -07008515 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008516 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008517 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008518
8519 /*
8520 * Do this upfront, so we won't have a grace period where the ring
8521 * is closed but resources aren't reaped yet. This can cause
8522 * spurious failure in setting up a new ring.
8523 */
Jens Axboe760618f2020-07-24 12:53:31 -06008524 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8525 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008526
Jens Axboe85faa7b2020-04-09 18:14:00 -06008527 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008528 /*
8529 * Use system_unbound_wq to avoid spawning tons of event kworkers
8530 * if we're exiting a ton of rings at the same time. It just adds
8531 * noise and overhead, there's no discernable change in runtime
8532 * over using system_wq.
8533 */
8534 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008535}
8536
8537static int io_uring_release(struct inode *inode, struct file *file)
8538{
8539 struct io_ring_ctx *ctx = file->private_data;
8540
8541 file->private_data = NULL;
8542 io_ring_ctx_wait_and_kill(ctx);
8543 return 0;
8544}
8545
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008546struct io_task_cancel {
8547 struct task_struct *task;
8548 struct files_struct *files;
8549};
Jens Axboef254ac02020-08-12 17:33:30 -06008550
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008551static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008552{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008553 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008554 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008555 bool ret;
8556
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008557 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008558 unsigned long flags;
8559 struct io_ring_ctx *ctx = req->ctx;
8560
8561 /* protect against races with linked timeouts */
8562 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008563 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008564 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8565 } else {
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008566 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008567 }
8568 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008569}
8570
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008571static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008572 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008573 struct files_struct *files)
8574{
8575 struct io_defer_entry *de = NULL;
8576 LIST_HEAD(list);
8577
8578 spin_lock_irq(&ctx->completion_lock);
8579 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovf6d93f82021-02-09 04:47:36 +00008580 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008581 list_cut_position(&list, &ctx->defer_list, &de->list);
8582 break;
8583 }
8584 }
8585 spin_unlock_irq(&ctx->completion_lock);
8586
8587 while (!list_empty(&list)) {
8588 de = list_first_entry(&list, struct io_defer_entry, list);
8589 list_del_init(&de->list);
8590 req_set_fail_links(de->req);
8591 io_put_req(de->req);
8592 io_req_complete(de->req, -ECANCELED);
8593 kfree(de);
8594 }
8595}
8596
Pavel Begunkovd300d032021-02-09 04:47:45 +00008597static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8598 struct task_struct *task,
8599 struct files_struct *files)
8600{
8601 struct io_kiocb *req;
8602 int cnt = 0;
8603
8604 spin_lock_irq(&ctx->inflight_lock);
8605 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8606 cnt += io_match_task(req, task, files);
8607 spin_unlock_irq(&ctx->inflight_lock);
8608 return cnt;
8609}
8610
Pavel Begunkov49250f32021-02-09 04:47:37 +00008611static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkova773dea2020-11-06 13:00:23 +00008612 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008613 struct files_struct *files)
8614{
Jens Axboefcb323c2019-10-24 12:39:47 -06008615 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkov1e7eb062021-02-09 04:47:40 +00008616 struct io_task_cancel cancel = { .task = task, .files = files };
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008617 DEFINE_WAIT(wait);
Pavel Begunkovd300d032021-02-09 04:47:45 +00008618 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008619
Pavel Begunkovd300d032021-02-09 04:47:45 +00008620 inflight = io_uring_count_inflight(ctx, task, files);
8621 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008622 break;
Pavel Begunkovd92d0082021-01-26 11:17:10 +00008623
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008624 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8625 io_poll_remove_all(ctx, task, files);
8626 io_kill_timeouts(ctx, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008627 /* cancellations _may_ trigger task work */
8628 io_run_task_work();
Pavel Begunkovd300d032021-02-09 04:47:45 +00008629
8630 prepare_to_wait(&task->io_uring->wait, &wait,
8631 TASK_UNINTERRUPTIBLE);
8632 if (inflight == io_uring_count_inflight(ctx, task, files))
8633 schedule();
Pavel Begunkov52382df82021-02-09 04:47:44 +00008634 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008635 }
8636}
8637
Pavel Begunkov49250f32021-02-09 04:47:37 +00008638static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8639 struct task_struct *task)
Jens Axboe0f212202020-09-13 13:09:39 -06008640{
Pavel Begunkov49250f32021-02-09 04:47:37 +00008641 while (1) {
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008642 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06008643 enum io_wq_cancel cret;
Pavel Begunkov49250f32021-02-09 04:47:37 +00008644 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008645
Pavel Begunkovdbdcde42021-02-09 04:47:39 +00008646 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
Jens Axboe0f212202020-09-13 13:09:39 -06008647 if (cret != IO_WQ_CANCEL_NOTFOUND)
8648 ret = true;
8649
8650 /* SQPOLL thread does its own polling */
8651 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8652 while (!list_empty_careful(&ctx->iopoll_list)) {
8653 io_iopoll_try_reap_events(ctx);
8654 ret = true;
8655 }
8656 }
8657
Pavel Begunkovf8fbdbb2021-02-09 04:47:38 +00008658 ret |= io_poll_remove_all(ctx, task, NULL);
8659 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov49250f32021-02-09 04:47:37 +00008660 if (!ret)
8661 break;
8662 io_run_task_work();
8663 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06008664 }
Jens Axboe0f212202020-09-13 13:09:39 -06008665}
8666
Pavel Begunkova63d9152021-01-26 11:17:03 +00008667static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8668{
Pavel Begunkova63d9152021-01-26 11:17:03 +00008669 mutex_lock(&ctx->uring_lock);
8670 ctx->sqo_dead = 1;
8671 mutex_unlock(&ctx->uring_lock);
8672
8673 /* make sure callers enter the ring to get error */
Pavel Begunkov0e3562e2021-01-26 11:17:04 +00008674 if (ctx->rings)
8675 io_ring_set_wakeup_flag(ctx);
Pavel Begunkova63d9152021-01-26 11:17:03 +00008676}
8677
Jens Axboe0f212202020-09-13 13:09:39 -06008678/*
8679 * We need to iteratively cancel requests, in case a request has dependent
8680 * hard links. These persist even for failure of cancelations, hence keep
8681 * looping until none are found.
8682 */
8683static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8684 struct files_struct *files)
8685{
8686 struct task_struct *task = current;
8687
Jens Axboefdaf0832020-10-30 09:37:30 -06008688 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkova63d9152021-01-26 11:17:03 +00008689 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008690 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008691 atomic_inc(&task->io_uring->in_idle);
8692 io_sq_thread_park(ctx->sq_data);
8693 }
Jens Axboe0f212202020-09-13 13:09:39 -06008694
Pavel Begunkova773dea2020-11-06 13:00:23 +00008695 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008696 io_cqring_overflow_flush(ctx, true, task, files);
8697
Pavel Begunkov88dbd082021-02-09 04:47:49 +00008698 io_uring_cancel_files(ctx, task, files);
Pavel Begunkov49250f32021-02-09 04:47:37 +00008699 if (!files)
8700 __io_uring_cancel_task_requests(ctx, task);
Jens Axboefdaf0832020-10-30 09:37:30 -06008701
8702 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8703 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008704 io_sq_thread_unpark(ctx->sq_data);
8705 }
Jens Axboe0f212202020-09-13 13:09:39 -06008706}
8707
8708/*
8709 * Note that this task has used io_uring. We use it for cancelation purposes.
8710 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008711static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008712{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008713 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008714 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008715
8716 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008717 ret = io_uring_alloc_task_context(current);
8718 if (unlikely(ret))
8719 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008720 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008721 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008722 if (tctx->last != file) {
8723 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008724
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008725 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008726 get_file(file);
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008727 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8728 file, GFP_KERNEL));
8729 if (ret) {
8730 fput(file);
8731 return ret;
8732 }
Jens Axboe0f212202020-09-13 13:09:39 -06008733 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008734 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008735 }
8736
Jens Axboefdaf0832020-10-30 09:37:30 -06008737 /*
8738 * This is race safe in that the task itself is doing this, hence it
8739 * cannot be going through the exit/cancel paths at the same time.
8740 * This cannot be modified while exit/cancel is running.
8741 */
8742 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8743 tctx->sqpoll = true;
8744
Jens Axboe0f212202020-09-13 13:09:39 -06008745 return 0;
8746}
8747
8748/*
8749 * Remove this io_uring_file -> task mapping.
8750 */
8751static void io_uring_del_task_file(struct file *file)
8752{
8753 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008754
8755 if (tctx->last == file)
8756 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008757 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008758 if (file)
8759 fput(file);
8760}
8761
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008762static void io_uring_remove_task_files(struct io_uring_task *tctx)
8763{
8764 struct file *file;
8765 unsigned long index;
8766
8767 xa_for_each(&tctx->xa, index, file)
8768 io_uring_del_task_file(file);
8769}
8770
Jens Axboe0f212202020-09-13 13:09:39 -06008771void __io_uring_files_cancel(struct files_struct *files)
8772{
8773 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008774 struct file *file;
8775 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008776
8777 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008778 atomic_inc(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008779 xa_for_each(&tctx->xa, index, file)
8780 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008781 atomic_dec(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008782
8783 if (files)
8784 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008785}
8786
8787static s64 tctx_inflight(struct io_uring_task *tctx)
8788{
8789 unsigned long index;
8790 struct file *file;
8791 s64 inflight;
8792
8793 inflight = percpu_counter_sum(&tctx->inflight);
8794 if (!tctx->sqpoll)
8795 return inflight;
8796
8797 /*
8798 * If we have SQPOLL rings, then we need to iterate and find them, and
8799 * add the pending count for those.
8800 */
8801 xa_for_each(&tctx->xa, index, file) {
8802 struct io_ring_ctx *ctx = file->private_data;
8803
8804 if (ctx->flags & IORING_SETUP_SQPOLL) {
8805 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
8806
8807 inflight += percpu_counter_sum(&__tctx->inflight);
8808 }
8809 }
8810
8811 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008812}
8813
Jens Axboe0f212202020-09-13 13:09:39 -06008814/*
8815 * Find any io_uring fd that this task has registered or done IO on, and cancel
8816 * requests.
8817 */
8818void __io_uring_task_cancel(void)
8819{
8820 struct io_uring_task *tctx = current->io_uring;
8821 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008822 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008823
8824 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008825 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008826
Pavel Begunkov186725a2021-01-26 11:17:08 +00008827 /* trigger io_disable_sqo_submit() */
8828 if (tctx->sqpoll)
8829 __io_uring_files_cancel(NULL);
8830
Jens Axboed8a6df12020-10-15 16:24:45 -06008831 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008832 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008833 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008834 if (!inflight)
8835 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008836 __io_uring_files_cancel(NULL);
8837
8838 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8839
8840 /*
Pavel Begunkovb462a7b2021-02-09 04:47:43 +00008841 * If we've seen completions, retry without waiting. This
8842 * avoids a race where a completion comes in before we did
8843 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008844 */
Pavel Begunkovb462a7b2021-02-09 04:47:43 +00008845 if (inflight == tctx_inflight(tctx))
8846 schedule();
8847 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008848 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008849
Jens Axboefdaf0832020-10-30 09:37:30 -06008850 atomic_dec(&tctx->in_idle);
Pavel Begunkov94dbb872021-01-04 20:43:29 +00008851
8852 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008853}
8854
Jens Axboefcb323c2019-10-24 12:39:47 -06008855static int io_uring_flush(struct file *file, void *data)
8856{
Pavel Begunkovda676312021-01-26 11:17:02 +00008857 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova63d9152021-01-26 11:17:03 +00008858 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkovda676312021-01-26 11:17:02 +00008859
Jens Axboef0ff1a92021-02-09 04:47:42 +00008860 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8861 io_uring_cancel_task_requests(ctx, NULL);
8862
Pavel Begunkovda676312021-01-26 11:17:02 +00008863 if (!tctx)
Pavel Begunkov18f31592021-01-26 11:17:01 +00008864 return 0;
8865
Pavel Begunkovda676312021-01-26 11:17:02 +00008866 /* we should have cancelled and erased it before PF_EXITING */
8867 WARN_ON_ONCE((current->flags & PF_EXITING) &&
8868 xa_load(&tctx->xa, (unsigned long)file));
8869
Pavel Begunkov18f31592021-01-26 11:17:01 +00008870 /*
8871 * fput() is pending, will be 2 if the only other ref is our potential
8872 * task file note. If the task is exiting, drop regardless of count.
8873 */
Pavel Begunkovda676312021-01-26 11:17:02 +00008874 if (atomic_long_read(&file->f_count) != 2)
8875 return 0;
Pavel Begunkov18f31592021-01-26 11:17:01 +00008876
Pavel Begunkova63d9152021-01-26 11:17:03 +00008877 if (ctx->flags & IORING_SETUP_SQPOLL) {
8878 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov54b4c4f2021-01-26 11:17:07 +00008879 WARN_ON_ONCE(ctx->sqo_task != current &&
8880 xa_load(&tctx->xa, (unsigned long)file));
8881 /* sqo_dead check is for when this happens after cancellation */
8882 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkova63d9152021-01-26 11:17:03 +00008883 !xa_load(&tctx->xa, (unsigned long)file));
8884
8885 io_disable_sqo_submit(ctx);
8886 }
8887
8888 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
8889 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008890 return 0;
8891}
8892
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008893static void *io_uring_validate_mmap_request(struct file *file,
8894 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008895{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008896 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008897 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008898 struct page *page;
8899 void *ptr;
8900
8901 switch (offset) {
8902 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008903 case IORING_OFF_CQ_RING:
8904 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008905 break;
8906 case IORING_OFF_SQES:
8907 ptr = ctx->sq_sqes;
8908 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008909 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008910 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008911 }
8912
8913 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008914 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008915 return ERR_PTR(-EINVAL);
8916
8917 return ptr;
8918}
8919
8920#ifdef CONFIG_MMU
8921
8922static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8923{
8924 size_t sz = vma->vm_end - vma->vm_start;
8925 unsigned long pfn;
8926 void *ptr;
8927
8928 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8929 if (IS_ERR(ptr))
8930 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008931
8932 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8933 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8934}
8935
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008936#else /* !CONFIG_MMU */
8937
8938static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8939{
8940 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8941}
8942
8943static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8944{
8945 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8946}
8947
8948static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8949 unsigned long addr, unsigned long len,
8950 unsigned long pgoff, unsigned long flags)
8951{
8952 void *ptr;
8953
8954 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8955 if (IS_ERR(ptr))
8956 return PTR_ERR(ptr);
8957
8958 return (unsigned long) ptr;
8959}
8960
8961#endif /* !CONFIG_MMU */
8962
Pavel Begunkova63d9152021-01-26 11:17:03 +00008963static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06008964{
Pavel Begunkova63d9152021-01-26 11:17:03 +00008965 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06008966 DEFINE_WAIT(wait);
8967
8968 do {
8969 if (!io_sqring_full(ctx))
8970 break;
8971
8972 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8973
Pavel Begunkova63d9152021-01-26 11:17:03 +00008974 if (unlikely(ctx->sqo_dead)) {
8975 ret = -EOWNERDEAD;
8976 goto out;
8977 }
8978
Jens Axboe90554202020-09-03 12:12:41 -06008979 if (!io_sqring_full(ctx))
8980 break;
8981
8982 schedule();
8983 } while (!signal_pending(current));
8984
8985 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkova63d9152021-01-26 11:17:03 +00008986out:
8987 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06008988}
8989
Jens Axboe2b188cc2019-01-07 10:46:33 -07008990SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8991 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8992 size_t, sigsz)
8993{
8994 struct io_ring_ctx *ctx;
8995 long ret = -EBADF;
8996 int submitted = 0;
8997 struct fd f;
8998
Jens Axboe4c6e2772020-07-01 11:29:10 -06008999 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009000
Jens Axboe90554202020-09-03 12:12:41 -06009001 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9002 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009003 return -EINVAL;
9004
9005 f = fdget(fd);
9006 if (!f.file)
9007 return -EBADF;
9008
9009 ret = -EOPNOTSUPP;
9010 if (f.file->f_op != &io_uring_fops)
9011 goto out_fput;
9012
9013 ret = -ENXIO;
9014 ctx = f.file->private_data;
9015 if (!percpu_ref_tryget(&ctx->refs))
9016 goto out_fput;
9017
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009018 ret = -EBADFD;
9019 if (ctx->flags & IORING_SETUP_R_DISABLED)
9020 goto out;
9021
Jens Axboe6c271ce2019-01-10 11:22:30 -07009022 /*
9023 * For SQ polling, the thread will do all submissions and completions.
9024 * Just return the requested submit count, and wake the thread if
9025 * we were asked to.
9026 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009027 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009028 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00009029 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkovbc924dd2021-01-12 21:17:25 +00009030
Pavel Begunkova63d9152021-01-26 11:17:03 +00009031 ret = -EOWNERDEAD;
9032 if (unlikely(ctx->sqo_dead))
9033 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009034 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009035 wake_up(&ctx->sq_data->wait);
Pavel Begunkova63d9152021-01-26 11:17:03 +00009036 if (flags & IORING_ENTER_SQ_WAIT) {
9037 ret = io_sqpoll_wait_sq(ctx);
9038 if (ret)
9039 goto out;
9040 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009041 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009042 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009043 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009044 if (unlikely(ret))
9045 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009046 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009047 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009048 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009049
9050 if (submitted != to_submit)
9051 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009052 }
9053 if (flags & IORING_ENTER_GETEVENTS) {
9054 min_complete = min(min_complete, ctx->cq_entries);
9055
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009056 /*
9057 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9058 * space applications don't need to do io completion events
9059 * polling again, they can rely on io_sq_thread to do polling
9060 * work, which can reduce cpu usage and uring_lock contention.
9061 */
9062 if (ctx->flags & IORING_SETUP_IOPOLL &&
9063 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009064 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009065 } else {
9066 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9067 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009068 }
9069
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009070out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009071 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009072out_fput:
9073 fdput(f);
9074 return submitted ? submitted : ret;
9075}
9076
Tobias Klauserbebdb652020-02-26 18:38:32 +01009077#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009078static int io_uring_show_cred(int id, void *p, void *data)
9079{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009080 struct io_identity *iod = p;
9081 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009082 struct seq_file *m = data;
9083 struct user_namespace *uns = seq_user_ns(m);
9084 struct group_info *gi;
9085 kernel_cap_t cap;
9086 unsigned __capi;
9087 int g;
9088
9089 seq_printf(m, "%5d\n", id);
9090 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9091 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9092 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9093 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9094 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9095 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9096 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9097 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9098 seq_puts(m, "\n\tGroups:\t");
9099 gi = cred->group_info;
9100 for (g = 0; g < gi->ngroups; g++) {
9101 seq_put_decimal_ull(m, g ? " " : "",
9102 from_kgid_munged(uns, gi->gid[g]));
9103 }
9104 seq_puts(m, "\n\tCapEff:\t");
9105 cap = cred->cap_effective;
9106 CAP_FOR_EACH_U32(__capi)
9107 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9108 seq_putc(m, '\n');
9109 return 0;
9110}
9111
9112static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9113{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009114 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009115 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009116 int i;
9117
Jens Axboefad8e0d2020-09-28 08:57:48 -06009118 /*
9119 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9120 * since fdinfo case grabs it in the opposite direction of normal use
9121 * cases. If we fail to get the lock, we just don't iterate any
9122 * structures that could be going away outside the io_uring mutex.
9123 */
9124 has_lock = mutex_trylock(&ctx->uring_lock);
9125
Joseph Qidbbe9c62020-09-29 09:01:22 -06009126 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9127 sq = ctx->sq_data;
9128
9129 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9130 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009131 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009132 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009133 struct fixed_file_table *table;
9134 struct file *f;
9135
9136 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9137 f = table->files[i & IORING_FILE_TABLE_MASK];
9138 if (f)
9139 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9140 else
9141 seq_printf(m, "%5u: <none>\n", i);
9142 }
9143 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009144 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009145 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9146
9147 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9148 (unsigned int) buf->len);
9149 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009150 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009151 seq_printf(m, "Personalities:\n");
9152 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9153 }
Jens Axboed7718a92020-02-14 22:23:12 -07009154 seq_printf(m, "PollList:\n");
9155 spin_lock_irq(&ctx->completion_lock);
9156 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9157 struct hlist_head *list = &ctx->cancel_hash[i];
9158 struct io_kiocb *req;
9159
9160 hlist_for_each_entry(req, list, hash_node)
9161 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9162 req->task->task_works != NULL);
9163 }
9164 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009165 if (has_lock)
9166 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009167}
9168
9169static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9170{
9171 struct io_ring_ctx *ctx = f->private_data;
9172
9173 if (percpu_ref_tryget(&ctx->refs)) {
9174 __io_uring_show_fdinfo(ctx, m);
9175 percpu_ref_put(&ctx->refs);
9176 }
9177}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009178#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009179
Jens Axboe2b188cc2019-01-07 10:46:33 -07009180static const struct file_operations io_uring_fops = {
9181 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009182 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009184#ifndef CONFIG_MMU
9185 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9186 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9187#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009188 .poll = io_uring_poll,
9189 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009190#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009191 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009192#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009193};
9194
9195static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9196 struct io_uring_params *p)
9197{
Hristo Venev75b28af2019-08-26 17:23:46 +00009198 struct io_rings *rings;
9199 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009200
Jens Axboebd740482020-08-05 12:58:23 -06009201 /* make sure these are sane, as we already accounted them */
9202 ctx->sq_entries = p->sq_entries;
9203 ctx->cq_entries = p->cq_entries;
9204
Hristo Venev75b28af2019-08-26 17:23:46 +00009205 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9206 if (size == SIZE_MAX)
9207 return -EOVERFLOW;
9208
9209 rings = io_mem_alloc(size);
9210 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009211 return -ENOMEM;
9212
Hristo Venev75b28af2019-08-26 17:23:46 +00009213 ctx->rings = rings;
9214 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9215 rings->sq_ring_mask = p->sq_entries - 1;
9216 rings->cq_ring_mask = p->cq_entries - 1;
9217 rings->sq_ring_entries = p->sq_entries;
9218 rings->cq_ring_entries = p->cq_entries;
9219 ctx->sq_mask = rings->sq_ring_mask;
9220 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009221
9222 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009223 if (size == SIZE_MAX) {
9224 io_mem_free(ctx->rings);
9225 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009226 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009227 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009228
9229 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009230 if (!ctx->sq_sqes) {
9231 io_mem_free(ctx->rings);
9232 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009233 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009234 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009235
Jens Axboe2b188cc2019-01-07 10:46:33 -07009236 return 0;
9237}
9238
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009239static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9240{
9241 int ret, fd;
9242
9243 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9244 if (fd < 0)
9245 return fd;
9246
9247 ret = io_uring_add_task_file(ctx, file);
9248 if (ret) {
9249 put_unused_fd(fd);
9250 return ret;
9251 }
9252 fd_install(fd, file);
9253 return fd;
9254}
9255
Jens Axboe2b188cc2019-01-07 10:46:33 -07009256/*
9257 * Allocate an anonymous fd, this is what constitutes the application
9258 * visible backing of an io_uring instance. The application mmaps this
9259 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9260 * we have to tie this fd to a socket for file garbage collection purposes.
9261 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009262static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009263{
9264 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009265#if defined(CONFIG_UNIX)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009266 int ret;
9267
Jens Axboe2b188cc2019-01-07 10:46:33 -07009268 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9269 &ctx->ring_sock);
9270 if (ret)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009271 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009272#endif
9273
Jens Axboe2b188cc2019-01-07 10:46:33 -07009274 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9275 O_RDWR | O_CLOEXEC);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009276#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009277 if (IS_ERR(file)) {
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009278 sock_release(ctx->ring_sock);
9279 ctx->ring_sock = NULL;
9280 } else {
9281 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009282 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009283#endif
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009284 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009285}
9286
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009287static int io_uring_create(unsigned entries, struct io_uring_params *p,
9288 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009289{
9290 struct user_struct *user = NULL;
9291 struct io_ring_ctx *ctx;
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009292 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009293 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009294 int ret;
9295
Jens Axboe8110c1a2019-12-28 15:39:54 -07009296 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009297 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009298 if (entries > IORING_MAX_ENTRIES) {
9299 if (!(p->flags & IORING_SETUP_CLAMP))
9300 return -EINVAL;
9301 entries = IORING_MAX_ENTRIES;
9302 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303
9304 /*
9305 * Use twice as many entries for the CQ ring. It's possible for the
9306 * application to drive a higher depth than the size of the SQ ring,
9307 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009308 * some flexibility in overcommitting a bit. If the application has
9309 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9310 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009311 */
9312 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009313 if (p->flags & IORING_SETUP_CQSIZE) {
9314 /*
9315 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9316 * to a power-of-two, if it isn't already. We do NOT impose
9317 * any cq vs sq ring sizing.
9318 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009319 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009320 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009321 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9322 if (!(p->flags & IORING_SETUP_CLAMP))
9323 return -EINVAL;
9324 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9325 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009326 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9327 if (p->cq_entries < p->sq_entries)
9328 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009329 } else {
9330 p->cq_entries = 2 * p->sq_entries;
9331 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009332
9333 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009334 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009335
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009336 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009337 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009338 ring_pages(p->sq_entries, p->cq_entries));
9339 if (ret) {
9340 free_uid(user);
9341 return ret;
9342 }
9343 }
9344
9345 ctx = io_ring_ctx_alloc(p);
9346 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009347 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009348 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009349 p->cq_entries));
9350 free_uid(user);
9351 return -ENOMEM;
9352 }
9353 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009354 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009355 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009356#ifdef CONFIG_AUDIT
9357 ctx->loginuid = current->loginuid;
9358 ctx->sessionid = current->sessionid;
9359#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009360 ctx->sqo_task = get_task_struct(current);
9361
9362 /*
9363 * This is just grabbed for accounting purposes. When a process exits,
9364 * the mm is exited and dropped before the files, hence we need to hang
9365 * on to this mm purely for the purposes of being able to unaccount
9366 * memory (locked/pinned vm). It's not used for anything else.
9367 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009368 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009369 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009370
Dennis Zhou91d8f512020-09-16 13:41:05 -07009371#ifdef CONFIG_BLK_CGROUP
9372 /*
9373 * The sq thread will belong to the original cgroup it was inited in.
9374 * If the cgroup goes offline (e.g. disabling the io controller), then
9375 * issued bios will be associated with the closest cgroup later in the
9376 * block layer.
9377 */
9378 rcu_read_lock();
9379 ctx->sqo_blkcg_css = blkcg_css();
9380 ret = css_tryget_online(ctx->sqo_blkcg_css);
9381 rcu_read_unlock();
9382 if (!ret) {
9383 /* don't init against a dying cgroup, have the user try again */
9384 ctx->sqo_blkcg_css = NULL;
9385 ret = -ENODEV;
9386 goto err;
9387 }
9388#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009389
Jens Axboe2b188cc2019-01-07 10:46:33 -07009390 /*
9391 * Account memory _before_ installing the file descriptor. Once
9392 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009393 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009394 * will un-account as well.
9395 */
9396 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9397 ACCT_LOCKED);
9398 ctx->limit_mem = limit_mem;
9399
9400 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009401 if (ret)
9402 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009403
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009404 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405 if (ret)
9406 goto err;
9407
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009408 if (!(p->flags & IORING_SETUP_R_DISABLED))
9409 io_sq_offload_start(ctx);
9410
Jens Axboe2b188cc2019-01-07 10:46:33 -07009411 memset(&p->sq_off, 0, sizeof(p->sq_off));
9412 p->sq_off.head = offsetof(struct io_rings, sq.head);
9413 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9414 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9415 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9416 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9417 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9418 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9419
9420 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009421 p->cq_off.head = offsetof(struct io_rings, cq.head);
9422 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9423 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9424 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9425 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9426 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009427 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009428
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009429 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9430 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009431 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9432 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009433
9434 if (copy_to_user(params, p, sizeof(*p))) {
9435 ret = -EFAULT;
9436 goto err;
9437 }
Jens Axboed1719f72020-07-30 13:43:53 -06009438
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009439 file = io_uring_get_file(ctx);
9440 if (IS_ERR(file)) {
9441 ret = PTR_ERR(file);
9442 goto err;
9443 }
9444
Jens Axboed1719f72020-07-30 13:43:53 -06009445 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009446 * Install ring fd as the very last thing, so we don't risk someone
9447 * having closed it before we finish setup
9448 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009449 ret = io_uring_install_fd(ctx, file);
9450 if (ret < 0) {
Pavel Begunkov8cb6f4d2021-01-26 11:17:05 +00009451 io_disable_sqo_submit(ctx);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009452 /* fput will clean it up */
9453 fput(file);
9454 return ret;
9455 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009456
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009457 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009458 return ret;
9459err:
Pavel Begunkova63d9152021-01-26 11:17:03 +00009460 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009461 io_ring_ctx_wait_and_kill(ctx);
9462 return ret;
9463}
9464
9465/*
9466 * Sets up an aio uring context, and returns the fd. Applications asks for a
9467 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9468 * params structure passed in.
9469 */
9470static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9471{
9472 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009473 int i;
9474
9475 if (copy_from_user(&p, params, sizeof(p)))
9476 return -EFAULT;
9477 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9478 if (p.resv[i])
9479 return -EINVAL;
9480 }
9481
Jens Axboe6c271ce2019-01-10 11:22:30 -07009482 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009483 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009484 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9485 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009486 return -EINVAL;
9487
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009488 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009489}
9490
9491SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9492 struct io_uring_params __user *, params)
9493{
9494 return io_uring_setup(entries, params);
9495}
9496
Jens Axboe66f4af92020-01-16 15:36:52 -07009497static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9498{
9499 struct io_uring_probe *p;
9500 size_t size;
9501 int i, ret;
9502
9503 size = struct_size(p, ops, nr_args);
9504 if (size == SIZE_MAX)
9505 return -EOVERFLOW;
9506 p = kzalloc(size, GFP_KERNEL);
9507 if (!p)
9508 return -ENOMEM;
9509
9510 ret = -EFAULT;
9511 if (copy_from_user(p, arg, size))
9512 goto out;
9513 ret = -EINVAL;
9514 if (memchr_inv(p, 0, size))
9515 goto out;
9516
9517 p->last_op = IORING_OP_LAST - 1;
9518 if (nr_args > IORING_OP_LAST)
9519 nr_args = IORING_OP_LAST;
9520
9521 for (i = 0; i < nr_args; i++) {
9522 p->ops[i].op = i;
9523 if (!io_op_defs[i].not_supported)
9524 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9525 }
9526 p->ops_len = i;
9527
9528 ret = 0;
9529 if (copy_to_user(arg, p, size))
9530 ret = -EFAULT;
9531out:
9532 kfree(p);
9533 return ret;
9534}
9535
Jens Axboe071698e2020-01-28 10:04:42 -07009536static int io_register_personality(struct io_ring_ctx *ctx)
9537{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009538 struct io_identity *id;
9539 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009540
Jens Axboe1e6fa522020-10-15 08:46:24 -06009541 id = kmalloc(sizeof(*id), GFP_KERNEL);
9542 if (unlikely(!id))
9543 return -ENOMEM;
9544
9545 io_init_identity(id);
9546 id->creds = get_current_cred();
9547
9548 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9549 if (ret < 0) {
9550 put_cred(id->creds);
9551 kfree(id);
9552 }
9553 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009554}
9555
9556static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9557{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009558 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009559
Jens Axboe1e6fa522020-10-15 08:46:24 -06009560 iod = idr_remove(&ctx->personality_idr, id);
9561 if (iod) {
9562 put_cred(iod->creds);
9563 if (refcount_dec_and_test(&iod->count))
9564 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009565 return 0;
9566 }
9567
9568 return -EINVAL;
9569}
9570
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009571static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9572 unsigned int nr_args)
9573{
9574 struct io_uring_restriction *res;
9575 size_t size;
9576 int i, ret;
9577
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009578 /* Restrictions allowed only if rings started disabled */
9579 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9580 return -EBADFD;
9581
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009582 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009583 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009584 return -EBUSY;
9585
9586 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9587 return -EINVAL;
9588
9589 size = array_size(nr_args, sizeof(*res));
9590 if (size == SIZE_MAX)
9591 return -EOVERFLOW;
9592
9593 res = memdup_user(arg, size);
9594 if (IS_ERR(res))
9595 return PTR_ERR(res);
9596
9597 ret = 0;
9598
9599 for (i = 0; i < nr_args; i++) {
9600 switch (res[i].opcode) {
9601 case IORING_RESTRICTION_REGISTER_OP:
9602 if (res[i].register_op >= IORING_REGISTER_LAST) {
9603 ret = -EINVAL;
9604 goto out;
9605 }
9606
9607 __set_bit(res[i].register_op,
9608 ctx->restrictions.register_op);
9609 break;
9610 case IORING_RESTRICTION_SQE_OP:
9611 if (res[i].sqe_op >= IORING_OP_LAST) {
9612 ret = -EINVAL;
9613 goto out;
9614 }
9615
9616 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9617 break;
9618 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9619 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9620 break;
9621 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9622 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9623 break;
9624 default:
9625 ret = -EINVAL;
9626 goto out;
9627 }
9628 }
9629
9630out:
9631 /* Reset all restrictions if an error happened */
9632 if (ret != 0)
9633 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9634 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009635 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009636
9637 kfree(res);
9638 return ret;
9639}
9640
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009641static int io_register_enable_rings(struct io_ring_ctx *ctx)
9642{
9643 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9644 return -EBADFD;
9645
9646 if (ctx->restrictions.registered)
9647 ctx->restricted = 1;
9648
9649 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9650
9651 io_sq_offload_start(ctx);
9652
9653 return 0;
9654}
9655
Jens Axboe071698e2020-01-28 10:04:42 -07009656static bool io_register_op_must_quiesce(int op)
9657{
9658 switch (op) {
9659 case IORING_UNREGISTER_FILES:
9660 case IORING_REGISTER_FILES_UPDATE:
9661 case IORING_REGISTER_PROBE:
9662 case IORING_REGISTER_PERSONALITY:
9663 case IORING_UNREGISTER_PERSONALITY:
9664 return false;
9665 default:
9666 return true;
9667 }
9668}
9669
Jens Axboeedafcce2019-01-09 09:16:05 -07009670static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9671 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009672 __releases(ctx->uring_lock)
9673 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009674{
9675 int ret;
9676
Jens Axboe35fa71a2019-04-22 10:23:23 -06009677 /*
9678 * We're inside the ring mutex, if the ref is already dying, then
9679 * someone else killed the ctx or is already going through
9680 * io_uring_register().
9681 */
9682 if (percpu_ref_is_dying(&ctx->refs))
9683 return -ENXIO;
9684
Jens Axboe071698e2020-01-28 10:04:42 -07009685 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009686 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009687
Jens Axboe05f3fb32019-12-09 11:22:50 -07009688 /*
9689 * Drop uring mutex before waiting for references to exit. If
9690 * another thread is currently inside io_uring_enter() it might
9691 * need to grab the uring_lock to make progress. If we hold it
9692 * here across the drain wait, then we can deadlock. It's safe
9693 * to drop the mutex here, since no new references will come in
9694 * after we've killed the percpu ref.
9695 */
9696 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009697 do {
9698 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9699 if (!ret)
9700 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009701 ret = io_run_task_work_sig();
9702 if (ret < 0)
9703 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009704 } while (1);
9705
Jens Axboe05f3fb32019-12-09 11:22:50 -07009706 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009707
Jens Axboec1503682020-01-08 08:26:07 -07009708 if (ret) {
9709 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009710 goto out_quiesce;
9711 }
9712 }
9713
9714 if (ctx->restricted) {
9715 if (opcode >= IORING_REGISTER_LAST) {
9716 ret = -EINVAL;
9717 goto out;
9718 }
9719
9720 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9721 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009722 goto out;
9723 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009724 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009725
9726 switch (opcode) {
9727 case IORING_REGISTER_BUFFERS:
9728 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9729 break;
9730 case IORING_UNREGISTER_BUFFERS:
9731 ret = -EINVAL;
9732 if (arg || nr_args)
9733 break;
9734 ret = io_sqe_buffer_unregister(ctx);
9735 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009736 case IORING_REGISTER_FILES:
9737 ret = io_sqe_files_register(ctx, arg, nr_args);
9738 break;
9739 case IORING_UNREGISTER_FILES:
9740 ret = -EINVAL;
9741 if (arg || nr_args)
9742 break;
9743 ret = io_sqe_files_unregister(ctx);
9744 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009745 case IORING_REGISTER_FILES_UPDATE:
9746 ret = io_sqe_files_update(ctx, arg, nr_args);
9747 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009748 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009749 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009750 ret = -EINVAL;
9751 if (nr_args != 1)
9752 break;
9753 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009754 if (ret)
9755 break;
9756 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9757 ctx->eventfd_async = 1;
9758 else
9759 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009760 break;
9761 case IORING_UNREGISTER_EVENTFD:
9762 ret = -EINVAL;
9763 if (arg || nr_args)
9764 break;
9765 ret = io_eventfd_unregister(ctx);
9766 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009767 case IORING_REGISTER_PROBE:
9768 ret = -EINVAL;
9769 if (!arg || nr_args > 256)
9770 break;
9771 ret = io_probe(ctx, arg, nr_args);
9772 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009773 case IORING_REGISTER_PERSONALITY:
9774 ret = -EINVAL;
9775 if (arg || nr_args)
9776 break;
9777 ret = io_register_personality(ctx);
9778 break;
9779 case IORING_UNREGISTER_PERSONALITY:
9780 ret = -EINVAL;
9781 if (arg)
9782 break;
9783 ret = io_unregister_personality(ctx, nr_args);
9784 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009785 case IORING_REGISTER_ENABLE_RINGS:
9786 ret = -EINVAL;
9787 if (arg || nr_args)
9788 break;
9789 ret = io_register_enable_rings(ctx);
9790 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009791 case IORING_REGISTER_RESTRICTIONS:
9792 ret = io_register_restrictions(ctx, arg, nr_args);
9793 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009794 default:
9795 ret = -EINVAL;
9796 break;
9797 }
9798
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009799out:
Jens Axboe071698e2020-01-28 10:04:42 -07009800 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009801 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009802 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009803out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009804 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009805 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009806 return ret;
9807}
9808
9809SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9810 void __user *, arg, unsigned int, nr_args)
9811{
9812 struct io_ring_ctx *ctx;
9813 long ret = -EBADF;
9814 struct fd f;
9815
9816 f = fdget(fd);
9817 if (!f.file)
9818 return -EBADF;
9819
9820 ret = -EOPNOTSUPP;
9821 if (f.file->f_op != &io_uring_fops)
9822 goto out_fput;
9823
9824 ctx = f.file->private_data;
9825
9826 mutex_lock(&ctx->uring_lock);
9827 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9828 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009829 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9830 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009831out_fput:
9832 fdput(f);
9833 return ret;
9834}
9835
Jens Axboe2b188cc2019-01-07 10:46:33 -07009836static int __init io_uring_init(void)
9837{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009838#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9839 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9840 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9841} while (0)
9842
9843#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9844 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9845 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9846 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9847 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9848 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9849 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9850 BUILD_BUG_SQE_ELEM(8, __u64, off);
9851 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9852 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009853 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009854 BUILD_BUG_SQE_ELEM(24, __u32, len);
9855 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9856 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9857 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9858 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009859 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9860 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009861 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9862 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9863 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9864 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9865 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9866 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9867 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9868 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009869 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009870 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9871 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9872 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009873 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009874
Jens Axboed3656342019-12-18 09:50:26 -07009875 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009876 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009877 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9878 return 0;
9879};
9880__initcall(io_uring_init);