blob: 5bccb235271f7fc60cffefadf5e71f0e0355c49b [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;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800248
249 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600250};
251
Jens Axboe2b188cc2019-01-07 10:46:33 -0700252struct io_ring_ctx {
253 struct {
254 struct percpu_ref refs;
255 } ____cacheline_aligned_in_smp;
256
257 struct {
258 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800259 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700260 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800261 unsigned int cq_overflow_flushed: 1;
262 unsigned int drain_next: 1;
263 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200264 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265
Hristo Venev75b28af2019-08-26 17:23:46 +0000266 /*
267 * Ring buffer of indices into array of io_uring_sqe, which is
268 * mmapped by the application using the IORING_OFF_SQES offset.
269 *
270 * This indirection could e.g. be used to assign fixed
271 * io_uring_sqe entries to operations and only submit them to
272 * the queue when needed.
273 *
274 * The kernel modifies neither the indices array nor the entries
275 * array.
276 */
277 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 unsigned cached_sq_head;
279 unsigned sq_entries;
280 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700281 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600282 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100283 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700284 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600285
286 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600287 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700288 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289
Jens Axboead3eb2c2019-12-18 17:12:20 -0700290 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700291 } ____cacheline_aligned_in_smp;
292
Hristo Venev75b28af2019-08-26 17:23:46 +0000293 struct io_rings *rings;
294
Jens Axboe2b188cc2019-01-07 10:46:33 -0700295 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600296 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600297
298 /*
299 * For SQPOLL usage - we hold a reference to the parent task, so we
300 * have access to the ->files
301 */
302 struct task_struct *sqo_task;
303
304 /* Only used for accounting purposes */
305 struct mm_struct *mm_account;
306
Dennis Zhou91d8f512020-09-16 13:41:05 -0700307#ifdef CONFIG_BLK_CGROUP
308 struct cgroup_subsys_state *sqo_blkcg_css;
309#endif
310
Jens Axboe534ca6d2020-09-02 13:52:19 -0600311 struct io_sq_data *sq_data; /* if using sq thread polling */
312
Jens Axboe90554202020-09-03 12:12:41 -0600313 struct wait_queue_head sqo_sq_wait;
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;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700356 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700357 struct wait_queue_head cq_wait;
358 struct fasync_struct *cq_fasync;
359 struct eventfd_ctx *cq_ev_fd;
360 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361
362 struct {
363 struct mutex uring_lock;
364 wait_queue_head_t wait;
365 } ____cacheline_aligned_in_smp;
366
367 struct {
368 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700369
Jens Axboedef596e2019-01-09 08:59:42 -0700370 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300371 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700372 * io_uring instances that don't use IORING_SETUP_SQPOLL.
373 * For SQPOLL, only the single threaded io_sq_thread() will
374 * manipulate the list, hence no extra locking is needed there.
375 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300376 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700377 struct hlist_head *cancel_hash;
378 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700379 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600380
381 spinlock_t inflight_lock;
382 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600384
Jens Axboe4a38aed22020-05-14 17:21:15 -0600385 struct delayed_work file_put_work;
386 struct llist_head file_put_llist;
387
Jens Axboe85faa7b2020-04-09 18:14:00 -0600388 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200389 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700390};
391
Jens Axboe09bb8392019-03-13 12:39:28 -0600392/*
393 * First field must be the file pointer in all the
394 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
395 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700396struct io_poll_iocb {
397 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000398 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700399 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600400 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700401 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700402 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700403};
404
Pavel Begunkov018043b2020-10-27 23:17:18 +0000405struct io_poll_remove {
406 struct file *file;
407 u64 addr;
408};
409
Jens Axboeb5dba592019-12-11 14:02:38 -0700410struct io_close {
411 struct file *file;
412 struct file *put_file;
413 int fd;
414};
415
Jens Axboead8a48a2019-11-15 08:49:11 -0700416struct io_timeout_data {
417 struct io_kiocb *req;
418 struct hrtimer timer;
419 struct timespec64 ts;
420 enum hrtimer_mode mode;
421};
422
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700423struct io_accept {
424 struct file *file;
425 struct sockaddr __user *addr;
426 int __user *addr_len;
427 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600428 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700429};
430
431struct io_sync {
432 struct file *file;
433 loff_t len;
434 loff_t off;
435 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700436 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700437};
438
Jens Axboefbf23842019-12-17 18:45:56 -0700439struct io_cancel {
440 struct file *file;
441 u64 addr;
442};
443
Jens Axboeb29472e2019-12-17 18:50:29 -0700444struct io_timeout {
445 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300446 u32 off;
447 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300448 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000449 /* head of the link, used by linked timeouts only */
450 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700451};
452
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100453struct io_timeout_rem {
454 struct file *file;
455 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000456
457 /* timeout update */
458 struct timespec64 ts;
459 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100460};
461
Jens Axboe9adbd452019-12-20 08:45:55 -0700462struct io_rw {
463 /* NOTE: kiocb has the file as the first member, so don't do it here */
464 struct kiocb kiocb;
465 u64 addr;
466 u64 len;
467};
468
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700469struct io_connect {
470 struct file *file;
471 struct sockaddr __user *addr;
472 int addr_len;
473};
474
Jens Axboee47293f2019-12-20 08:58:21 -0700475struct io_sr_msg {
476 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700477 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300478 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700479 void __user *buf;
480 };
Jens Axboee47293f2019-12-20 08:58:21 -0700481 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700482 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700483 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700484 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700485};
486
Jens Axboe15b71ab2019-12-11 11:20:36 -0700487struct io_open {
488 struct file *file;
489 int dfd;
Jens Axboe944d1442020-11-13 16:48:44 -0700490 bool ignore_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700491 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700492 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600493 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700494};
495
Jens Axboe05f3fb32019-12-09 11:22:50 -0700496struct io_files_update {
497 struct file *file;
498 u64 arg;
499 u32 nr_args;
500 u32 offset;
501};
502
Jens Axboe4840e412019-12-25 22:03:45 -0700503struct io_fadvise {
504 struct file *file;
505 u64 offset;
506 u32 len;
507 u32 advice;
508};
509
Jens Axboec1ca7572019-12-25 22:18:28 -0700510struct io_madvise {
511 struct file *file;
512 u64 addr;
513 u32 len;
514 u32 advice;
515};
516
Jens Axboe3e4827b2020-01-08 15:18:09 -0700517struct io_epoll {
518 struct file *file;
519 int epfd;
520 int op;
521 int fd;
522 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523};
524
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300525struct io_splice {
526 struct file *file_out;
527 struct file *file_in;
528 loff_t off_out;
529 loff_t off_in;
530 u64 len;
531 unsigned int flags;
532};
533
Jens Axboeddf0322d2020-02-23 16:41:33 -0700534struct io_provide_buf {
535 struct file *file;
536 __u64 addr;
537 __s32 len;
538 __u32 bgid;
539 __u16 nbufs;
540 __u16 bid;
541};
542
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700543struct io_statx {
544 struct file *file;
545 int dfd;
546 unsigned int mask;
547 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700548 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700549 struct statx __user *buffer;
550};
551
Jens Axboe36f4fa62020-09-05 11:14:22 -0600552struct io_shutdown {
553 struct file *file;
554 int how;
555};
556
Jens Axboe80a261f2020-09-28 14:23:58 -0600557struct io_rename {
558 struct file *file;
559 int old_dfd;
560 int new_dfd;
561 struct filename *oldpath;
562 struct filename *newpath;
563 int flags;
564};
565
Jens Axboe14a11432020-09-28 14:27:37 -0600566struct io_unlink {
567 struct file *file;
568 int dfd;
569 int flags;
570 struct filename *filename;
571};
572
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300573struct io_completion {
574 struct file *file;
575 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300576 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300577};
578
Jens Axboef499a022019-12-02 16:28:46 -0700579struct io_async_connect {
580 struct sockaddr_storage address;
581};
582
Jens Axboe03b12302019-12-02 18:50:25 -0700583struct io_async_msghdr {
584 struct iovec fast_iov[UIO_FASTIOV];
585 struct iovec *iov;
586 struct sockaddr __user *uaddr;
587 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700588 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700589};
590
Jens Axboef67676d2019-12-02 11:03:47 -0700591struct io_async_rw {
592 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600593 const struct iovec *free_iovec;
594 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600595 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600596 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700597};
598
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300599enum {
600 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
601 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
602 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
603 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
604 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700605 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300606
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300607 REQ_F_FAIL_LINK_BIT,
608 REQ_F_INFLIGHT_BIT,
609 REQ_F_CUR_POS_BIT,
610 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300613 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700614 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700615 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600616 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800617 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100618 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700619
620 /* not a real bit, just to check we're not overflowing the space */
621 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300622};
623
624enum {
625 /* ctx owns file */
626 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
627 /* drain existing IO first */
628 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
629 /* linked sqes */
630 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
631 /* doesn't sever on completion < 0 */
632 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
633 /* IOSQE_ASYNC */
634 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700635 /* IOSQE_BUFFER_SELECT */
636 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300637
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300638 /* fail rest of links */
639 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
640 /* on inflight list */
641 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
642 /* read/write uses file position */
643 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
644 /* must not punt to workers */
645 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100646 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300647 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300648 /* regular file */
649 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300650 /* needs cleanup */
651 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700652 /* already went through poll handler */
653 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700654 /* buffer already selected */
655 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600656 /* doesn't need file table for this request */
657 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800658 /* io_wq_work is initialized */
659 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100660 /* linked timeout is active, i.e. prepared by link's head */
661 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700662};
663
664struct async_poll {
665 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600666 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300667};
668
Jens Axboe09bb8392019-03-13 12:39:28 -0600669/*
670 * NOTE! Each of the iocb union members has the file pointer
671 * as the first entry in their struct definition. So you can
672 * access the file pointer through any of the sub-structs,
673 * or directly as just 'ki_filp' in this struct.
674 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700675struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700676 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600677 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700678 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700679 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000680 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700681 struct io_accept accept;
682 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700683 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700684 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100685 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700686 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700687 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700688 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700689 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700690 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700691 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700692 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700693 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300694 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700695 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700696 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600697 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600698 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600699 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300700 /* use only after cleaning per-op data, see io_clean_op() */
701 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700702 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700703
Jens Axboee8c2bc12020-08-15 18:44:09 -0700704 /* opcode allocated if it needs to store data for async defer */
705 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700706 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800707 /* polled IO has completed */
708 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700710 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300711 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700712
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300713 struct io_ring_ctx *ctx;
714 unsigned int flags;
715 refcount_t refs;
716 struct task_struct *task;
717 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000719 struct io_kiocb *link;
Pavel Begunkov04157672020-10-27 23:25:38 +0000720 struct percpu_ref *fixed_file_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700721
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300722 /*
723 * 1. used with ctx->iopoll_list with reads/writes
724 * 2. to track reqs with ->files (see io_op_def::file_table)
725 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300726 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300727 struct callback_head task_work;
728 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
729 struct hlist_node hash_node;
730 struct async_poll *apoll;
731 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700732};
733
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300734struct io_defer_entry {
735 struct list_head list;
736 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300737 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300738};
739
Jens Axboedef596e2019-01-09 08:59:42 -0700740#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741
Jens Axboe013538b2020-06-22 09:29:15 -0600742struct io_comp_state {
743 unsigned int nr;
744 struct list_head list;
745 struct io_ring_ctx *ctx;
746};
747
Jens Axboe9a56a232019-01-09 09:06:50 -0700748struct io_submit_state {
749 struct blk_plug plug;
750
751 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700752 * io_kiocb alloc cache
753 */
754 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300755 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700756
Jens Axboe27926b62020-10-28 09:33:23 -0600757 bool plug_started;
758
Jens Axboe2579f912019-01-09 09:10:43 -0700759 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600760 * Batch completion logic
761 */
762 struct io_comp_state comp;
763
764 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700765 * File reference cache
766 */
767 struct file *file;
768 unsigned int fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +0000769 unsigned int file_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700770 unsigned int ios_left;
771};
772
Jens Axboed3656342019-12-18 09:50:26 -0700773struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700774 /* needs req->file assigned */
775 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600776 /* don't fail if file grab fails */
777 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700778 /* hash wq insertion if file is a regular file */
779 unsigned hash_reg_file : 1;
780 /* unbound wq insertion if file is a non-regular file */
781 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700782 /* opcode is not supported by this kernel */
783 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700784 /* set if opcode supports polled "wait" */
785 unsigned pollin : 1;
786 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700787 /* op supports buffer selection */
788 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700789 /* must always have async data allocated */
790 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600791 /* should block plug */
792 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700793 /* size of async data needed, if any */
794 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600795 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700796};
797
Jens Axboe09186822020-10-13 15:01:40 -0600798static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300799 [IORING_OP_NOP] = {},
800 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .needs_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700803 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700804 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700805 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600806 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700807 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600808 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700811 .needs_file = 1,
812 .hash_reg_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700814 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700815 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600816 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700817 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600818 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
819 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700822 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600823 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_READ_FIXED] = {
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 Axboe27926b62020-10-28 09:33:23 -0600829 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700830 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600831 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700832 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300833 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700834 .needs_file = 1,
835 .hash_reg_file = 1,
836 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700837 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600838 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600840 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
841 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700842 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300843 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700844 .needs_file = 1,
845 .unbound_nonreg_file = 1,
846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_POLL_REMOVE] = {},
848 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700849 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600850 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700853 .needs_file = 1,
854 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700855 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700856 .needs_async_data = 1,
857 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000858 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700861 .needs_file = 1,
862 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700863 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700864 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700865 .needs_async_data = 1,
866 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000867 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700868 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300869 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700870 .needs_async_data = 1,
871 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600872 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700873 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000874 [IORING_OP_TIMEOUT_REMOVE] = {
875 /* used by timeout updates' prep() */
876 .work_flags = IO_WQ_WORK_MM,
877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700879 .needs_file = 1,
880 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700881 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600882 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_ASYNC_CANCEL] = {},
885 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700886 .needs_async_data = 1,
887 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600888 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700891 .needs_file = 1,
892 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700893 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700894 .needs_async_data = 1,
895 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600896 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700897 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300898 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700899 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600900 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700901 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300902 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600903 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600904 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600907 .needs_file = 1,
908 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600909 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700910 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300911 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600912 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600915 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
916 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700917 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300918 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700919 .needs_file = 1,
920 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700921 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700922 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600923 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700924 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600925 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700926 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300927 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700928 .needs_file = 1,
929 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700930 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600931 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700932 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600933 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
934 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700935 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300936 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700937 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600938 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700939 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300940 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600941 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700944 .needs_file = 1,
945 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700946 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600947 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700950 .needs_file = 1,
951 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700952 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700953 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600954 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700955 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300956 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600957 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600958 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700959 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700960 [IORING_OP_EPOLL_CTL] = {
961 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600962 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700963 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300964 [IORING_OP_SPLICE] = {
965 .needs_file = 1,
966 .hash_reg_file = 1,
967 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600968 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700969 },
970 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700971 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300972 [IORING_OP_TEE] = {
973 .needs_file = 1,
974 .hash_reg_file = 1,
975 .unbound_nonreg_file = 1,
976 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600977 [IORING_OP_SHUTDOWN] = {
978 .needs_file = 1,
979 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600980 [IORING_OP_RENAMEAT] = {
981 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
982 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
983 },
Jens Axboe14a11432020-09-28 14:27:37 -0600984 [IORING_OP_UNLINKAT] = {
985 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
986 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
987 },
Jens Axboed3656342019-12-18 09:50:26 -0700988};
989
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700990enum io_mem_account {
991 ACCT_LOCKED,
992 ACCT_PINNED,
993};
994
Pavel Begunkov90df0852021-01-04 20:43:30 +0000995static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
996 struct task_struct *task);
997
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000998static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node);
999static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
1000 struct io_ring_ctx *ctx);
1001
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001002static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
1003 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001004static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001005static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001006static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001007static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001008static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001009static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001010static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001011static int __io_sqe_files_update(struct io_ring_ctx *ctx,
1012 struct io_uring_files_update *ip,
1013 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001014static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001015static struct file *io_file_get(struct io_submit_state *state,
1016 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001017static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -06001018static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001019
Jens Axboeb63534c2020-06-04 11:28:00 -06001020static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1021 struct iovec **iovec, struct iov_iter *iter,
1022 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001023static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1024 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001025 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026
1027static struct kmem_cache *req_cachep;
1028
Jens Axboe09186822020-10-13 15:01:40 -06001029static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030
1031struct sock *io_uring_get_socket(struct file *file)
1032{
1033#if defined(CONFIG_UNIX)
1034 if (file->f_op == &io_uring_fops) {
1035 struct io_ring_ctx *ctx = file->private_data;
1036
1037 return ctx->ring_sock->sk;
1038 }
1039#endif
1040 return NULL;
1041}
1042EXPORT_SYMBOL(io_uring_get_socket);
1043
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001044#define io_for_each_link(pos, head) \
1045 for (pos = (head); pos; pos = pos->link)
1046
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001047static inline void io_clean_op(struct io_kiocb *req)
1048{
Pavel Begunkovbb175342020-08-20 11:33:35 +03001049 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1050 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001051 __io_clean_op(req);
1052}
1053
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001054static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001055{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001056 struct io_ring_ctx *ctx = req->ctx;
1057
1058 if (!req->fixed_file_refs) {
1059 req->fixed_file_refs = &ctx->file_data->node->refs;
1060 percpu_ref_get(req->fixed_file_refs);
1061 }
1062}
1063
Pavel Begunkov08d23632020-11-06 13:00:22 +00001064static bool io_match_task(struct io_kiocb *head,
1065 struct task_struct *task,
1066 struct files_struct *files)
1067{
1068 struct io_kiocb *req;
1069
1070 if (task && head->task != task)
1071 return false;
1072 if (!files)
1073 return true;
1074
1075 io_for_each_link(req, head) {
1076 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
1077 (req->work.flags & IO_WQ_WORK_FILES) &&
1078 req->work.identity->files == files)
1079 return true;
1080 }
1081 return false;
1082}
1083
Jens Axboe28cea78a2020-09-14 10:51:17 -06001084static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001085{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001086 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001087 struct mm_struct *mm = current->mm;
1088
1089 if (mm) {
1090 kthread_unuse_mm(mm);
1091 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001092 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001093 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001094 if (files) {
1095 struct nsproxy *nsproxy = current->nsproxy;
1096
1097 task_lock(current);
1098 current->files = NULL;
1099 current->nsproxy = NULL;
1100 task_unlock(current);
1101 put_files_struct(files);
1102 put_nsproxy(nsproxy);
1103 }
1104}
1105
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001106static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001107{
1108 if (!current->files) {
1109 struct files_struct *files;
1110 struct nsproxy *nsproxy;
1111
1112 task_lock(ctx->sqo_task);
1113 files = ctx->sqo_task->files;
1114 if (!files) {
1115 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001116 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001117 }
1118 atomic_inc(&files->count);
1119 get_nsproxy(ctx->sqo_task->nsproxy);
1120 nsproxy = ctx->sqo_task->nsproxy;
1121 task_unlock(ctx->sqo_task);
1122
1123 task_lock(current);
1124 current->files = files;
1125 current->nsproxy = nsproxy;
1126 task_unlock(current);
1127 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001128 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001129}
1130
1131static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1132{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001133 struct mm_struct *mm;
1134
1135 if (current->mm)
1136 return 0;
1137
1138 /* Should never happen */
1139 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1140 return -EFAULT;
1141
1142 task_lock(ctx->sqo_task);
1143 mm = ctx->sqo_task->mm;
1144 if (unlikely(!mm || !mmget_not_zero(mm)))
1145 mm = NULL;
1146 task_unlock(ctx->sqo_task);
1147
1148 if (mm) {
1149 kthread_use_mm(mm);
1150 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001151 }
1152
Jens Axboe4b70cf92020-11-02 10:39:05 -07001153 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001154}
1155
Jens Axboe28cea78a2020-09-14 10:51:17 -06001156static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1157 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001158{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001159 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001160 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001161
1162 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001163 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001164 if (unlikely(ret))
1165 return ret;
1166 }
1167
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001168 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1169 ret = __io_sq_thread_acquire_files(ctx);
1170 if (unlikely(ret))
1171 return ret;
1172 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001173
1174 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001175}
1176
Dennis Zhou91d8f512020-09-16 13:41:05 -07001177static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1178 struct cgroup_subsys_state **cur_css)
1179
1180{
1181#ifdef CONFIG_BLK_CGROUP
1182 /* puts the old one when swapping */
1183 if (*cur_css != ctx->sqo_blkcg_css) {
1184 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1185 *cur_css = ctx->sqo_blkcg_css;
1186 }
1187#endif
1188}
1189
1190static void io_sq_thread_unassociate_blkcg(void)
1191{
1192#ifdef CONFIG_BLK_CGROUP
1193 kthread_associate_blkcg(NULL);
1194#endif
1195}
1196
Jens Axboec40f6372020-06-25 15:39:59 -06001197static inline void req_set_fail_links(struct io_kiocb *req)
1198{
1199 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1200 req->flags |= REQ_F_FAIL_LINK;
1201}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001202
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001203/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001204 * None of these are dereferenced, they are simply used to check if any of
1205 * them have changed. If we're under current and check they are still the
1206 * same, we're fine to grab references to them for actual out-of-line use.
1207 */
1208static void io_init_identity(struct io_identity *id)
1209{
1210 id->files = current->files;
1211 id->mm = current->mm;
1212#ifdef CONFIG_BLK_CGROUP
1213 rcu_read_lock();
1214 id->blkcg_css = blkcg_css();
1215 rcu_read_unlock();
1216#endif
1217 id->creds = current_cred();
1218 id->nsproxy = current->nsproxy;
1219 id->fs = current->fs;
1220 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001221#ifdef CONFIG_AUDIT
1222 id->loginuid = current->loginuid;
1223 id->sessionid = current->sessionid;
1224#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001225 refcount_set(&id->count, 1);
1226}
1227
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001228static inline void __io_req_init_async(struct io_kiocb *req)
1229{
1230 memset(&req->work, 0, sizeof(req->work));
1231 req->flags |= REQ_F_WORK_INITIALIZED;
1232}
1233
Jens Axboe1e6fa522020-10-15 08:46:24 -06001234/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001235 * Note: must call io_req_init_async() for the first time you
1236 * touch any members of io_wq_work.
1237 */
1238static inline void io_req_init_async(struct io_kiocb *req)
1239{
Jens Axboe500a3732020-10-15 17:38:03 -06001240 struct io_uring_task *tctx = current->io_uring;
1241
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001242 if (req->flags & REQ_F_WORK_INITIALIZED)
1243 return;
1244
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001245 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001246
1247 /* Grab a ref if this isn't our static identity */
1248 req->work.identity = tctx->identity;
1249 if (tctx->identity != &tctx->__identity)
1250 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001251}
1252
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001253static inline bool io_async_submit(struct io_ring_ctx *ctx)
1254{
1255 return ctx->flags & IORING_SETUP_SQPOLL;
1256}
1257
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1259{
1260 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1261
Jens Axboe0f158b42020-05-14 17:18:39 -06001262 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263}
1264
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001265static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1266{
1267 return !req->timeout.off;
1268}
1269
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1271{
1272 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001273 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274
1275 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1276 if (!ctx)
1277 return NULL;
1278
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001279 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1280 if (!ctx->fallback_req)
1281 goto err;
1282
Jens Axboe78076bb2019-12-04 19:56:40 -07001283 /*
1284 * Use 5 bits less than the max cq entries, that should give us around
1285 * 32 entries per hash list if totally full and uniformly spread.
1286 */
1287 hash_bits = ilog2(p->cq_entries);
1288 hash_bits -= 5;
1289 if (hash_bits <= 0)
1290 hash_bits = 1;
1291 ctx->cancel_hash_bits = hash_bits;
1292 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1293 GFP_KERNEL);
1294 if (!ctx->cancel_hash)
1295 goto err;
1296 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1297
Roman Gushchin21482892019-05-07 10:01:48 -07001298 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001299 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1300 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001301
1302 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001303 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001304 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001305 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001306 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001307 init_completion(&ctx->ref_comp);
1308 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001309 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001310 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001311 mutex_init(&ctx->uring_lock);
1312 init_waitqueue_head(&ctx->wait);
1313 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001314 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001315 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001316 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001317 spin_lock_init(&ctx->inflight_lock);
1318 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001319 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1320 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001321 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001322err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001323 if (ctx->fallback_req)
1324 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001325 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001326 kfree(ctx);
1327 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328}
1329
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001330static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001331{
Jens Axboe2bc99302020-07-09 09:43:27 -06001332 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1333 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001334
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001335 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001336 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001337 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001338
Bob Liu9d858b22019-11-13 18:06:25 +08001339 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001340}
1341
Jens Axboede0617e2019-04-06 21:51:27 -06001342static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343{
Hristo Venev75b28af2019-08-26 17:23:46 +00001344 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345
Pavel Begunkov07910152020-01-17 03:52:46 +03001346 /* order cqe stores with ring update */
1347 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348
Pavel Begunkov07910152020-01-17 03:52:46 +03001349 if (wq_has_sleeper(&ctx->cq_wait)) {
1350 wake_up_interruptible(&ctx->cq_wait);
1351 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352 }
1353}
1354
Jens Axboe5c3462c2020-10-15 09:02:33 -06001355static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001356{
Jens Axboe500a3732020-10-15 17:38:03 -06001357 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001358 return;
1359 if (refcount_dec_and_test(&req->work.identity->count))
1360 kfree(req->work.identity);
1361}
1362
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001363static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001364{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001365 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001366 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001367
1368 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001369
Jens Axboedfead8a2020-10-14 10:12:37 -06001370 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001371 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001372 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001373 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001374#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001375 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001376 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001377 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001378 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001379#endif
1380 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001381 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001382 req->work.flags &= ~IO_WQ_WORK_CREDS;
1383 }
1384 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001385 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001386
Jens Axboe98447d62020-10-14 10:48:51 -06001387 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001388 if (--fs->users)
1389 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001390 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001391 if (fs)
1392 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001393 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001394 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001395
Jens Axboe5c3462c2020-10-15 09:02:33 -06001396 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001397}
1398
1399/*
1400 * Create a private copy of io_identity, since some fields don't match
1401 * the current context.
1402 */
1403static bool io_identity_cow(struct io_kiocb *req)
1404{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001405 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001406 const struct cred *creds = NULL;
1407 struct io_identity *id;
1408
1409 if (req->work.flags & IO_WQ_WORK_CREDS)
1410 creds = req->work.identity->creds;
1411
1412 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1413 if (unlikely(!id)) {
1414 req->work.flags |= IO_WQ_WORK_CANCEL;
1415 return false;
1416 }
1417
1418 /*
1419 * We can safely just re-init the creds we copied Either the field
1420 * matches the current one, or we haven't grabbed it yet. The only
1421 * exception is ->creds, through registered personalities, so handle
1422 * that one separately.
1423 */
1424 io_init_identity(id);
1425 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001426 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001427
1428 /* add one for this request */
1429 refcount_inc(&id->count);
1430
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001431 /* drop tctx and req identity references, if needed */
1432 if (tctx->identity != &tctx->__identity &&
1433 refcount_dec_and_test(&tctx->identity->count))
1434 kfree(tctx->identity);
1435 if (req->work.identity != &tctx->__identity &&
1436 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001437 kfree(req->work.identity);
1438
1439 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001440 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001441 return true;
1442}
1443
1444static bool io_grab_identity(struct io_kiocb *req)
1445{
1446 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001447 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001448 struct io_ring_ctx *ctx = req->ctx;
1449
Jens Axboe69228332020-10-20 14:28:41 -06001450 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1451 if (id->fsize != rlimit(RLIMIT_FSIZE))
1452 return false;
1453 req->work.flags |= IO_WQ_WORK_FSIZE;
1454 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001455#ifdef CONFIG_BLK_CGROUP
1456 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1457 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1458 rcu_read_lock();
1459 if (id->blkcg_css != blkcg_css()) {
1460 rcu_read_unlock();
1461 return false;
1462 }
1463 /*
1464 * This should be rare, either the cgroup is dying or the task
1465 * is moving cgroups. Just punt to root for the handful of ios.
1466 */
1467 if (css_tryget_online(id->blkcg_css))
1468 req->work.flags |= IO_WQ_WORK_BLKCG;
1469 rcu_read_unlock();
1470 }
1471#endif
1472 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1473 if (id->creds != current_cred())
1474 return false;
1475 get_cred(id->creds);
1476 req->work.flags |= IO_WQ_WORK_CREDS;
1477 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001478#ifdef CONFIG_AUDIT
1479 if (!uid_eq(current->loginuid, id->loginuid) ||
1480 current->sessionid != id->sessionid)
1481 return false;
1482#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001483 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1484 (def->work_flags & IO_WQ_WORK_FS)) {
1485 if (current->fs != id->fs)
1486 return false;
1487 spin_lock(&id->fs->lock);
1488 if (!id->fs->in_exec) {
1489 id->fs->users++;
1490 req->work.flags |= IO_WQ_WORK_FS;
1491 } else {
1492 req->work.flags |= IO_WQ_WORK_CANCEL;
1493 }
1494 spin_unlock(&current->fs->lock);
1495 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001496 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1497 (def->work_flags & IO_WQ_WORK_FILES) &&
1498 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1499 if (id->files != current->files ||
1500 id->nsproxy != current->nsproxy)
1501 return false;
1502 atomic_inc(&id->files->count);
1503 get_nsproxy(id->nsproxy);
1504 req->flags |= REQ_F_INFLIGHT;
1505
1506 spin_lock_irq(&ctx->inflight_lock);
1507 list_add(&req->inflight_entry, &ctx->inflight_list);
1508 spin_unlock_irq(&ctx->inflight_lock);
1509 req->work.flags |= IO_WQ_WORK_FILES;
1510 }
Jens Axboe77788772020-12-29 10:50:46 -07001511 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1512 (def->work_flags & IO_WQ_WORK_MM)) {
1513 if (id->mm != current->mm)
1514 return false;
1515 mmgrab(id->mm);
1516 req->work.flags |= IO_WQ_WORK_MM;
1517 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001518
1519 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001520}
1521
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001522static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001523{
Jens Axboed3656342019-12-18 09:50:26 -07001524 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001525 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5c3462c2020-10-15 09:02:33 -06001526 struct io_identity *id;
Jens Axboe54a91f32019-09-10 09:15:04 -06001527
Pavel Begunkov16d59802020-07-12 16:16:47 +03001528 io_req_init_async(req);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001529 id = req->work.identity;
Pavel Begunkov16d59802020-07-12 16:16:47 +03001530
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001531 if (req->flags & REQ_F_FORCE_ASYNC)
1532 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1533
Jens Axboed3656342019-12-18 09:50:26 -07001534 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001535 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001536 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001537 } else {
1538 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001539 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001540 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001541
Jens Axboe1e6fa522020-10-15 08:46:24 -06001542 /* if we fail grabbing identity, we must COW, regrab, and retry */
1543 if (io_grab_identity(req))
1544 return;
1545
1546 if (!io_identity_cow(req))
1547 return;
1548
1549 /* can't fail at this point */
1550 if (!io_grab_identity(req))
1551 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001552}
1553
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001554static void io_prep_async_link(struct io_kiocb *req)
1555{
1556 struct io_kiocb *cur;
1557
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001558 io_for_each_link(cur, req)
1559 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001560}
1561
Jens Axboe7271ef32020-08-10 09:55:22 -06001562static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001563{
Jackie Liua197f662019-11-08 08:09:12 -07001564 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001565 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001566
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001567 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1568 &req->work, req->flags);
1569 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001570 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001571}
1572
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001573static void io_queue_async_work(struct io_kiocb *req)
1574{
Jens Axboe7271ef32020-08-10 09:55:22 -06001575 struct io_kiocb *link;
1576
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001577 /* init ->work of the whole link before punting */
1578 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001579 link = __io_queue_async_work(req);
1580
1581 if (link)
1582 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001583}
1584
Jens Axboe5262f562019-09-17 12:26:57 -06001585static void io_kill_timeout(struct io_kiocb *req)
1586{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001587 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001588 int ret;
1589
Jens Axboee8c2bc12020-08-15 18:44:09 -07001590 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001591 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001592 atomic_set(&req->ctx->cq_timeouts,
1593 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001594 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001595 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001596 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001597 }
1598}
1599
Jens Axboe76e1b642020-09-26 15:05:03 -06001600/*
1601 * Returns true if we found and killed one or more timeouts
1602 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001603static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1604 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001605{
1606 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001607 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001608
1609 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001610 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001611 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001612 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001613 canceled++;
1614 }
Jens Axboef3606e32020-09-22 08:18:24 -06001615 }
Jens Axboe5262f562019-09-17 12:26:57 -06001616 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001617 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001618}
1619
Pavel Begunkov04518942020-05-26 20:34:05 +03001620static void __io_queue_deferred(struct io_ring_ctx *ctx)
1621{
1622 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001623 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1624 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001625 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001626
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001627 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001628 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001629 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001630 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001631 link = __io_queue_async_work(de->req);
1632 if (link) {
1633 __io_queue_linked_timeout(link);
1634 /* drop submission reference */
Pavel Begunkov216578e2020-10-13 09:44:00 +01001635 io_put_req_deferred(link, 1);
Jens Axboe7271ef32020-08-10 09:55:22 -06001636 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001637 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001638 } while (!list_empty(&ctx->defer_list));
1639}
1640
Pavel Begunkov360428f2020-05-30 14:54:17 +03001641static void io_flush_timeouts(struct io_ring_ctx *ctx)
1642{
1643 while (!list_empty(&ctx->timeout_list)) {
1644 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001645 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001646
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001647 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001648 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001649 if (req->timeout.target_seq != ctx->cached_cq_tail
1650 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001651 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001652
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001653 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001654 io_kill_timeout(req);
1655 }
1656}
1657
Jens Axboede0617e2019-04-06 21:51:27 -06001658static void io_commit_cqring(struct io_ring_ctx *ctx)
1659{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001660 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001661 __io_commit_cqring(ctx);
1662
Pavel Begunkov04518942020-05-26 20:34:05 +03001663 if (unlikely(!list_empty(&ctx->defer_list)))
1664 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001665}
1666
Jens Axboe90554202020-09-03 12:12:41 -06001667static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1668{
1669 struct io_rings *r = ctx->rings;
1670
1671 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1672}
1673
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1675{
Hristo Venev75b28af2019-08-26 17:23:46 +00001676 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677 unsigned tail;
1678
1679 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001680 /*
1681 * writes to the cq entry need to come after reading head; the
1682 * control dependency is enough as we're using WRITE_ONCE to
1683 * fill the cq entry
1684 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001685 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686 return NULL;
1687
1688 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001689 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690}
1691
Jens Axboef2842ab2020-01-08 11:04:00 -07001692static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1693{
Jens Axboef0b493e2020-02-01 21:30:11 -07001694 if (!ctx->cq_ev_fd)
1695 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001696 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1697 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001698 if (!ctx->eventfd_async)
1699 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001700 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001701}
1702
Pavel Begunkove23de152020-12-17 00:24:37 +00001703static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx)
1704{
1705 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1706}
1707
Jens Axboeb41e9852020-02-17 09:52:41 -07001708static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001709{
1710 if (waitqueue_active(&ctx->wait))
1711 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001712 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1713 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001714 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001715 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001716}
1717
Jens Axboec4a2ed72019-11-21 21:01:26 -07001718/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001719static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1720 struct task_struct *tsk,
1721 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001723 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001724 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001725 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001726 unsigned long flags;
Pavel Begunkov09e88402020-12-17 00:24:38 +00001727 bool all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001728 LIST_HEAD(list);
1729
Pavel Begunkove23de152020-12-17 00:24:37 +00001730 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1731 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001732
1733 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001734 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001735 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001736 continue;
1737
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001738 cqe = io_get_cqring(ctx);
1739 if (!cqe && !force)
1740 break;
1741
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001742 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001743 if (cqe) {
1744 WRITE_ONCE(cqe->user_data, req->user_data);
1745 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001746 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001747 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001748 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001749 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001750 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001751 }
1752 }
1753
Pavel Begunkov09e88402020-12-17 00:24:38 +00001754 all_flushed = list_empty(&ctx->cq_overflow_list);
1755 if (all_flushed) {
1756 clear_bit(0, &ctx->sq_check_overflow);
1757 clear_bit(0, &ctx->cq_check_overflow);
1758 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1759 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001760
Pavel Begunkov09e88402020-12-17 00:24:38 +00001761 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001762 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1763 io_cqring_ev_posted(ctx);
1764
1765 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001766 req = list_first_entry(&list, struct io_kiocb, compl.list);
1767 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001768 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001769 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001770
Pavel Begunkov09e88402020-12-17 00:24:38 +00001771 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001772}
1773
Pavel Begunkov6c503152021-01-04 20:36:36 +00001774static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1775 struct task_struct *tsk,
1776 struct files_struct *files)
1777{
1778 if (test_bit(0, &ctx->cq_check_overflow)) {
1779 /* iopoll syncs against uring_lock, not completion_lock */
1780 if (ctx->flags & IORING_SETUP_IOPOLL)
1781 mutex_lock(&ctx->uring_lock);
1782 __io_cqring_overflow_flush(ctx, force, tsk, files);
1783 if (ctx->flags & IORING_SETUP_IOPOLL)
1784 mutex_unlock(&ctx->uring_lock);
1785 }
1786}
1787
Jens Axboebcda7ba2020-02-23 16:42:51 -07001788static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001790 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791 struct io_uring_cqe *cqe;
1792
Jens Axboe78e19bb2019-11-06 15:21:34 -07001793 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001794
Jens Axboe2b188cc2019-01-07 10:46:33 -07001795 /*
1796 * If we can't get a cq entry, userspace overflowed the
1797 * submission (by quite a lot). Increment the overflow count in
1798 * the ring.
1799 */
1800 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001801 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001802 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001804 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001805 } else if (ctx->cq_overflow_flushed ||
1806 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001807 /*
1808 * If we're in ring overflow flush mode, or in task cancel mode,
1809 * then we cannot store the request for later flushing, we need
1810 * to drop it on the floor.
1811 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001812 ctx->cached_cq_overflow++;
1813 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001814 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001815 if (list_empty(&ctx->cq_overflow_list)) {
1816 set_bit(0, &ctx->sq_check_overflow);
1817 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001818 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001819 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001820 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001821 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001822 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001823 refcount_inc(&req->refs);
1824 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825 }
1826}
1827
Jens Axboebcda7ba2020-02-23 16:42:51 -07001828static void io_cqring_fill_event(struct io_kiocb *req, long res)
1829{
1830 __io_cqring_fill_event(req, res, 0);
1831}
1832
Jens Axboee1e16092020-06-22 09:17:17 -06001833static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001834{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001835 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001836 unsigned long flags;
1837
1838 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001839 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001840 io_commit_cqring(ctx);
1841 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1842
Jens Axboe8c838782019-03-12 15:48:16 -06001843 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844}
1845
Jens Axboe229a7b62020-06-22 10:13:11 -06001846static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001847{
Jens Axboe229a7b62020-06-22 10:13:11 -06001848 struct io_ring_ctx *ctx = cs->ctx;
1849
1850 spin_lock_irq(&ctx->completion_lock);
1851 while (!list_empty(&cs->list)) {
1852 struct io_kiocb *req;
1853
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001854 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1855 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001856 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001857
1858 /*
1859 * io_free_req() doesn't care about completion_lock unless one
1860 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1861 * because of a potential deadlock with req->work.fs->lock
1862 */
1863 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1864 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001865 spin_unlock_irq(&ctx->completion_lock);
1866 io_put_req(req);
1867 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001868 } else {
1869 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001870 }
1871 }
1872 io_commit_cqring(ctx);
1873 spin_unlock_irq(&ctx->completion_lock);
1874
1875 io_cqring_ev_posted(ctx);
1876 cs->nr = 0;
1877}
1878
1879static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1880 struct io_comp_state *cs)
1881{
1882 if (!cs) {
1883 io_cqring_add_event(req, res, cflags);
1884 io_put_req(req);
1885 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001886 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001887 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001888 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001889 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001890 if (++cs->nr >= 32)
1891 io_submit_flush_completions(cs);
1892 }
Jens Axboee1e16092020-06-22 09:17:17 -06001893}
1894
1895static void io_req_complete(struct io_kiocb *req, long res)
1896{
Jens Axboe229a7b62020-06-22 10:13:11 -06001897 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001898}
1899
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001900static inline bool io_is_fallback_req(struct io_kiocb *req)
1901{
1902 return req == (struct io_kiocb *)
1903 ((unsigned long) req->ctx->fallback_req & ~1UL);
1904}
1905
1906static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1907{
1908 struct io_kiocb *req;
1909
1910 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001911 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001912 return req;
1913
1914 return NULL;
1915}
1916
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001917static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1918 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001920 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001921 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001922 size_t sz;
1923 int ret;
1924
1925 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001926 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1927
1928 /*
1929 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1930 * retry single alloc to be on the safe side.
1931 */
1932 if (unlikely(ret <= 0)) {
1933 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1934 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001935 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001936 ret = 1;
1937 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001938 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001939 }
1940
Pavel Begunkov291b2822020-09-30 22:57:01 +03001941 state->free_reqs--;
1942 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001943fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001944 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001945}
1946
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001947static inline void io_put_file(struct io_kiocb *req, struct file *file,
1948 bool fixed)
1949{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001950 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001951 fput(file);
1952}
1953
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001954static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001955{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001956 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001957
Jens Axboee8c2bc12020-08-15 18:44:09 -07001958 if (req->async_data)
1959 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001960 if (req->file)
1961 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001962 if (req->fixed_file_refs)
1963 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001964 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001965}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001966
Pavel Begunkov216578e2020-10-13 09:44:00 +01001967static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001968{
Jens Axboe0f212202020-09-13 13:09:39 -06001969 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001970 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001971
Pavel Begunkov216578e2020-10-13 09:44:00 +01001972 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001973
Jens Axboed8a6df12020-10-15 16:24:45 -06001974 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06001975 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06001976 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001977 put_task_struct(req->task);
1978
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001979 if (likely(!io_is_fallback_req(req)))
1980 kmem_cache_free(req_cachep, req);
1981 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001982 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1983 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001984}
1985
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001986static inline void io_remove_next_linked(struct io_kiocb *req)
1987{
1988 struct io_kiocb *nxt = req->link;
1989
1990 req->link = nxt->link;
1991 nxt->link = NULL;
1992}
1993
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001994static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001995{
Jackie Liua197f662019-11-08 08:09:12 -07001996 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001997 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001998 bool cancelled = false;
1999 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002000
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002001 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002002 link = req->link;
2003
Pavel Begunkov900fad42020-10-19 16:39:16 +01002004 /*
2005 * Can happen if a linked timeout fired and link had been like
2006 * req -> link t-out -> link t-out [-> ...]
2007 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002008 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2009 struct io_timeout_data *io = link->async_data;
2010 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002011
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002012 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002013 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002014 ret = hrtimer_try_to_cancel(&io->timer);
2015 if (ret != -1) {
2016 io_cqring_fill_event(link, -ECANCELED);
2017 io_commit_cqring(ctx);
2018 cancelled = true;
2019 }
2020 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002021 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002022 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002023
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002024 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002025 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002026 io_put_req(link);
2027 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002028}
2029
Jens Axboe4d7dd462019-11-20 13:03:52 -07002030
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002031static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002032{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002033 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002034 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002035 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002036
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002037 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002038 link = req->link;
2039 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002040
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002041 while (link) {
2042 nxt = link->link;
2043 link->link = NULL;
2044
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002045 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002046 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002047
2048 /*
2049 * It's ok to free under spinlock as they're not linked anymore,
2050 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2051 * work.fs->lock.
2052 */
2053 if (link->flags & REQ_F_WORK_INITIALIZED)
2054 io_put_req_deferred(link, 2);
2055 else
2056 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002057 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002058 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002059 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002060 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002061
Jens Axboe2665abf2019-11-05 12:40:47 -07002062 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002063}
2064
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002065static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002066{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002067 if (req->flags & REQ_F_LINK_TIMEOUT)
2068 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002069
Jens Axboe9e645e112019-05-10 16:07:28 -06002070 /*
2071 * If LINK is set, we have dependent requests in this chain. If we
2072 * didn't fail this request, queue the first one up, moving any other
2073 * dependencies to the next request. In case of failure, fail the rest
2074 * of the chain.
2075 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002076 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2077 struct io_kiocb *nxt = req->link;
2078
2079 req->link = NULL;
2080 return nxt;
2081 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002082 io_fail_links(req);
2083 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002084}
Jens Axboe2665abf2019-11-05 12:40:47 -07002085
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002086static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002087{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002088 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002089 return NULL;
2090 return __io_req_find_next(req);
2091}
2092
Jens Axboe355fb9e2020-10-22 20:19:35 -06002093static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002094{
2095 struct task_struct *tsk = req->task;
2096 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002097 enum task_work_notify_mode notify;
2098 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002099
Jens Axboe6200b0a2020-09-13 14:38:30 -06002100 if (tsk->flags & PF_EXITING)
2101 return -ESRCH;
2102
Jens Axboec2c4c832020-07-01 15:37:11 -06002103 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002104 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2105 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2106 * processing task_work. There's no reliable way to tell if TWA_RESUME
2107 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002108 */
Jens Axboe91989c72020-10-16 09:02:26 -06002109 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002110 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002111 notify = TWA_SIGNAL;
2112
Jens Axboe87c43112020-09-30 21:00:14 -06002113 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002114 if (!ret)
2115 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002116
Jens Axboec2c4c832020-07-01 15:37:11 -06002117 return ret;
2118}
2119
Jens Axboec40f6372020-06-25 15:39:59 -06002120static void __io_req_task_cancel(struct io_kiocb *req, int error)
2121{
2122 struct io_ring_ctx *ctx = req->ctx;
2123
2124 spin_lock_irq(&ctx->completion_lock);
2125 io_cqring_fill_event(req, error);
2126 io_commit_cqring(ctx);
2127 spin_unlock_irq(&ctx->completion_lock);
2128
2129 io_cqring_ev_posted(ctx);
2130 req_set_fail_links(req);
2131 io_double_put_req(req);
2132}
2133
2134static void io_req_task_cancel(struct callback_head *cb)
2135{
2136 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002137 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002138
2139 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002140 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002141}
2142
2143static void __io_req_task_submit(struct io_kiocb *req)
2144{
2145 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002146 bool fail;
Jens Axboec40f6372020-06-25 15:39:59 -06002147
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002148 fail = __io_sq_thread_acquire_mm(ctx) ||
2149 __io_sq_thread_acquire_files(ctx);
2150 mutex_lock(&ctx->uring_lock);
2151 if (!fail)
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002152 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002153 else
Jens Axboec40f6372020-06-25 15:39:59 -06002154 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002155 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002156}
2157
Jens Axboec40f6372020-06-25 15:39:59 -06002158static void io_req_task_submit(struct callback_head *cb)
2159{
2160 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002161 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002162
2163 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002164 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002165}
2166
2167static void io_req_task_queue(struct io_kiocb *req)
2168{
Jens Axboec40f6372020-06-25 15:39:59 -06002169 int ret;
2170
2171 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002172 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002173
Jens Axboe355fb9e2020-10-22 20:19:35 -06002174 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002175 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002176 struct task_struct *tsk;
2177
Jens Axboec40f6372020-06-25 15:39:59 -06002178 init_task_work(&req->task_work, io_req_task_cancel);
2179 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002180 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002181 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002182 }
Jens Axboec40f6372020-06-25 15:39:59 -06002183}
2184
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002185static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002186{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002187 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002188
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002189 if (nxt)
2190 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002191}
2192
Jens Axboe9e645e112019-05-10 16:07:28 -06002193static void io_free_req(struct io_kiocb *req)
2194{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002195 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002196 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002197}
2198
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002199struct req_batch {
2200 void *reqs[IO_IOPOLL_BATCH];
2201 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002202
2203 struct task_struct *task;
2204 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002205};
2206
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002207static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002208{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002209 rb->to_free = 0;
2210 rb->task_refs = 0;
2211 rb->task = NULL;
2212}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002213
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002214static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2215 struct req_batch *rb)
2216{
2217 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2218 percpu_ref_put_many(&ctx->refs, rb->to_free);
2219 rb->to_free = 0;
2220}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002221
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002222static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2223 struct req_batch *rb)
2224{
2225 if (rb->to_free)
2226 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002227 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002228 struct io_uring_task *tctx = rb->task->io_uring;
2229
2230 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002231 put_task_struct_many(rb->task, rb->task_refs);
2232 rb->task = NULL;
2233 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002234}
2235
2236static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2237{
2238 if (unlikely(io_is_fallback_req(req))) {
2239 io_free_req(req);
2240 return;
2241 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002242 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002243
Jens Axboee3bc8e92020-09-24 08:45:57 -06002244 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002245 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002246 struct io_uring_task *tctx = rb->task->io_uring;
2247
2248 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002249 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002250 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002251 rb->task = req->task;
2252 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002253 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002254 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002255
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002256 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002257 rb->reqs[rb->to_free++] = req;
2258 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2259 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002260}
2261
Jens Axboeba816ad2019-09-28 11:36:45 -06002262/*
2263 * Drop reference to request, return next in chain (if there is one) if this
2264 * was the last reference to this request.
2265 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002266static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002267{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002268 struct io_kiocb *nxt = NULL;
2269
Jens Axboe2a44f462020-02-25 13:25:41 -07002270 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002271 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002272 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002273 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002274 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275}
2276
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277static void io_put_req(struct io_kiocb *req)
2278{
Jens Axboedef596e2019-01-09 08:59:42 -07002279 if (refcount_dec_and_test(&req->refs))
2280 io_free_req(req);
2281}
2282
Pavel Begunkov216578e2020-10-13 09:44:00 +01002283static void io_put_req_deferred_cb(struct callback_head *cb)
2284{
2285 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2286
2287 io_free_req(req);
2288}
2289
2290static void io_free_req_deferred(struct io_kiocb *req)
2291{
2292 int ret;
2293
2294 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002295 ret = io_req_task_work_add(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002296 if (unlikely(ret)) {
2297 struct task_struct *tsk;
2298
2299 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002300 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002301 wake_up_process(tsk);
2302 }
2303}
2304
2305static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2306{
2307 if (refcount_sub_and_test(refs, &req->refs))
2308 io_free_req_deferred(req);
2309}
2310
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002311static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002312{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002313 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002314
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002315 /*
2316 * A ref is owned by io-wq in which context we're. So, if that's the
2317 * last one, it's safe to steal next work. False negatives are Ok,
2318 * it just will be re-punted async in io_put_work()
2319 */
2320 if (refcount_read(&req->refs) != 1)
2321 return NULL;
2322
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002323 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002324 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002325}
2326
Jens Axboe978db572019-11-14 22:39:04 -07002327static void io_double_put_req(struct io_kiocb *req)
2328{
2329 /* drop both submit and complete references */
2330 if (refcount_sub_and_test(2, &req->refs))
2331 io_free_req(req);
2332}
2333
Pavel Begunkov6c503152021-01-04 20:36:36 +00002334static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002335{
2336 /* See comment at the top of this file */
2337 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002338 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002339}
2340
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002341static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2342{
2343 struct io_rings *rings = ctx->rings;
2344
2345 /* make sure SQ entry isn't read before tail */
2346 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2347}
2348
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002349static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002350{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002351 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002352
Jens Axboebcda7ba2020-02-23 16:42:51 -07002353 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2354 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002355 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002356 kfree(kbuf);
2357 return cflags;
2358}
2359
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002360static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2361{
2362 struct io_buffer *kbuf;
2363
2364 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2365 return io_put_kbuf(req, kbuf);
2366}
2367
Jens Axboe4c6e2772020-07-01 11:29:10 -06002368static inline bool io_run_task_work(void)
2369{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002370 /*
2371 * Not safe to run on exiting task, and the task_work handling will
2372 * not add work to such a task.
2373 */
2374 if (unlikely(current->flags & PF_EXITING))
2375 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002376 if (current->task_works) {
2377 __set_current_state(TASK_RUNNING);
2378 task_work_run();
2379 return true;
2380 }
2381
2382 return false;
2383}
2384
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002385static void io_iopoll_queue(struct list_head *again)
2386{
2387 struct io_kiocb *req;
2388
2389 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002390 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2391 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002392 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002393 } while (!list_empty(again));
2394}
2395
Jens Axboedef596e2019-01-09 08:59:42 -07002396/*
2397 * Find and free completed poll iocbs
2398 */
2399static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2400 struct list_head *done)
2401{
Jens Axboe8237e042019-12-28 10:48:22 -07002402 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002403 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002404 LIST_HEAD(again);
2405
2406 /* order with ->result store in io_complete_rw_iopoll() */
2407 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002408
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002409 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002410 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002411 int cflags = 0;
2412
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002413 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002414 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002415 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002416 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002417 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002418 continue;
2419 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002420 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002421
Jens Axboebcda7ba2020-02-23 16:42:51 -07002422 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002423 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002424
2425 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002426 (*nr_events)++;
2427
Pavel Begunkovc3524382020-06-28 12:52:32 +03002428 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002429 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002430 }
Jens Axboedef596e2019-01-09 08:59:42 -07002431
Jens Axboe09bb8392019-03-13 12:39:28 -06002432 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002433 if (ctx->flags & IORING_SETUP_SQPOLL)
2434 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002435 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002436
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002437 if (!list_empty(&again))
2438 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002439}
2440
Jens Axboedef596e2019-01-09 08:59:42 -07002441static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2442 long min)
2443{
2444 struct io_kiocb *req, *tmp;
2445 LIST_HEAD(done);
2446 bool spin;
2447 int ret;
2448
2449 /*
2450 * Only spin for completions if we don't have multiple devices hanging
2451 * off our complete list, and we're under the requested amount.
2452 */
2453 spin = !ctx->poll_multi_file && *nr_events < min;
2454
2455 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002456 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002457 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002458
2459 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002460 * Move completed and retryable entries to our local lists.
2461 * If we find a request that requires polling, break out
2462 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002463 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002464 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002465 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002466 continue;
2467 }
2468 if (!list_empty(&done))
2469 break;
2470
2471 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2472 if (ret < 0)
2473 break;
2474
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002475 /* iopoll may have completed current req */
2476 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002477 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002478
Jens Axboedef596e2019-01-09 08:59:42 -07002479 if (ret && spin)
2480 spin = false;
2481 ret = 0;
2482 }
2483
2484 if (!list_empty(&done))
2485 io_iopoll_complete(ctx, nr_events, &done);
2486
2487 return ret;
2488}
2489
2490/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002491 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002492 * non-spinning poll check - we'll still enter the driver poll loop, but only
2493 * as a non-spinning completion check.
2494 */
2495static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2496 long min)
2497{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002498 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002499 int ret;
2500
2501 ret = io_do_iopoll(ctx, nr_events, min);
2502 if (ret < 0)
2503 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002504 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002505 return 0;
2506 }
2507
2508 return 1;
2509}
2510
2511/*
2512 * We can't just wait for polled events to come to us, we have to actively
2513 * find and complete them.
2514 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002515static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002516{
2517 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2518 return;
2519
2520 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002521 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002522 unsigned int nr_events = 0;
2523
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002524 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002525
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002526 /* let it sleep and repeat later if can't complete a request */
2527 if (nr_events == 0)
2528 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002529 /*
2530 * Ensure we allow local-to-the-cpu processing to take place,
2531 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002532 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002533 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002534 if (need_resched()) {
2535 mutex_unlock(&ctx->uring_lock);
2536 cond_resched();
2537 mutex_lock(&ctx->uring_lock);
2538 }
Jens Axboedef596e2019-01-09 08:59:42 -07002539 }
2540 mutex_unlock(&ctx->uring_lock);
2541}
2542
Pavel Begunkov7668b922020-07-07 16:36:21 +03002543static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002544{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002545 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002546 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002547
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002548 /*
2549 * We disallow the app entering submit/complete with polling, but we
2550 * still need to lock the ring to prevent racing with polled issue
2551 * that got punted to a workqueue.
2552 */
2553 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002554 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002555 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002556 * Don't enter poll loop if we already have events pending.
2557 * If we do, we can potentially be spinning for commands that
2558 * already triggered a CQE (eg in error).
2559 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002560 if (test_bit(0, &ctx->cq_check_overflow))
2561 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2562 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002563 break;
2564
2565 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002566 * If a submit got punted to a workqueue, we can have the
2567 * application entering polling for a command before it gets
2568 * issued. That app will hold the uring_lock for the duration
2569 * of the poll right here, so we need to take a breather every
2570 * now and then to ensure that the issue has a chance to add
2571 * the poll to the issued list. Otherwise we can spin here
2572 * forever, while the workqueue is stuck trying to acquire the
2573 * very same mutex.
2574 */
2575 if (!(++iters & 7)) {
2576 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002577 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002578 mutex_lock(&ctx->uring_lock);
2579 }
2580
Pavel Begunkov7668b922020-07-07 16:36:21 +03002581 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002582 if (ret <= 0)
2583 break;
2584 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002585 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002586
Jens Axboe500f9fb2019-08-19 12:15:59 -06002587 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002588 return ret;
2589}
2590
Jens Axboe491381ce2019-10-17 09:20:46 -06002591static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002592{
Jens Axboe491381ce2019-10-17 09:20:46 -06002593 /*
2594 * Tell lockdep we inherited freeze protection from submission
2595 * thread.
2596 */
2597 if (req->flags & REQ_F_ISREG) {
2598 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002599
Jens Axboe491381ce2019-10-17 09:20:46 -06002600 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002601 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002602 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603}
2604
Jens Axboea1d7c392020-06-22 11:09:46 -06002605static void io_complete_rw_common(struct kiocb *kiocb, long res,
2606 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002607{
Jens Axboe9adbd452019-12-20 08:45:55 -07002608 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002609 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002610
Jens Axboe491381ce2019-10-17 09:20:46 -06002611 if (kiocb->ki_flags & IOCB_WRITE)
2612 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002614 if (res != req->result)
2615 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002616 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002617 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002618 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002619}
2620
Jens Axboeb63534c2020-06-04 11:28:00 -06002621#ifdef CONFIG_BLOCK
2622static bool io_resubmit_prep(struct io_kiocb *req, int error)
2623{
2624 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2625 ssize_t ret = -ECANCELED;
2626 struct iov_iter iter;
2627 int rw;
2628
2629 if (error) {
2630 ret = error;
2631 goto end_req;
2632 }
2633
2634 switch (req->opcode) {
2635 case IORING_OP_READV:
2636 case IORING_OP_READ_FIXED:
2637 case IORING_OP_READ:
2638 rw = READ;
2639 break;
2640 case IORING_OP_WRITEV:
2641 case IORING_OP_WRITE_FIXED:
2642 case IORING_OP_WRITE:
2643 rw = WRITE;
2644 break;
2645 default:
2646 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2647 req->opcode);
2648 goto end_req;
2649 }
2650
Jens Axboee8c2bc12020-08-15 18:44:09 -07002651 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002652 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2653 if (ret < 0)
2654 goto end_req;
2655 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2656 if (!ret)
2657 return true;
2658 kfree(iovec);
2659 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002660 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002661 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002662end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002663 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002664 return false;
2665}
Jens Axboeb63534c2020-06-04 11:28:00 -06002666#endif
2667
2668static bool io_rw_reissue(struct io_kiocb *req, long res)
2669{
2670#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002671 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002672 int ret;
2673
Jens Axboe355afae2020-09-02 09:30:31 -06002674 if (!S_ISBLK(mode) && !S_ISREG(mode))
2675 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002676 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2677 return false;
2678
Jens Axboe28cea78a2020-09-14 10:51:17 -06002679 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002680
Jens Axboefdee9462020-08-27 16:46:24 -06002681 if (io_resubmit_prep(req, ret)) {
2682 refcount_inc(&req->refs);
2683 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002684 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002685 }
2686
Jens Axboeb63534c2020-06-04 11:28:00 -06002687#endif
2688 return false;
2689}
2690
Jens Axboea1d7c392020-06-22 11:09:46 -06002691static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2692 struct io_comp_state *cs)
2693{
2694 if (!io_rw_reissue(req, res))
2695 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002696}
2697
2698static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2699{
Jens Axboe9adbd452019-12-20 08:45:55 -07002700 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002701
Jens Axboea1d7c392020-06-22 11:09:46 -06002702 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002703}
2704
Jens Axboedef596e2019-01-09 08:59:42 -07002705static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2706{
Jens Axboe9adbd452019-12-20 08:45:55 -07002707 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002708
Jens Axboe491381ce2019-10-17 09:20:46 -06002709 if (kiocb->ki_flags & IOCB_WRITE)
2710 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002711
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002712 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002713 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002714
2715 WRITE_ONCE(req->result, res);
2716 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002717 smp_wmb();
2718 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002719}
2720
2721/*
2722 * After the iocb has been issued, it's safe to be found on the poll list.
2723 * Adding the kiocb to the list AFTER submission ensures that we don't
2724 * find it from a io_iopoll_getevents() thread before the issuer is done
2725 * accessing the kiocb cookie.
2726 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002727static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002728{
2729 struct io_ring_ctx *ctx = req->ctx;
2730
2731 /*
2732 * Track whether we have multiple files in our lists. This will impact
2733 * how we do polling eventually, not spinning if we're on potentially
2734 * different devices.
2735 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002736 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002737 ctx->poll_multi_file = false;
2738 } else if (!ctx->poll_multi_file) {
2739 struct io_kiocb *list_req;
2740
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002741 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002742 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002743 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002744 ctx->poll_multi_file = true;
2745 }
2746
2747 /*
2748 * For fast devices, IO may have already completed. If it has, add
2749 * it to the front so we find it first.
2750 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002751 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002752 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002753 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002754 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002755
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002756 /*
2757 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2758 * task context or in io worker task context. If current task context is
2759 * sq thread, we don't need to check whether should wake up sq thread.
2760 */
2761 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002762 wq_has_sleeper(&ctx->sq_data->wait))
2763 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002764}
2765
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002766static inline void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002767{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002768 fput_many(state->file, state->file_refs);
2769 state->file_refs = 0;
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002770}
2771
2772static inline void io_state_file_put(struct io_submit_state *state)
2773{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002774 if (state->file_refs)
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002775 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002776}
2777
2778/*
2779 * Get as many references to a file as we have IOs left in this submission,
2780 * assuming most submissions are for one file, or at least that each file
2781 * has more than one submission.
2782 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002783static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002784{
2785 if (!state)
2786 return fget(fd);
2787
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002788 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002789 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002790 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002791 return state->file;
2792 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002793 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002794 }
2795 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002796 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002797 return NULL;
2798
2799 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002800 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002801 return state->file;
2802}
2803
Jens Axboe4503b762020-06-01 10:00:27 -06002804static bool io_bdev_nowait(struct block_device *bdev)
2805{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002806 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002807}
2808
Jens Axboe2b188cc2019-01-07 10:46:33 -07002809/*
2810 * If we tracked the file through the SCM inflight mechanism, we could support
2811 * any file. For now, just ensure that anything potentially problematic is done
2812 * inline.
2813 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002814static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815{
2816 umode_t mode = file_inode(file)->i_mode;
2817
Jens Axboe4503b762020-06-01 10:00:27 -06002818 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002819 if (IS_ENABLED(CONFIG_BLOCK) &&
2820 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002821 return true;
2822 return false;
2823 }
2824 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002825 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002826 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002827 if (IS_ENABLED(CONFIG_BLOCK) &&
2828 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002829 file->f_op != &io_uring_fops)
2830 return true;
2831 return false;
2832 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002833
Jens Axboec5b85622020-06-09 19:23:05 -06002834 /* any ->read/write should understand O_NONBLOCK */
2835 if (file->f_flags & O_NONBLOCK)
2836 return true;
2837
Jens Axboeaf197f52020-04-28 13:15:06 -06002838 if (!(file->f_mode & FMODE_NOWAIT))
2839 return false;
2840
2841 if (rw == READ)
2842 return file->f_op->read_iter != NULL;
2843
2844 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002845}
2846
Pavel Begunkova88fc402020-09-30 22:57:53 +03002847static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848{
Jens Axboedef596e2019-01-09 08:59:42 -07002849 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002850 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002851 unsigned ioprio;
2852 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853
Jens Axboe491381ce2019-10-17 09:20:46 -06002854 if (S_ISREG(file_inode(req->file)->i_mode))
2855 req->flags |= REQ_F_ISREG;
2856
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002858 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2859 req->flags |= REQ_F_CUR_POS;
2860 kiocb->ki_pos = req->file->f_pos;
2861 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002863 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2864 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2865 if (unlikely(ret))
2866 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867
2868 ioprio = READ_ONCE(sqe->ioprio);
2869 if (ioprio) {
2870 ret = ioprio_check_cap(ioprio);
2871 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002872 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002873
2874 kiocb->ki_ioprio = ioprio;
2875 } else
2876 kiocb->ki_ioprio = get_current_ioprio();
2877
Stefan Bühler8449eed2019-04-27 20:34:19 +02002878 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002879 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002880 req->flags |= REQ_F_NOWAIT;
2881
Jens Axboedef596e2019-01-09 08:59:42 -07002882 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002883 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2884 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002885 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886
Jens Axboedef596e2019-01-09 08:59:42 -07002887 kiocb->ki_flags |= IOCB_HIPRI;
2888 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002889 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002890 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002891 if (kiocb->ki_flags & IOCB_HIPRI)
2892 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002893 kiocb->ki_complete = io_complete_rw;
2894 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002895
Jens Axboe3529d8c2019-12-19 18:24:38 -07002896 req->rw.addr = READ_ONCE(sqe->addr);
2897 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002898 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002899 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900}
2901
2902static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2903{
2904 switch (ret) {
2905 case -EIOCBQUEUED:
2906 break;
2907 case -ERESTARTSYS:
2908 case -ERESTARTNOINTR:
2909 case -ERESTARTNOHAND:
2910 case -ERESTART_RESTARTBLOCK:
2911 /*
2912 * We can't just restart the syscall, since previously
2913 * submitted sqes may already be in progress. Just fail this
2914 * IO with EINTR.
2915 */
2916 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002917 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918 default:
2919 kiocb->ki_complete(kiocb, ret, 0);
2920 }
2921}
2922
Jens Axboea1d7c392020-06-22 11:09:46 -06002923static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2924 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002925{
Jens Axboeba042912019-12-25 16:33:42 -07002926 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002927 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002928
Jens Axboe227c0c92020-08-13 11:51:40 -06002929 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002930 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002931 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002932 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002933 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002934 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002935 }
2936
Jens Axboeba042912019-12-25 16:33:42 -07002937 if (req->flags & REQ_F_CUR_POS)
2938 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002939 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002940 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002941 else
2942 io_rw_done(kiocb, ret);
2943}
2944
Jens Axboe9adbd452019-12-20 08:45:55 -07002945static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002946 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002947{
Jens Axboe9adbd452019-12-20 08:45:55 -07002948 struct io_ring_ctx *ctx = req->ctx;
2949 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002950 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002951 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002952 size_t offset;
2953 u64 buf_addr;
2954
Jens Axboeedafcce2019-01-09 09:16:05 -07002955 if (unlikely(buf_index >= ctx->nr_user_bufs))
2956 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002957 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2958 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002959 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002960
2961 /* overflow */
2962 if (buf_addr + len < buf_addr)
2963 return -EFAULT;
2964 /* not inside the mapped region */
2965 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2966 return -EFAULT;
2967
2968 /*
2969 * May not be a start of buffer, set size appropriately
2970 * and advance us to the beginning.
2971 */
2972 offset = buf_addr - imu->ubuf;
2973 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002974
2975 if (offset) {
2976 /*
2977 * Don't use iov_iter_advance() here, as it's really slow for
2978 * using the latter parts of a big fixed buffer - it iterates
2979 * over each segment manually. We can cheat a bit here, because
2980 * we know that:
2981 *
2982 * 1) it's a BVEC iter, we set it up
2983 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2984 * first and last bvec
2985 *
2986 * So just find our index, and adjust the iterator afterwards.
2987 * If the offset is within the first bvec (or the whole first
2988 * bvec, just use iov_iter_advance(). This makes it easier
2989 * since we can just skip the first segment, which may not
2990 * be PAGE_SIZE aligned.
2991 */
2992 const struct bio_vec *bvec = imu->bvec;
2993
2994 if (offset <= bvec->bv_len) {
2995 iov_iter_advance(iter, offset);
2996 } else {
2997 unsigned long seg_skip;
2998
2999 /* skip first vec */
3000 offset -= bvec->bv_len;
3001 seg_skip = 1 + (offset >> PAGE_SHIFT);
3002
3003 iter->bvec = bvec + seg_skip;
3004 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003005 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003006 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003007 }
3008 }
3009
Jens Axboe5e559562019-11-13 16:12:46 -07003010 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003011}
3012
Jens Axboebcda7ba2020-02-23 16:42:51 -07003013static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3014{
3015 if (needs_lock)
3016 mutex_unlock(&ctx->uring_lock);
3017}
3018
3019static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3020{
3021 /*
3022 * "Normal" inline submissions always hold the uring_lock, since we
3023 * grab it from the system call. Same is true for the SQPOLL offload.
3024 * The only exception is when we've detached the request and issue it
3025 * from an async worker thread, grab the lock for that case.
3026 */
3027 if (needs_lock)
3028 mutex_lock(&ctx->uring_lock);
3029}
3030
3031static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3032 int bgid, struct io_buffer *kbuf,
3033 bool needs_lock)
3034{
3035 struct io_buffer *head;
3036
3037 if (req->flags & REQ_F_BUFFER_SELECTED)
3038 return kbuf;
3039
3040 io_ring_submit_lock(req->ctx, needs_lock);
3041
3042 lockdep_assert_held(&req->ctx->uring_lock);
3043
3044 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3045 if (head) {
3046 if (!list_empty(&head->list)) {
3047 kbuf = list_last_entry(&head->list, struct io_buffer,
3048 list);
3049 list_del(&kbuf->list);
3050 } else {
3051 kbuf = head;
3052 idr_remove(&req->ctx->io_buffer_idr, bgid);
3053 }
3054 if (*len > kbuf->len)
3055 *len = kbuf->len;
3056 } else {
3057 kbuf = ERR_PTR(-ENOBUFS);
3058 }
3059
3060 io_ring_submit_unlock(req->ctx, needs_lock);
3061
3062 return kbuf;
3063}
3064
Jens Axboe4d954c22020-02-27 07:31:19 -07003065static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3066 bool needs_lock)
3067{
3068 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003069 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003070
3071 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003072 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003073 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3074 if (IS_ERR(kbuf))
3075 return kbuf;
3076 req->rw.addr = (u64) (unsigned long) kbuf;
3077 req->flags |= REQ_F_BUFFER_SELECTED;
3078 return u64_to_user_ptr(kbuf->addr);
3079}
3080
3081#ifdef CONFIG_COMPAT
3082static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3083 bool needs_lock)
3084{
3085 struct compat_iovec __user *uiov;
3086 compat_ssize_t clen;
3087 void __user *buf;
3088 ssize_t len;
3089
3090 uiov = u64_to_user_ptr(req->rw.addr);
3091 if (!access_ok(uiov, sizeof(*uiov)))
3092 return -EFAULT;
3093 if (__get_user(clen, &uiov->iov_len))
3094 return -EFAULT;
3095 if (clen < 0)
3096 return -EINVAL;
3097
3098 len = clen;
3099 buf = io_rw_buffer_select(req, &len, needs_lock);
3100 if (IS_ERR(buf))
3101 return PTR_ERR(buf);
3102 iov[0].iov_base = buf;
3103 iov[0].iov_len = (compat_size_t) len;
3104 return 0;
3105}
3106#endif
3107
3108static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3109 bool needs_lock)
3110{
3111 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3112 void __user *buf;
3113 ssize_t len;
3114
3115 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3116 return -EFAULT;
3117
3118 len = iov[0].iov_len;
3119 if (len < 0)
3120 return -EINVAL;
3121 buf = io_rw_buffer_select(req, &len, needs_lock);
3122 if (IS_ERR(buf))
3123 return PTR_ERR(buf);
3124 iov[0].iov_base = buf;
3125 iov[0].iov_len = len;
3126 return 0;
3127}
3128
3129static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3130 bool needs_lock)
3131{
Jens Axboedddb3e22020-06-04 11:27:01 -06003132 if (req->flags & REQ_F_BUFFER_SELECTED) {
3133 struct io_buffer *kbuf;
3134
3135 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3136 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3137 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003138 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003139 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003140 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003141 return -EINVAL;
3142
3143#ifdef CONFIG_COMPAT
3144 if (req->ctx->compat)
3145 return io_compat_import(req, iov, needs_lock);
3146#endif
3147
3148 return __io_iov_buffer_select(req, iov, needs_lock);
3149}
3150
Pavel Begunkov2846c482020-11-07 13:16:27 +00003151static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboe8452fd02020-08-18 13:58:33 -07003152 struct iovec **iovec, struct iov_iter *iter,
3153 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003154{
Jens Axboe9adbd452019-12-20 08:45:55 -07003155 void __user *buf = u64_to_user_ptr(req->rw.addr);
3156 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003157 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003158 u8 opcode;
3159
Jens Axboed625c6e2019-12-17 19:53:05 -07003160 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003161 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003162 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003163 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003164 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003165
Jens Axboebcda7ba2020-02-23 16:42:51 -07003166 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003167 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003168 return -EINVAL;
3169
Jens Axboe3a6820f2019-12-22 15:19:35 -07003170 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003171 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003172 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003173 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003174 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003175 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003176 }
3177
Jens Axboe3a6820f2019-12-22 15:19:35 -07003178 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3179 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003180 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003181 }
3182
Jens Axboe4d954c22020-02-27 07:31:19 -07003183 if (req->flags & REQ_F_BUFFER_SELECT) {
3184 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003185 if (!ret) {
3186 ret = (*iovec)->iov_len;
3187 iov_iter_init(iter, rw, *iovec, 1, ret);
3188 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003189 *iovec = NULL;
3190 return ret;
3191 }
3192
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003193 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3194 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003195}
3196
Jens Axboe0fef9482020-08-26 10:36:20 -06003197static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3198{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003199 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003200}
3201
Jens Axboe32960612019-09-23 11:05:34 -06003202/*
3203 * For files that don't have ->read_iter() and ->write_iter(), handle them
3204 * by looping over ->read() or ->write() manually.
3205 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003206static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003207{
Jens Axboe4017eb92020-10-22 14:14:12 -06003208 struct kiocb *kiocb = &req->rw.kiocb;
3209 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003210 ssize_t ret = 0;
3211
3212 /*
3213 * Don't support polled IO through this interface, and we can't
3214 * support non-blocking either. For the latter, this just causes
3215 * the kiocb to be handled from an async context.
3216 */
3217 if (kiocb->ki_flags & IOCB_HIPRI)
3218 return -EOPNOTSUPP;
3219 if (kiocb->ki_flags & IOCB_NOWAIT)
3220 return -EAGAIN;
3221
3222 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003223 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003224 ssize_t nr;
3225
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003226 if (!iov_iter_is_bvec(iter)) {
3227 iovec = iov_iter_iovec(iter);
3228 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003229 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3230 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003231 }
3232
Jens Axboe32960612019-09-23 11:05:34 -06003233 if (rw == READ) {
3234 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003235 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003236 } else {
3237 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003238 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003239 }
3240
3241 if (nr < 0) {
3242 if (!ret)
3243 ret = nr;
3244 break;
3245 }
3246 ret += nr;
3247 if (nr != iovec.iov_len)
3248 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003249 req->rw.len -= nr;
3250 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003251 iov_iter_advance(iter, nr);
3252 }
3253
3254 return ret;
3255}
3256
Jens Axboeff6165b2020-08-13 09:47:43 -06003257static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3258 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003259{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003260 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003261
Jens Axboeff6165b2020-08-13 09:47:43 -06003262 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003263 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003264 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003265 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003266 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003267 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003268 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003269 unsigned iov_off = 0;
3270
3271 rw->iter.iov = rw->fast_iov;
3272 if (iter->iov != fast_iov) {
3273 iov_off = iter->iov - fast_iov;
3274 rw->iter.iov += iov_off;
3275 }
3276 if (rw->fast_iov != fast_iov)
3277 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003278 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003279 } else {
3280 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003281 }
3282}
3283
Jens Axboee8c2bc12020-08-15 18:44:09 -07003284static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003285{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003286 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3287 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3288 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003289}
3290
Jens Axboee8c2bc12020-08-15 18:44:09 -07003291static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003292{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003293 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003294 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003295
Jens Axboee8c2bc12020-08-15 18:44:09 -07003296 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003297}
3298
Jens Axboeff6165b2020-08-13 09:47:43 -06003299static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3300 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003301 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003302{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003303 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003304 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003305 if (!req->async_data) {
3306 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003307 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003308
Jens Axboeff6165b2020-08-13 09:47:43 -06003309 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003310 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003311 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003312}
3313
Pavel Begunkov73debe62020-09-30 22:57:54 +03003314static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003315{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003316 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003317 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003318 ssize_t ret;
3319
Pavel Begunkov2846c482020-11-07 13:16:27 +00003320 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003321 if (unlikely(ret < 0))
3322 return ret;
3323
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003324 iorw->bytes_done = 0;
3325 iorw->free_iovec = iov;
3326 if (iov)
3327 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003328 return 0;
3329}
3330
Pavel Begunkov73debe62020-09-30 22:57:54 +03003331static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003332{
3333 ssize_t ret;
3334
Pavel Begunkova88fc402020-09-30 22:57:53 +03003335 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003336 if (ret)
3337 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003338
Jens Axboe3529d8c2019-12-19 18:24:38 -07003339 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3340 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003341
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003342 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003343 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003344 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003345 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003346}
3347
Jens Axboec1dd91d2020-08-03 16:43:59 -06003348/*
3349 * This is our waitqueue callback handler, registered through lock_page_async()
3350 * when we initially tried to do the IO with the iocb armed our waitqueue.
3351 * This gets called when the page is unlocked, and we generally expect that to
3352 * happen when the page IO is completed and the page is now uptodate. This will
3353 * queue a task_work based retry of the operation, attempting to copy the data
3354 * again. If the latter fails because the page was NOT uptodate, then we will
3355 * do a thread based blocking retry of the operation. That's the unexpected
3356 * slow path.
3357 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003358static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3359 int sync, void *arg)
3360{
3361 struct wait_page_queue *wpq;
3362 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003363 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003364 int ret;
3365
3366 wpq = container_of(wait, struct wait_page_queue, wait);
3367
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003368 if (!wake_page_match(wpq, key))
3369 return 0;
3370
Hao Xuc8d317a2020-09-29 20:00:45 +08003371 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003372 list_del_init(&wait->entry);
3373
Pavel Begunkove7375122020-07-12 20:42:04 +03003374 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003375 percpu_ref_get(&req->ctx->refs);
3376
Jens Axboebcf5a062020-05-22 09:24:42 -06003377 /* submit ref gets dropped, acquire a new one */
3378 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003379 ret = io_req_task_work_add(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003380 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003381 struct task_struct *tsk;
3382
Jens Axboebcf5a062020-05-22 09:24:42 -06003383 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003384 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003385 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003386 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003387 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003388 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003389 return 1;
3390}
3391
Jens Axboec1dd91d2020-08-03 16:43:59 -06003392/*
3393 * This controls whether a given IO request should be armed for async page
3394 * based retry. If we return false here, the request is handed to the async
3395 * worker threads for retry. If we're doing buffered reads on a regular file,
3396 * we prepare a private wait_page_queue entry and retry the operation. This
3397 * will either succeed because the page is now uptodate and unlocked, or it
3398 * will register a callback when the page is unlocked at IO completion. Through
3399 * that callback, io_uring uses task_work to setup a retry of the operation.
3400 * That retry will attempt the buffered read again. The retry will generally
3401 * succeed, or in rare cases where it fails, we then fall back to using the
3402 * async worker threads for a blocking retry.
3403 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003404static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003405{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003406 struct io_async_rw *rw = req->async_data;
3407 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003408 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003409
3410 /* never retry for NOWAIT, we just complete with -EAGAIN */
3411 if (req->flags & REQ_F_NOWAIT)
3412 return false;
3413
Jens Axboe227c0c92020-08-13 11:51:40 -06003414 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003415 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003416 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003417
Jens Axboebcf5a062020-05-22 09:24:42 -06003418 /*
3419 * just use poll if we can, and don't attempt if the fs doesn't
3420 * support callback based unlocks
3421 */
3422 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3423 return false;
3424
Jens Axboe3b2a4432020-08-16 10:58:43 -07003425 wait->wait.func = io_async_buf_func;
3426 wait->wait.private = req;
3427 wait->wait.flags = 0;
3428 INIT_LIST_HEAD(&wait->wait.entry);
3429 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003430 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003431 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003432 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003433}
3434
3435static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3436{
3437 if (req->file->f_op->read_iter)
3438 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003439 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003440 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003441 else
3442 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003443}
3444
Jens Axboea1d7c392020-06-22 11:09:46 -06003445static int io_read(struct io_kiocb *req, bool force_nonblock,
3446 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003447{
3448 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003449 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003450 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003451 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003452 ssize_t io_size, ret, ret2;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003453 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003454
Pavel Begunkov2846c482020-11-07 13:16:27 +00003455 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003456 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003457 iovec = NULL;
3458 } else {
3459 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3460 if (ret < 0)
3461 return ret;
3462 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003463 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003464 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003465 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003466
Jens Axboefd6c2e42019-12-18 12:19:41 -07003467 /* Ensure we clear previously set non-block flag */
3468 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003469 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003470 else
3471 kiocb->ki_flags |= IOCB_NOWAIT;
3472
Jens Axboefd6c2e42019-12-18 12:19:41 -07003473
Pavel Begunkov24c74672020-06-21 13:09:51 +03003474 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003475 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3476 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003477 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003478
Pavel Begunkov632546c2020-11-07 13:16:26 +00003479 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003480 if (unlikely(ret))
3481 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003482
Jens Axboe227c0c92020-08-13 11:51:40 -06003483 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003484
Jens Axboe227c0c92020-08-13 11:51:40 -06003485 if (!ret) {
3486 goto done;
3487 } else if (ret == -EIOCBQUEUED) {
3488 ret = 0;
3489 goto out_free;
3490 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003491 /* IOPOLL retry should happen for io-wq threads */
3492 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003493 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003494 /* no retry on NONBLOCK marked file */
3495 if (req->file->f_flags & O_NONBLOCK)
3496 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003497 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003498 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003499 ret = 0;
3500 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003501 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003502 /* make sure -ERESTARTSYS -> -EINTR is done */
3503 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003504 }
3505
3506 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003507 if (!iov_iter_count(iter) || !force_nonblock ||
3508 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003509 goto done;
3510
3511 io_size -= ret;
3512copy_iov:
3513 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3514 if (ret2) {
3515 ret = ret2;
3516 goto out_free;
3517 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003518 if (no_async)
3519 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003520 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003521 /* it's copied and will be cleaned with ->io */
3522 iovec = NULL;
3523 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003524 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003525retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003526 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003527 /* if we can retry, do so with the callbacks armed */
3528 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003529 kiocb->ki_flags &= ~IOCB_WAITQ;
3530 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003531 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003532
3533 /*
3534 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3535 * get -EIOCBQUEUED, then we'll get a notification when the desired
3536 * page gets unlocked. We can also get a partial read here, and if we
3537 * do, then just retry at the new offset.
3538 */
3539 ret = io_iter_do_read(req, iter);
3540 if (ret == -EIOCBQUEUED) {
3541 ret = 0;
3542 goto out_free;
3543 } else if (ret > 0 && ret < io_size) {
3544 /* we got some bytes, but not all. retry. */
3545 goto retry;
3546 }
3547done:
3548 kiocb_done(kiocb, ret, cs);
3549 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003550out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003551 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003552 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003553 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003554 return ret;
3555}
3556
Pavel Begunkov73debe62020-09-30 22:57:54 +03003557static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003558{
3559 ssize_t ret;
3560
Pavel Begunkova88fc402020-09-30 22:57:53 +03003561 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003562 if (ret)
3563 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003564
Jens Axboe3529d8c2019-12-19 18:24:38 -07003565 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3566 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003567
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003568 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003569 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003570 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003571 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003572}
3573
Jens Axboea1d7c392020-06-22 11:09:46 -06003574static int io_write(struct io_kiocb *req, bool force_nonblock,
3575 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003576{
3577 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003578 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003579 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003580 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003581 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003582
Pavel Begunkov2846c482020-11-07 13:16:27 +00003583 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003584 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003585 iovec = NULL;
3586 } else {
3587 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3588 if (ret < 0)
3589 return ret;
3590 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003591 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003592 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003593
Jens Axboefd6c2e42019-12-18 12:19:41 -07003594 /* Ensure we clear previously set non-block flag */
3595 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003596 kiocb->ki_flags &= ~IOCB_NOWAIT;
3597 else
3598 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003599
Pavel Begunkov24c74672020-06-21 13:09:51 +03003600 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003601 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003602 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003603
Jens Axboe10d59342019-12-09 20:16:22 -07003604 /* file path doesn't support NOWAIT for non-direct_IO */
3605 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3606 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003607 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003608
Pavel Begunkov632546c2020-11-07 13:16:26 +00003609 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003610 if (unlikely(ret))
3611 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003612
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003613 /*
3614 * Open-code file_start_write here to grab freeze protection,
3615 * which will be released by another thread in
3616 * io_complete_rw(). Fool lockdep by telling it the lock got
3617 * released so that it doesn't complain about the held lock when
3618 * we return to userspace.
3619 */
3620 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003621 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003622 __sb_writers_release(file_inode(req->file)->i_sb,
3623 SB_FREEZE_WRITE);
3624 }
3625 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003626
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003627 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003628 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003629 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003630 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003631 else
3632 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003633
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003634 /*
3635 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3636 * retry them without IOCB_NOWAIT.
3637 */
3638 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3639 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003640 /* no retry on NONBLOCK marked file */
3641 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3642 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003643 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003644 /* IOPOLL retry should happen for io-wq threads */
3645 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3646 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003647done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003648 kiocb_done(kiocb, ret2, cs);
3649 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003650copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003651 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003652 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003653 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003654 if (!ret)
3655 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003656 }
Jens Axboe31b51512019-01-18 22:56:34 -07003657out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003658 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003659 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003660 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003661 return ret;
3662}
3663
Jens Axboe80a261f2020-09-28 14:23:58 -06003664static int io_renameat_prep(struct io_kiocb *req,
3665 const struct io_uring_sqe *sqe)
3666{
3667 struct io_rename *ren = &req->rename;
3668 const char __user *oldf, *newf;
3669
3670 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3671 return -EBADF;
3672
3673 ren->old_dfd = READ_ONCE(sqe->fd);
3674 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3675 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3676 ren->new_dfd = READ_ONCE(sqe->len);
3677 ren->flags = READ_ONCE(sqe->rename_flags);
3678
3679 ren->oldpath = getname(oldf);
3680 if (IS_ERR(ren->oldpath))
3681 return PTR_ERR(ren->oldpath);
3682
3683 ren->newpath = getname(newf);
3684 if (IS_ERR(ren->newpath)) {
3685 putname(ren->oldpath);
3686 return PTR_ERR(ren->newpath);
3687 }
3688
3689 req->flags |= REQ_F_NEED_CLEANUP;
3690 return 0;
3691}
3692
3693static int io_renameat(struct io_kiocb *req, bool force_nonblock)
3694{
3695 struct io_rename *ren = &req->rename;
3696 int ret;
3697
3698 if (force_nonblock)
3699 return -EAGAIN;
3700
3701 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3702 ren->newpath, ren->flags);
3703
3704 req->flags &= ~REQ_F_NEED_CLEANUP;
3705 if (ret < 0)
3706 req_set_fail_links(req);
3707 io_req_complete(req, ret);
3708 return 0;
3709}
3710
Jens Axboe14a11432020-09-28 14:27:37 -06003711static int io_unlinkat_prep(struct io_kiocb *req,
3712 const struct io_uring_sqe *sqe)
3713{
3714 struct io_unlink *un = &req->unlink;
3715 const char __user *fname;
3716
3717 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3718 return -EBADF;
3719
3720 un->dfd = READ_ONCE(sqe->fd);
3721
3722 un->flags = READ_ONCE(sqe->unlink_flags);
3723 if (un->flags & ~AT_REMOVEDIR)
3724 return -EINVAL;
3725
3726 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3727 un->filename = getname(fname);
3728 if (IS_ERR(un->filename))
3729 return PTR_ERR(un->filename);
3730
3731 req->flags |= REQ_F_NEED_CLEANUP;
3732 return 0;
3733}
3734
3735static int io_unlinkat(struct io_kiocb *req, bool force_nonblock)
3736{
3737 struct io_unlink *un = &req->unlink;
3738 int ret;
3739
3740 if (force_nonblock)
3741 return -EAGAIN;
3742
3743 if (un->flags & AT_REMOVEDIR)
3744 ret = do_rmdir(un->dfd, un->filename);
3745 else
3746 ret = do_unlinkat(un->dfd, un->filename);
3747
3748 req->flags &= ~REQ_F_NEED_CLEANUP;
3749 if (ret < 0)
3750 req_set_fail_links(req);
3751 io_req_complete(req, ret);
3752 return 0;
3753}
3754
Jens Axboe36f4fa62020-09-05 11:14:22 -06003755static int io_shutdown_prep(struct io_kiocb *req,
3756 const struct io_uring_sqe *sqe)
3757{
3758#if defined(CONFIG_NET)
3759 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3760 return -EINVAL;
3761 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3762 sqe->buf_index)
3763 return -EINVAL;
3764
3765 req->shutdown.how = READ_ONCE(sqe->len);
3766 return 0;
3767#else
3768 return -EOPNOTSUPP;
3769#endif
3770}
3771
3772static int io_shutdown(struct io_kiocb *req, bool force_nonblock)
3773{
3774#if defined(CONFIG_NET)
3775 struct socket *sock;
3776 int ret;
3777
3778 if (force_nonblock)
3779 return -EAGAIN;
3780
Linus Torvalds48aba792020-12-16 12:44:05 -08003781 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003782 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003783 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003784
3785 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003786 if (ret < 0)
3787 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003788 io_req_complete(req, ret);
3789 return 0;
3790#else
3791 return -EOPNOTSUPP;
3792#endif
3793}
3794
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003795static int __io_splice_prep(struct io_kiocb *req,
3796 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003797{
3798 struct io_splice* sp = &req->splice;
3799 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003800
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003801 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3802 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003803
3804 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003805 sp->len = READ_ONCE(sqe->len);
3806 sp->flags = READ_ONCE(sqe->splice_flags);
3807
3808 if (unlikely(sp->flags & ~valid_flags))
3809 return -EINVAL;
3810
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003811 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3812 (sp->flags & SPLICE_F_FD_IN_FIXED));
3813 if (!sp->file_in)
3814 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003815 req->flags |= REQ_F_NEED_CLEANUP;
3816
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003817 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3818 /*
3819 * Splice operation will be punted aync, and here need to
3820 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3821 */
3822 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003823 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003824 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003825
3826 return 0;
3827}
3828
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003829static int io_tee_prep(struct io_kiocb *req,
3830 const struct io_uring_sqe *sqe)
3831{
3832 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3833 return -EINVAL;
3834 return __io_splice_prep(req, sqe);
3835}
3836
3837static int io_tee(struct io_kiocb *req, bool force_nonblock)
3838{
3839 struct io_splice *sp = &req->splice;
3840 struct file *in = sp->file_in;
3841 struct file *out = sp->file_out;
3842 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3843 long ret = 0;
3844
3845 if (force_nonblock)
3846 return -EAGAIN;
3847 if (sp->len)
3848 ret = do_tee(in, out, sp->len, flags);
3849
3850 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3851 req->flags &= ~REQ_F_NEED_CLEANUP;
3852
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003853 if (ret != sp->len)
3854 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003855 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003856 return 0;
3857}
3858
3859static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3860{
3861 struct io_splice* sp = &req->splice;
3862
3863 sp->off_in = READ_ONCE(sqe->splice_off_in);
3864 sp->off_out = READ_ONCE(sqe->off);
3865 return __io_splice_prep(req, sqe);
3866}
3867
Pavel Begunkov014db002020-03-03 21:33:12 +03003868static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003869{
3870 struct io_splice *sp = &req->splice;
3871 struct file *in = sp->file_in;
3872 struct file *out = sp->file_out;
3873 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3874 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003875 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003876
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003877 if (force_nonblock)
3878 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003879
3880 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3881 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003882
Jens Axboe948a7742020-05-17 14:21:38 -06003883 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003884 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003885
3886 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3887 req->flags &= ~REQ_F_NEED_CLEANUP;
3888
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003889 if (ret != sp->len)
3890 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003891 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003892 return 0;
3893}
3894
Jens Axboe2b188cc2019-01-07 10:46:33 -07003895/*
3896 * IORING_OP_NOP just posts a completion event, nothing else.
3897 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003898static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003899{
3900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003901
Jens Axboedef596e2019-01-09 08:59:42 -07003902 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3903 return -EINVAL;
3904
Jens Axboe229a7b62020-06-22 10:13:11 -06003905 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003906 return 0;
3907}
3908
Jens Axboe3529d8c2019-12-19 18:24:38 -07003909static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003910{
Jens Axboe6b063142019-01-10 22:13:58 -07003911 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003912
Jens Axboe09bb8392019-03-13 12:39:28 -06003913 if (!req->file)
3914 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003915
Jens Axboe6b063142019-01-10 22:13:58 -07003916 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003917 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003918 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003919 return -EINVAL;
3920
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003921 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3922 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3923 return -EINVAL;
3924
3925 req->sync.off = READ_ONCE(sqe->off);
3926 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003927 return 0;
3928}
3929
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003930static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003931{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003932 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003933 int ret;
3934
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003935 /* fsync always requires a blocking context */
3936 if (force_nonblock)
3937 return -EAGAIN;
3938
Jens Axboe9adbd452019-12-20 08:45:55 -07003939 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003940 end > 0 ? end : LLONG_MAX,
3941 req->sync.flags & IORING_FSYNC_DATASYNC);
3942 if (ret < 0)
3943 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003944 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003945 return 0;
3946}
3947
Jens Axboed63d1b52019-12-10 10:38:56 -07003948static int io_fallocate_prep(struct io_kiocb *req,
3949 const struct io_uring_sqe *sqe)
3950{
3951 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3952 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003953 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3954 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003955
3956 req->sync.off = READ_ONCE(sqe->off);
3957 req->sync.len = READ_ONCE(sqe->addr);
3958 req->sync.mode = READ_ONCE(sqe->len);
3959 return 0;
3960}
3961
Pavel Begunkov014db002020-03-03 21:33:12 +03003962static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003963{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003964 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003965
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003966 /* fallocate always requiring blocking context */
3967 if (force_nonblock)
3968 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003969 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3970 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003971 if (ret < 0)
3972 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003973 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003974 return 0;
3975}
3976
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003977static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003978{
Jens Axboef8748882020-01-08 17:47:02 -07003979 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003980 int ret;
3981
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003982 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003983 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003984 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003985 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003986
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003987 /* open.how should be already initialised */
3988 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003989 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003990
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003991 req->open.dfd = READ_ONCE(sqe->fd);
3992 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003993 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003994 if (IS_ERR(req->open.filename)) {
3995 ret = PTR_ERR(req->open.filename);
3996 req->open.filename = NULL;
3997 return ret;
3998 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003999 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07004000 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004001 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004002 return 0;
4003}
4004
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004005static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4006{
4007 u64 flags, mode;
4008
Jens Axboe14587a462020-09-05 11:36:08 -06004009 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004010 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004011 mode = READ_ONCE(sqe->len);
4012 flags = READ_ONCE(sqe->open_flags);
4013 req->open.how = build_open_how(flags, mode);
4014 return __io_openat_prep(req, sqe);
4015}
4016
Jens Axboecebdb982020-01-08 17:59:24 -07004017static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4018{
4019 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004020 size_t len;
4021 int ret;
4022
Jens Axboe14587a462020-09-05 11:36:08 -06004023 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004024 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004025 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4026 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004027 if (len < OPEN_HOW_SIZE_VER0)
4028 return -EINVAL;
4029
4030 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4031 len);
4032 if (ret)
4033 return ret;
4034
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004035 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004036}
4037
Pavel Begunkov014db002020-03-03 21:33:12 +03004038static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004039{
4040 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004041 struct file *file;
4042 int ret;
4043
Jens Axboe944d1442020-11-13 16:48:44 -07004044 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004045 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004046
Jens Axboecebdb982020-01-08 17:59:24 -07004047 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004048 if (ret)
4049 goto err;
4050
Jens Axboe4022e7a2020-03-19 19:23:18 -06004051 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004052 if (ret < 0)
4053 goto err;
4054
4055 file = do_filp_open(req->open.dfd, req->open.filename, &op);
4056 if (IS_ERR(file)) {
4057 put_unused_fd(ret);
4058 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07004059 /*
4060 * A work-around to ensure that /proc/self works that way
4061 * that it should - if we get -EOPNOTSUPP back, then assume
4062 * that proc_self_get_link() failed us because we're in async
4063 * context. We should be safe to retry this from the task
4064 * itself with force_nonblock == false set, as it should not
4065 * block on lookup. Would be nice to know this upfront and
4066 * avoid the async dance, but doesn't seem feasible.
4067 */
4068 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
4069 req->open.ignore_nonblock = true;
4070 refcount_inc(&req->refs);
4071 io_req_task_queue(req);
4072 return 0;
4073 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004074 } else {
4075 fsnotify_open(file);
4076 fd_install(ret, file);
4077 }
4078err:
4079 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004080 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004081 if (ret < 0)
4082 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004083 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004084 return 0;
4085}
4086
Pavel Begunkov014db002020-03-03 21:33:12 +03004087static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07004088{
Pavel Begunkov014db002020-03-03 21:33:12 +03004089 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004090}
4091
Jens Axboe067524e2020-03-02 16:32:28 -07004092static int io_remove_buffers_prep(struct io_kiocb *req,
4093 const struct io_uring_sqe *sqe)
4094{
4095 struct io_provide_buf *p = &req->pbuf;
4096 u64 tmp;
4097
4098 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4099 return -EINVAL;
4100
4101 tmp = READ_ONCE(sqe->fd);
4102 if (!tmp || tmp > USHRT_MAX)
4103 return -EINVAL;
4104
4105 memset(p, 0, sizeof(*p));
4106 p->nbufs = tmp;
4107 p->bgid = READ_ONCE(sqe->buf_group);
4108 return 0;
4109}
4110
4111static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4112 int bgid, unsigned nbufs)
4113{
4114 unsigned i = 0;
4115
4116 /* shouldn't happen */
4117 if (!nbufs)
4118 return 0;
4119
4120 /* the head kbuf is the list itself */
4121 while (!list_empty(&buf->list)) {
4122 struct io_buffer *nxt;
4123
4124 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4125 list_del(&nxt->list);
4126 kfree(nxt);
4127 if (++i == nbufs)
4128 return i;
4129 }
4130 i++;
4131 kfree(buf);
4132 idr_remove(&ctx->io_buffer_idr, bgid);
4133
4134 return i;
4135}
4136
Jens Axboe229a7b62020-06-22 10:13:11 -06004137static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
4138 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004139{
4140 struct io_provide_buf *p = &req->pbuf;
4141 struct io_ring_ctx *ctx = req->ctx;
4142 struct io_buffer *head;
4143 int ret = 0;
4144
4145 io_ring_submit_lock(ctx, !force_nonblock);
4146
4147 lockdep_assert_held(&ctx->uring_lock);
4148
4149 ret = -ENOENT;
4150 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4151 if (head)
4152 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004153 if (ret < 0)
4154 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004155
4156 /* need to hold the lock to complete IOPOLL requests */
4157 if (ctx->flags & IORING_SETUP_IOPOLL) {
4158 __io_req_complete(req, ret, 0, cs);
4159 io_ring_submit_unlock(ctx, !force_nonblock);
4160 } else {
4161 io_ring_submit_unlock(ctx, !force_nonblock);
4162 __io_req_complete(req, ret, 0, cs);
4163 }
Jens Axboe067524e2020-03-02 16:32:28 -07004164 return 0;
4165}
4166
Jens Axboeddf0322d2020-02-23 16:41:33 -07004167static int io_provide_buffers_prep(struct io_kiocb *req,
4168 const struct io_uring_sqe *sqe)
4169{
4170 struct io_provide_buf *p = &req->pbuf;
4171 u64 tmp;
4172
4173 if (sqe->ioprio || sqe->rw_flags)
4174 return -EINVAL;
4175
4176 tmp = READ_ONCE(sqe->fd);
4177 if (!tmp || tmp > USHRT_MAX)
4178 return -E2BIG;
4179 p->nbufs = tmp;
4180 p->addr = READ_ONCE(sqe->addr);
4181 p->len = READ_ONCE(sqe->len);
4182
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004183 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004184 return -EFAULT;
4185
4186 p->bgid = READ_ONCE(sqe->buf_group);
4187 tmp = READ_ONCE(sqe->off);
4188 if (tmp > USHRT_MAX)
4189 return -E2BIG;
4190 p->bid = tmp;
4191 return 0;
4192}
4193
4194static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4195{
4196 struct io_buffer *buf;
4197 u64 addr = pbuf->addr;
4198 int i, bid = pbuf->bid;
4199
4200 for (i = 0; i < pbuf->nbufs; i++) {
4201 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4202 if (!buf)
4203 break;
4204
4205 buf->addr = addr;
4206 buf->len = pbuf->len;
4207 buf->bid = bid;
4208 addr += pbuf->len;
4209 bid++;
4210 if (!*head) {
4211 INIT_LIST_HEAD(&buf->list);
4212 *head = buf;
4213 } else {
4214 list_add_tail(&buf->list, &(*head)->list);
4215 }
4216 }
4217
4218 return i ? i : -ENOMEM;
4219}
4220
Jens Axboe229a7b62020-06-22 10:13:11 -06004221static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4222 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004223{
4224 struct io_provide_buf *p = &req->pbuf;
4225 struct io_ring_ctx *ctx = req->ctx;
4226 struct io_buffer *head, *list;
4227 int ret = 0;
4228
4229 io_ring_submit_lock(ctx, !force_nonblock);
4230
4231 lockdep_assert_held(&ctx->uring_lock);
4232
4233 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4234
4235 ret = io_add_buffers(p, &head);
4236 if (ret < 0)
4237 goto out;
4238
4239 if (!list) {
4240 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4241 GFP_KERNEL);
4242 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004243 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004244 goto out;
4245 }
4246 }
4247out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004248 if (ret < 0)
4249 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004250
4251 /* need to hold the lock to complete IOPOLL requests */
4252 if (ctx->flags & IORING_SETUP_IOPOLL) {
4253 __io_req_complete(req, ret, 0, cs);
4254 io_ring_submit_unlock(ctx, !force_nonblock);
4255 } else {
4256 io_ring_submit_unlock(ctx, !force_nonblock);
4257 __io_req_complete(req, ret, 0, cs);
4258 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004259 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004260}
4261
Jens Axboe3e4827b2020-01-08 15:18:09 -07004262static int io_epoll_ctl_prep(struct io_kiocb *req,
4263 const struct io_uring_sqe *sqe)
4264{
4265#if defined(CONFIG_EPOLL)
4266 if (sqe->ioprio || sqe->buf_index)
4267 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004268 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004269 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004270
4271 req->epoll.epfd = READ_ONCE(sqe->fd);
4272 req->epoll.op = READ_ONCE(sqe->len);
4273 req->epoll.fd = READ_ONCE(sqe->off);
4274
4275 if (ep_op_has_event(req->epoll.op)) {
4276 struct epoll_event __user *ev;
4277
4278 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4279 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4280 return -EFAULT;
4281 }
4282
4283 return 0;
4284#else
4285 return -EOPNOTSUPP;
4286#endif
4287}
4288
Jens Axboe229a7b62020-06-22 10:13:11 -06004289static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4290 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004291{
4292#if defined(CONFIG_EPOLL)
4293 struct io_epoll *ie = &req->epoll;
4294 int ret;
4295
4296 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4297 if (force_nonblock && ret == -EAGAIN)
4298 return -EAGAIN;
4299
4300 if (ret < 0)
4301 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004302 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004303 return 0;
4304#else
4305 return -EOPNOTSUPP;
4306#endif
4307}
4308
Jens Axboec1ca7572019-12-25 22:18:28 -07004309static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4310{
4311#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4312 if (sqe->ioprio || sqe->buf_index || sqe->off)
4313 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004314 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4315 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004316
4317 req->madvise.addr = READ_ONCE(sqe->addr);
4318 req->madvise.len = READ_ONCE(sqe->len);
4319 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4320 return 0;
4321#else
4322 return -EOPNOTSUPP;
4323#endif
4324}
4325
Pavel Begunkov014db002020-03-03 21:33:12 +03004326static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004327{
4328#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4329 struct io_madvise *ma = &req->madvise;
4330 int ret;
4331
4332 if (force_nonblock)
4333 return -EAGAIN;
4334
Minchan Kim0726b012020-10-17 16:14:50 -07004335 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004336 if (ret < 0)
4337 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004338 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004339 return 0;
4340#else
4341 return -EOPNOTSUPP;
4342#endif
4343}
4344
Jens Axboe4840e412019-12-25 22:03:45 -07004345static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4346{
4347 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4348 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004349 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4350 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004351
4352 req->fadvise.offset = READ_ONCE(sqe->off);
4353 req->fadvise.len = READ_ONCE(sqe->len);
4354 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4355 return 0;
4356}
4357
Pavel Begunkov014db002020-03-03 21:33:12 +03004358static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004359{
4360 struct io_fadvise *fa = &req->fadvise;
4361 int ret;
4362
Jens Axboe3e694262020-02-01 09:22:49 -07004363 if (force_nonblock) {
4364 switch (fa->advice) {
4365 case POSIX_FADV_NORMAL:
4366 case POSIX_FADV_RANDOM:
4367 case POSIX_FADV_SEQUENTIAL:
4368 break;
4369 default:
4370 return -EAGAIN;
4371 }
4372 }
Jens Axboe4840e412019-12-25 22:03:45 -07004373
4374 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4375 if (ret < 0)
4376 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004377 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004378 return 0;
4379}
4380
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004381static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4382{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004383 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004384 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004385 if (sqe->ioprio || sqe->buf_index)
4386 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004387 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004388 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004389
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004390 req->statx.dfd = READ_ONCE(sqe->fd);
4391 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004392 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004393 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4394 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004395
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004396 return 0;
4397}
4398
Pavel Begunkov014db002020-03-03 21:33:12 +03004399static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004400{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004401 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004402 int ret;
4403
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004404 if (force_nonblock) {
4405 /* only need file table for an actual valid fd */
4406 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4407 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004408 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004409 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004410
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004411 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4412 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004413
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004414 if (ret < 0)
4415 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004416 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004417 return 0;
4418}
4419
Jens Axboeb5dba592019-12-11 14:02:38 -07004420static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4421{
4422 /*
4423 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004424 * leave the 'file' in an undeterminate state, and here need to modify
4425 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004426 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004427 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004428 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4429
Jens Axboe14587a462020-09-05 11:36:08 -06004430 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004431 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004432 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4433 sqe->rw_flags || sqe->buf_index)
4434 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004435 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004436 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004437
4438 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004439 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004440 return -EBADF;
4441
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004442 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004443 return 0;
4444}
4445
Jens Axboe229a7b62020-06-22 10:13:11 -06004446static int io_close(struct io_kiocb *req, bool force_nonblock,
4447 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004448{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004449 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004450 int ret;
4451
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004452 /* might be already done during nonblock submission */
4453 if (!close->put_file) {
Eric W. Biederman9fe83c42020-11-20 17:14:40 -06004454 ret = close_fd_get_file(close->fd, &close->put_file);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004455 if (ret < 0)
4456 return (ret == -ENOENT) ? -EBADF : ret;
4457 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004458
4459 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004460 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004461 /* was never set, but play safe */
4462 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004463 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004464 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004465 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004466 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004467
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004468 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004469 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004470 if (ret < 0)
4471 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004472 fput(close->put_file);
4473 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004474 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004475 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004476}
4477
Jens Axboe3529d8c2019-12-19 18:24:38 -07004478static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004479{
4480 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004481
4482 if (!req->file)
4483 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004484
4485 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4486 return -EINVAL;
4487 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4488 return -EINVAL;
4489
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004490 req->sync.off = READ_ONCE(sqe->off);
4491 req->sync.len = READ_ONCE(sqe->len);
4492 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004493 return 0;
4494}
4495
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004496static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004497{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004498 int ret;
4499
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004500 /* sync_file_range always requires a blocking context */
4501 if (force_nonblock)
4502 return -EAGAIN;
4503
Jens Axboe9adbd452019-12-20 08:45:55 -07004504 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004505 req->sync.flags);
4506 if (ret < 0)
4507 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004508 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004509 return 0;
4510}
4511
YueHaibing469956e2020-03-04 15:53:52 +08004512#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004513static int io_setup_async_msg(struct io_kiocb *req,
4514 struct io_async_msghdr *kmsg)
4515{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004516 struct io_async_msghdr *async_msg = req->async_data;
4517
4518 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004519 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004520 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004521 if (kmsg->iov != kmsg->fast_iov)
4522 kfree(kmsg->iov);
4523 return -ENOMEM;
4524 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004525 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004526 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004527 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004528 return -EAGAIN;
4529}
4530
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004531static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4532 struct io_async_msghdr *iomsg)
4533{
4534 iomsg->iov = iomsg->fast_iov;
4535 iomsg->msg.msg_name = &iomsg->addr;
4536 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4537 req->sr_msg.msg_flags, &iomsg->iov);
4538}
4539
Jens Axboe3529d8c2019-12-19 18:24:38 -07004540static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004541{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004542 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004543 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004544 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004545
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004546 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4547 return -EINVAL;
4548
Jens Axboee47293f2019-12-20 08:58:21 -07004549 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004550 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004551 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004552
Jens Axboed8768362020-02-27 14:17:49 -07004553#ifdef CONFIG_COMPAT
4554 if (req->ctx->compat)
4555 sr->msg_flags |= MSG_CMSG_COMPAT;
4556#endif
4557
Jens Axboee8c2bc12020-08-15 18:44:09 -07004558 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004559 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004560 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004561 if (!ret)
4562 req->flags |= REQ_F_NEED_CLEANUP;
4563 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004564}
4565
Jens Axboe229a7b62020-06-22 10:13:11 -06004566static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4567 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004568{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004569 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004570 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004571 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004572 int ret;
4573
Florent Revestdba4a922020-12-04 12:36:04 +01004574 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004575 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004576 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004577
Jens Axboee8c2bc12020-08-15 18:44:09 -07004578 if (req->async_data) {
4579 kmsg = req->async_data;
4580 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004581 /* if iov is set, it's allocated already */
4582 if (!kmsg->iov)
4583 kmsg->iov = kmsg->fast_iov;
4584 kmsg->msg.msg_iter.iov = kmsg->iov;
4585 } else {
4586 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004587 if (ret)
4588 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004589 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004590 }
4591
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004592 flags = req->sr_msg.msg_flags;
4593 if (flags & MSG_DONTWAIT)
4594 req->flags |= REQ_F_NOWAIT;
4595 else if (force_nonblock)
4596 flags |= MSG_DONTWAIT;
4597
4598 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4599 if (force_nonblock && ret == -EAGAIN)
4600 return io_setup_async_msg(req, kmsg);
4601 if (ret == -ERESTARTSYS)
4602 ret = -EINTR;
4603
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004604 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004605 kfree(kmsg->iov);
4606 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004607 if (ret < 0)
4608 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004609 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004610 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004611}
4612
Jens Axboe229a7b62020-06-22 10:13:11 -06004613static int io_send(struct io_kiocb *req, bool force_nonblock,
4614 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004615{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004616 struct io_sr_msg *sr = &req->sr_msg;
4617 struct msghdr msg;
4618 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004619 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004620 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004621 int ret;
4622
Florent Revestdba4a922020-12-04 12:36:04 +01004623 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004624 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004625 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004626
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004627 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4628 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004629 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004630
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 msg.msg_name = NULL;
4632 msg.msg_control = NULL;
4633 msg.msg_controllen = 0;
4634 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004635
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004636 flags = req->sr_msg.msg_flags;
4637 if (flags & MSG_DONTWAIT)
4638 req->flags |= REQ_F_NOWAIT;
4639 else if (force_nonblock)
4640 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004641
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004642 msg.msg_flags = flags;
4643 ret = sock_sendmsg(sock, &msg);
4644 if (force_nonblock && ret == -EAGAIN)
4645 return -EAGAIN;
4646 if (ret == -ERESTARTSYS)
4647 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004648
Jens Axboe03b12302019-12-02 18:50:25 -07004649 if (ret < 0)
4650 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004651 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004652 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004653}
4654
Pavel Begunkov1400e692020-07-12 20:41:05 +03004655static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4656 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004657{
4658 struct io_sr_msg *sr = &req->sr_msg;
4659 struct iovec __user *uiov;
4660 size_t iov_len;
4661 int ret;
4662
Pavel Begunkov1400e692020-07-12 20:41:05 +03004663 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4664 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004665 if (ret)
4666 return ret;
4667
4668 if (req->flags & REQ_F_BUFFER_SELECT) {
4669 if (iov_len > 1)
4670 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004671 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004672 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004673 sr->len = iomsg->iov[0].iov_len;
4674 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004675 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004676 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004677 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004678 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4679 &iomsg->iov, &iomsg->msg.msg_iter,
4680 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004681 if (ret > 0)
4682 ret = 0;
4683 }
4684
4685 return ret;
4686}
4687
4688#ifdef CONFIG_COMPAT
4689static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004690 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004691{
4692 struct compat_msghdr __user *msg_compat;
4693 struct io_sr_msg *sr = &req->sr_msg;
4694 struct compat_iovec __user *uiov;
4695 compat_uptr_t ptr;
4696 compat_size_t len;
4697 int ret;
4698
Pavel Begunkov270a5942020-07-12 20:41:04 +03004699 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004700 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004701 &ptr, &len);
4702 if (ret)
4703 return ret;
4704
4705 uiov = compat_ptr(ptr);
4706 if (req->flags & REQ_F_BUFFER_SELECT) {
4707 compat_ssize_t clen;
4708
4709 if (len > 1)
4710 return -EINVAL;
4711 if (!access_ok(uiov, sizeof(*uiov)))
4712 return -EFAULT;
4713 if (__get_user(clen, &uiov->iov_len))
4714 return -EFAULT;
4715 if (clen < 0)
4716 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004717 sr->len = clen;
4718 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004719 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004720 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004721 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4722 UIO_FASTIOV, &iomsg->iov,
4723 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004724 if (ret < 0)
4725 return ret;
4726 }
4727
4728 return 0;
4729}
Jens Axboe03b12302019-12-02 18:50:25 -07004730#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004731
Pavel Begunkov1400e692020-07-12 20:41:05 +03004732static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4733 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004734{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004735 iomsg->msg.msg_name = &iomsg->addr;
4736 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004737
4738#ifdef CONFIG_COMPAT
4739 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004740 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004741#endif
4742
Pavel Begunkov1400e692020-07-12 20:41:05 +03004743 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004744}
4745
Jens Axboebcda7ba2020-02-23 16:42:51 -07004746static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004747 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004748{
4749 struct io_sr_msg *sr = &req->sr_msg;
4750 struct io_buffer *kbuf;
4751
Jens Axboebcda7ba2020-02-23 16:42:51 -07004752 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4753 if (IS_ERR(kbuf))
4754 return kbuf;
4755
4756 sr->kbuf = kbuf;
4757 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004758 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004759}
4760
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004761static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4762{
4763 return io_put_kbuf(req, req->sr_msg.kbuf);
4764}
4765
Jens Axboe3529d8c2019-12-19 18:24:38 -07004766static int io_recvmsg_prep(struct io_kiocb *req,
4767 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004768{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004769 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004770 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004771 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004772
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004773 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4774 return -EINVAL;
4775
Jens Axboe3529d8c2019-12-19 18:24:38 -07004776 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004777 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004778 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004779 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004780
Jens Axboed8768362020-02-27 14:17:49 -07004781#ifdef CONFIG_COMPAT
4782 if (req->ctx->compat)
4783 sr->msg_flags |= MSG_CMSG_COMPAT;
4784#endif
4785
Jens Axboee8c2bc12020-08-15 18:44:09 -07004786 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004787 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004788 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004789 if (!ret)
4790 req->flags |= REQ_F_NEED_CLEANUP;
4791 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004792}
4793
Jens Axboe229a7b62020-06-22 10:13:11 -06004794static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4795 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004796{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004797 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004798 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004799 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004800 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004801 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004802
Florent Revestdba4a922020-12-04 12:36:04 +01004803 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004804 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004805 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004806
Jens Axboee8c2bc12020-08-15 18:44:09 -07004807 if (req->async_data) {
4808 kmsg = req->async_data;
4809 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004810 /* if iov is set, it's allocated already */
4811 if (!kmsg->iov)
4812 kmsg->iov = kmsg->fast_iov;
4813 kmsg->msg.msg_iter.iov = kmsg->iov;
4814 } else {
4815 ret = io_recvmsg_copy_hdr(req, &iomsg);
4816 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004817 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004818 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004819 }
4820
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004821 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004822 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004823 if (IS_ERR(kbuf))
4824 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004825 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4826 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4827 1, req->sr_msg.len);
4828 }
4829
4830 flags = req->sr_msg.msg_flags;
4831 if (flags & MSG_DONTWAIT)
4832 req->flags |= REQ_F_NOWAIT;
4833 else if (force_nonblock)
4834 flags |= MSG_DONTWAIT;
4835
4836 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4837 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004838 if (force_nonblock && ret == -EAGAIN)
4839 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004840 if (ret == -ERESTARTSYS)
4841 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004842
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004843 if (req->flags & REQ_F_BUFFER_SELECTED)
4844 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004845 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004846 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004847 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004848 if (ret < 0)
4849 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004850 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004851 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004852}
4853
Jens Axboe229a7b62020-06-22 10:13:11 -06004854static int io_recv(struct io_kiocb *req, bool force_nonblock,
4855 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004856{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004857 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004858 struct io_sr_msg *sr = &req->sr_msg;
4859 struct msghdr msg;
4860 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004861 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004862 struct iovec iov;
4863 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004864 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004865
Florent Revestdba4a922020-12-04 12:36:04 +01004866 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004867 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004868 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004869
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004870 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004871 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004872 if (IS_ERR(kbuf))
4873 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004874 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004875 }
4876
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004877 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004878 if (unlikely(ret))
4879 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004880
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004881 msg.msg_name = NULL;
4882 msg.msg_control = NULL;
4883 msg.msg_controllen = 0;
4884 msg.msg_namelen = 0;
4885 msg.msg_iocb = NULL;
4886 msg.msg_flags = 0;
4887
4888 flags = req->sr_msg.msg_flags;
4889 if (flags & MSG_DONTWAIT)
4890 req->flags |= REQ_F_NOWAIT;
4891 else if (force_nonblock)
4892 flags |= MSG_DONTWAIT;
4893
4894 ret = sock_recvmsg(sock, &msg, flags);
4895 if (force_nonblock && ret == -EAGAIN)
4896 return -EAGAIN;
4897 if (ret == -ERESTARTSYS)
4898 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004899out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004900 if (req->flags & REQ_F_BUFFER_SELECTED)
4901 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004902 if (ret < 0)
4903 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004904 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004905 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004906}
4907
Jens Axboe3529d8c2019-12-19 18:24:38 -07004908static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004909{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004910 struct io_accept *accept = &req->accept;
4911
Jens Axboe14587a462020-09-05 11:36:08 -06004912 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004913 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004914 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004915 return -EINVAL;
4916
Jens Axboed55e5f52019-12-11 16:12:15 -07004917 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4918 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004919 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004920 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004921 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004922}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004923
Jens Axboe229a7b62020-06-22 10:13:11 -06004924static int io_accept(struct io_kiocb *req, bool force_nonblock,
4925 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004926{
4927 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004928 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004929 int ret;
4930
Jiufei Xuee697dee2020-06-10 13:41:59 +08004931 if (req->file->f_flags & O_NONBLOCK)
4932 req->flags |= REQ_F_NOWAIT;
4933
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004934 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004935 accept->addr_len, accept->flags,
4936 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004937 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004938 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004939 if (ret < 0) {
4940 if (ret == -ERESTARTSYS)
4941 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004942 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004943 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004944 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004945 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004946}
4947
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004949{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004950 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004951 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004952
Jens Axboe14587a462020-09-05 11:36:08 -06004953 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004954 return -EINVAL;
4955 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4956 return -EINVAL;
4957
Jens Axboe3529d8c2019-12-19 18:24:38 -07004958 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4959 conn->addr_len = READ_ONCE(sqe->addr2);
4960
4961 if (!io)
4962 return 0;
4963
4964 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004965 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004966}
4967
Jens Axboe229a7b62020-06-22 10:13:11 -06004968static int io_connect(struct io_kiocb *req, bool force_nonblock,
4969 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004970{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004971 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004972 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004973 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004974
Jens Axboee8c2bc12020-08-15 18:44:09 -07004975 if (req->async_data) {
4976 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004977 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004978 ret = move_addr_to_kernel(req->connect.addr,
4979 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004980 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004981 if (ret)
4982 goto out;
4983 io = &__io;
4984 }
4985
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004986 file_flags = force_nonblock ? O_NONBLOCK : 0;
4987
Jens Axboee8c2bc12020-08-15 18:44:09 -07004988 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004989 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004990 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004991 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004992 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004993 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004994 ret = -ENOMEM;
4995 goto out;
4996 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004997 io = req->async_data;
4998 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004999 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005000 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005001 if (ret == -ERESTARTSYS)
5002 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005003out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005004 if (ret < 0)
5005 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005006 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005007 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005008}
YueHaibing469956e2020-03-04 15:53:52 +08005009#else /* !CONFIG_NET */
5010static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5011{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005012 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005013}
5014
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005015static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
5016 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005017{
YueHaibing469956e2020-03-04 15:53:52 +08005018 return -EOPNOTSUPP;
5019}
5020
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005021static int io_send(struct io_kiocb *req, bool force_nonblock,
5022 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005023{
5024 return -EOPNOTSUPP;
5025}
5026
5027static int io_recvmsg_prep(struct io_kiocb *req,
5028 const struct io_uring_sqe *sqe)
5029{
5030 return -EOPNOTSUPP;
5031}
5032
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005033static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
5034 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005035{
5036 return -EOPNOTSUPP;
5037}
5038
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005039static int io_recv(struct io_kiocb *req, bool force_nonblock,
5040 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005041{
5042 return -EOPNOTSUPP;
5043}
5044
5045static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5046{
5047 return -EOPNOTSUPP;
5048}
5049
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005050static int io_accept(struct io_kiocb *req, bool force_nonblock,
5051 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005052{
5053 return -EOPNOTSUPP;
5054}
5055
5056static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5057{
5058 return -EOPNOTSUPP;
5059}
5060
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005061static int io_connect(struct io_kiocb *req, bool force_nonblock,
5062 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005063{
5064 return -EOPNOTSUPP;
5065}
5066#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005067
Jens Axboed7718a92020-02-14 22:23:12 -07005068struct io_poll_table {
5069 struct poll_table_struct pt;
5070 struct io_kiocb *req;
5071 int error;
5072};
5073
Jens Axboed7718a92020-02-14 22:23:12 -07005074static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5075 __poll_t mask, task_work_func_t func)
5076{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005077 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005078
5079 /* for instances that support it check for an event match first: */
5080 if (mask && !(mask & poll->events))
5081 return 0;
5082
5083 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5084
5085 list_del_init(&poll->wait.entry);
5086
Jens Axboed7718a92020-02-14 22:23:12 -07005087 req->result = mask;
5088 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005089 percpu_ref_get(&req->ctx->refs);
5090
Jens Axboed7718a92020-02-14 22:23:12 -07005091 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005092 * If this fails, then the task is exiting. When a task exits, the
5093 * work gets canceled, so just cancel this request as well instead
5094 * of executing it. We can't safely execute it anyway, as we may not
5095 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005096 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005097 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005098 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06005099 struct task_struct *tsk;
5100
Jens Axboee3aabf92020-05-18 11:04:17 -06005101 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005102 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06005103 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06005104 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005105 }
Jens Axboed7718a92020-02-14 22:23:12 -07005106 return 1;
5107}
5108
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005109static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5110 __acquires(&req->ctx->completion_lock)
5111{
5112 struct io_ring_ctx *ctx = req->ctx;
5113
5114 if (!req->result && !READ_ONCE(poll->canceled)) {
5115 struct poll_table_struct pt = { ._key = poll->events };
5116
5117 req->result = vfs_poll(req->file, &pt) & poll->events;
5118 }
5119
5120 spin_lock_irq(&ctx->completion_lock);
5121 if (!req->result && !READ_ONCE(poll->canceled)) {
5122 add_wait_queue(poll->head, &poll->wait);
5123 return true;
5124 }
5125
5126 return false;
5127}
5128
Jens Axboed4e7cd32020-08-15 11:44:50 -07005129static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005130{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005131 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005132 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005133 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005134 return req->apoll->double_poll;
5135}
5136
5137static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5138{
5139 if (req->opcode == IORING_OP_POLL_ADD)
5140 return &req->poll;
5141 return &req->apoll->poll;
5142}
5143
5144static void io_poll_remove_double(struct io_kiocb *req)
5145{
5146 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005147
5148 lockdep_assert_held(&req->ctx->completion_lock);
5149
5150 if (poll && poll->head) {
5151 struct wait_queue_head *head = poll->head;
5152
5153 spin_lock(&head->lock);
5154 list_del_init(&poll->wait.entry);
5155 if (poll->wait.private)
5156 refcount_dec(&req->refs);
5157 poll->head = NULL;
5158 spin_unlock(&head->lock);
5159 }
5160}
5161
5162static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5163{
5164 struct io_ring_ctx *ctx = req->ctx;
5165
Jens Axboed4e7cd32020-08-15 11:44:50 -07005166 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005167 req->poll.done = true;
5168 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5169 io_commit_cqring(ctx);
5170}
5171
Jens Axboe18bceab2020-05-15 11:56:54 -06005172static void io_poll_task_func(struct callback_head *cb)
5173{
5174 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005175 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005176 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005177
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005178 if (io_poll_rewait(req, &req->poll)) {
5179 spin_unlock_irq(&ctx->completion_lock);
5180 } else {
5181 hash_del(&req->hash_node);
5182 io_poll_complete(req, req->result, 0);
5183 spin_unlock_irq(&ctx->completion_lock);
5184
5185 nxt = io_put_req_find_next(req);
5186 io_cqring_ev_posted(ctx);
5187 if (nxt)
5188 __io_req_task_submit(nxt);
5189 }
5190
Jens Axboe6d816e02020-08-11 08:04:14 -06005191 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005192}
5193
5194static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5195 int sync, void *key)
5196{
5197 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005198 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005199 __poll_t mask = key_to_poll(key);
5200
5201 /* for instances that support it check for an event match first: */
5202 if (mask && !(mask & poll->events))
5203 return 0;
5204
Jens Axboe8706e042020-09-28 08:38:54 -06005205 list_del_init(&wait->entry);
5206
Jens Axboe807abcb2020-07-17 17:09:27 -06005207 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005208 bool done;
5209
Jens Axboe807abcb2020-07-17 17:09:27 -06005210 spin_lock(&poll->head->lock);
5211 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005212 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005213 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005214 /* make sure double remove sees this as being gone */
5215 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005216 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005217 if (!done) {
5218 /* use wait func handler, so it matches the rq type */
5219 poll->wait.func(&poll->wait, mode, sync, key);
5220 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005221 }
5222 refcount_dec(&req->refs);
5223 return 1;
5224}
5225
5226static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5227 wait_queue_func_t wake_func)
5228{
5229 poll->head = NULL;
5230 poll->done = false;
5231 poll->canceled = false;
5232 poll->events = events;
5233 INIT_LIST_HEAD(&poll->wait.entry);
5234 init_waitqueue_func_entry(&poll->wait, wake_func);
5235}
5236
5237static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005238 struct wait_queue_head *head,
5239 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005240{
5241 struct io_kiocb *req = pt->req;
5242
5243 /*
5244 * If poll->head is already set, it's because the file being polled
5245 * uses multiple waitqueues for poll handling (eg one for read, one
5246 * for write). Setup a separate io_poll_iocb if this happens.
5247 */
5248 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005249 struct io_poll_iocb *poll_one = poll;
5250
Jens Axboe18bceab2020-05-15 11:56:54 -06005251 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005252 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005253 pt->error = -EINVAL;
5254 return;
5255 }
5256 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5257 if (!poll) {
5258 pt->error = -ENOMEM;
5259 return;
5260 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005261 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005262 refcount_inc(&req->refs);
5263 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005264 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005265 }
5266
5267 pt->error = 0;
5268 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005269
5270 if (poll->events & EPOLLEXCLUSIVE)
5271 add_wait_queue_exclusive(head, &poll->wait);
5272 else
5273 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005274}
5275
5276static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5277 struct poll_table_struct *p)
5278{
5279 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005280 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005281
Jens Axboe807abcb2020-07-17 17:09:27 -06005282 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005283}
5284
Jens Axboed7718a92020-02-14 22:23:12 -07005285static void io_async_task_func(struct callback_head *cb)
5286{
5287 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5288 struct async_poll *apoll = req->apoll;
5289 struct io_ring_ctx *ctx = req->ctx;
5290
5291 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5292
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005293 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005294 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005295 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005296 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005297 }
5298
Jens Axboe31067252020-05-17 17:43:31 -06005299 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005300 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005301 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005302
Jens Axboed4e7cd32020-08-15 11:44:50 -07005303 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005304 spin_unlock_irq(&ctx->completion_lock);
5305
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005306 if (!READ_ONCE(apoll->poll.canceled))
5307 __io_req_task_submit(req);
5308 else
5309 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005310
Jens Axboe6d816e02020-08-11 08:04:14 -06005311 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005312 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005313 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005314}
5315
5316static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5317 void *key)
5318{
5319 struct io_kiocb *req = wait->private;
5320 struct io_poll_iocb *poll = &req->apoll->poll;
5321
5322 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5323 key_to_poll(key));
5324
5325 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5326}
5327
5328static void io_poll_req_insert(struct io_kiocb *req)
5329{
5330 struct io_ring_ctx *ctx = req->ctx;
5331 struct hlist_head *list;
5332
5333 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5334 hlist_add_head(&req->hash_node, list);
5335}
5336
5337static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5338 struct io_poll_iocb *poll,
5339 struct io_poll_table *ipt, __poll_t mask,
5340 wait_queue_func_t wake_func)
5341 __acquires(&ctx->completion_lock)
5342{
5343 struct io_ring_ctx *ctx = req->ctx;
5344 bool cancel = false;
5345
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005346 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005347 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005348 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005349 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005350
5351 ipt->pt._key = mask;
5352 ipt->req = req;
5353 ipt->error = -EINVAL;
5354
Jens Axboed7718a92020-02-14 22:23:12 -07005355 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5356
5357 spin_lock_irq(&ctx->completion_lock);
5358 if (likely(poll->head)) {
5359 spin_lock(&poll->head->lock);
5360 if (unlikely(list_empty(&poll->wait.entry))) {
5361 if (ipt->error)
5362 cancel = true;
5363 ipt->error = 0;
5364 mask = 0;
5365 }
5366 if (mask || ipt->error)
5367 list_del_init(&poll->wait.entry);
5368 else if (cancel)
5369 WRITE_ONCE(poll->canceled, true);
5370 else if (!poll->done) /* actually waiting for an event */
5371 io_poll_req_insert(req);
5372 spin_unlock(&poll->head->lock);
5373 }
5374
5375 return mask;
5376}
5377
5378static bool io_arm_poll_handler(struct io_kiocb *req)
5379{
5380 const struct io_op_def *def = &io_op_defs[req->opcode];
5381 struct io_ring_ctx *ctx = req->ctx;
5382 struct async_poll *apoll;
5383 struct io_poll_table ipt;
5384 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005385 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005386
5387 if (!req->file || !file_can_poll(req->file))
5388 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005389 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005390 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005391 if (def->pollin)
5392 rw = READ;
5393 else if (def->pollout)
5394 rw = WRITE;
5395 else
5396 return false;
5397 /* if we can't nonblock try, then no point in arming a poll handler */
5398 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005399 return false;
5400
5401 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5402 if (unlikely(!apoll))
5403 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005404 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005405
5406 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005407 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005408
Nathan Chancellor8755d972020-03-02 16:01:19 -07005409 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005410 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005411 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005412 if (def->pollout)
5413 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005414
5415 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5416 if ((req->opcode == IORING_OP_RECVMSG) &&
5417 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5418 mask &= ~POLLIN;
5419
Jens Axboed7718a92020-02-14 22:23:12 -07005420 mask |= POLLERR | POLLPRI;
5421
5422 ipt.pt._qproc = io_async_queue_proc;
5423
5424 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5425 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005426 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005427 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005428 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005429 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005430 kfree(apoll);
5431 return false;
5432 }
5433 spin_unlock_irq(&ctx->completion_lock);
5434 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5435 apoll->poll.events);
5436 return true;
5437}
5438
5439static bool __io_poll_remove_one(struct io_kiocb *req,
5440 struct io_poll_iocb *poll)
5441{
Jens Axboeb41e9852020-02-17 09:52:41 -07005442 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005443
5444 spin_lock(&poll->head->lock);
5445 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005446 if (!list_empty(&poll->wait.entry)) {
5447 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005448 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005449 }
5450 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005451 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005452 return do_complete;
5453}
5454
5455static bool io_poll_remove_one(struct io_kiocb *req)
5456{
5457 bool do_complete;
5458
Jens Axboed4e7cd32020-08-15 11:44:50 -07005459 io_poll_remove_double(req);
5460
Jens Axboed7718a92020-02-14 22:23:12 -07005461 if (req->opcode == IORING_OP_POLL_ADD) {
5462 do_complete = __io_poll_remove_one(req, &req->poll);
5463 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005464 struct async_poll *apoll = req->apoll;
5465
Jens Axboed7718a92020-02-14 22:23:12 -07005466 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005467 do_complete = __io_poll_remove_one(req, &apoll->poll);
5468 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005469 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005470 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005471 kfree(apoll);
5472 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005473 }
5474
Jens Axboeb41e9852020-02-17 09:52:41 -07005475 if (do_complete) {
5476 io_cqring_fill_event(req, -ECANCELED);
5477 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005478 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005479 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005480 }
5481
5482 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005483}
5484
Jens Axboe76e1b642020-09-26 15:05:03 -06005485/*
5486 * Returns true if we found and killed one or more poll requests
5487 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005488static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5489 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005490{
Jens Axboe78076bb2019-12-04 19:56:40 -07005491 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005492 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005493 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005494
5495 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005496 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5497 struct hlist_head *list;
5498
5499 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005500 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005501 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005502 posted += io_poll_remove_one(req);
5503 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005504 }
5505 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005506
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005507 if (posted)
5508 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005509
5510 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005511}
5512
Jens Axboe47f46762019-11-09 17:43:02 -07005513static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5514{
Jens Axboe78076bb2019-12-04 19:56:40 -07005515 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005516 struct io_kiocb *req;
5517
Jens Axboe78076bb2019-12-04 19:56:40 -07005518 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5519 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005520 if (sqe_addr != req->user_data)
5521 continue;
5522 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005523 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005524 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005525 }
5526
5527 return -ENOENT;
5528}
5529
Jens Axboe3529d8c2019-12-19 18:24:38 -07005530static int io_poll_remove_prep(struct io_kiocb *req,
5531 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005532{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005533 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5534 return -EINVAL;
5535 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5536 sqe->poll_events)
5537 return -EINVAL;
5538
Pavel Begunkov018043b2020-10-27 23:17:18 +00005539 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005540 return 0;
5541}
5542
5543/*
5544 * Find a running poll command that matches one specified in sqe->addr,
5545 * and remove it if found.
5546 */
5547static int io_poll_remove(struct io_kiocb *req)
5548{
5549 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005550 int ret;
5551
Jens Axboe221c5eb2019-01-17 09:41:58 -07005552 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005553 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005554 spin_unlock_irq(&ctx->completion_lock);
5555
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005556 if (ret < 0)
5557 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005558 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005559 return 0;
5560}
5561
Jens Axboe221c5eb2019-01-17 09:41:58 -07005562static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5563 void *key)
5564{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005565 struct io_kiocb *req = wait->private;
5566 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005567
Jens Axboed7718a92020-02-14 22:23:12 -07005568 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005569}
5570
Jens Axboe221c5eb2019-01-17 09:41:58 -07005571static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5572 struct poll_table_struct *p)
5573{
5574 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5575
Jens Axboee8c2bc12020-08-15 18:44:09 -07005576 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005577}
5578
Jens Axboe3529d8c2019-12-19 18:24:38 -07005579static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005580{
5581 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005582 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005583
5584 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5585 return -EINVAL;
5586 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5587 return -EINVAL;
5588
Jiufei Xue5769a352020-06-17 17:53:55 +08005589 events = READ_ONCE(sqe->poll32_events);
5590#ifdef __BIG_ENDIAN
5591 events = swahw32(events);
5592#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005593 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5594 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005595 return 0;
5596}
5597
Pavel Begunkov014db002020-03-03 21:33:12 +03005598static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005599{
5600 struct io_poll_iocb *poll = &req->poll;
5601 struct io_ring_ctx *ctx = req->ctx;
5602 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005603 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005604
Jens Axboed7718a92020-02-14 22:23:12 -07005605 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005606
Jens Axboed7718a92020-02-14 22:23:12 -07005607 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5608 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005609
Jens Axboe8c838782019-03-12 15:48:16 -06005610 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005611 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005612 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005613 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005614 spin_unlock_irq(&ctx->completion_lock);
5615
Jens Axboe8c838782019-03-12 15:48:16 -06005616 if (mask) {
5617 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005618 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005619 }
Jens Axboe8c838782019-03-12 15:48:16 -06005620 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005621}
5622
Jens Axboe5262f562019-09-17 12:26:57 -06005623static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5624{
Jens Axboead8a48a2019-11-15 08:49:11 -07005625 struct io_timeout_data *data = container_of(timer,
5626 struct io_timeout_data, timer);
5627 struct io_kiocb *req = data->req;
5628 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005629 unsigned long flags;
5630
Jens Axboe5262f562019-09-17 12:26:57 -06005631 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005632 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005633 atomic_set(&req->ctx->cq_timeouts,
5634 atomic_read(&req->ctx->cq_timeouts) + 1);
5635
Jens Axboe78e19bb2019-11-06 15:21:34 -07005636 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005637 io_commit_cqring(ctx);
5638 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5639
5640 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005641 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005642 io_put_req(req);
5643 return HRTIMER_NORESTART;
5644}
5645
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005646static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5647 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005648{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005649 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005650 struct io_kiocb *req;
5651 int ret = -ENOENT;
5652
5653 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5654 if (user_data == req->user_data) {
5655 ret = 0;
5656 break;
5657 }
5658 }
5659
5660 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005661 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005662
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005663 io = req->async_data;
5664 ret = hrtimer_try_to_cancel(&io->timer);
5665 if (ret == -1)
5666 return ERR_PTR(-EALREADY);
5667 list_del_init(&req->timeout.list);
5668 return req;
5669}
5670
5671static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5672{
5673 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5674
5675 if (IS_ERR(req))
5676 return PTR_ERR(req);
5677
5678 req_set_fail_links(req);
5679 io_cqring_fill_event(req, -ECANCELED);
5680 io_put_req_deferred(req, 1);
5681 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005682}
5683
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005684static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5685 struct timespec64 *ts, enum hrtimer_mode mode)
5686{
5687 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5688 struct io_timeout_data *data;
5689
5690 if (IS_ERR(req))
5691 return PTR_ERR(req);
5692
5693 req->timeout.off = 0; /* noseq */
5694 data = req->async_data;
5695 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5696 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5697 data->timer.function = io_timeout_fn;
5698 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5699 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005700}
5701
Jens Axboe3529d8c2019-12-19 18:24:38 -07005702static int io_timeout_remove_prep(struct io_kiocb *req,
5703 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005704{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005705 struct io_timeout_rem *tr = &req->timeout_rem;
5706
Jens Axboeb29472e2019-12-17 18:50:29 -07005707 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5708 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005709 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5710 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005711 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005712 return -EINVAL;
5713
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005714 tr->addr = READ_ONCE(sqe->addr);
5715 tr->flags = READ_ONCE(sqe->timeout_flags);
5716 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5717 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5718 return -EINVAL;
5719 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5720 return -EFAULT;
5721 } else if (tr->flags) {
5722 /* timeout removal doesn't support flags */
5723 return -EINVAL;
5724 }
5725
Jens Axboeb29472e2019-12-17 18:50:29 -07005726 return 0;
5727}
5728
Jens Axboe11365042019-10-16 09:08:32 -06005729/*
5730 * Remove or update an existing timeout command
5731 */
Jens Axboefc4df992019-12-10 14:38:45 -07005732static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005733{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005734 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005735 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005736 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005737
Jens Axboe11365042019-10-16 09:08:32 -06005738 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005739 if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) {
5740 enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS)
5741 ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
5742
5743 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
5744 } else {
5745 ret = io_timeout_cancel(ctx, tr->addr);
5746 }
Jens Axboe11365042019-10-16 09:08:32 -06005747
Jens Axboe47f46762019-11-09 17:43:02 -07005748 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005749 io_commit_cqring(ctx);
5750 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005751 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005752 if (ret < 0)
5753 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005754 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005755 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005756}
5757
Jens Axboe3529d8c2019-12-19 18:24:38 -07005758static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005759 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005760{
Jens Axboead8a48a2019-11-15 08:49:11 -07005761 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005762 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005763 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005764
Jens Axboead8a48a2019-11-15 08:49:11 -07005765 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005766 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005767 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005768 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005769 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005770 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005771 flags = READ_ONCE(sqe->timeout_flags);
5772 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005773 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005774
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005775 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005776
Jens Axboee8c2bc12020-08-15 18:44:09 -07005777 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005778 return -ENOMEM;
5779
Jens Axboee8c2bc12020-08-15 18:44:09 -07005780 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005781 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005782
5783 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005784 return -EFAULT;
5785
Jens Axboe11365042019-10-16 09:08:32 -06005786 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005787 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005788 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005789 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005790
Jens Axboead8a48a2019-11-15 08:49:11 -07005791 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5792 return 0;
5793}
5794
Jens Axboefc4df992019-12-10 14:38:45 -07005795static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005796{
Jens Axboead8a48a2019-11-15 08:49:11 -07005797 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005798 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005799 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005800 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005801
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005802 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005803
Jens Axboe5262f562019-09-17 12:26:57 -06005804 /*
5805 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005806 * timeout event to be satisfied. If it isn't set, then this is
5807 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005808 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005809 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005810 entry = ctx->timeout_list.prev;
5811 goto add;
5812 }
Jens Axboe5262f562019-09-17 12:26:57 -06005813
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005814 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5815 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005816
5817 /*
5818 * Insertion sort, ensuring the first entry in the list is always
5819 * the one we need first.
5820 */
Jens Axboe5262f562019-09-17 12:26:57 -06005821 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005822 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5823 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005824
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005825 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005826 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005827 /* nxt.seq is behind @tail, otherwise would've been completed */
5828 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005829 break;
5830 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005831add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005832 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005833 data->timer.function = io_timeout_fn;
5834 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005835 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005836 return 0;
5837}
5838
Jens Axboe62755e32019-10-28 21:49:21 -06005839static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005840{
Jens Axboe62755e32019-10-28 21:49:21 -06005841 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005842
Jens Axboe62755e32019-10-28 21:49:21 -06005843 return req->user_data == (unsigned long) data;
5844}
5845
Jens Axboee977d6d2019-11-05 12:39:45 -07005846static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005847{
Jens Axboe62755e32019-10-28 21:49:21 -06005848 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005849 int ret = 0;
5850
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005851 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005852 switch (cancel_ret) {
5853 case IO_WQ_CANCEL_OK:
5854 ret = 0;
5855 break;
5856 case IO_WQ_CANCEL_RUNNING:
5857 ret = -EALREADY;
5858 break;
5859 case IO_WQ_CANCEL_NOTFOUND:
5860 ret = -ENOENT;
5861 break;
5862 }
5863
Jens Axboee977d6d2019-11-05 12:39:45 -07005864 return ret;
5865}
5866
Jens Axboe47f46762019-11-09 17:43:02 -07005867static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5868 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005869 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005870{
5871 unsigned long flags;
5872 int ret;
5873
5874 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5875 if (ret != -ENOENT) {
5876 spin_lock_irqsave(&ctx->completion_lock, flags);
5877 goto done;
5878 }
5879
5880 spin_lock_irqsave(&ctx->completion_lock, flags);
5881 ret = io_timeout_cancel(ctx, sqe_addr);
5882 if (ret != -ENOENT)
5883 goto done;
5884 ret = io_poll_cancel(ctx, sqe_addr);
5885done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005886 if (!ret)
5887 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005888 io_cqring_fill_event(req, ret);
5889 io_commit_cqring(ctx);
5890 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5891 io_cqring_ev_posted(ctx);
5892
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005893 if (ret < 0)
5894 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005895 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005896}
5897
Jens Axboe3529d8c2019-12-19 18:24:38 -07005898static int io_async_cancel_prep(struct io_kiocb *req,
5899 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005900{
Jens Axboefbf23842019-12-17 18:45:56 -07005901 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005902 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005903 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5904 return -EINVAL;
5905 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005906 return -EINVAL;
5907
Jens Axboefbf23842019-12-17 18:45:56 -07005908 req->cancel.addr = READ_ONCE(sqe->addr);
5909 return 0;
5910}
5911
Pavel Begunkov014db002020-03-03 21:33:12 +03005912static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005913{
5914 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005915
Pavel Begunkov014db002020-03-03 21:33:12 +03005916 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005917 return 0;
5918}
5919
Jens Axboe05f3fb32019-12-09 11:22:50 -07005920static int io_files_update_prep(struct io_kiocb *req,
5921 const struct io_uring_sqe *sqe)
5922{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005923 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5924 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005925 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5926 return -EINVAL;
5927 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005928 return -EINVAL;
5929
5930 req->files_update.offset = READ_ONCE(sqe->off);
5931 req->files_update.nr_args = READ_ONCE(sqe->len);
5932 if (!req->files_update.nr_args)
5933 return -EINVAL;
5934 req->files_update.arg = READ_ONCE(sqe->addr);
5935 return 0;
5936}
5937
Jens Axboe229a7b62020-06-22 10:13:11 -06005938static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5939 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005940{
5941 struct io_ring_ctx *ctx = req->ctx;
5942 struct io_uring_files_update up;
5943 int ret;
5944
Jens Axboef86cd202020-01-29 13:46:44 -07005945 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005946 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005947
5948 up.offset = req->files_update.offset;
5949 up.fds = req->files_update.arg;
5950
5951 mutex_lock(&ctx->uring_lock);
5952 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5953 mutex_unlock(&ctx->uring_lock);
5954
5955 if (ret < 0)
5956 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005957 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005958 return 0;
5959}
5960
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005961static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005962{
Jens Axboed625c6e2019-12-17 19:53:05 -07005963 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005964 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005965 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005966 case IORING_OP_READV:
5967 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005968 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005969 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005970 case IORING_OP_WRITEV:
5971 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005972 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005973 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005974 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005975 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005976 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005977 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005978 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005979 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005980 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005981 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005982 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005983 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005984 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005985 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005986 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005987 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005988 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005989 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005990 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005991 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005992 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005993 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005994 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005995 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005996 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005997 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005998 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005999 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006000 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006001 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006002 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006003 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006004 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006005 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006006 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006007 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006008 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006009 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006010 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006011 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006012 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006013 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006014 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006015 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006016 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006017 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006018 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006019 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006020 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006021 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006022 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006023 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006024 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006025 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006026 case IORING_OP_SHUTDOWN:
6027 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006028 case IORING_OP_RENAMEAT:
6029 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006030 case IORING_OP_UNLINKAT:
6031 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006032 }
6033
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6035 req->opcode);
6036 return-EINVAL;
6037}
6038
Jens Axboedef596e2019-01-09 08:59:42 -07006039static int io_req_defer_prep(struct io_kiocb *req,
6040 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006041{
Jens Axboedef596e2019-01-09 08:59:42 -07006042 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006044 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006045 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006047}
6048
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006049static u32 io_get_sequence(struct io_kiocb *req)
6050{
6051 struct io_kiocb *pos;
6052 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006053 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006054
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006055 io_for_each_link(pos, req)
6056 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006057
6058 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6059 return total_submitted - nr_reqs;
6060}
6061
Jens Axboe3529d8c2019-12-19 18:24:38 -07006062static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063{
6064 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006065 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006067 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068
6069 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006070 if (likely(list_empty_careful(&ctx->defer_list) &&
6071 !(req->flags & REQ_F_IO_DRAIN)))
6072 return 0;
6073
6074 seq = io_get_sequence(req);
6075 /* Still a chance to pass the sequence check */
6076 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006077 return 0;
6078
Jens Axboee8c2bc12020-08-15 18:44:09 -07006079 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006080 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006081 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006082 return ret;
6083 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006084 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006085 de = kmalloc(sizeof(*de), GFP_KERNEL);
6086 if (!de)
6087 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006088
6089 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006090 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006091 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006092 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006093 io_queue_async_work(req);
6094 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006095 }
6096
6097 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006098 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006099 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006100 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006101 spin_unlock_irq(&ctx->completion_lock);
6102 return -EIOCBQUEUED;
6103}
Jens Axboeedafcce2019-01-09 09:16:05 -07006104
Jens Axboef573d382020-09-22 10:19:24 -06006105static void io_req_drop_files(struct io_kiocb *req)
6106{
6107 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc98de082020-11-15 12:56:32 +00006108 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06006109 unsigned long flags;
6110
Jens Axboe98447d62020-10-14 10:48:51 -06006111 put_files_struct(req->work.identity->files);
6112 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006113 spin_lock_irqsave(&ctx->inflight_lock, flags);
6114 list_del(&req->inflight_entry);
6115 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
6116 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06006117 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006118 if (atomic_read(&tctx->in_idle))
6119 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06006120}
6121
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006122static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006123{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006124 if (req->flags & REQ_F_BUFFER_SELECTED) {
6125 switch (req->opcode) {
6126 case IORING_OP_READV:
6127 case IORING_OP_READ_FIXED:
6128 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006129 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006130 break;
6131 case IORING_OP_RECVMSG:
6132 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006133 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006134 break;
6135 }
6136 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006137 }
6138
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006139 if (req->flags & REQ_F_NEED_CLEANUP) {
6140 switch (req->opcode) {
6141 case IORING_OP_READV:
6142 case IORING_OP_READ_FIXED:
6143 case IORING_OP_READ:
6144 case IORING_OP_WRITEV:
6145 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006146 case IORING_OP_WRITE: {
6147 struct io_async_rw *io = req->async_data;
6148 if (io->free_iovec)
6149 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006150 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006151 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006152 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006153 case IORING_OP_SENDMSG: {
6154 struct io_async_msghdr *io = req->async_data;
6155 if (io->iov != io->fast_iov)
6156 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006157 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006158 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006159 case IORING_OP_SPLICE:
6160 case IORING_OP_TEE:
6161 io_put_file(req, req->splice.file_in,
6162 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6163 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006164 case IORING_OP_OPENAT:
6165 case IORING_OP_OPENAT2:
6166 if (req->open.filename)
6167 putname(req->open.filename);
6168 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006169 case IORING_OP_RENAMEAT:
6170 putname(req->rename.oldpath);
6171 putname(req->rename.newpath);
6172 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006173 case IORING_OP_UNLINKAT:
6174 putname(req->unlink.filename);
6175 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006176 }
6177 req->flags &= ~REQ_F_NEED_CLEANUP;
6178 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03006179
Jens Axboef573d382020-09-22 10:19:24 -06006180 if (req->flags & REQ_F_INFLIGHT)
6181 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006182}
6183
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006184static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6185 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006186{
Jens Axboeedafcce2019-01-09 09:16:05 -07006187 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006188 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006189
Jens Axboed625c6e2019-12-17 19:53:05 -07006190 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006192 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006193 break;
6194 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006195 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006196 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006197 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198 break;
6199 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006200 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006201 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006202 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203 break;
6204 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006205 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206 break;
6207 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006208 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 break;
6210 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006211 ret = io_poll_remove(req);
6212 break;
6213 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006214 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006215 break;
6216 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006217 ret = io_sendmsg(req, force_nonblock, cs);
6218 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006219 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006220 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006221 break;
6222 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006223 ret = io_recvmsg(req, force_nonblock, cs);
6224 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006225 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006226 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006227 break;
6228 case IORING_OP_TIMEOUT:
6229 ret = io_timeout(req);
6230 break;
6231 case IORING_OP_TIMEOUT_REMOVE:
6232 ret = io_timeout_remove(req);
6233 break;
6234 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006235 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006236 break;
6237 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006238 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006239 break;
6240 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006241 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006242 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006243 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006244 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006245 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006246 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006247 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006248 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006249 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006250 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006251 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006252 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006253 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006254 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006255 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006256 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006257 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006258 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006259 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006260 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006261 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006262 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006263 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006264 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006265 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006266 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006267 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006268 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006269 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006270 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006271 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006272 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006273 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006274 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006275 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006276 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006277 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006278 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006279 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006280 ret = io_tee(req, force_nonblock);
6281 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006282 case IORING_OP_SHUTDOWN:
6283 ret = io_shutdown(req, force_nonblock);
6284 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006285 case IORING_OP_RENAMEAT:
6286 ret = io_renameat(req, force_nonblock);
6287 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006288 case IORING_OP_UNLINKAT:
6289 ret = io_unlinkat(req, force_nonblock);
6290 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291 default:
6292 ret = -EINVAL;
6293 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006294 }
6295
6296 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006297 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006298
Jens Axboeb5325762020-05-19 21:20:27 -06006299 /* If the op doesn't have a file, we're not polling for it */
6300 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006301 const bool in_async = io_wq_current_is_worker();
6302
Jens Axboe11ba8202020-01-15 21:51:17 -07006303 /* workqueue context doesn't hold uring_lock, grab it now */
6304 if (in_async)
6305 mutex_lock(&ctx->uring_lock);
6306
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006307 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006308
6309 if (in_async)
6310 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311 }
6312
6313 return 0;
6314}
6315
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006316static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006317{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006318 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006319 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006320 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006322 timeout = io_prep_linked_timeout(req);
6323 if (timeout)
6324 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006325
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006326 /* if NO_CANCEL is set, we must still run the work */
6327 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6328 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006329 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006330 }
Jens Axboe31b51512019-01-18 22:56:34 -07006331
Jens Axboe561fb042019-10-24 07:25:42 -06006332 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006333 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006334 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006335 /*
6336 * We can get EAGAIN for polled IO even though we're
6337 * forcing a sync submission from here, since we can't
6338 * wait for request slots on the block side.
6339 */
6340 if (ret != -EAGAIN)
6341 break;
6342 cond_resched();
6343 } while (1);
6344 }
Jens Axboe31b51512019-01-18 22:56:34 -07006345
Jens Axboe561fb042019-10-24 07:25:42 -06006346 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006347 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006348
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006349 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6350 lock_ctx = req->ctx;
6351
6352 /*
6353 * io_iopoll_complete() does not hold completion_lock to
6354 * complete polled io, so here for polled io, we can not call
6355 * io_req_complete() directly, otherwise there maybe concurrent
6356 * access to cqring, defer_list, etc, which is not safe. Given
6357 * that io_iopoll_complete() is always called under uring_lock,
6358 * so here for polled io, we also get uring_lock to complete
6359 * it.
6360 */
6361 if (lock_ctx)
6362 mutex_lock(&lock_ctx->uring_lock);
6363
6364 req_set_fail_links(req);
6365 io_req_complete(req, ret);
6366
6367 if (lock_ctx)
6368 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006369 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006370
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006371 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006372}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373
Jens Axboe65e19f52019-10-26 07:20:21 -06006374static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6375 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006376{
Jens Axboe65e19f52019-10-26 07:20:21 -06006377 struct fixed_file_table *table;
6378
Jens Axboe05f3fb32019-12-09 11:22:50 -07006379 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006380 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006381}
6382
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006383static struct file *io_file_get(struct io_submit_state *state,
6384 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006385{
6386 struct io_ring_ctx *ctx = req->ctx;
6387 struct file *file;
6388
6389 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006390 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006391 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006392 fd = array_index_nospec(fd, ctx->nr_user_files);
6393 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006394 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006395 } else {
6396 trace_io_uring_file_get(ctx, fd);
6397 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006398 }
6399
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006400 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006401}
6402
Jens Axboe2665abf2019-11-05 12:40:47 -07006403static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6404{
Jens Axboead8a48a2019-11-15 08:49:11 -07006405 struct io_timeout_data *data = container_of(timer,
6406 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006407 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006408 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006409 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006410
6411 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006412 prev = req->timeout.head;
6413 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006414
6415 /*
6416 * We don't expect the list to be empty, that will only happen if we
6417 * race with the completion of the linked work.
6418 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006419 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006420 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006421 else
6422 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006423 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6424
6425 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006426 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006427 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006428 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006429 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006430 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006431 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006432 return HRTIMER_NORESTART;
6433}
6434
Jens Axboe7271ef32020-08-10 09:55:22 -06006435static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006436{
Jens Axboe76a46e02019-11-10 23:34:16 -07006437 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006438 * If the back reference is NULL, then our linked request finished
6439 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006440 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006441 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006442 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006443
Jens Axboead8a48a2019-11-15 08:49:11 -07006444 data->timer.function = io_link_timeout_fn;
6445 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6446 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006447 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006448}
6449
6450static void io_queue_linked_timeout(struct io_kiocb *req)
6451{
6452 struct io_ring_ctx *ctx = req->ctx;
6453
6454 spin_lock_irq(&ctx->completion_lock);
6455 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006456 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006457
Jens Axboe2665abf2019-11-05 12:40:47 -07006458 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006459 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006460}
6461
Jens Axboead8a48a2019-11-15 08:49:11 -07006462static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006463{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006464 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006465
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006466 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6467 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006468 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006469
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006470 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006471 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006472 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006473 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006474}
6475
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006476static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006477{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006478 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006479 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006480 int ret;
6481
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006482again:
6483 linked_timeout = io_prep_linked_timeout(req);
6484
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006485 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6486 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006487 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006488 if (old_creds)
6489 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006490 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006491 old_creds = NULL; /* restored original creds */
6492 else
Jens Axboe98447d62020-10-14 10:48:51 -06006493 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006494 }
6495
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006496 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006497
6498 /*
6499 * We async punt it if the file wasn't marked NOWAIT, or if the file
6500 * doesn't support non-blocking read/write attempts
6501 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006502 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006503 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006504 /*
6505 * Queued up for async execution, worker will release
6506 * submit reference when the iocb is actually submitted.
6507 */
6508 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006509 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006510
Pavel Begunkovf063c542020-07-25 14:41:59 +03006511 if (linked_timeout)
6512 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006513 } else if (likely(!ret)) {
6514 /* drop submission reference */
6515 req = io_put_req_find_next(req);
6516 if (linked_timeout)
6517 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006518
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006519 if (req) {
6520 if (!(req->flags & REQ_F_FORCE_ASYNC))
6521 goto again;
6522 io_queue_async_work(req);
6523 }
6524 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006525 /* un-prep timeout, so it'll be killed as any other linked */
6526 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006527 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006528 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006529 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006530 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006531
Jens Axboe193155c2020-02-22 23:22:19 -07006532 if (old_creds)
6533 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006534}
6535
Jens Axboef13fad72020-06-22 09:34:30 -06006536static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6537 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006538{
6539 int ret;
6540
Jens Axboe3529d8c2019-12-19 18:24:38 -07006541 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006542 if (ret) {
6543 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006544fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006545 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006546 io_put_req(req);
6547 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006548 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006549 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006550 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006551 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006552 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006553 goto fail_req;
6554 }
Jens Axboece35a472019-12-17 08:04:44 -07006555 io_queue_async_work(req);
6556 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006557 if (sqe) {
6558 ret = io_req_prep(req, sqe);
6559 if (unlikely(ret))
6560 goto fail_req;
6561 }
6562 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006563 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006564}
6565
Jens Axboef13fad72020-06-22 09:34:30 -06006566static inline void io_queue_link_head(struct io_kiocb *req,
6567 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006568{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006569 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006570 io_put_req(req);
6571 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006572 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006573 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006574}
6575
Pavel Begunkov863e0562020-10-27 23:25:35 +00006576struct io_submit_link {
6577 struct io_kiocb *head;
6578 struct io_kiocb *last;
6579};
6580
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006581static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006582 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006583{
Jackie Liua197f662019-11-08 08:09:12 -07006584 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006585 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006586
Jens Axboe9e645e112019-05-10 16:07:28 -06006587 /*
6588 * If we already have a head request, queue this one for async
6589 * submittal once the head completes. If we don't have a head but
6590 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6591 * submitted sync once the chain is complete. If none of those
6592 * conditions are true (normal request), then just queue it.
6593 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006594 if (link->head) {
6595 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006596
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006597 /*
6598 * Taking sequential execution of a link, draining both sides
6599 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6600 * requests in the link. So, it drains the head and the
6601 * next after the link request. The last one is done via
6602 * drain_next flag to persist the effect across calls.
6603 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006604 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006605 head->flags |= REQ_F_IO_DRAIN;
6606 ctx->drain_next = 1;
6607 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006608 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006609 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006610 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006611 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006612 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006613 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006614 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006615 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006616 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006617
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006618 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006619 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006620 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006621 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006622 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006623 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006624 if (unlikely(ctx->drain_next)) {
6625 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006626 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006627 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006628 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006629 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006630 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006631 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006632 link->head = req;
6633 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006634 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006635 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006636 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006637 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006638
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006639 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006640}
6641
Jens Axboe9a56a232019-01-09 09:06:50 -07006642/*
6643 * Batched submission is done, ensure local IO is flushed out.
6644 */
6645static void io_submit_state_end(struct io_submit_state *state)
6646{
Jens Axboef13fad72020-06-22 09:34:30 -06006647 if (!list_empty(&state->comp.list))
6648 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006649 if (state->plug_started)
6650 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006651 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006652 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006653 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006654}
6655
6656/*
6657 * Start submission side cache.
6658 */
6659static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006660 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006661{
Jens Axboe27926b62020-10-28 09:33:23 -06006662 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006663 state->comp.nr = 0;
6664 INIT_LIST_HEAD(&state->comp.list);
6665 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006666 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006667 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006668 state->ios_left = max_ios;
6669}
6670
Jens Axboe2b188cc2019-01-07 10:46:33 -07006671static void io_commit_sqring(struct io_ring_ctx *ctx)
6672{
Hristo Venev75b28af2019-08-26 17:23:46 +00006673 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006674
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006675 /*
6676 * Ensure any loads from the SQEs are done at this point,
6677 * since once we write the new head, the application could
6678 * write new data to them.
6679 */
6680 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006681}
6682
6683/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006684 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006685 * that is mapped by userspace. This means that care needs to be taken to
6686 * ensure that reads are stable, as we cannot rely on userspace always
6687 * being a good citizen. If members of the sqe are validated and then later
6688 * used, it's important that those reads are done through READ_ONCE() to
6689 * prevent a re-load down the line.
6690 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006691static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006692{
Hristo Venev75b28af2019-08-26 17:23:46 +00006693 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006694 unsigned head;
6695
6696 /*
6697 * The cached sq head (or cq tail) serves two purposes:
6698 *
6699 * 1) allows us to batch the cost of updating the user visible
6700 * head updates.
6701 * 2) allows the kernel side to track the head on its own, even
6702 * though the application is the one updating it.
6703 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006704 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006705 if (likely(head < ctx->sq_entries))
6706 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006707
6708 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006709 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006710 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006711 return NULL;
6712}
6713
6714static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6715{
6716 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006717}
6718
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006719/*
6720 * Check SQE restrictions (opcode and flags).
6721 *
6722 * Returns 'true' if SQE is allowed, 'false' otherwise.
6723 */
6724static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6725 struct io_kiocb *req,
6726 unsigned int sqe_flags)
6727{
6728 if (!ctx->restricted)
6729 return true;
6730
6731 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6732 return false;
6733
6734 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6735 ctx->restrictions.sqe_flags_required)
6736 return false;
6737
6738 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6739 ctx->restrictions.sqe_flags_required))
6740 return false;
6741
6742 return true;
6743}
6744
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006745#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6746 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6747 IOSQE_BUFFER_SELECT)
6748
6749static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6750 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006751 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006752{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006753 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006754 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006755
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006756 req->opcode = READ_ONCE(sqe->opcode);
6757 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006758 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006759 req->file = NULL;
6760 req->ctx = ctx;
6761 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006762 req->link = NULL;
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006763 req->fixed_file_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006764 /* one is dropped after submission, the other at completion */
6765 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006766 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006767 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006768
6769 if (unlikely(req->opcode >= IORING_OP_LAST))
6770 return -EINVAL;
6771
Jens Axboe28cea78a2020-09-14 10:51:17 -06006772 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006773 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006774
6775 sqe_flags = READ_ONCE(sqe->flags);
6776 /* enforce forwards compatibility on users */
6777 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6778 return -EINVAL;
6779
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006780 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6781 return -EACCES;
6782
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006783 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6784 !io_op_defs[req->opcode].buffer_select)
6785 return -EOPNOTSUPP;
6786
6787 id = READ_ONCE(sqe->personality);
6788 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006789 struct io_identity *iod;
6790
Jens Axboe1e6fa522020-10-15 08:46:24 -06006791 iod = idr_find(&ctx->personality_idr, id);
6792 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006793 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006794 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006795
6796 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006797 get_cred(iod->creds);
6798 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006799 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006800 }
6801
6802 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006803 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006804
Jens Axboe27926b62020-10-28 09:33:23 -06006805 /*
6806 * Plug now if we have more than 1 IO left after this, and the target
6807 * is potentially a read/write to block based storage.
6808 */
6809 if (!state->plug_started && state->ios_left > 1 &&
6810 io_op_defs[req->opcode].plug) {
6811 blk_start_plug(&state->plug);
6812 state->plug_started = true;
6813 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006814
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006815 ret = 0;
6816 if (io_op_defs[req->opcode].needs_file) {
6817 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006818
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006819 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
6820 if (unlikely(!req->file &&
6821 !io_op_defs[req->opcode].needs_file_no_error))
6822 ret = -EBADF;
6823 }
6824
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006825 state->ios_left--;
6826 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006827}
6828
Jens Axboe0f212202020-09-13 13:09:39 -06006829static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006830{
Jens Axboeac8691c2020-06-01 08:30:41 -06006831 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006832 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006833 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006834
Jens Axboec4a2ed72019-11-21 21:01:26 -07006835 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006836 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006837 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006838 return -EBUSY;
6839 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006840
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006841 /* make sure SQ entry isn't read before tail */
6842 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006843
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006844 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6845 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006846
Jens Axboed8a6df12020-10-15 16:24:45 -06006847 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006848 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006849
Jens Axboe6c271ce2019-01-10 11:22:30 -07006850 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006851 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006852
Jens Axboe6c271ce2019-01-10 11:22:30 -07006853 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006854 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006855 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006856 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006857
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006858 sqe = io_get_sqe(ctx);
6859 if (unlikely(!sqe)) {
6860 io_consume_sqe(ctx);
6861 break;
6862 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006863 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006864 if (unlikely(!req)) {
6865 if (!submitted)
6866 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006867 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006868 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006869 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006870 /* will complete beyond this point, count as submitted */
6871 submitted++;
6872
Pavel Begunkov692d8362020-10-10 18:34:13 +01006873 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006874 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006875fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006876 io_put_req(req);
6877 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006878 break;
6879 }
6880
Jens Axboe354420f2020-01-08 18:55:15 -07006881 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006882 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006883 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006884 if (err)
6885 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006886 }
6887
Pavel Begunkov9466f432020-01-25 22:34:01 +03006888 if (unlikely(submitted != nr)) {
6889 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006890 struct io_uring_task *tctx = current->io_uring;
6891 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006892
Jens Axboed8a6df12020-10-15 16:24:45 -06006893 percpu_ref_put_many(&ctx->refs, unused);
6894 percpu_counter_sub(&tctx->inflight, unused);
6895 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006896 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006897 if (link.head)
6898 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006899 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006900
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006901 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6902 io_commit_sqring(ctx);
6903
Jens Axboe6c271ce2019-01-10 11:22:30 -07006904 return submitted;
6905}
6906
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006907static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6908{
6909 /* Tell userspace we may need a wakeup call */
6910 spin_lock_irq(&ctx->completion_lock);
6911 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6912 spin_unlock_irq(&ctx->completion_lock);
6913}
6914
6915static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6916{
6917 spin_lock_irq(&ctx->completion_lock);
6918 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6919 spin_unlock_irq(&ctx->completion_lock);
6920}
6921
Xiaoguang Wang08369242020-11-03 14:15:59 +08006922static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006923{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006924 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006925 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006926
Jens Axboec8d1ba52020-09-14 11:07:26 -06006927 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006928 /* if we're handling multiple rings, cap submit size for fairness */
6929 if (cap_entries && to_submit > 8)
6930 to_submit = 8;
6931
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006932 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6933 unsigned nr_events = 0;
6934
Xiaoguang Wang08369242020-11-03 14:15:59 +08006935 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006936 if (!list_empty(&ctx->iopoll_list))
6937 io_do_iopoll(ctx, &nr_events, 0);
6938
6939 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006940 ret = io_submit_sqes(ctx, to_submit);
6941 mutex_unlock(&ctx->uring_lock);
6942 }
Jens Axboe90554202020-09-03 12:12:41 -06006943
6944 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6945 wake_up(&ctx->sqo_sq_wait);
6946
Xiaoguang Wang08369242020-11-03 14:15:59 +08006947 return ret;
6948}
6949
6950static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6951{
6952 struct io_ring_ctx *ctx;
6953 unsigned sq_thread_idle = 0;
6954
6955 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6956 if (sq_thread_idle < ctx->sq_thread_idle)
6957 sq_thread_idle = ctx->sq_thread_idle;
6958 }
6959
6960 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006961}
6962
Jens Axboe69fb2132020-09-14 11:16:23 -06006963static void io_sqd_init_new(struct io_sq_data *sqd)
6964{
6965 struct io_ring_ctx *ctx;
6966
6967 while (!list_empty(&sqd->ctx_new_list)) {
6968 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006969 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6970 complete(&ctx->sq_thread_comp);
6971 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006972
6973 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006974}
6975
Jens Axboe6c271ce2019-01-10 11:22:30 -07006976static int io_sq_thread(void *data)
6977{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006978 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006979 struct files_struct *old_files = current->files;
6980 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006981 const struct cred *old_cred = NULL;
6982 struct io_sq_data *sqd = data;
6983 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006984 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006985 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006986
Jens Axboe28cea78a2020-09-14 10:51:17 -06006987 task_lock(current);
6988 current->files = NULL;
6989 current->nsproxy = NULL;
6990 task_unlock(current);
6991
Jens Axboe69fb2132020-09-14 11:16:23 -06006992 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006993 int ret;
6994 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006995
6996 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006997 * Any changes to the sqd lists are synchronized through the
6998 * kthread parking. This synchronizes the thread vs users,
6999 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007000 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007001 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007002 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007003 /*
7004 * When sq thread is unparked, in case the previous park operation
7005 * comes from io_put_sq_data(), which means that sq thread is going
7006 * to be stopped, so here needs to have a check.
7007 */
7008 if (kthread_should_stop())
7009 break;
7010 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007011
Xiaoguang Wang08369242020-11-03 14:15:59 +08007012 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007013 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007014 timeout = jiffies + sqd->sq_thread_idle;
7015 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007016
Xiaoguang Wang08369242020-11-03 14:15:59 +08007017 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007018 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007019 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7020 if (current->cred != ctx->creds) {
7021 if (old_cred)
7022 revert_creds(old_cred);
7023 old_cred = override_creds(ctx->creds);
7024 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007025 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007026#ifdef CONFIG_AUDIT
7027 current->loginuid = ctx->loginuid;
7028 current->sessionid = ctx->sessionid;
7029#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007030
Xiaoguang Wang08369242020-11-03 14:15:59 +08007031 ret = __io_sq_thread(ctx, cap_entries);
7032 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7033 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007034
Jens Axboe28cea78a2020-09-14 10:51:17 -06007035 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007036 }
7037
Xiaoguang Wang08369242020-11-03 14:15:59 +08007038 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007039 io_run_task_work();
7040 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007041 if (sqt_spin)
7042 timeout = jiffies + sqd->sq_thread_idle;
7043 continue;
7044 }
7045
7046 if (kthread_should_park())
7047 continue;
7048
7049 needs_sched = true;
7050 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7051 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7052 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7053 !list_empty_careful(&ctx->iopoll_list)) {
7054 needs_sched = false;
7055 break;
7056 }
7057 if (io_sqring_entries(ctx)) {
7058 needs_sched = false;
7059 break;
7060 }
7061 }
7062
7063 if (needs_sched) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007064 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7065 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007066
Jens Axboe69fb2132020-09-14 11:16:23 -06007067 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007068 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7069 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007070 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007071
7072 finish_wait(&sqd->wait, &wait);
7073 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007074 }
7075
Jens Axboe4c6e2772020-07-01 11:29:10 -06007076 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07007077
Dennis Zhou91d8f512020-09-16 13:41:05 -07007078 if (cur_css)
7079 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007080 if (old_cred)
7081 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007082
Jens Axboe28cea78a2020-09-14 10:51:17 -06007083 task_lock(current);
7084 current->files = old_files;
7085 current->nsproxy = old_nsproxy;
7086 task_unlock(current);
7087
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007088 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007089
Jens Axboe6c271ce2019-01-10 11:22:30 -07007090 return 0;
7091}
7092
Jens Axboebda52162019-09-24 13:47:15 -06007093struct io_wait_queue {
7094 struct wait_queue_entry wq;
7095 struct io_ring_ctx *ctx;
7096 unsigned to_wait;
7097 unsigned nr_timeouts;
7098};
7099
Pavel Begunkov6c503152021-01-04 20:36:36 +00007100static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007101{
7102 struct io_ring_ctx *ctx = iowq->ctx;
7103
7104 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007105 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007106 * started waiting. For timeouts, we always want to return to userspace,
7107 * regardless of event count.
7108 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007109 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007110 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7111}
7112
7113static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7114 int wake_flags, void *key)
7115{
7116 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7117 wq);
7118
Pavel Begunkov6c503152021-01-04 20:36:36 +00007119 /*
7120 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7121 * the task, and the next invocation will do it.
7122 */
7123 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7124 return autoremove_wake_function(curr, mode, wake_flags, key);
7125 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007126}
7127
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007128static int io_run_task_work_sig(void)
7129{
7130 if (io_run_task_work())
7131 return 1;
7132 if (!signal_pending(current))
7133 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007134 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7135 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007136 return -EINTR;
7137}
7138
Jens Axboe2b188cc2019-01-07 10:46:33 -07007139/*
7140 * Wait until events become available, if we don't already have some. The
7141 * application must reap them itself, as they reside on the shared cq ring.
7142 */
7143static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007144 const sigset_t __user *sig, size_t sigsz,
7145 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007146{
Jens Axboebda52162019-09-24 13:47:15 -06007147 struct io_wait_queue iowq = {
7148 .wq = {
7149 .private = current,
7150 .func = io_wake_function,
7151 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7152 },
7153 .ctx = ctx,
7154 .to_wait = min_events,
7155 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007156 struct io_rings *rings = ctx->rings;
Hao Xuc73ebb62020-11-03 10:54:37 +08007157 struct timespec64 ts;
7158 signed long timeout = 0;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08007159 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007160
Jens Axboeb41e9852020-02-17 09:52:41 -07007161 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007162 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7163 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007164 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007165 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007166 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007167 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007168
7169 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007170#ifdef CONFIG_COMPAT
7171 if (in_compat_syscall())
7172 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007173 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007174 else
7175#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007176 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007177
Jens Axboe2b188cc2019-01-07 10:46:33 -07007178 if (ret)
7179 return ret;
7180 }
7181
Hao Xuc73ebb62020-11-03 10:54:37 +08007182 if (uts) {
7183 if (get_timespec64(&ts, uts))
7184 return -EFAULT;
7185 timeout = timespec64_to_jiffies(&ts);
7186 }
7187
Jens Axboebda52162019-09-24 13:47:15 -06007188 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007189 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007190 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007191 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007192 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7193 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007194 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007195 ret = io_run_task_work_sig();
7196 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06007197 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007198 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007199 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007200 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007201 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007202 if (test_bit(0, &ctx->cq_check_overflow))
7203 continue;
Hao Xuc73ebb62020-11-03 10:54:37 +08007204 if (uts) {
7205 timeout = schedule_timeout(timeout);
7206 if (timeout == 0) {
7207 ret = -ETIME;
7208 break;
7209 }
7210 } else {
7211 schedule();
7212 }
Jens Axboebda52162019-09-24 13:47:15 -06007213 } while (1);
7214 finish_wait(&ctx->wait, &iowq.wq);
7215
Jens Axboeb7db41c2020-07-04 08:55:50 -06007216 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007217
Hristo Venev75b28af2019-08-26 17:23:46 +00007218 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007219}
7220
Jens Axboe6b063142019-01-10 22:13:58 -07007221static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7222{
7223#if defined(CONFIG_UNIX)
7224 if (ctx->ring_sock) {
7225 struct sock *sock = ctx->ring_sock->sk;
7226 struct sk_buff *skb;
7227
7228 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7229 kfree_skb(skb);
7230 }
7231#else
7232 int i;
7233
Jens Axboe65e19f52019-10-26 07:20:21 -06007234 for (i = 0; i < ctx->nr_user_files; i++) {
7235 struct file *file;
7236
7237 file = io_file_from_index(ctx, i);
7238 if (file)
7239 fput(file);
7240 }
Jens Axboe6b063142019-01-10 22:13:58 -07007241#endif
7242}
7243
Jens Axboe05f3fb32019-12-09 11:22:50 -07007244static void io_file_ref_kill(struct percpu_ref *ref)
7245{
7246 struct fixed_file_data *data;
7247
7248 data = container_of(ref, struct fixed_file_data, refs);
7249 complete(&data->done);
7250}
7251
Pavel Begunkov1642b442020-12-30 21:34:14 +00007252static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7253 struct fixed_file_ref_node *ref_node)
7254{
7255 spin_lock_bh(&file_data->lock);
7256 file_data->node = ref_node;
7257 list_add_tail(&ref_node->node, &file_data->ref_list);
7258 spin_unlock_bh(&file_data->lock);
7259 percpu_ref_get(&file_data->refs);
7260}
7261
Jens Axboe6b063142019-01-10 22:13:58 -07007262static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7263{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007264 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007265 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007266 unsigned nr_tables, i;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007267 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007268
Jens Axboe05f3fb32019-12-09 11:22:50 -07007269 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007270 return -ENXIO;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007271 backup_node = alloc_fixed_file_ref_node(ctx);
7272 if (!backup_node)
7273 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007274
Jens Axboeac0648a2020-11-23 09:37:51 -07007275 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007276 ref_node = data->node;
Jens Axboeac0648a2020-11-23 09:37:51 -07007277 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007278 if (ref_node)
7279 percpu_ref_kill(&ref_node->refs);
7280
7281 percpu_ref_kill(&data->refs);
7282
7283 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007284 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007285 do {
7286 ret = wait_for_completion_interruptible(&data->done);
7287 if (!ret)
7288 break;
7289 ret = io_run_task_work_sig();
7290 if (ret < 0) {
7291 percpu_ref_resurrect(&data->refs);
7292 reinit_completion(&data->done);
7293 io_sqe_files_set_node(data, backup_node);
7294 return ret;
7295 }
7296 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007297
Jens Axboe6b063142019-01-10 22:13:58 -07007298 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007299 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7300 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007301 kfree(data->table[i].files);
7302 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007303 percpu_ref_exit(&data->refs);
7304 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007305 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007306 ctx->nr_user_files = 0;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007307 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007308 return 0;
7309}
7310
Jens Axboe534ca6d2020-09-02 13:52:19 -06007311static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007312{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007313 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007314 /*
7315 * The park is a bit of a work-around, without it we get
7316 * warning spews on shutdown with SQPOLL set and affinity
7317 * set to a single CPU.
7318 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007319 if (sqd->thread) {
7320 kthread_park(sqd->thread);
7321 kthread_stop(sqd->thread);
7322 }
7323
7324 kfree(sqd);
7325 }
7326}
7327
Jens Axboeaa061652020-09-02 14:50:27 -06007328static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7329{
7330 struct io_ring_ctx *ctx_attach;
7331 struct io_sq_data *sqd;
7332 struct fd f;
7333
7334 f = fdget(p->wq_fd);
7335 if (!f.file)
7336 return ERR_PTR(-ENXIO);
7337 if (f.file->f_op != &io_uring_fops) {
7338 fdput(f);
7339 return ERR_PTR(-EINVAL);
7340 }
7341
7342 ctx_attach = f.file->private_data;
7343 sqd = ctx_attach->sq_data;
7344 if (!sqd) {
7345 fdput(f);
7346 return ERR_PTR(-EINVAL);
7347 }
7348
7349 refcount_inc(&sqd->refs);
7350 fdput(f);
7351 return sqd;
7352}
7353
Jens Axboe534ca6d2020-09-02 13:52:19 -06007354static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7355{
7356 struct io_sq_data *sqd;
7357
Jens Axboeaa061652020-09-02 14:50:27 -06007358 if (p->flags & IORING_SETUP_ATTACH_WQ)
7359 return io_attach_sq_data(p);
7360
Jens Axboe534ca6d2020-09-02 13:52:19 -06007361 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7362 if (!sqd)
7363 return ERR_PTR(-ENOMEM);
7364
7365 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007366 INIT_LIST_HEAD(&sqd->ctx_list);
7367 INIT_LIST_HEAD(&sqd->ctx_new_list);
7368 mutex_init(&sqd->ctx_lock);
7369 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007370 init_waitqueue_head(&sqd->wait);
7371 return sqd;
7372}
7373
Jens Axboe69fb2132020-09-14 11:16:23 -06007374static void io_sq_thread_unpark(struct io_sq_data *sqd)
7375 __releases(&sqd->lock)
7376{
7377 if (!sqd->thread)
7378 return;
7379 kthread_unpark(sqd->thread);
7380 mutex_unlock(&sqd->lock);
7381}
7382
7383static void io_sq_thread_park(struct io_sq_data *sqd)
7384 __acquires(&sqd->lock)
7385{
7386 if (!sqd->thread)
7387 return;
7388 mutex_lock(&sqd->lock);
7389 kthread_park(sqd->thread);
7390}
7391
Jens Axboe534ca6d2020-09-02 13:52:19 -06007392static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7393{
7394 struct io_sq_data *sqd = ctx->sq_data;
7395
7396 if (sqd) {
7397 if (sqd->thread) {
7398 /*
7399 * We may arrive here from the error branch in
7400 * io_sq_offload_create() where the kthread is created
7401 * without being waked up, thus wake it up now to make
7402 * sure the wait will complete.
7403 */
7404 wake_up_process(sqd->thread);
7405 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007406
7407 io_sq_thread_park(sqd);
7408 }
7409
7410 mutex_lock(&sqd->ctx_lock);
7411 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007412 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007413 mutex_unlock(&sqd->ctx_lock);
7414
Xiaoguang Wang08369242020-11-03 14:15:59 +08007415 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007416 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007417
7418 io_put_sq_data(sqd);
7419 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007420 }
7421}
7422
Jens Axboe6b063142019-01-10 22:13:58 -07007423static void io_finish_async(struct io_ring_ctx *ctx)
7424{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007425 io_sq_thread_stop(ctx);
7426
Jens Axboe561fb042019-10-24 07:25:42 -06007427 if (ctx->io_wq) {
7428 io_wq_destroy(ctx->io_wq);
7429 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007430 }
7431}
7432
7433#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007434/*
7435 * Ensure the UNIX gc is aware of our file set, so we are certain that
7436 * the io_uring can be safely unregistered on process exit, even if we have
7437 * loops in the file referencing.
7438 */
7439static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7440{
7441 struct sock *sk = ctx->ring_sock->sk;
7442 struct scm_fp_list *fpl;
7443 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007444 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007445
Jens Axboe6b063142019-01-10 22:13:58 -07007446 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7447 if (!fpl)
7448 return -ENOMEM;
7449
7450 skb = alloc_skb(0, GFP_KERNEL);
7451 if (!skb) {
7452 kfree(fpl);
7453 return -ENOMEM;
7454 }
7455
7456 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007457
Jens Axboe08a45172019-10-03 08:11:03 -06007458 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007459 fpl->user = get_uid(ctx->user);
7460 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007461 struct file *file = io_file_from_index(ctx, i + offset);
7462
7463 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007464 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007465 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007466 unix_inflight(fpl->user, fpl->fp[nr_files]);
7467 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007468 }
7469
Jens Axboe08a45172019-10-03 08:11:03 -06007470 if (nr_files) {
7471 fpl->max = SCM_MAX_FD;
7472 fpl->count = nr_files;
7473 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007474 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007475 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7476 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007477
Jens Axboe08a45172019-10-03 08:11:03 -06007478 for (i = 0; i < nr_files; i++)
7479 fput(fpl->fp[i]);
7480 } else {
7481 kfree_skb(skb);
7482 kfree(fpl);
7483 }
Jens Axboe6b063142019-01-10 22:13:58 -07007484
7485 return 0;
7486}
7487
7488/*
7489 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7490 * causes regular reference counting to break down. We rely on the UNIX
7491 * garbage collection to take care of this problem for us.
7492 */
7493static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7494{
7495 unsigned left, total;
7496 int ret = 0;
7497
7498 total = 0;
7499 left = ctx->nr_user_files;
7500 while (left) {
7501 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007502
7503 ret = __io_sqe_files_scm(ctx, this_files, total);
7504 if (ret)
7505 break;
7506 left -= this_files;
7507 total += this_files;
7508 }
7509
7510 if (!ret)
7511 return 0;
7512
7513 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007514 struct file *file = io_file_from_index(ctx, total);
7515
7516 if (file)
7517 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007518 total++;
7519 }
7520
7521 return ret;
7522}
7523#else
7524static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7525{
7526 return 0;
7527}
7528#endif
7529
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007530static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7531 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007532{
7533 int i;
7534
7535 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007536 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007537 unsigned this_files;
7538
7539 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7540 table->files = kcalloc(this_files, sizeof(struct file *),
7541 GFP_KERNEL);
7542 if (!table->files)
7543 break;
7544 nr_files -= this_files;
7545 }
7546
7547 if (i == nr_tables)
7548 return 0;
7549
7550 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007551 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007552 kfree(table->files);
7553 }
7554 return 1;
7555}
7556
Jens Axboe05f3fb32019-12-09 11:22:50 -07007557static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007558{
7559#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007560 struct sock *sock = ctx->ring_sock->sk;
7561 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7562 struct sk_buff *skb;
7563 int i;
7564
7565 __skb_queue_head_init(&list);
7566
7567 /*
7568 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7569 * remove this entry and rearrange the file array.
7570 */
7571 skb = skb_dequeue(head);
7572 while (skb) {
7573 struct scm_fp_list *fp;
7574
7575 fp = UNIXCB(skb).fp;
7576 for (i = 0; i < fp->count; i++) {
7577 int left;
7578
7579 if (fp->fp[i] != file)
7580 continue;
7581
7582 unix_notinflight(fp->user, fp->fp[i]);
7583 left = fp->count - 1 - i;
7584 if (left) {
7585 memmove(&fp->fp[i], &fp->fp[i + 1],
7586 left * sizeof(struct file *));
7587 }
7588 fp->count--;
7589 if (!fp->count) {
7590 kfree_skb(skb);
7591 skb = NULL;
7592 } else {
7593 __skb_queue_tail(&list, skb);
7594 }
7595 fput(file);
7596 file = NULL;
7597 break;
7598 }
7599
7600 if (!file)
7601 break;
7602
7603 __skb_queue_tail(&list, skb);
7604
7605 skb = skb_dequeue(head);
7606 }
7607
7608 if (skb_peek(&list)) {
7609 spin_lock_irq(&head->lock);
7610 while ((skb = __skb_dequeue(&list)) != NULL)
7611 __skb_queue_tail(head, skb);
7612 spin_unlock_irq(&head->lock);
7613 }
7614#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007615 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007616#endif
7617}
7618
Jens Axboe05f3fb32019-12-09 11:22:50 -07007619struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007620 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007621 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007622};
7623
Jens Axboe4a38aed22020-05-14 17:21:15 -06007624static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007625{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007626 struct fixed_file_data *file_data = ref_node->file_data;
7627 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007628 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007629
7630 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007631 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007632 io_ring_file_put(ctx, pfile->file);
7633 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007634 }
7635
Xiaoguang Wang05589552020-03-31 14:05:18 +08007636 percpu_ref_exit(&ref_node->refs);
7637 kfree(ref_node);
7638 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007639}
7640
Jens Axboe4a38aed22020-05-14 17:21:15 -06007641static void io_file_put_work(struct work_struct *work)
7642{
7643 struct io_ring_ctx *ctx;
7644 struct llist_node *node;
7645
7646 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7647 node = llist_del_all(&ctx->file_put_llist);
7648
7649 while (node) {
7650 struct fixed_file_ref_node *ref_node;
7651 struct llist_node *next = node->next;
7652
7653 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7654 __io_file_put_work(ref_node);
7655 node = next;
7656 }
7657}
7658
Jens Axboe05f3fb32019-12-09 11:22:50 -07007659static void io_file_data_ref_zero(struct percpu_ref *ref)
7660{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007661 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007662 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007663 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007664 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007665 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007666
Xiaoguang Wang05589552020-03-31 14:05:18 +08007667 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007668 data = ref_node->file_data;
7669 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007670
Jens Axboeac0648a2020-11-23 09:37:51 -07007671 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007672 ref_node->done = true;
7673
7674 while (!list_empty(&data->ref_list)) {
7675 ref_node = list_first_entry(&data->ref_list,
7676 struct fixed_file_ref_node, node);
7677 /* recycle ref nodes in order */
7678 if (!ref_node->done)
7679 break;
7680 list_del(&ref_node->node);
7681 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7682 }
Jens Axboeac0648a2020-11-23 09:37:51 -07007683 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007684
7685 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007686 delay = 0;
7687
Jens Axboe4a38aed22020-05-14 17:21:15 -06007688 if (!delay)
7689 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7690 else if (first_add)
7691 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007692}
7693
7694static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7695 struct io_ring_ctx *ctx)
7696{
7697 struct fixed_file_ref_node *ref_node;
7698
7699 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7700 if (!ref_node)
7701 return ERR_PTR(-ENOMEM);
7702
7703 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7704 0, GFP_KERNEL)) {
7705 kfree(ref_node);
7706 return ERR_PTR(-ENOMEM);
7707 }
7708 INIT_LIST_HEAD(&ref_node->node);
7709 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007710 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007711 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007712 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007713}
7714
7715static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7716{
7717 percpu_ref_exit(&ref_node->refs);
7718 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007719}
7720
7721static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7722 unsigned nr_args)
7723{
7724 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007725 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007726 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007727 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007728 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007729 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007730
7731 if (ctx->file_data)
7732 return -EBUSY;
7733 if (!nr_args)
7734 return -EINVAL;
7735 if (nr_args > IORING_MAX_FIXED_FILES)
7736 return -EMFILE;
7737
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007738 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7739 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007740 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007741 file_data->ctx = ctx;
7742 init_completion(&file_data->done);
7743 INIT_LIST_HEAD(&file_data->ref_list);
7744 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007745
7746 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007747 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007748 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007749 if (!file_data->table)
7750 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007751
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007752 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007753 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7754 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007755
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007756 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7757 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007758 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007759
7760 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7761 struct fixed_file_table *table;
7762 unsigned index;
7763
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007764 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7765 ret = -EFAULT;
7766 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007767 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007768 /* allow sparse sets */
7769 if (fd == -1)
7770 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007771
Jens Axboe05f3fb32019-12-09 11:22:50 -07007772 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007773 ret = -EBADF;
7774 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007775 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007776
7777 /*
7778 * Don't allow io_uring instances to be registered. If UNIX
7779 * isn't enabled, then this causes a reference cycle and this
7780 * instance can never get freed. If UNIX is enabled we'll
7781 * handle it just fine, but there's still no point in allowing
7782 * a ring fd as it doesn't support regular read/write anyway.
7783 */
7784 if (file->f_op == &io_uring_fops) {
7785 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007786 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007787 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007788 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7789 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007790 table->files[index] = file;
7791 }
7792
Jens Axboe05f3fb32019-12-09 11:22:50 -07007793 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007794 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007795 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007796 return ret;
7797 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798
Xiaoguang Wang05589552020-03-31 14:05:18 +08007799 ref_node = alloc_fixed_file_ref_node(ctx);
7800 if (IS_ERR(ref_node)) {
7801 io_sqe_files_unregister(ctx);
7802 return PTR_ERR(ref_node);
7803 }
7804
Pavel Begunkov1642b442020-12-30 21:34:14 +00007805 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007806 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007807out_fput:
7808 for (i = 0; i < ctx->nr_user_files; i++) {
7809 file = io_file_from_index(ctx, i);
7810 if (file)
7811 fput(file);
7812 }
7813 for (i = 0; i < nr_tables; i++)
7814 kfree(file_data->table[i].files);
7815 ctx->nr_user_files = 0;
7816out_ref:
7817 percpu_ref_exit(&file_data->refs);
7818out_free:
7819 kfree(file_data->table);
7820 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007821 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007822 return ret;
7823}
7824
Jens Axboec3a31e62019-10-03 13:59:56 -06007825static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7826 int index)
7827{
7828#if defined(CONFIG_UNIX)
7829 struct sock *sock = ctx->ring_sock->sk;
7830 struct sk_buff_head *head = &sock->sk_receive_queue;
7831 struct sk_buff *skb;
7832
7833 /*
7834 * See if we can merge this file into an existing skb SCM_RIGHTS
7835 * file set. If there's no room, fall back to allocating a new skb
7836 * and filling it in.
7837 */
7838 spin_lock_irq(&head->lock);
7839 skb = skb_peek(head);
7840 if (skb) {
7841 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7842
7843 if (fpl->count < SCM_MAX_FD) {
7844 __skb_unlink(skb, head);
7845 spin_unlock_irq(&head->lock);
7846 fpl->fp[fpl->count] = get_file(file);
7847 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7848 fpl->count++;
7849 spin_lock_irq(&head->lock);
7850 __skb_queue_head(head, skb);
7851 } else {
7852 skb = NULL;
7853 }
7854 }
7855 spin_unlock_irq(&head->lock);
7856
7857 if (skb) {
7858 fput(file);
7859 return 0;
7860 }
7861
7862 return __io_sqe_files_scm(ctx, 1, index);
7863#else
7864 return 0;
7865#endif
7866}
7867
Hillf Dantona5318d32020-03-23 17:47:15 +08007868static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007869 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007870{
Hillf Dantona5318d32020-03-23 17:47:15 +08007871 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007872 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007873
Jens Axboe05f3fb32019-12-09 11:22:50 -07007874 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007875 if (!pfile)
7876 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007877
7878 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007879 list_add(&pfile->list, &ref_node->file_list);
7880
Hillf Dantona5318d32020-03-23 17:47:15 +08007881 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007882}
7883
7884static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7885 struct io_uring_files_update *up,
7886 unsigned nr_args)
7887{
7888 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007889 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007890 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007891 __s32 __user *fds;
7892 int fd, i, err;
7893 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007894 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007895
Jens Axboe05f3fb32019-12-09 11:22:50 -07007896 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007897 return -EOVERFLOW;
7898 if (done > ctx->nr_user_files)
7899 return -EINVAL;
7900
Xiaoguang Wang05589552020-03-31 14:05:18 +08007901 ref_node = alloc_fixed_file_ref_node(ctx);
7902 if (IS_ERR(ref_node))
7903 return PTR_ERR(ref_node);
7904
Jens Axboec3a31e62019-10-03 13:59:56 -06007905 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007906 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007907 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007908 struct fixed_file_table *table;
7909 unsigned index;
7910
Jens Axboec3a31e62019-10-03 13:59:56 -06007911 err = 0;
7912 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7913 err = -EFAULT;
7914 break;
7915 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007916 i = array_index_nospec(up->offset, ctx->nr_user_files);
7917 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007918 index = i & IORING_FILE_TABLE_MASK;
7919 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007920 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007921 err = io_queue_file_removal(data, file);
7922 if (err)
7923 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007924 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007925 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007926 }
7927 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007928 file = fget(fd);
7929 if (!file) {
7930 err = -EBADF;
7931 break;
7932 }
7933 /*
7934 * Don't allow io_uring instances to be registered. If
7935 * UNIX isn't enabled, then this causes a reference
7936 * cycle and this instance can never get freed. If UNIX
7937 * is enabled we'll handle it just fine, but there's
7938 * still no point in allowing a ring fd as it doesn't
7939 * support regular read/write anyway.
7940 */
7941 if (file->f_op == &io_uring_fops) {
7942 fput(file);
7943 err = -EBADF;
7944 break;
7945 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007946 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007947 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007948 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007949 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007950 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007951 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007952 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007953 }
7954 nr_args--;
7955 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007956 up->offset++;
7957 }
7958
Xiaoguang Wang05589552020-03-31 14:05:18 +08007959 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007960 percpu_ref_kill(&data->node->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007961 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007962 } else
7963 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007964
7965 return done ? done : err;
7966}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007967
Jens Axboe05f3fb32019-12-09 11:22:50 -07007968static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7969 unsigned nr_args)
7970{
7971 struct io_uring_files_update up;
7972
7973 if (!ctx->file_data)
7974 return -ENXIO;
7975 if (!nr_args)
7976 return -EINVAL;
7977 if (copy_from_user(&up, arg, sizeof(up)))
7978 return -EFAULT;
7979 if (up.resv)
7980 return -EINVAL;
7981
7982 return __io_sqe_files_update(ctx, &up, nr_args);
7983}
Jens Axboec3a31e62019-10-03 13:59:56 -06007984
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007985static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007986{
7987 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7988
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007989 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007990 io_put_req(req);
7991}
7992
Pavel Begunkov24369c22020-01-28 03:15:48 +03007993static int io_init_wq_offload(struct io_ring_ctx *ctx,
7994 struct io_uring_params *p)
7995{
7996 struct io_wq_data data;
7997 struct fd f;
7998 struct io_ring_ctx *ctx_attach;
7999 unsigned int concurrency;
8000 int ret = 0;
8001
8002 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008003 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008004 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008005
8006 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8007 /* Do QD, or 4 * CPUS, whatever is smallest */
8008 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8009
8010 ctx->io_wq = io_wq_create(concurrency, &data);
8011 if (IS_ERR(ctx->io_wq)) {
8012 ret = PTR_ERR(ctx->io_wq);
8013 ctx->io_wq = NULL;
8014 }
8015 return ret;
8016 }
8017
8018 f = fdget(p->wq_fd);
8019 if (!f.file)
8020 return -EBADF;
8021
8022 if (f.file->f_op != &io_uring_fops) {
8023 ret = -EINVAL;
8024 goto out_fput;
8025 }
8026
8027 ctx_attach = f.file->private_data;
8028 /* @io_wq is protected by holding the fd */
8029 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8030 ret = -EINVAL;
8031 goto out_fput;
8032 }
8033
8034 ctx->io_wq = ctx_attach->io_wq;
8035out_fput:
8036 fdput(f);
8037 return ret;
8038}
8039
Jens Axboe0f212202020-09-13 13:09:39 -06008040static int io_uring_alloc_task_context(struct task_struct *task)
8041{
8042 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008043 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008044
8045 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8046 if (unlikely(!tctx))
8047 return -ENOMEM;
8048
Jens Axboed8a6df12020-10-15 16:24:45 -06008049 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8050 if (unlikely(ret)) {
8051 kfree(tctx);
8052 return ret;
8053 }
8054
Jens Axboe0f212202020-09-13 13:09:39 -06008055 xa_init(&tctx->xa);
8056 init_waitqueue_head(&tctx->wait);
8057 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008058 atomic_set(&tctx->in_idle, 0);
8059 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008060 io_init_identity(&tctx->__identity);
8061 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008062 task->io_uring = tctx;
8063 return 0;
8064}
8065
8066void __io_uring_free(struct task_struct *tsk)
8067{
8068 struct io_uring_task *tctx = tsk->io_uring;
8069
8070 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008071 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8072 if (tctx->identity != &tctx->__identity)
8073 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008074 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008075 kfree(tctx);
8076 tsk->io_uring = NULL;
8077}
8078
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008079static int io_sq_offload_create(struct io_ring_ctx *ctx,
8080 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008081{
8082 int ret;
8083
Jens Axboe6c271ce2019-01-10 11:22:30 -07008084 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008085 struct io_sq_data *sqd;
8086
Jens Axboe3ec482d2019-04-08 10:51:01 -06008087 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008088 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008089 goto err;
8090
Jens Axboe534ca6d2020-09-02 13:52:19 -06008091 sqd = io_get_sq_data(p);
8092 if (IS_ERR(sqd)) {
8093 ret = PTR_ERR(sqd);
8094 goto err;
8095 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008096
Jens Axboe534ca6d2020-09-02 13:52:19 -06008097 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008098 io_sq_thread_park(sqd);
8099 mutex_lock(&sqd->ctx_lock);
8100 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8101 mutex_unlock(&sqd->ctx_lock);
8102 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008103
Jens Axboe917257d2019-04-13 09:28:55 -06008104 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8105 if (!ctx->sq_thread_idle)
8106 ctx->sq_thread_idle = HZ;
8107
Jens Axboeaa061652020-09-02 14:50:27 -06008108 if (sqd->thread)
8109 goto done;
8110
Jens Axboe6c271ce2019-01-10 11:22:30 -07008111 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008112 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008113
Jens Axboe917257d2019-04-13 09:28:55 -06008114 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008115 if (cpu >= nr_cpu_ids)
8116 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008117 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008118 goto err;
8119
Jens Axboe69fb2132020-09-14 11:16:23 -06008120 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008121 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008122 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008123 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008124 "io_uring-sq");
8125 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008126 if (IS_ERR(sqd->thread)) {
8127 ret = PTR_ERR(sqd->thread);
8128 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008129 goto err;
8130 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008131 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008132 if (ret)
8133 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008134 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8135 /* Can't have SQ_AFF without SQPOLL */
8136 ret = -EINVAL;
8137 goto err;
8138 }
8139
Jens Axboeaa061652020-09-02 14:50:27 -06008140done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008141 ret = io_init_wq_offload(ctx, p);
8142 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008143 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008144
8145 return 0;
8146err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008147 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008148 return ret;
8149}
8150
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008151static void io_sq_offload_start(struct io_ring_ctx *ctx)
8152{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008153 struct io_sq_data *sqd = ctx->sq_data;
8154
8155 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8156 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008157}
8158
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008159static inline void __io_unaccount_mem(struct user_struct *user,
8160 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008161{
8162 atomic_long_sub(nr_pages, &user->locked_vm);
8163}
8164
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008165static inline int __io_account_mem(struct user_struct *user,
8166 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008167{
8168 unsigned long page_limit, cur_pages, new_pages;
8169
8170 /* Don't allow more pages than we can safely lock */
8171 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8172
8173 do {
8174 cur_pages = atomic_long_read(&user->locked_vm);
8175 new_pages = cur_pages + nr_pages;
8176 if (new_pages > page_limit)
8177 return -ENOMEM;
8178 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8179 new_pages) != cur_pages);
8180
8181 return 0;
8182}
8183
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008184static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8185 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008186{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008187 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008188 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008189
Jens Axboe2aede0e2020-09-14 10:45:53 -06008190 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008191 if (acct == ACCT_LOCKED) {
8192 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008193 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008194 mmap_write_unlock(ctx->mm_account);
8195 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008196 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008197 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008198 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008199}
8200
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008201static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8202 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008203{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008204 int ret;
8205
8206 if (ctx->limit_mem) {
8207 ret = __io_account_mem(ctx->user, nr_pages);
8208 if (ret)
8209 return ret;
8210 }
8211
Jens Axboe2aede0e2020-09-14 10:45:53 -06008212 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008213 if (acct == ACCT_LOCKED) {
8214 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008215 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008216 mmap_write_unlock(ctx->mm_account);
8217 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008218 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008219 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008220 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008221
8222 return 0;
8223}
8224
Jens Axboe2b188cc2019-01-07 10:46:33 -07008225static void io_mem_free(void *ptr)
8226{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008227 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008228
Mark Rutland52e04ef2019-04-30 17:30:21 +01008229 if (!ptr)
8230 return;
8231
8232 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008233 if (put_page_testzero(page))
8234 free_compound_page(page);
8235}
8236
8237static void *io_mem_alloc(size_t size)
8238{
8239 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8240 __GFP_NORETRY;
8241
8242 return (void *) __get_free_pages(gfp_flags, get_order(size));
8243}
8244
Hristo Venev75b28af2019-08-26 17:23:46 +00008245static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8246 size_t *sq_offset)
8247{
8248 struct io_rings *rings;
8249 size_t off, sq_array_size;
8250
8251 off = struct_size(rings, cqes, cq_entries);
8252 if (off == SIZE_MAX)
8253 return SIZE_MAX;
8254
8255#ifdef CONFIG_SMP
8256 off = ALIGN(off, SMP_CACHE_BYTES);
8257 if (off == 0)
8258 return SIZE_MAX;
8259#endif
8260
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008261 if (sq_offset)
8262 *sq_offset = off;
8263
Hristo Venev75b28af2019-08-26 17:23:46 +00008264 sq_array_size = array_size(sizeof(u32), sq_entries);
8265 if (sq_array_size == SIZE_MAX)
8266 return SIZE_MAX;
8267
8268 if (check_add_overflow(off, sq_array_size, &off))
8269 return SIZE_MAX;
8270
Hristo Venev75b28af2019-08-26 17:23:46 +00008271 return off;
8272}
8273
Jens Axboe2b188cc2019-01-07 10:46:33 -07008274static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8275{
Hristo Venev75b28af2019-08-26 17:23:46 +00008276 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008277
Hristo Venev75b28af2019-08-26 17:23:46 +00008278 pages = (size_t)1 << get_order(
8279 rings_size(sq_entries, cq_entries, NULL));
8280 pages += (size_t)1 << get_order(
8281 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008282
Hristo Venev75b28af2019-08-26 17:23:46 +00008283 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008284}
8285
Jens Axboeedafcce2019-01-09 09:16:05 -07008286static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8287{
8288 int i, j;
8289
8290 if (!ctx->user_bufs)
8291 return -ENXIO;
8292
8293 for (i = 0; i < ctx->nr_user_bufs; i++) {
8294 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8295
8296 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008297 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008298
Jens Axboede293932020-09-17 16:19:16 -06008299 if (imu->acct_pages)
8300 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008301 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008302 imu->nr_bvecs = 0;
8303 }
8304
8305 kfree(ctx->user_bufs);
8306 ctx->user_bufs = NULL;
8307 ctx->nr_user_bufs = 0;
8308 return 0;
8309}
8310
8311static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8312 void __user *arg, unsigned index)
8313{
8314 struct iovec __user *src;
8315
8316#ifdef CONFIG_COMPAT
8317 if (ctx->compat) {
8318 struct compat_iovec __user *ciovs;
8319 struct compat_iovec ciov;
8320
8321 ciovs = (struct compat_iovec __user *) arg;
8322 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8323 return -EFAULT;
8324
Jens Axboed55e5f52019-12-11 16:12:15 -07008325 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008326 dst->iov_len = ciov.iov_len;
8327 return 0;
8328 }
8329#endif
8330 src = (struct iovec __user *) arg;
8331 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8332 return -EFAULT;
8333 return 0;
8334}
8335
Jens Axboede293932020-09-17 16:19:16 -06008336/*
8337 * Not super efficient, but this is just a registration time. And we do cache
8338 * the last compound head, so generally we'll only do a full search if we don't
8339 * match that one.
8340 *
8341 * We check if the given compound head page has already been accounted, to
8342 * avoid double accounting it. This allows us to account the full size of the
8343 * page, not just the constituent pages of a huge page.
8344 */
8345static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8346 int nr_pages, struct page *hpage)
8347{
8348 int i, j;
8349
8350 /* check current page array */
8351 for (i = 0; i < nr_pages; i++) {
8352 if (!PageCompound(pages[i]))
8353 continue;
8354 if (compound_head(pages[i]) == hpage)
8355 return true;
8356 }
8357
8358 /* check previously registered pages */
8359 for (i = 0; i < ctx->nr_user_bufs; i++) {
8360 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8361
8362 for (j = 0; j < imu->nr_bvecs; j++) {
8363 if (!PageCompound(imu->bvec[j].bv_page))
8364 continue;
8365 if (compound_head(imu->bvec[j].bv_page) == hpage)
8366 return true;
8367 }
8368 }
8369
8370 return false;
8371}
8372
8373static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8374 int nr_pages, struct io_mapped_ubuf *imu,
8375 struct page **last_hpage)
8376{
8377 int i, ret;
8378
8379 for (i = 0; i < nr_pages; i++) {
8380 if (!PageCompound(pages[i])) {
8381 imu->acct_pages++;
8382 } else {
8383 struct page *hpage;
8384
8385 hpage = compound_head(pages[i]);
8386 if (hpage == *last_hpage)
8387 continue;
8388 *last_hpage = hpage;
8389 if (headpage_already_acct(ctx, pages, i, hpage))
8390 continue;
8391 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8392 }
8393 }
8394
8395 if (!imu->acct_pages)
8396 return 0;
8397
8398 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8399 if (ret)
8400 imu->acct_pages = 0;
8401 return ret;
8402}
8403
Jens Axboeedafcce2019-01-09 09:16:05 -07008404static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8405 unsigned nr_args)
8406{
8407 struct vm_area_struct **vmas = NULL;
8408 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008409 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008410 int i, j, got_pages = 0;
8411 int ret = -EINVAL;
8412
8413 if (ctx->user_bufs)
8414 return -EBUSY;
8415 if (!nr_args || nr_args > UIO_MAXIOV)
8416 return -EINVAL;
8417
8418 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8419 GFP_KERNEL);
8420 if (!ctx->user_bufs)
8421 return -ENOMEM;
8422
8423 for (i = 0; i < nr_args; i++) {
8424 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8425 unsigned long off, start, end, ubuf;
8426 int pret, nr_pages;
8427 struct iovec iov;
8428 size_t size;
8429
8430 ret = io_copy_iov(ctx, &iov, arg, i);
8431 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008432 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008433
8434 /*
8435 * Don't impose further limits on the size and buffer
8436 * constraints here, we'll -EINVAL later when IO is
8437 * submitted if they are wrong.
8438 */
8439 ret = -EFAULT;
8440 if (!iov.iov_base || !iov.iov_len)
8441 goto err;
8442
8443 /* arbitrary limit, but we need something */
8444 if (iov.iov_len > SZ_1G)
8445 goto err;
8446
8447 ubuf = (unsigned long) iov.iov_base;
8448 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8449 start = ubuf >> PAGE_SHIFT;
8450 nr_pages = end - start;
8451
Jens Axboeedafcce2019-01-09 09:16:05 -07008452 ret = 0;
8453 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008454 kvfree(vmas);
8455 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008456 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008457 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008458 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008459 sizeof(struct vm_area_struct *),
8460 GFP_KERNEL);
8461 if (!pages || !vmas) {
8462 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008463 goto err;
8464 }
8465 got_pages = nr_pages;
8466 }
8467
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008468 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008469 GFP_KERNEL);
8470 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008471 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008472 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008473
8474 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008475 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008476 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008477 FOLL_WRITE | FOLL_LONGTERM,
8478 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008479 if (pret == nr_pages) {
8480 /* don't support file backed memory */
8481 for (j = 0; j < nr_pages; j++) {
8482 struct vm_area_struct *vma = vmas[j];
8483
8484 if (vma->vm_file &&
8485 !is_file_hugepages(vma->vm_file)) {
8486 ret = -EOPNOTSUPP;
8487 break;
8488 }
8489 }
8490 } else {
8491 ret = pret < 0 ? pret : -EFAULT;
8492 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008493 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008494 if (ret) {
8495 /*
8496 * if we did partial map, or found file backed vmas,
8497 * release any pages we did get
8498 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008499 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008500 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008501 kvfree(imu->bvec);
8502 goto err;
8503 }
8504
8505 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8506 if (ret) {
8507 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008508 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008509 goto err;
8510 }
8511
8512 off = ubuf & ~PAGE_MASK;
8513 size = iov.iov_len;
8514 for (j = 0; j < nr_pages; j++) {
8515 size_t vec_len;
8516
8517 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8518 imu->bvec[j].bv_page = pages[j];
8519 imu->bvec[j].bv_len = vec_len;
8520 imu->bvec[j].bv_offset = off;
8521 off = 0;
8522 size -= vec_len;
8523 }
8524 /* store original address for later verification */
8525 imu->ubuf = ubuf;
8526 imu->len = iov.iov_len;
8527 imu->nr_bvecs = nr_pages;
8528
8529 ctx->nr_user_bufs++;
8530 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008531 kvfree(pages);
8532 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008533 return 0;
8534err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008535 kvfree(pages);
8536 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008537 io_sqe_buffer_unregister(ctx);
8538 return ret;
8539}
8540
Jens Axboe9b402842019-04-11 11:45:41 -06008541static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8542{
8543 __s32 __user *fds = arg;
8544 int fd;
8545
8546 if (ctx->cq_ev_fd)
8547 return -EBUSY;
8548
8549 if (copy_from_user(&fd, fds, sizeof(*fds)))
8550 return -EFAULT;
8551
8552 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8553 if (IS_ERR(ctx->cq_ev_fd)) {
8554 int ret = PTR_ERR(ctx->cq_ev_fd);
8555 ctx->cq_ev_fd = NULL;
8556 return ret;
8557 }
8558
8559 return 0;
8560}
8561
8562static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8563{
8564 if (ctx->cq_ev_fd) {
8565 eventfd_ctx_put(ctx->cq_ev_fd);
8566 ctx->cq_ev_fd = NULL;
8567 return 0;
8568 }
8569
8570 return -ENXIO;
8571}
8572
Jens Axboe5a2e7452020-02-23 16:23:11 -07008573static int __io_destroy_buffers(int id, void *p, void *data)
8574{
8575 struct io_ring_ctx *ctx = data;
8576 struct io_buffer *buf = p;
8577
Jens Axboe067524e2020-03-02 16:32:28 -07008578 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008579 return 0;
8580}
8581
8582static void io_destroy_buffers(struct io_ring_ctx *ctx)
8583{
8584 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8585 idr_destroy(&ctx->io_buffer_idr);
8586}
8587
Jens Axboe2b188cc2019-01-07 10:46:33 -07008588static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8589{
Jens Axboe6b063142019-01-10 22:13:58 -07008590 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008591 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008592
8593 if (ctx->sqo_task) {
8594 put_task_struct(ctx->sqo_task);
8595 ctx->sqo_task = NULL;
8596 mmdrop(ctx->mm_account);
8597 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008598 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008599
Dennis Zhou91d8f512020-09-16 13:41:05 -07008600#ifdef CONFIG_BLK_CGROUP
8601 if (ctx->sqo_blkcg_css)
8602 css_put(ctx->sqo_blkcg_css);
8603#endif
8604
Jens Axboe6b063142019-01-10 22:13:58 -07008605 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008606 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008607 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008608 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008609
Jens Axboe2b188cc2019-01-07 10:46:33 -07008610#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008611 if (ctx->ring_sock) {
8612 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008613 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008614 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008615#endif
8616
Hristo Venev75b28af2019-08-26 17:23:46 +00008617 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008618 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008619
8620 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008621 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008622 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008623 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008624 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008625 kfree(ctx);
8626}
8627
8628static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8629{
8630 struct io_ring_ctx *ctx = file->private_data;
8631 __poll_t mask = 0;
8632
8633 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008634 /*
8635 * synchronizes with barrier from wq_has_sleeper call in
8636 * io_commit_cqring
8637 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008638 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008639 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008640 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008641 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8642 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008643 mask |= EPOLLIN | EPOLLRDNORM;
8644
8645 return mask;
8646}
8647
8648static int io_uring_fasync(int fd, struct file *file, int on)
8649{
8650 struct io_ring_ctx *ctx = file->private_data;
8651
8652 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8653}
8654
Jens Axboe071698e2020-01-28 10:04:42 -07008655static int io_remove_personalities(int id, void *p, void *data)
8656{
8657 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008658 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008659
Jens Axboe1e6fa522020-10-15 08:46:24 -06008660 iod = idr_remove(&ctx->personality_idr, id);
8661 if (iod) {
8662 put_cred(iod->creds);
8663 if (refcount_dec_and_test(&iod->count))
8664 kfree(iod);
8665 }
Jens Axboe071698e2020-01-28 10:04:42 -07008666 return 0;
8667}
8668
Jens Axboe85faa7b2020-04-09 18:14:00 -06008669static void io_ring_exit_work(struct work_struct *work)
8670{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008671 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8672 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008673
Jens Axboe56952e92020-06-17 15:00:04 -06008674 /*
8675 * If we're doing polled IO and end up having requests being
8676 * submitted async (out-of-line), then completions can come in while
8677 * we're waiting for refs to drop. We need to reap these manually,
8678 * as nobody else will be looking for them.
8679 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008680 do {
Pavel Begunkov90df0852021-01-04 20:43:30 +00008681 __io_uring_cancel_task_requests(ctx, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008682 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008683 io_ring_ctx_free(ctx);
8684}
8685
Jens Axboe00c18642020-12-20 10:45:02 -07008686static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8687{
8688 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8689
8690 return req->ctx == data;
8691}
8692
Jens Axboe2b188cc2019-01-07 10:46:33 -07008693static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8694{
8695 mutex_lock(&ctx->uring_lock);
8696 percpu_ref_kill(&ctx->refs);
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008697 /* if force is set, the ring is going away. always drop after that */
8698 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008699 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008700 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008701 mutex_unlock(&ctx->uring_lock);
8702
Pavel Begunkov6b819282020-11-06 13:00:25 +00008703 io_kill_timeouts(ctx, NULL, NULL);
8704 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008705
8706 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008707 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008708
Jens Axboe15dff282019-11-13 09:09:23 -07008709 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008710 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008711 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008712
8713 /*
8714 * Do this upfront, so we won't have a grace period where the ring
8715 * is closed but resources aren't reaped yet. This can cause
8716 * spurious failure in setting up a new ring.
8717 */
Jens Axboe760618f2020-07-24 12:53:31 -06008718 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8719 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008720
Jens Axboe85faa7b2020-04-09 18:14:00 -06008721 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008722 /*
8723 * Use system_unbound_wq to avoid spawning tons of event kworkers
8724 * if we're exiting a ton of rings at the same time. It just adds
8725 * noise and overhead, there's no discernable change in runtime
8726 * over using system_wq.
8727 */
8728 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008729}
8730
8731static int io_uring_release(struct inode *inode, struct file *file)
8732{
8733 struct io_ring_ctx *ctx = file->private_data;
8734
8735 file->private_data = NULL;
8736 io_ring_ctx_wait_and_kill(ctx);
8737 return 0;
8738}
8739
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008740struct io_task_cancel {
8741 struct task_struct *task;
8742 struct files_struct *files;
8743};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008744
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008745static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008746{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008747 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008748 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008749 bool ret;
8750
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008751 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008752 unsigned long flags;
8753 struct io_ring_ctx *ctx = req->ctx;
8754
8755 /* protect against races with linked timeouts */
8756 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008757 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008758 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8759 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008760 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008761 }
8762 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008763}
8764
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008765static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008766 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008767 struct files_struct *files)
8768{
8769 struct io_defer_entry *de = NULL;
8770 LIST_HEAD(list);
8771
8772 spin_lock_irq(&ctx->completion_lock);
8773 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008774 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008775 list_cut_position(&list, &ctx->defer_list, &de->list);
8776 break;
8777 }
8778 }
8779 spin_unlock_irq(&ctx->completion_lock);
8780
8781 while (!list_empty(&list)) {
8782 de = list_first_entry(&list, struct io_defer_entry, list);
8783 list_del_init(&de->list);
8784 req_set_fail_links(de->req);
8785 io_put_req(de->req);
8786 io_req_complete(de->req, -ECANCELED);
8787 kfree(de);
8788 }
8789}
8790
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008791static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008792 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008793 struct files_struct *files)
8794{
Jens Axboefcb323c2019-10-24 12:39:47 -06008795 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008796 struct io_task_cancel cancel = { .task = task, .files = files };
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008797 struct io_kiocb *req;
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008798 DEFINE_WAIT(wait);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008799 bool found = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06008800
8801 spin_lock_irq(&ctx->inflight_lock);
8802 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008803 if (req->task != task ||
Jens Axboe98447d62020-10-14 10:48:51 -06008804 req->work.identity->files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008805 continue;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008806 found = true;
Jens Axboe768134d2019-11-10 20:30:53 -07008807 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008808 }
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008809 if (found)
Pavel Begunkovc98de082020-11-15 12:56:32 +00008810 prepare_to_wait(&task->io_uring->wait, &wait,
8811 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008812 spin_unlock_irq(&ctx->inflight_lock);
8813
Jens Axboe768134d2019-11-10 20:30:53 -07008814 /* We need to keep going until we don't find a matching req */
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008815 if (!found)
Jens Axboefcb323c2019-10-24 12:39:47 -06008816 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008817
8818 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8819 io_poll_remove_all(ctx, task, files);
8820 io_kill_timeouts(ctx, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008821 /* cancellations _may_ trigger task work */
8822 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008823 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008824 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008825 }
8826}
8827
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008828static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8829 struct task_struct *task)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008830{
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008831 while (1) {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008832 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06008833 enum io_wq_cancel cret;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008834 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008835
Pavel Begunkov90df0852021-01-04 20:43:30 +00008836 if (ctx->io_wq) {
8837 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8838 &cancel, true);
8839 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8840 }
Jens Axboe0f212202020-09-13 13:09:39 -06008841
8842 /* SQPOLL thread does its own polling */
8843 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8844 while (!list_empty_careful(&ctx->iopoll_list)) {
8845 io_iopoll_try_reap_events(ctx);
8846 ret = true;
8847 }
8848 }
8849
Pavel Begunkov6b819282020-11-06 13:00:25 +00008850 ret |= io_poll_remove_all(ctx, task, NULL);
8851 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov55583d72020-12-20 13:21:43 +00008852 ret |= io_run_task_work();
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008853 if (!ret)
8854 break;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008855 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06008856 }
Jens Axboe0f212202020-09-13 13:09:39 -06008857}
8858
8859/*
8860 * We need to iteratively cancel requests, in case a request has dependent
8861 * hard links. These persist even for failure of cancelations, hence keep
8862 * looping until none are found.
8863 */
8864static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8865 struct files_struct *files)
8866{
8867 struct task_struct *task = current;
8868
Jens Axboefdaf0832020-10-30 09:37:30 -06008869 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008870 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008871 atomic_inc(&task->io_uring->in_idle);
8872 io_sq_thread_park(ctx->sq_data);
8873 }
Jens Axboe0f212202020-09-13 13:09:39 -06008874
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008875 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008876 io_cqring_overflow_flush(ctx, true, task, files);
8877
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008878 if (!files)
8879 __io_uring_cancel_task_requests(ctx, task);
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008880 else
8881 io_uring_cancel_files(ctx, task, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008882
8883 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8884 atomic_dec(&task->io_uring->in_idle);
8885 /*
8886 * If the files that are going away are the ones in the thread
8887 * identity, clear them out.
8888 */
8889 if (task->io_uring->identity->files == files)
8890 task->io_uring->identity->files = NULL;
8891 io_sq_thread_unpark(ctx->sq_data);
8892 }
Jens Axboe0f212202020-09-13 13:09:39 -06008893}
8894
8895/*
8896 * Note that this task has used io_uring. We use it for cancelation purposes.
8897 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008898static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008899{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008900 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008901 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008902
8903 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008904 ret = io_uring_alloc_task_context(current);
8905 if (unlikely(ret))
8906 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008907 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008908 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008909 if (tctx->last != file) {
8910 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008911
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008912 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008913 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008914 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8915 file, GFP_KERNEL));
8916 if (ret) {
8917 fput(file);
8918 return ret;
8919 }
Jens Axboe0f212202020-09-13 13:09:39 -06008920 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008921 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008922 }
8923
Jens Axboefdaf0832020-10-30 09:37:30 -06008924 /*
8925 * This is race safe in that the task itself is doing this, hence it
8926 * cannot be going through the exit/cancel paths at the same time.
8927 * This cannot be modified while exit/cancel is running.
8928 */
8929 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8930 tctx->sqpoll = true;
8931
Jens Axboe0f212202020-09-13 13:09:39 -06008932 return 0;
8933}
8934
8935/*
8936 * Remove this io_uring_file -> task mapping.
8937 */
8938static void io_uring_del_task_file(struct file *file)
8939{
8940 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008941
8942 if (tctx->last == file)
8943 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008944 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008945 if (file)
8946 fput(file);
8947}
8948
Jens Axboe0f212202020-09-13 13:09:39 -06008949/*
8950 * Drop task note for this file if we're the only ones that hold it after
8951 * pending fput()
8952 */
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008953static void io_uring_attempt_task_drop(struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008954{
8955 if (!current->io_uring)
8956 return;
8957 /*
8958 * fput() is pending, will be 2 if the only other ref is our potential
8959 * task file note. If the task is exiting, drop regardless of count.
8960 */
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008961 if (fatal_signal_pending(current) || (current->flags & PF_EXITING) ||
8962 atomic_long_read(&file->f_count) == 2)
8963 io_uring_del_task_file(file);
Jens Axboe0f212202020-09-13 13:09:39 -06008964}
8965
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008966static void io_uring_remove_task_files(struct io_uring_task *tctx)
8967{
8968 struct file *file;
8969 unsigned long index;
8970
8971 xa_for_each(&tctx->xa, index, file)
8972 io_uring_del_task_file(file);
8973}
8974
Jens Axboe0f212202020-09-13 13:09:39 -06008975void __io_uring_files_cancel(struct files_struct *files)
8976{
8977 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008978 struct file *file;
8979 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008980
8981 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008982 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008983 xa_for_each(&tctx->xa, index, file)
8984 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008985 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008986
8987 if (files)
8988 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008989}
8990
8991static s64 tctx_inflight(struct io_uring_task *tctx)
8992{
8993 unsigned long index;
8994 struct file *file;
8995 s64 inflight;
8996
8997 inflight = percpu_counter_sum(&tctx->inflight);
8998 if (!tctx->sqpoll)
8999 return inflight;
9000
9001 /*
9002 * If we have SQPOLL rings, then we need to iterate and find them, and
9003 * add the pending count for those.
9004 */
9005 xa_for_each(&tctx->xa, index, file) {
9006 struct io_ring_ctx *ctx = file->private_data;
9007
9008 if (ctx->flags & IORING_SETUP_SQPOLL) {
9009 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
9010
9011 inflight += percpu_counter_sum(&__tctx->inflight);
9012 }
9013 }
9014
9015 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009016}
9017
Jens Axboe0f212202020-09-13 13:09:39 -06009018/*
9019 * Find any io_uring fd that this task has registered or done IO on, and cancel
9020 * requests.
9021 */
9022void __io_uring_task_cancel(void)
9023{
9024 struct io_uring_task *tctx = current->io_uring;
9025 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009026 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009027
9028 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009029 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009030
Jens Axboed8a6df12020-10-15 16:24:45 -06009031 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009032 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009033 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009034 if (!inflight)
9035 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009036 __io_uring_files_cancel(NULL);
9037
9038 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9039
9040 /*
9041 * If we've seen completions, retry. This avoids a race where
9042 * a completion comes in before we did prepare_to_wait().
9043 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009044 if (inflight != tctx_inflight(tctx))
Jens Axboe0f212202020-09-13 13:09:39 -06009045 continue;
Jens Axboe0f212202020-09-13 13:09:39 -06009046 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009047 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009048 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009049
Jens Axboefdaf0832020-10-30 09:37:30 -06009050 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009051
9052 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009053}
9054
Jens Axboefcb323c2019-10-24 12:39:47 -06009055static int io_uring_flush(struct file *file, void *data)
9056{
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01009057 io_uring_attempt_task_drop(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009058 return 0;
9059}
9060
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009061static void *io_uring_validate_mmap_request(struct file *file,
9062 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009063{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009064 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009065 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009066 struct page *page;
9067 void *ptr;
9068
9069 switch (offset) {
9070 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009071 case IORING_OFF_CQ_RING:
9072 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009073 break;
9074 case IORING_OFF_SQES:
9075 ptr = ctx->sq_sqes;
9076 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009077 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009078 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009079 }
9080
9081 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009082 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009083 return ERR_PTR(-EINVAL);
9084
9085 return ptr;
9086}
9087
9088#ifdef CONFIG_MMU
9089
9090static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9091{
9092 size_t sz = vma->vm_end - vma->vm_start;
9093 unsigned long pfn;
9094 void *ptr;
9095
9096 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9097 if (IS_ERR(ptr))
9098 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009099
9100 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9101 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9102}
9103
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009104#else /* !CONFIG_MMU */
9105
9106static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9107{
9108 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9109}
9110
9111static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9112{
9113 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9114}
9115
9116static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9117 unsigned long addr, unsigned long len,
9118 unsigned long pgoff, unsigned long flags)
9119{
9120 void *ptr;
9121
9122 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9123 if (IS_ERR(ptr))
9124 return PTR_ERR(ptr);
9125
9126 return (unsigned long) ptr;
9127}
9128
9129#endif /* !CONFIG_MMU */
9130
Jens Axboe90554202020-09-03 12:12:41 -06009131static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
9132{
9133 DEFINE_WAIT(wait);
9134
9135 do {
9136 if (!io_sqring_full(ctx))
9137 break;
9138
9139 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9140
9141 if (!io_sqring_full(ctx))
9142 break;
9143
9144 schedule();
9145 } while (!signal_pending(current));
9146
9147 finish_wait(&ctx->sqo_sq_wait, &wait);
9148}
9149
Hao Xuc73ebb62020-11-03 10:54:37 +08009150static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9151 struct __kernel_timespec __user **ts,
9152 const sigset_t __user **sig)
9153{
9154 struct io_uring_getevents_arg arg;
9155
9156 /*
9157 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9158 * is just a pointer to the sigset_t.
9159 */
9160 if (!(flags & IORING_ENTER_EXT_ARG)) {
9161 *sig = (const sigset_t __user *) argp;
9162 *ts = NULL;
9163 return 0;
9164 }
9165
9166 /*
9167 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9168 * timespec and sigset_t pointers if good.
9169 */
9170 if (*argsz != sizeof(arg))
9171 return -EINVAL;
9172 if (copy_from_user(&arg, argp, sizeof(arg)))
9173 return -EFAULT;
9174 *sig = u64_to_user_ptr(arg.sigmask);
9175 *argsz = arg.sigmask_sz;
9176 *ts = u64_to_user_ptr(arg.ts);
9177 return 0;
9178}
9179
Jens Axboe2b188cc2019-01-07 10:46:33 -07009180SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009181 u32, min_complete, u32, flags, const void __user *, argp,
9182 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183{
9184 struct io_ring_ctx *ctx;
9185 long ret = -EBADF;
9186 int submitted = 0;
9187 struct fd f;
9188
Jens Axboe4c6e2772020-07-01 11:29:10 -06009189 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009190
Jens Axboe90554202020-09-03 12:12:41 -06009191 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009192 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009193 return -EINVAL;
9194
9195 f = fdget(fd);
9196 if (!f.file)
9197 return -EBADF;
9198
9199 ret = -EOPNOTSUPP;
9200 if (f.file->f_op != &io_uring_fops)
9201 goto out_fput;
9202
9203 ret = -ENXIO;
9204 ctx = f.file->private_data;
9205 if (!percpu_ref_tryget(&ctx->refs))
9206 goto out_fput;
9207
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009208 ret = -EBADFD;
9209 if (ctx->flags & IORING_SETUP_R_DISABLED)
9210 goto out;
9211
Jens Axboe6c271ce2019-01-10 11:22:30 -07009212 /*
9213 * For SQ polling, the thread will do all submissions and completions.
9214 * Just return the requested submit count, and wake the thread if
9215 * we were asked to.
9216 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009217 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009218 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009219 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009220
Jens Axboe6c271ce2019-01-10 11:22:30 -07009221 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009222 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06009223 if (flags & IORING_ENTER_SQ_WAIT)
9224 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07009225 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009226 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009227 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009228 if (unlikely(ret))
9229 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009230 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009231 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009232 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009233
9234 if (submitted != to_submit)
9235 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009236 }
9237 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009238 const sigset_t __user *sig;
9239 struct __kernel_timespec __user *ts;
9240
9241 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9242 if (unlikely(ret))
9243 goto out;
9244
Jens Axboe2b188cc2019-01-07 10:46:33 -07009245 min_complete = min(min_complete, ctx->cq_entries);
9246
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009247 /*
9248 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9249 * space applications don't need to do io completion events
9250 * polling again, they can rely on io_sq_thread to do polling
9251 * work, which can reduce cpu usage and uring_lock contention.
9252 */
9253 if (ctx->flags & IORING_SETUP_IOPOLL &&
9254 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009255 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009256 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009257 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009258 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009259 }
9260
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009261out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009262 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009263out_fput:
9264 fdput(f);
9265 return submitted ? submitted : ret;
9266}
9267
Tobias Klauserbebdb652020-02-26 18:38:32 +01009268#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009269static int io_uring_show_cred(int id, void *p, void *data)
9270{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009271 struct io_identity *iod = p;
9272 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009273 struct seq_file *m = data;
9274 struct user_namespace *uns = seq_user_ns(m);
9275 struct group_info *gi;
9276 kernel_cap_t cap;
9277 unsigned __capi;
9278 int g;
9279
9280 seq_printf(m, "%5d\n", id);
9281 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9282 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9283 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9284 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9285 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9286 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9287 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9288 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9289 seq_puts(m, "\n\tGroups:\t");
9290 gi = cred->group_info;
9291 for (g = 0; g < gi->ngroups; g++) {
9292 seq_put_decimal_ull(m, g ? " " : "",
9293 from_kgid_munged(uns, gi->gid[g]));
9294 }
9295 seq_puts(m, "\n\tCapEff:\t");
9296 cap = cred->cap_effective;
9297 CAP_FOR_EACH_U32(__capi)
9298 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9299 seq_putc(m, '\n');
9300 return 0;
9301}
9302
9303static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9304{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009305 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009306 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009307 int i;
9308
Jens Axboefad8e0d2020-09-28 08:57:48 -06009309 /*
9310 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9311 * since fdinfo case grabs it in the opposite direction of normal use
9312 * cases. If we fail to get the lock, we just don't iterate any
9313 * structures that could be going away outside the io_uring mutex.
9314 */
9315 has_lock = mutex_trylock(&ctx->uring_lock);
9316
Joseph Qidbbe9c62020-09-29 09:01:22 -06009317 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9318 sq = ctx->sq_data;
9319
9320 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9321 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009322 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009323 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009324 struct fixed_file_table *table;
9325 struct file *f;
9326
9327 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9328 f = table->files[i & IORING_FILE_TABLE_MASK];
9329 if (f)
9330 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9331 else
9332 seq_printf(m, "%5u: <none>\n", i);
9333 }
9334 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009335 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009336 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9337
9338 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9339 (unsigned int) buf->len);
9340 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009341 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009342 seq_printf(m, "Personalities:\n");
9343 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9344 }
Jens Axboed7718a92020-02-14 22:23:12 -07009345 seq_printf(m, "PollList:\n");
9346 spin_lock_irq(&ctx->completion_lock);
9347 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9348 struct hlist_head *list = &ctx->cancel_hash[i];
9349 struct io_kiocb *req;
9350
9351 hlist_for_each_entry(req, list, hash_node)
9352 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9353 req->task->task_works != NULL);
9354 }
9355 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009356 if (has_lock)
9357 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009358}
9359
9360static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9361{
9362 struct io_ring_ctx *ctx = f->private_data;
9363
9364 if (percpu_ref_tryget(&ctx->refs)) {
9365 __io_uring_show_fdinfo(ctx, m);
9366 percpu_ref_put(&ctx->refs);
9367 }
9368}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009369#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009370
Jens Axboe2b188cc2019-01-07 10:46:33 -07009371static const struct file_operations io_uring_fops = {
9372 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009373 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009374 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009375#ifndef CONFIG_MMU
9376 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9377 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9378#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009379 .poll = io_uring_poll,
9380 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009381#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009382 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009383#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009384};
9385
9386static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9387 struct io_uring_params *p)
9388{
Hristo Venev75b28af2019-08-26 17:23:46 +00009389 struct io_rings *rings;
9390 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009391
Jens Axboebd740482020-08-05 12:58:23 -06009392 /* make sure these are sane, as we already accounted them */
9393 ctx->sq_entries = p->sq_entries;
9394 ctx->cq_entries = p->cq_entries;
9395
Hristo Venev75b28af2019-08-26 17:23:46 +00009396 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9397 if (size == SIZE_MAX)
9398 return -EOVERFLOW;
9399
9400 rings = io_mem_alloc(size);
9401 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009402 return -ENOMEM;
9403
Hristo Venev75b28af2019-08-26 17:23:46 +00009404 ctx->rings = rings;
9405 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9406 rings->sq_ring_mask = p->sq_entries - 1;
9407 rings->cq_ring_mask = p->cq_entries - 1;
9408 rings->sq_ring_entries = p->sq_entries;
9409 rings->cq_ring_entries = p->cq_entries;
9410 ctx->sq_mask = rings->sq_ring_mask;
9411 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009412
9413 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009414 if (size == SIZE_MAX) {
9415 io_mem_free(ctx->rings);
9416 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009418 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009419
9420 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009421 if (!ctx->sq_sqes) {
9422 io_mem_free(ctx->rings);
9423 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009424 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009425 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009426
Jens Axboe2b188cc2019-01-07 10:46:33 -07009427 return 0;
9428}
9429
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009430static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9431{
9432 int ret, fd;
9433
9434 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9435 if (fd < 0)
9436 return fd;
9437
9438 ret = io_uring_add_task_file(ctx, file);
9439 if (ret) {
9440 put_unused_fd(fd);
9441 return ret;
9442 }
9443 fd_install(fd, file);
9444 return fd;
9445}
9446
Jens Axboe2b188cc2019-01-07 10:46:33 -07009447/*
9448 * Allocate an anonymous fd, this is what constitutes the application
9449 * visible backing of an io_uring instance. The application mmaps this
9450 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9451 * we have to tie this fd to a socket for file garbage collection purposes.
9452 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009453static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009454{
9455 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009456#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009457 int ret;
9458
Jens Axboe2b188cc2019-01-07 10:46:33 -07009459 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9460 &ctx->ring_sock);
9461 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009462 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009463#endif
9464
Jens Axboe2b188cc2019-01-07 10:46:33 -07009465 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9466 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009467#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009468 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009469 sock_release(ctx->ring_sock);
9470 ctx->ring_sock = NULL;
9471 } else {
9472 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009473 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009474#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009475 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009476}
9477
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009478static int io_uring_create(unsigned entries, struct io_uring_params *p,
9479 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009480{
9481 struct user_struct *user = NULL;
9482 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009483 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009484 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 int ret;
9486
Jens Axboe8110c1a2019-12-28 15:39:54 -07009487 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009488 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009489 if (entries > IORING_MAX_ENTRIES) {
9490 if (!(p->flags & IORING_SETUP_CLAMP))
9491 return -EINVAL;
9492 entries = IORING_MAX_ENTRIES;
9493 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009494
9495 /*
9496 * Use twice as many entries for the CQ ring. It's possible for the
9497 * application to drive a higher depth than the size of the SQ ring,
9498 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009499 * some flexibility in overcommitting a bit. If the application has
9500 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9501 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009502 */
9503 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009504 if (p->flags & IORING_SETUP_CQSIZE) {
9505 /*
9506 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9507 * to a power-of-two, if it isn't already. We do NOT impose
9508 * any cq vs sq ring sizing.
9509 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009510 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009511 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009512 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9513 if (!(p->flags & IORING_SETUP_CLAMP))
9514 return -EINVAL;
9515 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9516 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009517 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9518 if (p->cq_entries < p->sq_entries)
9519 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009520 } else {
9521 p->cq_entries = 2 * p->sq_entries;
9522 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009523
9524 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009525 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009526
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009527 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009528 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009529 ring_pages(p->sq_entries, p->cq_entries));
9530 if (ret) {
9531 free_uid(user);
9532 return ret;
9533 }
9534 }
9535
9536 ctx = io_ring_ctx_alloc(p);
9537 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009538 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009539 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009540 p->cq_entries));
9541 free_uid(user);
9542 return -ENOMEM;
9543 }
9544 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009545 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009546 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009547#ifdef CONFIG_AUDIT
9548 ctx->loginuid = current->loginuid;
9549 ctx->sessionid = current->sessionid;
9550#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009551 ctx->sqo_task = get_task_struct(current);
9552
9553 /*
9554 * This is just grabbed for accounting purposes. When a process exits,
9555 * the mm is exited and dropped before the files, hence we need to hang
9556 * on to this mm purely for the purposes of being able to unaccount
9557 * memory (locked/pinned vm). It's not used for anything else.
9558 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009559 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009560 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009561
Dennis Zhou91d8f512020-09-16 13:41:05 -07009562#ifdef CONFIG_BLK_CGROUP
9563 /*
9564 * The sq thread will belong to the original cgroup it was inited in.
9565 * If the cgroup goes offline (e.g. disabling the io controller), then
9566 * issued bios will be associated with the closest cgroup later in the
9567 * block layer.
9568 */
9569 rcu_read_lock();
9570 ctx->sqo_blkcg_css = blkcg_css();
9571 ret = css_tryget_online(ctx->sqo_blkcg_css);
9572 rcu_read_unlock();
9573 if (!ret) {
9574 /* don't init against a dying cgroup, have the user try again */
9575 ctx->sqo_blkcg_css = NULL;
9576 ret = -ENODEV;
9577 goto err;
9578 }
9579#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009580
Jens Axboe2b188cc2019-01-07 10:46:33 -07009581 /*
9582 * Account memory _before_ installing the file descriptor. Once
9583 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009584 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009585 * will un-account as well.
9586 */
9587 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9588 ACCT_LOCKED);
9589 ctx->limit_mem = limit_mem;
9590
9591 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592 if (ret)
9593 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009594
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009595 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009596 if (ret)
9597 goto err;
9598
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009599 if (!(p->flags & IORING_SETUP_R_DISABLED))
9600 io_sq_offload_start(ctx);
9601
Jens Axboe2b188cc2019-01-07 10:46:33 -07009602 memset(&p->sq_off, 0, sizeof(p->sq_off));
9603 p->sq_off.head = offsetof(struct io_rings, sq.head);
9604 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9605 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9606 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9607 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9608 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9609 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9610
9611 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009612 p->cq_off.head = offsetof(struct io_rings, cq.head);
9613 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9614 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9615 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9616 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9617 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009618 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009619
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009620 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9621 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009622 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009623 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9624 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009625
9626 if (copy_to_user(params, p, sizeof(*p))) {
9627 ret = -EFAULT;
9628 goto err;
9629 }
Jens Axboed1719f72020-07-30 13:43:53 -06009630
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009631 file = io_uring_get_file(ctx);
9632 if (IS_ERR(file)) {
9633 ret = PTR_ERR(file);
9634 goto err;
9635 }
9636
Jens Axboed1719f72020-07-30 13:43:53 -06009637 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009638 * Install ring fd as the very last thing, so we don't risk someone
9639 * having closed it before we finish setup
9640 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009641 ret = io_uring_install_fd(ctx, file);
9642 if (ret < 0) {
9643 /* fput will clean it up */
9644 fput(file);
9645 return ret;
9646 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009647
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009648 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649 return ret;
9650err:
9651 io_ring_ctx_wait_and_kill(ctx);
9652 return ret;
9653}
9654
9655/*
9656 * Sets up an aio uring context, and returns the fd. Applications asks for a
9657 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9658 * params structure passed in.
9659 */
9660static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9661{
9662 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009663 int i;
9664
9665 if (copy_from_user(&p, params, sizeof(p)))
9666 return -EFAULT;
9667 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9668 if (p.resv[i])
9669 return -EINVAL;
9670 }
9671
Jens Axboe6c271ce2019-01-10 11:22:30 -07009672 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009673 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009674 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9675 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009676 return -EINVAL;
9677
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009678 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009679}
9680
9681SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9682 struct io_uring_params __user *, params)
9683{
9684 return io_uring_setup(entries, params);
9685}
9686
Jens Axboe66f4af92020-01-16 15:36:52 -07009687static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9688{
9689 struct io_uring_probe *p;
9690 size_t size;
9691 int i, ret;
9692
9693 size = struct_size(p, ops, nr_args);
9694 if (size == SIZE_MAX)
9695 return -EOVERFLOW;
9696 p = kzalloc(size, GFP_KERNEL);
9697 if (!p)
9698 return -ENOMEM;
9699
9700 ret = -EFAULT;
9701 if (copy_from_user(p, arg, size))
9702 goto out;
9703 ret = -EINVAL;
9704 if (memchr_inv(p, 0, size))
9705 goto out;
9706
9707 p->last_op = IORING_OP_LAST - 1;
9708 if (nr_args > IORING_OP_LAST)
9709 nr_args = IORING_OP_LAST;
9710
9711 for (i = 0; i < nr_args; i++) {
9712 p->ops[i].op = i;
9713 if (!io_op_defs[i].not_supported)
9714 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9715 }
9716 p->ops_len = i;
9717
9718 ret = 0;
9719 if (copy_to_user(arg, p, size))
9720 ret = -EFAULT;
9721out:
9722 kfree(p);
9723 return ret;
9724}
9725
Jens Axboe071698e2020-01-28 10:04:42 -07009726static int io_register_personality(struct io_ring_ctx *ctx)
9727{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009728 struct io_identity *id;
9729 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009730
Jens Axboe1e6fa522020-10-15 08:46:24 -06009731 id = kmalloc(sizeof(*id), GFP_KERNEL);
9732 if (unlikely(!id))
9733 return -ENOMEM;
9734
9735 io_init_identity(id);
9736 id->creds = get_current_cred();
9737
9738 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9739 if (ret < 0) {
9740 put_cred(id->creds);
9741 kfree(id);
9742 }
9743 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009744}
9745
9746static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9747{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009748 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009749
Jens Axboe1e6fa522020-10-15 08:46:24 -06009750 iod = idr_remove(&ctx->personality_idr, id);
9751 if (iod) {
9752 put_cred(iod->creds);
9753 if (refcount_dec_and_test(&iod->count))
9754 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009755 return 0;
9756 }
9757
9758 return -EINVAL;
9759}
9760
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009761static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9762 unsigned int nr_args)
9763{
9764 struct io_uring_restriction *res;
9765 size_t size;
9766 int i, ret;
9767
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009768 /* Restrictions allowed only if rings started disabled */
9769 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9770 return -EBADFD;
9771
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009772 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009773 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009774 return -EBUSY;
9775
9776 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9777 return -EINVAL;
9778
9779 size = array_size(nr_args, sizeof(*res));
9780 if (size == SIZE_MAX)
9781 return -EOVERFLOW;
9782
9783 res = memdup_user(arg, size);
9784 if (IS_ERR(res))
9785 return PTR_ERR(res);
9786
9787 ret = 0;
9788
9789 for (i = 0; i < nr_args; i++) {
9790 switch (res[i].opcode) {
9791 case IORING_RESTRICTION_REGISTER_OP:
9792 if (res[i].register_op >= IORING_REGISTER_LAST) {
9793 ret = -EINVAL;
9794 goto out;
9795 }
9796
9797 __set_bit(res[i].register_op,
9798 ctx->restrictions.register_op);
9799 break;
9800 case IORING_RESTRICTION_SQE_OP:
9801 if (res[i].sqe_op >= IORING_OP_LAST) {
9802 ret = -EINVAL;
9803 goto out;
9804 }
9805
9806 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9807 break;
9808 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9809 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9810 break;
9811 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9812 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9813 break;
9814 default:
9815 ret = -EINVAL;
9816 goto out;
9817 }
9818 }
9819
9820out:
9821 /* Reset all restrictions if an error happened */
9822 if (ret != 0)
9823 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9824 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009825 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009826
9827 kfree(res);
9828 return ret;
9829}
9830
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009831static int io_register_enable_rings(struct io_ring_ctx *ctx)
9832{
9833 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9834 return -EBADFD;
9835
9836 if (ctx->restrictions.registered)
9837 ctx->restricted = 1;
9838
9839 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9840
9841 io_sq_offload_start(ctx);
9842
9843 return 0;
9844}
9845
Jens Axboe071698e2020-01-28 10:04:42 -07009846static bool io_register_op_must_quiesce(int op)
9847{
9848 switch (op) {
9849 case IORING_UNREGISTER_FILES:
9850 case IORING_REGISTER_FILES_UPDATE:
9851 case IORING_REGISTER_PROBE:
9852 case IORING_REGISTER_PERSONALITY:
9853 case IORING_UNREGISTER_PERSONALITY:
9854 return false;
9855 default:
9856 return true;
9857 }
9858}
9859
Jens Axboeedafcce2019-01-09 09:16:05 -07009860static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9861 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009862 __releases(ctx->uring_lock)
9863 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009864{
9865 int ret;
9866
Jens Axboe35fa71a2019-04-22 10:23:23 -06009867 /*
9868 * We're inside the ring mutex, if the ref is already dying, then
9869 * someone else killed the ctx or is already going through
9870 * io_uring_register().
9871 */
9872 if (percpu_ref_is_dying(&ctx->refs))
9873 return -ENXIO;
9874
Jens Axboe071698e2020-01-28 10:04:42 -07009875 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009876 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009877
Jens Axboe05f3fb32019-12-09 11:22:50 -07009878 /*
9879 * Drop uring mutex before waiting for references to exit. If
9880 * another thread is currently inside io_uring_enter() it might
9881 * need to grab the uring_lock to make progress. If we hold it
9882 * here across the drain wait, then we can deadlock. It's safe
9883 * to drop the mutex here, since no new references will come in
9884 * after we've killed the percpu ref.
9885 */
9886 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009887 do {
9888 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9889 if (!ret)
9890 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009891 ret = io_run_task_work_sig();
9892 if (ret < 0)
9893 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009894 } while (1);
9895
Jens Axboe05f3fb32019-12-09 11:22:50 -07009896 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009897
Jens Axboec1503682020-01-08 08:26:07 -07009898 if (ret) {
9899 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009900 goto out_quiesce;
9901 }
9902 }
9903
9904 if (ctx->restricted) {
9905 if (opcode >= IORING_REGISTER_LAST) {
9906 ret = -EINVAL;
9907 goto out;
9908 }
9909
9910 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9911 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009912 goto out;
9913 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009914 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009915
9916 switch (opcode) {
9917 case IORING_REGISTER_BUFFERS:
9918 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9919 break;
9920 case IORING_UNREGISTER_BUFFERS:
9921 ret = -EINVAL;
9922 if (arg || nr_args)
9923 break;
9924 ret = io_sqe_buffer_unregister(ctx);
9925 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009926 case IORING_REGISTER_FILES:
9927 ret = io_sqe_files_register(ctx, arg, nr_args);
9928 break;
9929 case IORING_UNREGISTER_FILES:
9930 ret = -EINVAL;
9931 if (arg || nr_args)
9932 break;
9933 ret = io_sqe_files_unregister(ctx);
9934 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009935 case IORING_REGISTER_FILES_UPDATE:
9936 ret = io_sqe_files_update(ctx, arg, nr_args);
9937 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009938 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009939 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009940 ret = -EINVAL;
9941 if (nr_args != 1)
9942 break;
9943 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009944 if (ret)
9945 break;
9946 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9947 ctx->eventfd_async = 1;
9948 else
9949 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009950 break;
9951 case IORING_UNREGISTER_EVENTFD:
9952 ret = -EINVAL;
9953 if (arg || nr_args)
9954 break;
9955 ret = io_eventfd_unregister(ctx);
9956 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009957 case IORING_REGISTER_PROBE:
9958 ret = -EINVAL;
9959 if (!arg || nr_args > 256)
9960 break;
9961 ret = io_probe(ctx, arg, nr_args);
9962 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009963 case IORING_REGISTER_PERSONALITY:
9964 ret = -EINVAL;
9965 if (arg || nr_args)
9966 break;
9967 ret = io_register_personality(ctx);
9968 break;
9969 case IORING_UNREGISTER_PERSONALITY:
9970 ret = -EINVAL;
9971 if (arg)
9972 break;
9973 ret = io_unregister_personality(ctx, nr_args);
9974 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009975 case IORING_REGISTER_ENABLE_RINGS:
9976 ret = -EINVAL;
9977 if (arg || nr_args)
9978 break;
9979 ret = io_register_enable_rings(ctx);
9980 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009981 case IORING_REGISTER_RESTRICTIONS:
9982 ret = io_register_restrictions(ctx, arg, nr_args);
9983 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009984 default:
9985 ret = -EINVAL;
9986 break;
9987 }
9988
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009989out:
Jens Axboe071698e2020-01-28 10:04:42 -07009990 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009991 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009992 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009993out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009994 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009995 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009996 return ret;
9997}
9998
9999SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10000 void __user *, arg, unsigned int, nr_args)
10001{
10002 struct io_ring_ctx *ctx;
10003 long ret = -EBADF;
10004 struct fd f;
10005
10006 f = fdget(fd);
10007 if (!f.file)
10008 return -EBADF;
10009
10010 ret = -EOPNOTSUPP;
10011 if (f.file->f_op != &io_uring_fops)
10012 goto out_fput;
10013
10014 ctx = f.file->private_data;
10015
10016 mutex_lock(&ctx->uring_lock);
10017 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10018 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010019 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10020 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010021out_fput:
10022 fdput(f);
10023 return ret;
10024}
10025
Jens Axboe2b188cc2019-01-07 10:46:33 -070010026static int __init io_uring_init(void)
10027{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010028#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10029 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10030 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10031} while (0)
10032
10033#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10034 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10035 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10036 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10037 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10038 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10039 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10040 BUILD_BUG_SQE_ELEM(8, __u64, off);
10041 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10042 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010043 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010044 BUILD_BUG_SQE_ELEM(24, __u32, len);
10045 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10046 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10047 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10048 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010049 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10050 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010051 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10052 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10053 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10054 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10055 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10056 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10057 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10058 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010059 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010060 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10061 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10062 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010063 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010064
Jens Axboed3656342019-12-18 09:50:26 -070010065 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010066 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010067 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10068 return 0;
10069};
10070__initcall(io_uring_init);